samples/apifest1/day2/welltestedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:52:45 +0200
changeset 52 4257f4cf226b
permissions -rw-r--r--
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
jtulach@52
     1
/*
jtulach@52
     2
 * The contents of this file are subject to the terms of the Common Development
jtulach@52
     3
 * and Distribution License (the License). You may not use this file except in
jtulach@52
     4
 * compliance with the License.
jtulach@52
     5
 *
jtulach@52
     6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
jtulach@52
     7
 * or http://www.netbeans.org/cddl.txt.
jtulach@52
     8
 *
jtulach@52
     9
 * When distributing Covered Code, include this CDDL Header Notice in each file
jtulach@52
    10
 * and include the License file at http://www.netbeans.org/cddl.txt.
jtulach@52
    11
 * If applicable, add the following below the CDDL Header, with the fields
jtulach@52
    12
 * enclosed by brackets [] replaced by your own identifying information:
jtulach@52
    13
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@52
    14
 *
jtulach@52
    15
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@52
    16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@52
    17
 * Microsystems, Inc. All Rights Reserved.
jtulach@52
    18
 */
jtulach@52
    19
jtulach@52
    20
package org.netbeans.apifest.boolcircuit;
jtulach@52
    21
jtulach@52
    22
import java.security.CodeSource;
jtulach@52
    23
import java.security.Permission;
jtulach@52
    24
import java.security.PermissionCollection;
jtulach@52
    25
import java.security.Policy;
jtulach@52
    26
import java.util.Collection;
jtulach@52
    27
import java.util.Collections;
jtulach@52
    28
import java.util.Enumeration;
jtulach@52
    29
import junit.framework.TestCase;
jtulach@52
    30
import junit.framework.*;
jtulach@52
    31
jtulach@52
    32
/** The initial quest for this APIFest is to create an API for boolean 
jtulach@52
    33
 * circuits. Such API shall be able to compose a boolean circuit from
jtulach@52
    34
 * basic elements and evaluate the result given initial values for 
jtulach@52
    35
 * input variables.
jtulach@52
    36
 * <p>
jtulach@52
    37
 * The basic elements include:
jtulach@52
    38
 * <ul>
jtulach@52
    39
 *   <li>negation - has one input and one output and changes 0 on input to 
jtulach@52
    40
 *          on output 1 and 1 to 0
jtulach@52
    41
 *   <li>and - has two inputs and one output. The output is 1 only if both 
jtulach@52
    42
 *          inputs are 1, otherwise it is 0
jtulach@52
    43
 *   <li>or - has two inputs and one output. The output is 1 always, except
jtulach@52
    44
 *          in the case when both inputs are 0
jtulach@52
    45
 * </ul>
jtulach@52
    46
 *
jtulach@52
    47
 * <p>
jtulach@52
    48
 * The boolean circuit can be used to represent boolean formulas and compute
jtulach@52
    49
 * the results for certain values of its inputs. The individual tasks described
jtulach@52
    50
 * as tests bellow.
jtulach@52
    51
 *
jtulach@52
    52
 * <p>
jtulach@52
    53
 * Links of interest:
jtulach@52
    54
 * <ul>
jtulach@52
    55
 *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
jtulach@52
    56
 *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
jtulach@52
    57
 * </ul>
jtulach@52
    58
 */
jtulach@52
    59
public class CircuitTest 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 CircuitTest(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
    /** 
jtulach@52
    76
     * Create a circuit to evaluate x1 and x2 and then
jtulach@52
    77
     * verify that its result is false for input (false, true) and
jtulach@52
    78
     * it is true for input (true, true).
jtulach@52
    79
     */
jtulach@52
    80
    public void testX1andX2() {
jtulach@52
    81
        assertFalse("x1 and x2 for (false, true)", Circuit.createAndCircuit(null, null).evaluate(false, true));
jtulach@52
    82
        assertTrue("x1 and x2 for (true, true)", Circuit.createAndCircuit(null, null).evaluate(true, true));
jtulach@52
    83
    }
jtulach@52
    84
    
jtulach@52
    85
    /** 
jtulach@52
    86
     * Create a circuit to evaluate (x1 and x2) or x3 and then
jtulach@52
    87
     * verify that its result is false for input (false, true, false) and
jtulach@52
    88
     * it is true for input (false, false, true).
jtulach@52
    89
     */
jtulach@52
    90
    public void testX1andX2orX3() {
jtulach@52
    91
        Circuit c = Circuit.createOrCircuit(Circuit.createAndCircuit(null, null), null);
jtulach@52
    92
        assertFalse("(x1 and x2) or x3", c.evaluate(false, true, false));
jtulach@52
    93
        assertTrue("(x1 and x2) or x3", c.evaluate(false, false, true));
jtulach@52
    94
        
jtulach@52
    95
        assertTrue("(x1 and x2) or x3", c.evaluate(true, true, true));
jtulach@52
    96
        assertFalse("(x1 and x2) or x3", c.evaluate(true, false, false));
jtulach@52
    97
    }
jtulach@52
    98
    /** 
jtulach@52
    99
     * Create a circuit to evaluate (x1 or not(x1)) and then
jtulach@52
   100
     * verify that its result is true for all values of x1.
jtulach@52
   101
     */
jtulach@52
   102
    public void testAlwaysTrue() {
jtulach@52
   103
        Circuit c = Circuit.createOrCircuit(null, Circuit.createNotCircuit(null));
jtulach@52
   104
        assertTrue("(x1 or not(x1)) for false", c.evaluate(false, false));
jtulach@52
   105
        assertTrue("(x1 or not(x1)) for false", c.evaluate(true, true));
jtulach@52
   106
    }
jtulach@52
   107
    
jtulach@52
   108
    public void testOr() {
jtulach@52
   109
        Circuit c = Circuit.createOrCircuit(null, null);
jtulach@52
   110
        try {
jtulach@52
   111
            c.evaluate();
jtulach@52
   112
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   113
        }
jtulach@52
   114
        try {
jtulach@52
   115
            c.evaluate(true);
jtulach@52
   116
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   117
        }
jtulach@52
   118
        try {
jtulach@52
   119
            c.evaluate(true, true, true);
jtulach@52
   120
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   121
        }
jtulach@52
   122
        assertFalse("(x1 or x2)", c.evaluate(false, false));
jtulach@52
   123
        assertTrue("(x1 or x2)", c.evaluate(true, false));
jtulach@52
   124
        assertTrue("(x1 or x2)", c.evaluate(false, true));
jtulach@52
   125
        assertTrue("(x1 or x2)", c.evaluate(true, true));
jtulach@52
   126
        assertEquals("0.0, 0.0", 0.0, c.evaluateFuzzy(0.0, 0.0), 0.0);
jtulach@52
   127
        assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
jtulach@52
   128
        assertEquals("1.0, 1.0", 1.0, c.evaluateFuzzy(1.0, 1.0), 0.0);
jtulach@52
   129
        assertEquals("0.3, 0.5", 0.65, c.evaluateFuzzy(0.3, 0.5), 0.0);
jtulach@52
   130
        
jtulach@52
   131
    }
jtulach@52
   132
    
jtulach@52
   133
    public void testOrCorrectly() {
jtulach@52
   134
        Circuit c = Circuit.createOrCircuit(null, null);
jtulach@52
   135
        try {
jtulach@52
   136
            c.evaluate();
jtulach@52
   137
            fail("missing");
jtulach@52
   138
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   139
        }
jtulach@52
   140
        try {
jtulach@52
   141
            c.evaluate(true);
jtulach@52
   142
            fail("missing");
jtulach@52
   143
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   144
        }
jtulach@52
   145
    }
jtulach@52
   146
    
jtulach@52
   147
    public void testAnd() {
jtulach@52
   148
        Circuit c = Circuit.createAndCircuit(null, null);
jtulach@52
   149
        try {
jtulach@52
   150
            c.evaluate();
jtulach@52
   151
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   152
        }
jtulach@52
   153
        try {
jtulach@52
   154
            c.evaluate(true);
jtulach@52
   155
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   156
        }
jtulach@52
   157
        try {
jtulach@52
   158
            c.evaluate(true, true, true);
jtulach@52
   159
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   160
        }
jtulach@52
   161
        assertFalse("(x1 or x2)", c.evaluate(false, false));
jtulach@52
   162
        assertFalse("(x1 or x2)", c.evaluate(true, false));
jtulach@52
   163
        assertFalse("(x1 or x2)", c.evaluate(false, true));
jtulach@52
   164
        assertTrue("(x1 or x2)", c.evaluate(true, true));
jtulach@52
   165
        
jtulach@52
   166
        assertEquals("0.0, 0.0", 0.0, c.evaluateFuzzy(0.0, 0.0), 0.0);
jtulach@52
   167
        assertEquals("0.0, 1.0", 0.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
jtulach@52
   168
        assertEquals("1.0, 1.0", 1.0, c.evaluateFuzzy(1.0, 1.0), 0.0);
jtulach@52
   169
        assertEquals("0.3, 0.5", 0.15, c.evaluateFuzzy(0.3, 0.5), 0.0);
jtulach@52
   170
    }
jtulach@52
   171
    
jtulach@52
   172
    public void testAndCorrectly() {
jtulach@52
   173
        Circuit c = Circuit.createAndCircuit(null, null);
jtulach@52
   174
        try {
jtulach@52
   175
            c.evaluate();
jtulach@52
   176
            fail("missing");
jtulach@52
   177
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   178
        }
jtulach@52
   179
        try {
jtulach@52
   180
            c.evaluate(true);
jtulach@52
   181
            fail("missing");
jtulach@52
   182
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   183
        }
jtulach@52
   184
        // actually if more values are passed they are ignored
jtulach@52
   185
        /*
jtulach@52
   186
        try {
jtulach@52
   187
            c.evaluate(true, true, true);
jtulach@52
   188
            fail("too much");
jtulach@52
   189
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   190
        }
jtulach@52
   191
         */
jtulach@52
   192
    }
jtulach@52
   193
    
jtulach@52
   194
    public void testNot() {
jtulach@52
   195
        Circuit c = Circuit.createNotCircuit(null);
jtulach@52
   196
        try {
jtulach@52
   197
            c.evaluate();
jtulach@52
   198
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   199
        }
jtulach@52
   200
        try {
jtulach@52
   201
            c.evaluate(true, true);
jtulach@52
   202
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   203
        }
jtulach@52
   204
        assertFalse("(x1 or x2)", c.evaluate(true));
jtulach@52
   205
        assertTrue("(x1 or x2)", c.evaluate(false));
jtulach@52
   206
        
jtulach@52
   207
        assertEquals("0.0", 1.0, c.evaluateFuzzy(0.0), 0.0);
jtulach@52
   208
        assertEquals("1.0", 0.0, c.evaluateFuzzy(1.0), 0.0);
jtulach@52
   209
        assertEquals("0.2", 0.8, c.evaluateFuzzy(0.2), 0.0);
jtulach@52
   210
    }
jtulach@52
   211
    
jtulach@52
   212
    public void testNotCorrectly() {
jtulach@52
   213
        Circuit c = Circuit.createNotCircuit(null);
jtulach@52
   214
        try {
jtulach@52
   215
            c.evaluate();
jtulach@52
   216
            fail("missing");
jtulach@52
   217
        } catch (IllegalArgumentException iae) { // expected
jtulach@52
   218
        }
jtulach@52
   219
    }
jtulach@52
   220
    
jtulach@52
   221
}