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
jtulach@52
     1
package org.netbeans.apifest.boolcircuit;
jtulach@52
     2
jtulach@52
     3
import junit.framework.TestCase;
jtulach@52
     4
import junit.framework.*;
jtulach@52
     5
import org.netbeans.apifest.custom.Gte;
jtulach@52
     6
jtulach@52
     7
/** This file contains the APIFest quest for day 2. Simply, turn the 
jtulach@154
     8
 * boolean circuit into circuit that can compute with double 
jtulach@154
     9
 * values from 0 to 1.
jtulach@52
    10
 * <p>
jtulach@52
    11
 * This means that where ever a boolean was used to represent input or 
jtulach@52
    12
 * output values, one can now use any double number from >= 0 and <= 1.
jtulach@52
    13
 * Still, to support backward compatibility, the operations with booleans
jtulach@52
    14
 * has to be kept available and have to work. In fact False shall be 
jtulach@52
    15
 * treated as 0 and True as 1.
jtulach@52
    16
 * <p>
jtulach@52
    17
 * The basic elements has to be modified to work on doubles in the following
jtulach@52
    18
 * way:
jtulach@52
    19
 * <ul>
jtulach@154
    20
 *   <li>negation - neg(x) = 1 - x
jtulach@154
    21
 *   <li>and - and(x,y) = x * y
jtulach@154
    22
 *   <li>or - or(x,y) = 1 - (1 - x) * (1 - y)
jtulach@52
    23
 * </ul>
jtulach@52
    24
 * <p>
jtulach@154
    25
 * However as the circuits with doubles are more rich than plain 
jtulach@154
    26
 * boolean circuits, there is additional requirement to allow any user 
jtulach@154
    27
 * of your API to write its own "element" type. This is all going to 
jtulach@154
    28
 * be exercise in the tests bellow
jtulach@52
    29
 * which you are supposed to implement.
jtulach@52
    30
 */
jtulach@54
    31
// BEGIN: apifest.day2.welltestedsolution.RealTest
jtulach@52
    32
public class RealTest extends TestCase {
jtulach@52
    33
    static {
jtulach@52
    34
        // your code shall run without any permissions
jtulach@52
    35
    }
jtulach@52
    36
    
jtulach@52
    37
    public RealTest(String testName) {
jtulach@52
    38
        super(testName);
jtulach@52
    39
    }
jtulach@52
    40
jtulach@52
    41
    protected void setUp() throws Exception {
jtulach@52
    42
    }
jtulach@52
    43
jtulach@52
    44
    protected void tearDown() throws Exception {
jtulach@52
    45
    }
jtulach@52
    46
    
jtulach@52
    47
    
jtulach@52
    48
    /** First of all create a circuit which will evaluate
jtulach@52
    49
     * expression (X1 and X2) or not(x1). Hold the circuit
jtulach@52
    50
     * in some variable.
jtulach@52
    51
     *
jtulach@52
    52
     * Feed this circuit with x1=true, x2=false, assert result is false
jtulach@52
    53
     *
jtulach@154
    54
     * Feed the same circuit with x1=false, x2=true, assert result is 
jtulach@154
    55
     * true
jtulach@52
    56
     *
jtulach@52
    57
     * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
jtulach@52
    58
     *
jtulach@52
    59
     * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
jtulach@52
    60
     *
jtulach@154
    61
     * Feed the same circuit with x1=0.0, x2=2.0
jtulach@154
    62
     * , make sure it throws an exception
jtulach@52
    63
     */
jtulach@52
    64
    public void testX1andX2orNotX1() {
jtulach@52
    65
        Circuit c = Circuit.createOrCircuit(
jtulach@154
    66
            Circuit.createAndCircuit(Circuit.input(0), 
jtulach@154
    67
            Circuit.input(1)),
jtulach@154
    68
            Circuit.createNotCircuit(Circuit.input(0))
jtulach@154
    69
        );
jtulach@52
    70
        assertFalse("true, false", c.evaluate(true, false));
jtulach@52
    71
        assertTrue("false, true", c.evaluate(false, true));
jtulach@52
    72
        assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
jtulach@52
    73
    }
jtulach@52
    74
    
jtulach@154
    75
    /** Ensure that one variable cannot be filled with two different 
jtulach@154
    76
     * values. Create a circuit for x1 and x1. Make sure that for any 
jtulach@154
    77
     * usage of your API that would not lead to x1 * x1 result, an 
jtulach@154
    78
     * exception is thrown. For example if there was a way to feed the 
jtulach@154
    79
     * circuit with two different values 0.3 and 0.5 an exception is 
jtulach@154
    80
     * thrown indicating that this is improper use of the circuit.
jtulach@52
    81
     */
jtulach@52
    82
    public void testImproperUseOfTheCircuit() {
jtulach@52
    83
        // does not apply
jtulach@52
    84
        
jtulach@52
    85
        Circuit x1 = Circuit.input(0);
jtulach@52
    86
        Circuit c = Circuit.createOrCircuit(x1, x1);
jtulach@52
    87
        assertTrue("x1 or x1", c.evaluate(true));
jtulach@52
    88
        assertFalse("x1 or x1", c.evaluate(false));
jtulach@52
    89
        try {
jtulach@52
    90
            c.evaluate();
jtulach@52
    91
            fail("x1 or x1 with wrong params");
jtulach@52
    92
        } catch (IllegalArgumentException iea) {
jtulach@52
    93
            //expected
jtulach@52
    94
        }
jtulach@52
    95
        // the same with two instances of pin
jtulach@52
    96
        c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0));
jtulach@52
    97
        assertTrue("x1 or x1", c.evaluate(true));
jtulach@52
    98
        assertTrue("x1 or x1", c.evaluate(true, false));
jtulach@52
    99
        assertTrue("x1 or x1", c.evaluate(true, true));
jtulach@52
   100
        assertFalse("x1 or x1", c.evaluate(false));
jtulach@52
   101
        try {
jtulach@52
   102
            c.evaluate();
jtulach@52
   103
            fail("x1 or x1 with wrong params");
jtulach@52
   104
        } catch (IllegalArgumentException iea) {
jtulach@52
   105
            //expected
jtulach@52
   106
        }
jtulach@52
   107
    }
jtulach@52
   108
    
jtulach@154
   109
    /** Write your own element type called "gte" that 
jtulach@154
   110
     * will have two inputs and one output.
jtulach@52
   111
     * The output value will be 1 if x1 >= x2 and 0 otherwise. 
jtulach@52
   112
     * 
jtulach@52
   113
     * Create 
jtulach@52
   114
     * circuit for following expression: (x1 and not(x1)) gte x1
jtulach@52
   115
     *
jtulach@52
   116
     * Feed the circuit with 0.5 and verify the result is 0
jtulach@52
   117
     *
jtulach@52
   118
     * Feed the same circuit with 1 and verify the result is 0
jtulach@52
   119
     *
jtulach@52
   120
     * Feed the same circuit with 0 and verify the result is 1
jtulach@52
   121
     */
jtulach@52
   122
    public void testGreaterThanEqualElement() {
jtulach@52
   123
        Circuit gte = new Gte(Circuit.createAndCircuit(
jtulach@154
   124
            Circuit.input(0),
jtulach@154
   125
            Circuit.createNotCircuit(Circuit.input(0))),
jtulach@154
   126
            Circuit.input(0)
jtulach@154
   127
        );
jtulach@52
   128
        assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0);
jtulach@52
   129
        assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0);
jtulach@52
   130
        assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0);
jtulach@52
   131
        
jtulach@52
   132
    }
jtulach@52
   133
    
jtulach@52
   134
    public void testSilly() {
jtulach@52
   135
        // (x1 and not x2) or x3
jtulach@52
   136
        Circuit c = Circuit.createOrCircuit(
jtulach@154
   137
            Circuit.createAndCircuit(
jtulach@154
   138
            null,
jtulach@154
   139
            Circuit.createNotCircuit(null)),
jtulach@154
   140
            null
jtulach@154
   141
        );
jtulach@52
   142
        assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0);
jtulach@52
   143
        assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0);
jtulach@52
   144
        assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0);
jtulach@52
   145
        assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0);
jtulach@52
   146
        assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0);
jtulach@52
   147
        assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0);
jtulach@52
   148
        assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0);
jtulach@52
   149
        assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0);
jtulach@52
   150
    }
jtulach@52
   151
}
jtulach@54
   152
// END: apifest.day2.welltestedsolution.RealTest
jtulach@54
   153