samples/apifest1/day1/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
changeset 52 4257f4cf226b
child 132 3bc4c54f4bcc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day1/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + * The contents of this file are subject to the terms of the Common Development
     1.6 + * and Distribution License (the License). You may not use this file except in
     1.7 + * compliance with the License.
     1.8 + *
     1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    1.10 + * or http://www.netbeans.org/cddl.txt.
    1.11 + *
    1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    1.14 + * If applicable, add the following below the CDDL Header, with the fields
    1.15 + * enclosed by brackets [] replaced by your own identifying information:
    1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.17 + *
    1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.20 + * Microsystems, Inc. All Rights Reserved.
    1.21 + */
    1.22 +
    1.23 +package org.netbeans.apifest.boolcircuit;
    1.24 +
    1.25 +import java.util.Arrays;
    1.26 +import java.util.Stack;
    1.27 +import junit.framework.TestCase;
    1.28 +
    1.29 +/** The initial quest for this APIFest is to create an API for boolean 
    1.30 + * circuits. Such API shall be able to compose a boolean circuit from
    1.31 + * basic elements and evaluate the result given initial values for 
    1.32 + * input variables.
    1.33 + * <p>
    1.34 + * The basic elements include:
    1.35 + * <ul>
    1.36 + *   <li>negation - has one input and one output and changes 0 on input to 
    1.37 + *          on output 1 and 1 to 0
    1.38 + *   <li>and - has two inputs and one output. The output is 1 only if both 
    1.39 + *          inputs are 1, otherwise it is 0
    1.40 + *   <li>or - has two inputs and one output. The output is 1 always, except
    1.41 + *          in the case when both inputs are 0
    1.42 + * </ul>
    1.43 + *
    1.44 + * <p>
    1.45 + * The boolean circuit can be used to represent boolean formulas and compute
    1.46 + * the results for certain values of its inputs. The individual tasks described
    1.47 + * as tests bellow.
    1.48 + *
    1.49 + * <p>
    1.50 + * Links of interest:
    1.51 + * <ul>
    1.52 + *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    1.53 + *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    1.54 + * </ul>
    1.55 + */
    1.56 +public class CircuitTest extends TestCase {
    1.57 +    static {
    1.58 +        // your code shall run without any permissions
    1.59 +    }
    1.60 +    
    1.61 +    public CircuitTest(String testName) {
    1.62 +        super(testName);
    1.63 +    }
    1.64 +
    1.65 +    protected void setUp() throws Exception {
    1.66 +    }
    1.67 +
    1.68 +    protected void tearDown() throws Exception {
    1.69 +    }
    1.70 +    
    1.71 +    // BEGIN: apifest.day1.stackbasedsolution.CircuitTest
    1.72 +    /** 
    1.73 +     * Create a circuit to evaluate x1 and x2 and then
    1.74 +     * verify that its result is false for input (false, true) and
    1.75 +     * it is true for input (true, true).
    1.76 +     */
    1.77 +    public void testX1andX2() {
    1.78 +        Stack<Character> s = new Stack<Character> ();
    1.79 +        s.addAll(Arrays.asList('1', '1'));
    1.80 +        assertEquals("'1' for '11' input.", '1', CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
    1.81 +        s.addAll(Arrays.asList('1', '0'));
    1.82 +        assertEquals("'0' for '10' input.", '0', CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
    1.83 +    }
    1.84 +    
    1.85 +    /** 
    1.86 +     * Create a circuit to evaluate (x1 and x2) or x3 and then
    1.87 +     * verify that its result is false for input (false, true, false) and
    1.88 +     * it is true for input (false, false, true).
    1.89 +     */
    1.90 +    public void testX1andX2orX3() {
    1.91 +        Stack<Character> s = new Stack<Character> ();
    1.92 +        s.addAll(Arrays.asList('0', '1', '0'));
    1.93 +        assertEquals("'0' for '010' input.", '0', CircuitFactory.join(CircuitFactory.getTrivialCircuit(), CircuitFactory.getBasicCircuit(Operation.OR), Operation.AND).evaluate(s));
    1.94 +        s.addAll(Arrays.asList('0', '0', '1'));
    1.95 +        assertEquals("'1' for '001' input.", '1', CircuitFactory.join(CircuitFactory.getTrivialCircuit(), CircuitFactory.getBasicCircuit(Operation.OR), Operation.AND).evaluate(s));
    1.96 +    }
    1.97 +    /** 
    1.98 +     * Create a circuit to evaluate (x1 or not(x1)) and then
    1.99 +     * verify that its result is true for all values of x1.
   1.100 +     */
   1.101 +    public void testAlwaysTrue() {
   1.102 +        Circuit alwaysTrue = CircuitFactory.join(CircuitFactory.getTrivialCircuit(), CircuitFactory.getBasicCircuit(Operation.NEG), Operation.OR);
   1.103 +        Stack<Character> s = new Stack<Character> ();
   1.104 +        s.addAll(Arrays.asList('0', '0'));
   1.105 +        assertEquals ("'1' for '00'", '1', alwaysTrue.evaluate(s));
   1.106 +        s.addAll(Arrays.asList('1', '1'));
   1.107 +        assertEquals ("'1' for '11'", '1', alwaysTrue.evaluate(s));
   1.108 +    }
   1.109 +    // END: apifest.day1.stackbasedsolution.CircuitTest
   1.110 +}