samples/apifest1/day1/inputandoperation/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
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.
6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7 * or http://www.netbeans.org/cddl.txt.
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]"
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.
20 package org.netbeans.apifest.boolcircuit;
22 import junit.framework.TestCase;
23 import junit.framework.*;
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
30 * The basic elements include:
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
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
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>
52 public class CircuitTest extends TestCase {
54 // your code shall run without any permissions
59 public CircuitTest(String testName) {
63 // BEGIN: apifest.day1.inputandoperation.CircuitTest
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).
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));
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).
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
90 assertFalse(Circuit.evaluateBooleanOperation(op2));
92 op1 = Factory.createAndOperation(inFalse, inFalse);
93 op2 = Factory.createOrOperation(
94 Factory.createOperationBasedBooleanInput(op1), inTrue
96 assertTrue(Circuit.evaluateBooleanOperation(op2));
99 * Create a circuit to evaluate (x1 or not(x1)) and then
100 * verify that its result is true for all values of x1.
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
109 assertTrue(Circuit.evaluateBooleanOperation(or));
110 not = Factory.createNotOperation(inFalse);
111 or = Factory.createOrOperation(
112 Factory.createOperationBasedBooleanInput(not), inFalse
114 assertTrue(Circuit.evaluateBooleanOperation(or));
116 // END: apifest.day1.inputandoperation.CircuitTest