samples/apifest1/day1/pinbasedsolution/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 junit.framework.TestCase;
    23 import junit.framework.*;
    24 
    25 /** The initial quest for this APIFest is to create an API for boolean 
    26  * circuits. Such API shall be able to compose a boolean circuit from
    27  * basic elements and evaluate the result given initial values for 
    28  * input variables.
    29  * <p>
    30  * The basic elements include:
    31  * <ul>
    32  *   <li>negation - has one input and one output and changes 0 on input to 
    33  *          on output 1 and 1 to 0
    34  *   <li>and - has two inputs and one output. The output is 1 only if both 
    35  *          inputs are 1, otherwise it is 0
    36  *   <li>or - has two inputs and one output. The output is 1 always, except
    37  *          in the case when both inputs are 0
    38  * </ul>
    39  *
    40  * <p>
    41  * The boolean circuit can be used to represent boolean formulas and compute
    42  * the results for certain values of its inputs. The individual tasks described
    43  * as tests bellow.
    44  *
    45  * <p>
    46  * Links of interest:
    47  * <ul>
    48  *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    49  *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    50  * </ul>
    51  */
    52 public class CircuitTest extends TestCase {
    53     static {
    54         // your code shall run without any permissions
    55     }
    56     
    57     public CircuitTest(String testName) {
    58         super(testName);
    59     }
    60 
    61     // BEGIN: apifest.day1.pinbasedsolution.CircuitTest
    62     /** 
    63      * Create a circuit to evaluate x1 and x2 and then
    64      * verify that its result is false for input (false, true) and
    65      * it is true for input (true, true).
    66      */
    67     public void testX1andX2() throws Exception {
    68         Circuit c = Circuit.construct(
    69             Element.createAnd(
    70                 Element.createInput(0),
    71                 Element.createInput(1)
    72             )
    73         );
    74         
    75         assertFalse ("false AND true is false", c.evaluate(false, true));
    76         assertTrue ("true AND true is true", c.evaluate(true, true));
    77     }
    78     
    79     /** 
    80      * Create a circuit to evaluate (x1 and x2) or x3 and then
    81      * verify that its result is false for input (false, true, false) and
    82      * it is true for input (false, false, true).
    83      */
    84     public void testX1andX2orX3() throws Exception {
    85         Circuit c = Circuit.construct(
    86             Element.createOr(
    87                 Element.createAnd(
    88                     Element.createInput(0),
    89                     Element.createInput(1)
    90                 ),
    91                 Element.createInput(2)
    92             )
    93         );
    94         
    95         assertFalse (
    96             "(false AND true) OR false is false", 
    97             c.evaluate(false, true, false)
    98         );
    99         assertTrue (
   100             "(false AND false) OR true is true", 
   101             c.evaluate(false, false, true)
   102         );
   103     }
   104     
   105     /** 
   106      * Create a circuit to evaluate (x1 or not(x1)) and then
   107      * verify that its result is true for all values of x1.
   108      */
   109     public void testAlwaysTrue() throws Exception {
   110         Circuit c = Circuit.construct(
   111             Element.createOr(
   112                 Element.createInput(0),
   113                 Element.createNot(Element.createInput(0))
   114             )
   115         );
   116         
   117         assertTrue ("tautology is true", c.evaluate(false));
   118         assertTrue ("tautology is true", c.evaluate(true));
   119     }
   120     // END: apifest.day1.pinbasedsolution.CircuitTest
   121 }