samples/apifest1/day1/inputandoperation/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     Input inTrue;
    58     Input inFalse;
    59     public CircuitTest(String testName) {
    60         super(testName);
    61     }
    62 
    63     // BEGIN: apifest.day1.inputandoperation.CircuitTest
    64     /** 
    65      * Create a circuit to evaluate x1 and x2 and then
    66      * verify that its result is false for input (false, true) and
    67      * it is true for input (true, true).
    68      */
    69     public void testX1andX2() {
    70         inTrue = Factory.createSimpleBooleanInput(true);
    71         inFalse = Factory.createSimpleBooleanInput(false);
    72         Operation op1 = Factory.createAndOperation(inFalse, inTrue);
    73         assertFalse(Circuit.evaluateBooleanOperation(op1));
    74         Operation op2 = Factory.createAndOperation(inTrue, inTrue);
    75         assertTrue(Circuit.evaluateBooleanOperation(op2));
    76     }
    77     
    78     /** 
    79      * Create a circuit to evaluate (x1 and x2) or x3 and then
    80      * verify that its result is false for input (false, true, false) and
    81      * it is true for input (false, false, true).
    82      */
    83     public void testX1andX2orX3() {
    84         inTrue = Factory.createSimpleBooleanInput(true);
    85         inFalse = Factory.createSimpleBooleanInput(false);
    86         Operation op1 = Factory.createAndOperation(inFalse, inTrue);
    87         Operation op2 = Factory.createOrOperation(
    88             Factory.createOperationBasedBooleanInput(op1), inFalse
    89         );
    90         assertFalse(Circuit.evaluateBooleanOperation(op2));
    91         
    92         op1 = Factory.createAndOperation(inFalse, inFalse);
    93         op2 = Factory.createOrOperation(
    94             Factory.createOperationBasedBooleanInput(op1), inTrue
    95         );
    96         assertTrue(Circuit.evaluateBooleanOperation(op2));
    97     }
    98     /** 
    99      * Create a circuit to evaluate (x1 or not(x1)) and then
   100      * verify that its result is true for all values of x1.
   101      */
   102     public void testAlwaysTrue() {
   103         inTrue = Factory.createSimpleBooleanInput(true);
   104         inFalse = Factory.createSimpleBooleanInput(false);
   105         Operation not = Factory.createNotOperation(inTrue);
   106         Operation or = Factory.createOrOperation(
   107             Factory.createOperationBasedBooleanInput(not), inTrue
   108         );
   109         assertTrue(Circuit.evaluateBooleanOperation(or));
   110         not = Factory.createNotOperation(inFalse);
   111         or = Factory.createOrOperation(
   112             Factory.createOperationBasedBooleanInput(not), inFalse
   113         );
   114         assertTrue(Circuit.evaluateBooleanOperation(or));
   115     }
   116     // END: apifest.day1.inputandoperation.CircuitTest
   117 }