samples/apifest1/day2/welltestedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:56:12 +0200
changeset 132 3bc4c54f4bcc
parent 54 45b0d58e66ca
child 133 50bf1b976c0d
permissions -rw-r--r--
Truncating all examples to 80 characters per line
     1 package org.netbeans.apifest.boolcircuit;
     2 
     3 import junit.framework.TestCase;
     4 import junit.framework.*;
     5 import org.netbeans.apifest.custom.Gte;
     6 
     7 /** This file contains the APIFest quest for day 2. Simply, turn the 
     8  * boolean circuit into circuit that can compute with double 
     9  * values from 0 to 1.
    10  * <p>
    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.
    16  * <p>
    17  * The basic elements has to be modified to work on doubles in the following
    18  * way:
    19  * <ul>
    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)
    23  * </ul>
    24  * <p>
    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.
    30  */
    31 // BEGIN: apifest.day2.welltestedsolution.RealTest
    32 public class RealTest extends TestCase {
    33     static {
    34         // your code shall run without any permissions
    35     }
    36     
    37     public RealTest(String testName) {
    38         super(testName);
    39     }
    40 
    41     protected void setUp() throws Exception {
    42     }
    43 
    44     protected void tearDown() throws Exception {
    45     }
    46     
    47     
    48     /** First of all create a circuit which will evaluate
    49      * expression (X1 and X2) or not(x1). Hold the circuit
    50      * in some variable.
    51      *
    52      * Feed this circuit with x1=true, x2=false, assert result is false
    53      *
    54      * Feed the same circuit with x1=false, x2=true, assert result is true
    55      *
    56      * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    57      *
    58      * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    59      *
    60      * Feed the same circuit with x1=0.0, x2=2.0
    61      * , make sure it throws an exception
    62      */
    63     public void testX1andX2orNotX1() {
    64         Circuit c = Circuit.createOrCircuit(
    65             Circuit.createAndCircuit(Circuit.input(0), 
    66             Circuit.input(1)),
    67             Circuit.createNotCircuit(Circuit.input(0))
    68         );
    69         assertFalse("true, false", c.evaluate(true, false));
    70         assertTrue("false, true", c.evaluate(false, true));
    71         assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
    72     }
    73     
    74     /** Ensure that one variable cannot be filled with two different values.
    75      * Create a circuit for x1 and x1. Make sure that for any usage of your
    76      * API that would not lead to x1 * x1 result, an exception is thrown.
    77      * For example if there was a way to feed the circuit with two different 
    78      * values 0.3 and 0.5 an exception is thrown indicating that this is 
    79      * improper use of the circuit.
    80      */
    81     public void testImproperUseOfTheCircuit() {
    82         // does not apply
    83         
    84         Circuit x1 = Circuit.input(0);
    85         Circuit c = Circuit.createOrCircuit(x1, x1);
    86         assertTrue("x1 or x1", c.evaluate(true));
    87         assertFalse("x1 or x1", c.evaluate(false));
    88         try {
    89             c.evaluate();
    90             fail("x1 or x1 with wrong params");
    91         } catch (IllegalArgumentException iea) {
    92             //expected
    93         }
    94         // the same with two instances of pin
    95         c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0));
    96         assertTrue("x1 or x1", c.evaluate(true));
    97         assertTrue("x1 or x1", c.evaluate(true, false));
    98         assertTrue("x1 or x1", c.evaluate(true, true));
    99         assertFalse("x1 or x1", c.evaluate(false));
   100         try {
   101             c.evaluate();
   102             fail("x1 or x1 with wrong params");
   103         } catch (IllegalArgumentException iea) {
   104             //expected
   105         }
   106     }
   107     
   108     /** Write your own element type called "gte" that 
   109      * will have two inputs and one output.
   110      * The output value will be 1 if x1 >= x2 and 0 otherwise. 
   111      * 
   112      * Create 
   113      * circuit for following expression: (x1 and not(x1)) gte x1
   114      *
   115      * Feed the circuit with 0.5 and verify the result is 0
   116      *
   117      * Feed the same circuit with 1 and verify the result is 0
   118      *
   119      * Feed the same circuit with 0 and verify the result is 1
   120      */
   121     public void testGreaterThanEqualElement() {
   122         Circuit gte = new Gte(Circuit.createAndCircuit(
   123             Circuit.input(0),
   124             Circuit.createNotCircuit(Circuit.input(0))),
   125             Circuit.input(0)
   126         );
   127         assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0);
   128         assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0);
   129         assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0);
   130         
   131     }
   132     
   133     public void testSilly() {
   134         // (x1 and not x2) or x3
   135         Circuit c = Circuit.createOrCircuit(
   136             Circuit.createAndCircuit(
   137             null,
   138             Circuit.createNotCircuit(null)),
   139             null
   140         );
   141         assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0);
   142         assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0);
   143         assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0);
   144         assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0);
   145         assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0);
   146         assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0);
   147         assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0);
   148         assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0);
   149     }
   150 }
   151 // END: apifest.day2.welltestedsolution.RealTest
   152