jtulach@52
|
1 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
2 |
|
jtulach@52
|
3 |
import junit.framework.TestCase;
|
jtulach@52
|
4 |
import junit.framework.*;
|
jtulach@52
|
5 |
import org.netbeans.apifest.custom.Gte;
|
jtulach@52
|
6 |
|
jtulach@52
|
7 |
/** This file contains the APIFest quest for day 2. Simply, turn the
|
jtulach@132
|
8 |
* boolean circuit into circuit that can compute with double
|
jtulach@132
|
9 |
* values from 0 to 1.
|
jtulach@52
|
10 |
* <p>
|
jtulach@52
|
11 |
* This means that where ever a boolean was used to represent input or
|
jtulach@52
|
12 |
* output values, one can now use any double number from >= 0 and <= 1.
|
jtulach@52
|
13 |
* Still, to support backward compatibility, the operations with booleans
|
jtulach@52
|
14 |
* has to be kept available and have to work. In fact False shall be
|
jtulach@52
|
15 |
* treated as 0 and True as 1.
|
jtulach@52
|
16 |
* <p>
|
jtulach@52
|
17 |
* The basic elements has to be modified to work on doubles in the following
|
jtulach@52
|
18 |
* way:
|
jtulach@52
|
19 |
* <ul>
|
jtulach@132
|
20 |
* <li>negation - neg(x) = 1 - x
|
jtulach@132
|
21 |
* <li>and - and(x,y) = x * y
|
jtulach@132
|
22 |
* <li>or - or(x,y) = 1 - (1 - x) * (1 - y)
|
jtulach@52
|
23 |
* </ul>
|
jtulach@52
|
24 |
* <p>
|
jtulach@132
|
25 |
* However as the circuits with doubles are more rich than plain
|
jtulach@132
|
26 |
* boolean circuits, there is additional requirement to allow any user
|
jtulach@132
|
27 |
* of your API to write its own "element" type. This is all going to
|
jtulach@132
|
28 |
* be exercise in the tests bellow
|
jtulach@52
|
29 |
* which you are supposed to implement.
|
jtulach@52
|
30 |
*/
|
jtulach@54
|
31 |
// BEGIN: apifest.day2.welltestedsolution.RealTest
|
jtulach@52
|
32 |
public class RealTest extends TestCase {
|
jtulach@52
|
33 |
static {
|
jtulach@52
|
34 |
// your code shall run without any permissions
|
jtulach@52
|
35 |
}
|
jtulach@52
|
36 |
|
jtulach@52
|
37 |
public RealTest(String testName) {
|
jtulach@52
|
38 |
super(testName);
|
jtulach@52
|
39 |
}
|
jtulach@52
|
40 |
|
jtulach@52
|
41 |
protected void setUp() throws Exception {
|
jtulach@52
|
42 |
}
|
jtulach@52
|
43 |
|
jtulach@52
|
44 |
protected void tearDown() throws Exception {
|
jtulach@52
|
45 |
}
|
jtulach@52
|
46 |
|
jtulach@52
|
47 |
|
jtulach@52
|
48 |
/** First of all create a circuit which will evaluate
|
jtulach@52
|
49 |
* expression (X1 and X2) or not(x1). Hold the circuit
|
jtulach@52
|
50 |
* in some variable.
|
jtulach@52
|
51 |
*
|
jtulach@52
|
52 |
* Feed this circuit with x1=true, x2=false, assert result is false
|
jtulach@52
|
53 |
*
|
jtulach@52
|
54 |
* Feed the same circuit with x1=false, x2=true, assert result is true
|
jtulach@52
|
55 |
*
|
jtulach@52
|
56 |
* Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
|
jtulach@52
|
57 |
*
|
jtulach@52
|
58 |
* Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
|
jtulach@52
|
59 |
*
|
jtulach@132
|
60 |
* Feed the same circuit with x1=0.0, x2=2.0
|
jtulach@132
|
61 |
* , make sure it throws an exception
|
jtulach@52
|
62 |
*/
|
jtulach@52
|
63 |
public void testX1andX2orNotX1() {
|
jtulach@52
|
64 |
Circuit c = Circuit.createOrCircuit(
|
jtulach@132
|
65 |
Circuit.createAndCircuit(Circuit.input(0),
|
jtulach@132
|
66 |
Circuit.input(1)),
|
jtulach@132
|
67 |
Circuit.createNotCircuit(Circuit.input(0))
|
jtulach@132
|
68 |
);
|
jtulach@52
|
69 |
assertFalse("true, false", c.evaluate(true, false));
|
jtulach@52
|
70 |
assertTrue("false, true", c.evaluate(false, true));
|
jtulach@52
|
71 |
assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
|
jtulach@52
|
72 |
}
|
jtulach@52
|
73 |
|
jtulach@52
|
74 |
/** Ensure that one variable cannot be filled with two different values.
|
jtulach@52
|
75 |
* Create a circuit for x1 and x1. Make sure that for any usage of your
|
jtulach@52
|
76 |
* API that would not lead to x1 * x1 result, an exception is thrown.
|
jtulach@52
|
77 |
* For example if there was a way to feed the circuit with two different
|
jtulach@52
|
78 |
* values 0.3 and 0.5 an exception is thrown indicating that this is
|
jtulach@52
|
79 |
* improper use of the circuit.
|
jtulach@52
|
80 |
*/
|
jtulach@52
|
81 |
public void testImproperUseOfTheCircuit() {
|
jtulach@52
|
82 |
// does not apply
|
jtulach@52
|
83 |
|
jtulach@52
|
84 |
Circuit x1 = Circuit.input(0);
|
jtulach@52
|
85 |
Circuit c = Circuit.createOrCircuit(x1, x1);
|
jtulach@52
|
86 |
assertTrue("x1 or x1", c.evaluate(true));
|
jtulach@52
|
87 |
assertFalse("x1 or x1", c.evaluate(false));
|
jtulach@52
|
88 |
try {
|
jtulach@52
|
89 |
c.evaluate();
|
jtulach@52
|
90 |
fail("x1 or x1 with wrong params");
|
jtulach@52
|
91 |
} catch (IllegalArgumentException iea) {
|
jtulach@52
|
92 |
//expected
|
jtulach@52
|
93 |
}
|
jtulach@52
|
94 |
// the same with two instances of pin
|
jtulach@52
|
95 |
c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0));
|
jtulach@52
|
96 |
assertTrue("x1 or x1", c.evaluate(true));
|
jtulach@52
|
97 |
assertTrue("x1 or x1", c.evaluate(true, false));
|
jtulach@52
|
98 |
assertTrue("x1 or x1", c.evaluate(true, true));
|
jtulach@52
|
99 |
assertFalse("x1 or x1", c.evaluate(false));
|
jtulach@52
|
100 |
try {
|
jtulach@52
|
101 |
c.evaluate();
|
jtulach@52
|
102 |
fail("x1 or x1 with wrong params");
|
jtulach@52
|
103 |
} catch (IllegalArgumentException iea) {
|
jtulach@52
|
104 |
//expected
|
jtulach@52
|
105 |
}
|
jtulach@52
|
106 |
}
|
jtulach@52
|
107 |
|
jtulach@132
|
108 |
/** Write your own element type called "gte" that
|
jtulach@132
|
109 |
* will have two inputs and one output.
|
jtulach@52
|
110 |
* The output value will be 1 if x1 >= x2 and 0 otherwise.
|
jtulach@52
|
111 |
*
|
jtulach@52
|
112 |
* Create
|
jtulach@52
|
113 |
* circuit for following expression: (x1 and not(x1)) gte x1
|
jtulach@52
|
114 |
*
|
jtulach@52
|
115 |
* Feed the circuit with 0.5 and verify the result is 0
|
jtulach@52
|
116 |
*
|
jtulach@52
|
117 |
* Feed the same circuit with 1 and verify the result is 0
|
jtulach@52
|
118 |
*
|
jtulach@52
|
119 |
* Feed the same circuit with 0 and verify the result is 1
|
jtulach@52
|
120 |
*/
|
jtulach@52
|
121 |
public void testGreaterThanEqualElement() {
|
jtulach@52
|
122 |
Circuit gte = new Gte(Circuit.createAndCircuit(
|
jtulach@132
|
123 |
Circuit.input(0),
|
jtulach@132
|
124 |
Circuit.createNotCircuit(Circuit.input(0))),
|
jtulach@132
|
125 |
Circuit.input(0)
|
jtulach@132
|
126 |
);
|
jtulach@52
|
127 |
assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0);
|
jtulach@52
|
128 |
assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0);
|
jtulach@52
|
129 |
assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0);
|
jtulach@52
|
130 |
|
jtulach@52
|
131 |
}
|
jtulach@52
|
132 |
|
jtulach@52
|
133 |
public void testSilly() {
|
jtulach@52
|
134 |
// (x1 and not x2) or x3
|
jtulach@52
|
135 |
Circuit c = Circuit.createOrCircuit(
|
jtulach@132
|
136 |
Circuit.createAndCircuit(
|
jtulach@132
|
137 |
null,
|
jtulach@132
|
138 |
Circuit.createNotCircuit(null)),
|
jtulach@132
|
139 |
null
|
jtulach@132
|
140 |
);
|
jtulach@52
|
141 |
assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0);
|
jtulach@52
|
142 |
assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0);
|
jtulach@52
|
143 |
assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0);
|
jtulach@52
|
144 |
assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0);
|
jtulach@52
|
145 |
assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0);
|
jtulach@52
|
146 |
assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0);
|
jtulach@52
|
147 |
assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0);
|
jtulach@52
|
148 |
assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0);
|
jtulach@52
|
149 |
}
|
jtulach@52
|
150 |
}
|
jtulach@54
|
151 |
// END: apifest.day2.welltestedsolution.RealTest
|
jtulach@54
|
152 |
|