samples/apifest1/day1/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 52 4257f4cf226b
child 153 b5cbb797ec0a
permissions -rw-r--r--
Truncating all examples to 80 characters per line
     1 /*
     2  * The contents of this file are subject to the terms of the Common Development
     3  * and Distribution License (the License). You may not use this file except in
     4  * compliance with the License.
     5  *
     6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
     7  * or http://www.netbeans.org/cddl.txt.
     8  *
     9  * When distributing Covered Code, include this CDDL Header Notice in each file
    10  * and include the License file at http://www.netbeans.org/cddl.txt.
    11  * If applicable, add the following below the CDDL Header, with the fields
    12  * enclosed by brackets [] replaced by your own identifying information:
    13  * "Portions Copyrighted [year] [name of copyright owner]"
    14  *
    15  * The Original Software is NetBeans. The Initial Developer of the Original
    16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    17  * Microsystems, Inc. All Rights Reserved.
    18  */
    19 
    20 package org.netbeans.apifest.boolcircuit;
    21 
    22 import java.util.Arrays;
    23 import java.util.Stack;
    24 import junit.framework.TestCase;
    25 
    26 /** The initial quest for this APIFest is to create an API for boolean 
    27  * circuits. Such API shall be able to compose a boolean circuit from
    28  * basic elements and evaluate the result given initial values for 
    29  * input variables.
    30  * <p>
    31  * The basic elements include:
    32  * <ul>
    33  *   <li>negation - has one input and one output and changes 0 on input to 
    34  *          on output 1 and 1 to 0
    35  *   <li>and - has two inputs and one output. The output is 1 only if both 
    36  *          inputs are 1, otherwise it is 0
    37  *   <li>or - has two inputs and one output. The output is 1 always, except
    38  *          in the case when both inputs are 0
    39  * </ul>
    40  *
    41  * <p>
    42  * The boolean circuit can be used to represent boolean formulas and compute
    43  * the results for certain values of its inputs. The individual tasks described
    44  * as tests bellow.
    45  *
    46  * <p>
    47  * Links of interest:
    48  * <ul>
    49  *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    50  *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    51  * </ul>
    52  */
    53 public class CircuitTest extends TestCase {
    54     static {
    55         // your code shall run without any permissions
    56     }
    57     
    58     public CircuitTest(String testName) {
    59         super(testName);
    60     }
    61 
    62     protected void setUp() throws Exception {
    63     }
    64 
    65     protected void tearDown() throws Exception {
    66     }
    67     
    68     // BEGIN: apifest.day1.stackbasedsolution.CircuitTest
    69     /** 
    70      * Create a circuit to evaluate x1 and x2 and then
    71      * verify that its result is false for input (false, true) and
    72      * it is true for input (true, true).
    73      */
    74     public void testX1andX2() {
    75         Stack<Character> s = new Stack<Character> ();
    76         s.addAll(Arrays.asList('1', '1'));
    77         assertEquals("'1' for '11' input.", '1', 
    78             CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
    79         s.addAll(Arrays.asList('1', '0'));
    80         assertEquals("'0' for '10' input.", '0', 
    81             CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
    82     }
    83     
    84     /** 
    85      * Create a circuit to evaluate (x1 and x2) or x3 and then
    86      * verify that its result is false for input (false, true, false) and
    87      * it is true for input (false, false, true).
    88      */
    89     public void testX1andX2orX3() {
    90         Stack<Character> s = new Stack<Character> ();
    91         s.addAll(Arrays.asList('0', '1', '0'));
    92         assertEquals("'0' for '010' input.", '0', 
    93             CircuitFactory.join(CircuitFactory.getTrivialCircuit(), 
    94             CircuitFactory.getBasicCircuit(Operation.OR), 
    95             Operation.AND).evaluate(s)
    96         );
    97         s.addAll(Arrays.asList('0', '0', '1'));
    98         assertEquals("'1' for '001' input.", '1', 
    99             CircuitFactory.join(CircuitFactory.getTrivialCircuit(), 
   100             CircuitFactory.getBasicCircuit(Operation.OR), 
   101             Operation.AND).evaluate(s)
   102         );
   103     }
   104     /** 
   105      * Create a circuit to evaluate (x1 or not(x1)) and then
   106      * verify that its result is true for all values of x1.
   107      */
   108     public void testAlwaysTrue() {
   109         Circuit alwaysTrue = CircuitFactory.join(
   110             CircuitFactory.getTrivialCircuit(), 
   111             CircuitFactory.getBasicCircuit(Operation.NEG), 
   112             Operation.OR
   113         );
   114         Stack<Character> s = new Stack<Character> ();
   115         s.addAll(Arrays.asList('0', '0'));
   116         assertEquals ("'1' for '00'", '1', alwaysTrue.evaluate(s));
   117         s.addAll(Arrays.asList('1', '1'));
   118         assertEquals ("'1' for '11'", '1', alwaysTrue.evaluate(s));
   119     }
   120     // END: apifest.day1.stackbasedsolution.CircuitTest
   121 }