samples/apifest1/day2/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 java.security.CodeSource;
23 import java.security.Permission;
24 import java.security.PermissionCollection;
25 import java.security.Policy;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import junit.framework.TestCase;
30 import junit.framework.*;
32 /** The initial quest for this APIFest is to create an API for boolean
33 * circuits. Such API shall be able to compose a boolean circuit from
34 * basic elements and evaluate the result given initial values for
37 * The basic elements include:
39 * <li>negation - has one input and one output and changes 0 on input to
40 * on output 1 and 1 to 0
41 * <li>and - has two inputs and one output. The output is 1 only if both
42 * inputs are 1, otherwise it is 0
43 * <li>or - has two inputs and one output. The output is 1 always, except
44 * in the case when both inputs are 0
48 * The boolean circuit can be used to represent boolean formulas and compute
49 * the results for certain values of its inputs. The individual tasks described
55 * <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
56 * <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
59 public class CircuitTest extends TestCase {
61 // your code shall run without any permissions
66 public CircuitTest(String testName) {
72 * Create a circuit to evaluate x1 and x2 and then
73 * verify that its result is false for input (false, true) and
74 * it is true for input (true, true).
76 public void testX1andX2() {
77 inTrue = Factory.createSimpleBooleanInput(true);
78 inFalse = Factory.createSimpleBooleanInput(false);
79 Operation op1 = Factory.createAndOperation(inFalse, inTrue);
80 assertFalse(Circuit.evaluateBooleanOperation(op1));
81 Operation op2 = Factory.createAndOperation(inTrue, inTrue);
82 assertTrue(Circuit.evaluateBooleanOperation(op2));
86 * Create a circuit to evaluate (x1 and x2) or x3 and then
87 * verify that its result is false for input (false, true, false) and
88 * it is true for input (false, false, true).
90 public void testX1andX2orX3() {
91 inTrue = Factory.createSimpleBooleanInput(true);
92 inFalse = Factory.createSimpleBooleanInput(false);
93 Operation op1 = Factory.createAndOperation(inFalse, inTrue);
94 Operation op2 = Factory.createOrOperation(Factory.createOperationBasedBooleanInput(op1), inFalse);
95 assertFalse(Circuit.evaluateBooleanOperation(op2));
97 op1 = Factory.createAndOperation(inFalse, inFalse);
98 op2 = Factory.createOrOperation(Factory.createOperationBasedBooleanInput(op1), inTrue);
99 assertTrue(Circuit.evaluateBooleanOperation(op2));
102 * Create a circuit to evaluate (x1 or not(x1)) and then
103 * verify that its result is true for all values of x1.
105 public void testAlwaysTrue() {
106 inTrue = Factory.createSimpleBooleanInput(true);
107 inFalse = Factory.createSimpleBooleanInput(false);
108 Operation not = Factory.createNotOperation(inTrue);
109 Operation or = Factory.createOrOperation(Factory.createOperationBasedBooleanInput(not), inTrue);
110 assertTrue(Circuit.evaluateBooleanOperation(or));
111 not = Factory.createNotOperation(inFalse);
112 or = Factory.createOrOperation(Factory.createOperationBasedBooleanInput(not), inFalse);
113 assertTrue(Circuit.evaluateBooleanOperation(or));