jtulach@153
|
1 |
/*
|
jtulach@153
|
2 |
* The contents of this file are subject to the terms of the Common Development
|
jtulach@153
|
3 |
* and Distribution License (the License). You may not use this file except in
|
jtulach@153
|
4 |
* compliance with the License.
|
jtulach@153
|
5 |
*
|
jtulach@153
|
6 |
* You can obtain a copy of the License at http://www.netbeans.org/cddl.html
|
jtulach@153
|
7 |
* or http://www.netbeans.org/cddl.txt.
|
jtulach@153
|
8 |
*
|
jtulach@153
|
9 |
* When distributing Covered Code, include this CDDL Header Notice in each file
|
jtulach@153
|
10 |
* and include the License file at http://www.netbeans.org/cddl.txt.
|
jtulach@153
|
11 |
* If applicable, add the following below the CDDL Header, with the fields
|
jtulach@153
|
12 |
* enclosed by brackets [] replaced by your own identifying information:
|
jtulach@153
|
13 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jtulach@153
|
14 |
*
|
jtulach@153
|
15 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jtulach@153
|
16 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
|
jtulach@153
|
17 |
* Microsystems, Inc. All Rights Reserved.
|
jtulach@153
|
18 |
*/
|
jtulach@153
|
19 |
|
jtulach@52
|
20 |
package org.netbeans.apifest.boolcircuit;
|
jtulach@52
|
21 |
|
jtulach@153
|
22 |
import java.security.CodeSource;
|
jtulach@153
|
23 |
import java.security.Permission;
|
jtulach@153
|
24 |
import java.security.PermissionCollection;
|
jtulach@153
|
25 |
import java.security.Policy;
|
jtulach@153
|
26 |
import java.util.Collection;
|
jtulach@153
|
27 |
import java.util.Collections;
|
jtulach@153
|
28 |
import java.util.Enumeration;
|
jtulach@52
|
29 |
import junit.framework.TestCase;
|
jtulach@52
|
30 |
import junit.framework.*;
|
jtulach@52
|
31 |
import org.netbeans.apifest.custom.Gte;
|
jtulach@52
|
32 |
|
jtulach@52
|
33 |
/** This file contains the APIFest quest for day 2. Simply, turn the
|
jtulach@153
|
34 |
* boolean circuit into circuit that can compute with double values from 0 to 1.
|
jtulach@52
|
35 |
* <p>
|
jtulach@52
|
36 |
* This means that where ever a boolean was used to represent input or
|
jtulach@52
|
37 |
* output values, one can now use any double number from >= 0 and <= 1.
|
jtulach@52
|
38 |
* Still, to support backward compatibility, the operations with booleans
|
jtulach@52
|
39 |
* has to be kept available and have to work. In fact False shall be
|
jtulach@52
|
40 |
* treated as 0 and True as 1.
|
jtulach@52
|
41 |
* <p>
|
jtulach@52
|
42 |
* The basic elements has to be modified to work on doubles in the following
|
jtulach@52
|
43 |
* way:
|
jtulach@52
|
44 |
* <ul>
|
jtulach@153
|
45 |
* <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
|
jtulach@153
|
46 |
* <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
|
jtulach@153
|
47 |
* and(false,true)=0*1=0=false
|
jtulach@153
|
48 |
* <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
|
jtulach@153
|
49 |
* or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
|
jtulach@153
|
50 |
* or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
|
jtulach@52
|
51 |
* </ul>
|
jtulach@52
|
52 |
* <p>
|
jtulach@153
|
53 |
* However as the circuits with doubles are more rich than plain boolean circuits,
|
jtulach@153
|
54 |
* there is additional requirement to allow any user of your API to write its
|
jtulach@153
|
55 |
* own "element" type. This is all going to be exercise in the tests bellow
|
jtulach@52
|
56 |
* which you are supposed to implement.
|
jtulach@52
|
57 |
*/
|
jtulach@54
|
58 |
// BEGIN: apifest.day2.welltestedsolution.RealTest
|
jtulach@52
|
59 |
public class RealTest extends TestCase {
|
jtulach@52
|
60 |
static {
|
jtulach@52
|
61 |
// your code shall run without any permissions
|
jtulach@52
|
62 |
}
|
jtulach@52
|
63 |
|
jtulach@52
|
64 |
public RealTest(String testName) {
|
jtulach@52
|
65 |
super(testName);
|
jtulach@52
|
66 |
}
|
jtulach@52
|
67 |
|
jtulach@52
|
68 |
protected void setUp() throws Exception {
|
jtulach@52
|
69 |
}
|
jtulach@52
|
70 |
|
jtulach@52
|
71 |
protected void tearDown() throws Exception {
|
jtulach@52
|
72 |
}
|
jtulach@52
|
73 |
|
jtulach@52
|
74 |
|
jtulach@52
|
75 |
/** First of all create a circuit which will evaluate
|
jtulach@52
|
76 |
* expression (X1 and X2) or not(x1). Hold the circuit
|
jtulach@52
|
77 |
* in some variable.
|
jtulach@52
|
78 |
*
|
jtulach@52
|
79 |
* Feed this circuit with x1=true, x2=false, assert result is false
|
jtulach@52
|
80 |
*
|
jtulach@153
|
81 |
* Feed the same circuit with x1=false, x2=true, assert result is true
|
jtulach@52
|
82 |
*
|
jtulach@52
|
83 |
* Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
|
jtulach@52
|
84 |
*
|
jtulach@52
|
85 |
* Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
|
jtulach@52
|
86 |
*
|
jtulach@153
|
87 |
* Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
|
jtulach@52
|
88 |
*/
|
jtulach@52
|
89 |
public void testX1andX2orNotX1() {
|
jtulach@52
|
90 |
Circuit c = Circuit.createOrCircuit(
|
jtulach@153
|
91 |
Circuit.createAndCircuit(Circuit.input(0), Circuit.input(1)),
|
jtulach@153
|
92 |
Circuit.createNotCircuit(Circuit.input(0))
|
jtulach@153
|
93 |
);
|
jtulach@52
|
94 |
assertFalse("true, false", c.evaluate(true, false));
|
jtulach@52
|
95 |
assertTrue("false, true", c.evaluate(false, true));
|
jtulach@52
|
96 |
assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
|
jtulach@52
|
97 |
}
|
jtulach@52
|
98 |
|
jtulach@153
|
99 |
/** Ensure that one variable cannot be filled with two different values.
|
jtulach@153
|
100 |
* Create a circuit for x1 and x1. Make sure that for any usage of your
|
jtulach@153
|
101 |
* API that would not lead to x1 * x1 result, an exception is thrown.
|
jtulach@153
|
102 |
* For example if there was a way to feed the circuit with two different
|
jtulach@153
|
103 |
* values 0.3 and 0.5 an exception is thrown indicating that this is
|
jtulach@153
|
104 |
* improper use of the circuit.
|
jtulach@52
|
105 |
*/
|
jtulach@52
|
106 |
public void testImproperUseOfTheCircuit() {
|
jtulach@52
|
107 |
// does not apply
|
jtulach@52
|
108 |
|
jtulach@52
|
109 |
Circuit x1 = Circuit.input(0);
|
jtulach@52
|
110 |
Circuit c = Circuit.createOrCircuit(x1, x1);
|
jtulach@52
|
111 |
assertTrue("x1 or x1", c.evaluate(true));
|
jtulach@52
|
112 |
assertFalse("x1 or x1", c.evaluate(false));
|
jtulach@52
|
113 |
try {
|
jtulach@52
|
114 |
c.evaluate();
|
jtulach@52
|
115 |
fail("x1 or x1 with wrong params");
|
jtulach@52
|
116 |
} catch (IllegalArgumentException iea) {
|
jtulach@52
|
117 |
//expected
|
jtulach@52
|
118 |
}
|
jtulach@52
|
119 |
// the same with two instances of pin
|
jtulach@52
|
120 |
c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0));
|
jtulach@52
|
121 |
assertTrue("x1 or x1", c.evaluate(true));
|
jtulach@52
|
122 |
assertTrue("x1 or x1", c.evaluate(true, false));
|
jtulach@52
|
123 |
assertTrue("x1 or x1", c.evaluate(true, true));
|
jtulach@52
|
124 |
assertFalse("x1 or x1", c.evaluate(false));
|
jtulach@52
|
125 |
try {
|
jtulach@52
|
126 |
c.evaluate();
|
jtulach@52
|
127 |
fail("x1 or x1 with wrong params");
|
jtulach@52
|
128 |
} catch (IllegalArgumentException iea) {
|
jtulach@52
|
129 |
//expected
|
jtulach@52
|
130 |
}
|
jtulach@52
|
131 |
}
|
jtulach@52
|
132 |
|
jtulach@153
|
133 |
/** Write your own element type called "gte" that will have two inputs and one output.
|
jtulach@52
|
134 |
* The output value will be 1 if x1 >= x2 and 0 otherwise.
|
jtulach@52
|
135 |
*
|
jtulach@52
|
136 |
* Create
|
jtulach@52
|
137 |
* circuit for following expression: (x1 and not(x1)) gte x1
|
jtulach@52
|
138 |
*
|
jtulach@52
|
139 |
* Feed the circuit with 0.5 and verify the result is 0
|
jtulach@52
|
140 |
*
|
jtulach@52
|
141 |
* Feed the same circuit with 1 and verify the result is 0
|
jtulach@52
|
142 |
*
|
jtulach@52
|
143 |
* Feed the same circuit with 0 and verify the result is 1
|
jtulach@52
|
144 |
*/
|
jtulach@52
|
145 |
public void testGreaterThanEqualElement() {
|
jtulach@52
|
146 |
Circuit gte = new Gte(Circuit.createAndCircuit(
|
jtulach@153
|
147 |
Circuit.input(0),
|
jtulach@153
|
148 |
Circuit.createNotCircuit(Circuit.input(0))),
|
jtulach@153
|
149 |
Circuit.input(0)
|
jtulach@153
|
150 |
);
|
jtulach@52
|
151 |
assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0);
|
jtulach@52
|
152 |
assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0);
|
jtulach@52
|
153 |
assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0);
|
jtulach@52
|
154 |
|
jtulach@52
|
155 |
}
|
jtulach@52
|
156 |
|
jtulach@52
|
157 |
public void testSilly() {
|
jtulach@52
|
158 |
// (x1 and not x2) or x3
|
jtulach@52
|
159 |
Circuit c = Circuit.createOrCircuit(
|
jtulach@153
|
160 |
Circuit.createAndCircuit(
|
jtulach@153
|
161 |
null,
|
jtulach@153
|
162 |
Circuit.createNotCircuit(null)),
|
jtulach@153
|
163 |
null
|
jtulach@153
|
164 |
);
|
jtulach@52
|
165 |
assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0);
|
jtulach@52
|
166 |
assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0);
|
jtulach@52
|
167 |
assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0);
|
jtulach@52
|
168 |
assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0);
|
jtulach@52
|
169 |
assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0);
|
jtulach@52
|
170 |
assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0);
|
jtulach@52
|
171 |
assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0);
|
jtulach@52
|
172 |
assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0);
|
jtulach@52
|
173 |
}
|
jtulach@52
|
174 |
}
|
jtulach@54
|
175 |
// END: apifest.day2.welltestedsolution.RealTest
|
jtulach@54
|
176 |
|