jtulach@52: package org.netbeans.apifest.boolcircuit; jtulach@52: jtulach@52: import junit.framework.TestCase; jtulach@52: import junit.framework.*; jtulach@52: import org.netbeans.apifest.custom.Gte; jtulach@52: jtulach@52: /** This file contains the APIFest quest for day 2. Simply, turn the jtulach@132: * boolean circuit into circuit that can compute with double jtulach@132: * values from 0 to 1. jtulach@52: *

jtulach@52: * This means that where ever a boolean was used to represent input or jtulach@52: * output values, one can now use any double number from >= 0 and <= 1. jtulach@52: * Still, to support backward compatibility, the operations with booleans jtulach@52: * has to be kept available and have to work. In fact False shall be jtulach@52: * treated as 0 and True as 1. jtulach@52: *

jtulach@52: * The basic elements has to be modified to work on doubles in the following jtulach@52: * way: jtulach@52: *

jtulach@52: *

jtulach@132: * However as the circuits with doubles are more rich than plain jtulach@132: * boolean circuits, there is additional requirement to allow any user jtulach@132: * of your API to write its own "element" type. This is all going to jtulach@132: * be exercise in the tests bellow jtulach@52: * which you are supposed to implement. jtulach@52: */ jtulach@54: // BEGIN: apifest.day2.welltestedsolution.RealTest jtulach@52: public class RealTest extends TestCase { jtulach@52: static { jtulach@52: // your code shall run without any permissions jtulach@52: } jtulach@52: jtulach@52: public RealTest(String testName) { jtulach@52: super(testName); jtulach@52: } jtulach@52: jtulach@52: protected void setUp() throws Exception { jtulach@52: } jtulach@52: jtulach@52: protected void tearDown() throws Exception { jtulach@52: } jtulach@52: jtulach@52: jtulach@52: /** First of all create a circuit which will evaluate jtulach@52: * expression (X1 and X2) or not(x1). Hold the circuit jtulach@52: * in some variable. jtulach@52: * jtulach@52: * Feed this circuit with x1=true, x2=false, assert result is false jtulach@52: * jtulach@52: * Feed the same circuit with x1=false, x2=true, assert result is true jtulach@52: * jtulach@52: * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0 jtulach@52: * jtulach@52: * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625 jtulach@52: * jtulach@132: * Feed the same circuit with x1=0.0, x2=2.0 jtulach@132: * , make sure it throws an exception jtulach@52: */ jtulach@52: public void testX1andX2orNotX1() { jtulach@52: Circuit c = Circuit.createOrCircuit( jtulach@132: Circuit.createAndCircuit(Circuit.input(0), jtulach@132: Circuit.input(1)), jtulach@132: Circuit.createNotCircuit(Circuit.input(0)) jtulach@132: ); jtulach@52: assertFalse("true, false", c.evaluate(true, false)); jtulach@52: assertTrue("false, true", c.evaluate(false, true)); jtulach@52: assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0); jtulach@52: } jtulach@52: jtulach@52: /** Ensure that one variable cannot be filled with two different values. jtulach@52: * Create a circuit for x1 and x1. Make sure that for any usage of your jtulach@52: * API that would not lead to x1 * x1 result, an exception is thrown. jtulach@52: * For example if there was a way to feed the circuit with two different jtulach@52: * values 0.3 and 0.5 an exception is thrown indicating that this is jtulach@52: * improper use of the circuit. jtulach@52: */ jtulach@52: public void testImproperUseOfTheCircuit() { jtulach@52: // does not apply jtulach@52: jtulach@52: Circuit x1 = Circuit.input(0); jtulach@52: Circuit c = Circuit.createOrCircuit(x1, x1); jtulach@52: assertTrue("x1 or x1", c.evaluate(true)); jtulach@52: assertFalse("x1 or x1", c.evaluate(false)); jtulach@52: try { jtulach@52: c.evaluate(); jtulach@52: fail("x1 or x1 with wrong params"); jtulach@52: } catch (IllegalArgumentException iea) { jtulach@52: //expected jtulach@52: } jtulach@52: // the same with two instances of pin jtulach@52: c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0)); jtulach@52: assertTrue("x1 or x1", c.evaluate(true)); jtulach@52: assertTrue("x1 or x1", c.evaluate(true, false)); jtulach@52: assertTrue("x1 or x1", c.evaluate(true, true)); jtulach@52: assertFalse("x1 or x1", c.evaluate(false)); jtulach@52: try { jtulach@52: c.evaluate(); jtulach@52: fail("x1 or x1 with wrong params"); jtulach@52: } catch (IllegalArgumentException iea) { jtulach@52: //expected jtulach@52: } jtulach@52: } jtulach@52: jtulach@132: /** Write your own element type called "gte" that jtulach@132: * will have two inputs and one output. jtulach@52: * The output value will be 1 if x1 >= x2 and 0 otherwise. jtulach@52: * jtulach@52: * Create jtulach@52: * circuit for following expression: (x1 and not(x1)) gte x1 jtulach@52: * jtulach@52: * Feed the circuit with 0.5 and verify the result is 0 jtulach@52: * jtulach@52: * Feed the same circuit with 1 and verify the result is 0 jtulach@52: * jtulach@52: * Feed the same circuit with 0 and verify the result is 1 jtulach@52: */ jtulach@52: public void testGreaterThanEqualElement() { jtulach@52: Circuit gte = new Gte(Circuit.createAndCircuit( jtulach@132: Circuit.input(0), jtulach@132: Circuit.createNotCircuit(Circuit.input(0))), jtulach@132: Circuit.input(0) jtulach@132: ); jtulach@52: assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0); jtulach@52: assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0); jtulach@52: assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0); jtulach@52: jtulach@52: } jtulach@52: jtulach@52: public void testSilly() { jtulach@52: // (x1 and not x2) or x3 jtulach@52: Circuit c = Circuit.createOrCircuit( jtulach@132: Circuit.createAndCircuit( jtulach@132: null, jtulach@132: Circuit.createNotCircuit(null)), jtulach@132: null jtulach@132: ); jtulach@52: assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0); jtulach@52: assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0); jtulach@52: assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0); jtulach@52: assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0); jtulach@52: assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0); jtulach@52: assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0); jtulach@52: assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0); jtulach@52: assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0); jtulach@52: } jtulach@52: } jtulach@54: // END: apifest.day2.welltestedsolution.RealTest jtulach@54: