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