samples/apifest1/day1/alwayscreatenewcircuit/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 52 4257f4cf226b
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
     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 
    24 /** The initial quest for this APIFest is to create an API for boolean 
    25  * circuits. Such API shall be able to compose a boolean circuit from
    26  * basic elements and evaluate the result given initial values for 
    27  * input variables.
    28  * <p>
    29  * The basic elements include:
    30  * <ul>
    31  *   <li>negation - has one input and one output and changes 0 on input to 
    32  *          on output 1 and 1 to 0
    33  *   <li>and - has two inputs and one output. The output is 1 only if both 
    34  *          inputs are 1, otherwise it is 0
    35  *   <li>or - has two inputs and one output. The output is 1 always, except
    36  *          in the case when both inputs are 0
    37  * </ul>
    38  *
    39  * <p>
    40  * The boolean circuit can be used to represent boolean formulas and compute
    41  * the results for certain values of its inputs. The individual tasks described
    42  * as tests bellow.
    43  *
    44  * <p>
    45  * Links of interest:
    46  * <ul>
    47  *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    48  *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    49  * </ul>
    50  */
    51 public class CircuitTest extends TestCase {
    52     static {
    53         // your code shall run without any permissions
    54     }
    55     
    56     public CircuitTest(String testName) {
    57         super(testName);
    58     }
    59 
    60     // BEGIN: apifest.day1.alwayscreatenewcircuit.CircuitTest
    61     /** 
    62      * Create a circuit to evaluate x1 and x2 and then
    63      * verify that its result is false for input (false, true) and
    64      * it is true for input (true, true).
    65      */
    66     public void testX1andX2() {
    67         boolean x1 = true;
    68         boolean x2 = true;
    69 
    70         Circuit outputCircuit = Circuit.and(x1, x2);
    71         assertTrue(outputCircuit.output());
    72 
    73         x1 = false;
    74         x2 = true;
    75         outputCircuit = Circuit.and(x1, x2);
    76         assertFalse(outputCircuit.output());
    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() {
    85         boolean x1 = false;
    86         boolean x2 = true;
    87         boolean x3 = false;
    88         Circuit outputCircuit = Circuit.or(Circuit.and(x1,x2),x3);
    89         assertFalse(outputCircuit.output());
    90 
    91         x1 = false;
    92         x2 = false;
    93         x3 = true;
    94         outputCircuit = Circuit.or(Circuit.and(x1,x2),x3);
    95         assertTrue(outputCircuit.output());
    96     }
    97     /** 
    98      * Create a circuit to evaluate (x1 or not(x1)) and then
    99      * verify that its result is true for all values of x1.
   100      */
   101     public void testAlwaysTrue() {
   102         boolean x1 = true;
   103         Circuit outputCircuit = Circuit.or(x1,Circuit.negate(x1));
   104         assertTrue(outputCircuit.output());
   105 
   106         x1 = false;
   107         outputCircuit = Circuit.or(x1,Circuit.negate(x1));
   108         assertTrue(outputCircuit.output());
   109     }
   110     // END: apifest.day1.alwayscreatenewcircuit.CircuitTest
   111     
   112 }