samples/apifest1/day2/welltestedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:11 +0200
changeset 154 0fd5e9c500b9
parent 153 b5cbb797ec0a
permissions -rw-r--r--
Merge: Geertjan's changs up to 2000
     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 
    55      * true
    56      *
    57      * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    58      *
    59      * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    60      *
    61      * Feed the same circuit with x1=0.0, x2=2.0
    62      * , make sure it throws an exception
    63      */
    64     public void testX1andX2orNotX1() {
    65         Circuit c = Circuit.createOrCircuit(
    66             Circuit.createAndCircuit(Circuit.input(0), 
    67             Circuit.input(1)),
    68             Circuit.createNotCircuit(Circuit.input(0))
    69         );
    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);
    73     }
    74     
    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.
    81      */
    82     public void testImproperUseOfTheCircuit() {
    83         // does not apply
    84         
    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));
    89         try {
    90             c.evaluate();
    91             fail("x1 or x1 with wrong params");
    92         } catch (IllegalArgumentException iea) {
    93             //expected
    94         }
    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));
   101         try {
   102             c.evaluate();
   103             fail("x1 or x1 with wrong params");
   104         } catch (IllegalArgumentException iea) {
   105             //expected
   106         }
   107     }
   108     
   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. 
   112      * 
   113      * Create 
   114      * circuit for following expression: (x1 and not(x1)) gte x1
   115      *
   116      * Feed the circuit with 0.5 and verify the result is 0
   117      *
   118      * Feed the same circuit with 1 and verify the result is 0
   119      *
   120      * Feed the same circuit with 0 and verify the result is 1
   121      */
   122     public void testGreaterThanEqualElement() {
   123         Circuit gte = new Gte(Circuit.createAndCircuit(
   124             Circuit.input(0),
   125             Circuit.createNotCircuit(Circuit.input(0))),
   126             Circuit.input(0)
   127         );
   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);
   131         
   132     }
   133     
   134     public void testSilly() {
   135         // (x1 and not x2) or x3
   136         Circuit c = Circuit.createOrCircuit(
   137             Circuit.createAndCircuit(
   138             null,
   139             Circuit.createNotCircuit(null)),
   140             null
   141         );
   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);
   150     }
   151 }
   152 // END: apifest.day2.welltestedsolution.RealTest
   153