jtulach@52
|
1 |
/*
|
jtulach@52
|
2 |
* The contents of this file are subject to the terms of the Common Development
|
jtulach@52
|
3 |
* and Distribution License (the License). You may not use this file except in
|
jtulach@52
|
4 |
* compliance with the License.
|
jtulach@52
|
5 |
*
|
jtulach@52
|
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html
|
jtulach@52
|
7 |
* or http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
8 |
*
|
jtulach@52
|
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file
|
jtulach@52
|
10 |
* and include the License file at http://www.netbeans.org/cddl.txt.
|
jtulach@52
|
11 |
* If applicable, add the following below the CDDL Header, with the fields
|
jtulach@52
|
12 |
* enclosed by brackets [] replaced by your own identifying information:
|
jtulach@52
|
13 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@52
|
14 |
*
|
jtulach@52
|
15 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@52
|
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
|
jtulach@52
|
17 |
* Microsystems, Inc. All Rights Reserved.
|
jtulach@52
|
18 |
*/
|
jtulach@52
|
19 |
|
jtulach@52
|
20 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
21 |
|
jtulach@52
|
22 |
import java.util.Arrays;
|
jtulach@52
|
23 |
import java.util.Stack;
|
jtulach@52
|
24 |
import junit.framework.TestCase;
|
jtulach@52
|
25 |
|
jtulach@52
|
26 |
/** The initial quest for this APIFest is to create an API for boolean
|
jtulach@52
|
27 |
* circuits. Such API shall be able to compose a boolean circuit from
|
jtulach@52
|
28 |
* basic elements and evaluate the result given initial values for
|
jtulach@52
|
29 |
* input variables.
|
jtulach@52
|
30 |
* <p>
|
jtulach@52
|
31 |
* The basic elements include:
|
jtulach@52
|
32 |
* <ul>
|
jtulach@52
|
33 |
* <li>negation - has one input and one output and changes 0 on input to
|
jtulach@52
|
34 |
* on output 1 and 1 to 0
|
jtulach@52
|
35 |
* <li>and - has two inputs and one output. The output is 1 only if both
|
jtulach@52
|
36 |
* inputs are 1, otherwise it is 0
|
jtulach@52
|
37 |
* <li>or - has two inputs and one output. The output is 1 always, except
|
jtulach@52
|
38 |
* in the case when both inputs are 0
|
jtulach@52
|
39 |
* </ul>
|
jtulach@52
|
40 |
*
|
jtulach@52
|
41 |
* <p>
|
jtulach@52
|
42 |
* The boolean circuit can be used to represent boolean formulas and compute
|
jtulach@52
|
43 |
* the results for certain values of its inputs. The individual tasks described
|
jtulach@52
|
44 |
* as tests bellow.
|
jtulach@52
|
45 |
*
|
jtulach@52
|
46 |
* <p>
|
jtulach@52
|
47 |
* Links of interest:
|
jtulach@52
|
48 |
* <ul>
|
jtulach@52
|
49 |
* <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
|
jtulach@52
|
50 |
* <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
|
jtulach@52
|
51 |
* </ul>
|
jtulach@52
|
52 |
*/
|
jtulach@52
|
53 |
public class CircuitTest extends TestCase {
|
jtulach@52
|
54 |
static {
|
jtulach@52
|
55 |
// your code shall run without any permissions
|
jtulach@52
|
56 |
}
|
jtulach@52
|
57 |
|
jtulach@52
|
58 |
public CircuitTest(String testName) {
|
jtulach@52
|
59 |
super(testName);
|
jtulach@52
|
60 |
}
|
jtulach@52
|
61 |
|
jtulach@52
|
62 |
protected void setUp() throws Exception {
|
jtulach@52
|
63 |
}
|
jtulach@52
|
64 |
|
jtulach@52
|
65 |
protected void tearDown() throws Exception {
|
jtulach@52
|
66 |
}
|
jtulach@52
|
67 |
|
jtulach@52
|
68 |
// BEGIN: apifest.day1.stackbasedsolution.CircuitTest
|
jtulach@52
|
69 |
/**
|
jtulach@52
|
70 |
* Create a circuit to evaluate x1 and x2 and then
|
jtulach@52
|
71 |
* verify that its result is false for input (false, true) and
|
jtulach@52
|
72 |
* it is true for input (true, true).
|
jtulach@52
|
73 |
*/
|
jtulach@52
|
74 |
public void testX1andX2() {
|
jtulach@52
|
75 |
Stack<Character> s = new Stack<Character> ();
|
jtulach@52
|
76 |
s.addAll(Arrays.asList('1', '1'));
|
jtulach@154
|
77 |
assertEquals("'1' for '11' input.", '1',
|
jtulach@154
|
78 |
CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
|
jtulach@52
|
79 |
s.addAll(Arrays.asList('1', '0'));
|
jtulach@154
|
80 |
assertEquals("'0' for '10' input.", '0',
|
jtulach@154
|
81 |
CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
|
jtulach@52
|
82 |
}
|
jtulach@52
|
83 |
|
jtulach@52
|
84 |
/**
|
jtulach@52
|
85 |
* Create a circuit to evaluate (x1 and x2) or x3 and then
|
jtulach@52
|
86 |
* verify that its result is false for input (false, true, false) and
|
jtulach@52
|
87 |
* it is true for input (false, false, true).
|
jtulach@52
|
88 |
*/
|
jtulach@52
|
89 |
public void testX1andX2orX3() {
|
jtulach@52
|
90 |
Stack<Character> s = new Stack<Character> ();
|
jtulach@52
|
91 |
s.addAll(Arrays.asList('0', '1', '0'));
|
jtulach@154
|
92 |
assertEquals("'0' for '010' input.", '0',
|
jtulach@154
|
93 |
CircuitFactory.join(CircuitFactory.getTrivialCircuit(),
|
jtulach@154
|
94 |
CircuitFactory.getBasicCircuit(Operation.OR),
|
jtulach@154
|
95 |
Operation.AND).evaluate(s)
|
jtulach@154
|
96 |
);
|
jtulach@52
|
97 |
s.addAll(Arrays.asList('0', '0', '1'));
|
jtulach@154
|
98 |
assertEquals("'1' for '001' input.", '1',
|
jtulach@154
|
99 |
CircuitFactory.join(CircuitFactory.getTrivialCircuit(),
|
jtulach@154
|
100 |
CircuitFactory.getBasicCircuit(Operation.OR),
|
jtulach@154
|
101 |
Operation.AND).evaluate(s)
|
jtulach@154
|
102 |
);
|
jtulach@52
|
103 |
}
|
jtulach@52
|
104 |
/**
|
jtulach@52
|
105 |
* Create a circuit to evaluate (x1 or not(x1)) and then
|
jtulach@52
|
106 |
* verify that its result is true for all values of x1.
|
jtulach@52
|
107 |
*/
|
jtulach@52
|
108 |
public void testAlwaysTrue() {
|
jtulach@154
|
109 |
Circuit alwaysTrue = CircuitFactory.join(
|
jtulach@154
|
110 |
CircuitFactory.getTrivialCircuit(),
|
jtulach@154
|
111 |
CircuitFactory.getBasicCircuit(Operation.NEG),
|
jtulach@154
|
112 |
Operation.OR
|
jtulach@154
|
113 |
);
|
jtulach@52
|
114 |
Stack<Character> s = new Stack<Character> ();
|
jtulach@52
|
115 |
s.addAll(Arrays.asList('0', '0'));
|
jtulach@52
|
116 |
assertEquals ("'1' for '00'", '1', alwaysTrue.evaluate(s));
|
jtulach@52
|
117 |
s.addAll(Arrays.asList('1', '1'));
|
jtulach@52
|
118 |
assertEquals ("'1' for '11'", '1', alwaysTrue.evaluate(s));
|
jtulach@52
|
119 |
}
|
jtulach@52
|
120 |
// END: apifest.day1.stackbasedsolution.CircuitTest
|
jtulach@52
|
121 |
}
|