samples/apifest1/day2/welltestedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 09:58:08 +0200
changeset 153 b5cbb797ec0a
parent 133 50bf1b976c0d
child 154 0fd5e9c500b9
permissions -rw-r--r--
up to line 2000
     1 /*
     2  * The contents of this file are subject to the terms of the Common Development
     3  * and Distribution License (the License). You may not use this file except in
     4  * compliance with the License.
     5  *
     6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
     7  * or http://www.netbeans.org/cddl.txt.
     8  *
     9  * When distributing Covered Code, include this CDDL Header Notice in each file
    10  * and include the License file at http://www.netbeans.org/cddl.txt.
    11  * If applicable, add the following below the CDDL Header, with the fields
    12  * enclosed by brackets [] replaced by your own identifying information:
    13  * "Portions Copyrighted [year] [name of copyright owner]"
    14  *
    15  * The Original Software is NetBeans. The Initial Developer of the Original
    16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    17  * Microsystems, Inc. All Rights Reserved.
    18  */
    19 
    20 package org.netbeans.apifest.boolcircuit;
    21 
    22 import java.security.CodeSource;
    23 import java.security.Permission;
    24 import java.security.PermissionCollection;
    25 import java.security.Policy;
    26 import java.util.Collection;
    27 import java.util.Collections;
    28 import java.util.Enumeration;
    29 import junit.framework.TestCase;
    30 import junit.framework.*;
    31 import org.netbeans.apifest.custom.Gte;
    32 
    33 /** This file contains the APIFest quest for day 2. Simply, turn the 
    34  * boolean circuit into circuit that can compute with double values from 0 to 1.
    35  * <p>
    36  * This means that where ever a boolean was used to represent input or 
    37  * output values, one can now use any double number from >= 0 and <= 1.
    38  * Still, to support backward compatibility, the operations with booleans
    39  * has to be kept available and have to work. In fact False shall be 
    40  * treated as 0 and True as 1.
    41  * <p>
    42  * The basic elements has to be modified to work on doubles in the following
    43  * way:
    44  * <ul>
    45  *   <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
    46  *   <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
    47  *             and(false,true)=0*1=0=false
    48  *   <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
    49  *             or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
    50  *             or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
    51  * </ul>
    52  * <p>
    53  * However as the circuits with doubles are more rich than plain boolean circuits,
    54  * there is additional requirement to allow any user of your API to write its 
    55  * own "element" type. This is all going to be exercise in the tests bellow
    56  * which you are supposed to implement.
    57  */
    58 // BEGIN: apifest.day2.welltestedsolution.RealTest
    59 public class RealTest extends TestCase {
    60     static {
    61         // your code shall run without any permissions
    62     }
    63     
    64     public RealTest(String testName) {
    65         super(testName);
    66     }
    67 
    68     protected void setUp() throws Exception {
    69     }
    70 
    71     protected void tearDown() throws Exception {
    72     }
    73     
    74     
    75     /** First of all create a circuit which will evaluate
    76      * expression (X1 and X2) or not(x1). Hold the circuit
    77      * in some variable.
    78      *
    79      * Feed this circuit with x1=true, x2=false, assert result is false
    80      *
    81      * Feed the same circuit with x1=false, x2=true, assert result is true
    82      *
    83      * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    84      *
    85      * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    86      *
    87      * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
    88      */
    89     public void testX1andX2orNotX1() {
    90         Circuit c = Circuit.createOrCircuit(
    91                 Circuit.createAndCircuit(Circuit.input(0), Circuit.input(1)),
    92                 Circuit.createNotCircuit(Circuit.input(0))
    93                 );
    94         assertFalse("true, false", c.evaluate(true, false));
    95         assertTrue("false, true", c.evaluate(false, true));
    96         assertEquals("0.0, 1.0", 1.0, c.evaluateFuzzy(0.0, 1.0), 0.0);
    97     }
    98     
    99     /** Ensure that one variable cannot be filled with two different values.
   100      * Create a circuit for x1 and x1. Make sure that for any usage of your
   101      * API that would not lead to x1 * x1 result, an exception is thrown.
   102      * For example if there was a way to feed the circuit with two different 
   103      * values 0.3 and 0.5 an exception is thrown indicating that this is 
   104      * improper use of the circuit.
   105      */
   106     public void testImproperUseOfTheCircuit() {
   107         // does not apply
   108         
   109         Circuit x1 = Circuit.input(0);
   110         Circuit c = Circuit.createOrCircuit(x1, x1);
   111         assertTrue("x1 or x1", c.evaluate(true));
   112         assertFalse("x1 or x1", c.evaluate(false));
   113         try {
   114             c.evaluate();
   115             fail("x1 or x1 with wrong params");
   116         } catch (IllegalArgumentException iea) {
   117             //expected
   118         }
   119         // the same with two instances of pin
   120         c = Circuit.createOrCircuit(Circuit.input(0), Circuit.input(0));
   121         assertTrue("x1 or x1", c.evaluate(true));
   122         assertTrue("x1 or x1", c.evaluate(true, false));
   123         assertTrue("x1 or x1", c.evaluate(true, true));
   124         assertFalse("x1 or x1", c.evaluate(false));
   125         try {
   126             c.evaluate();
   127             fail("x1 or x1 with wrong params");
   128         } catch (IllegalArgumentException iea) {
   129             //expected
   130         }
   131     }
   132     
   133     /** Write your own element type called "gte" that will have two inputs and one output.
   134      * The output value will be 1 if x1 >= x2 and 0 otherwise. 
   135      * 
   136      * Create 
   137      * circuit for following expression: (x1 and not(x1)) gte x1
   138      *
   139      * Feed the circuit with 0.5 and verify the result is 0
   140      *
   141      * Feed the same circuit with 1 and verify the result is 0
   142      *
   143      * Feed the same circuit with 0 and verify the result is 1
   144      */
   145     public void testGreaterThanEqualElement() {
   146         Circuit gte = new Gte(Circuit.createAndCircuit(
   147                                   Circuit.input(0),
   148                                   Circuit.createNotCircuit(Circuit.input(0))),
   149                               Circuit.input(0)
   150                           );
   151         assertEquals("0.5", 0.0, gte.evaluateFuzzy(0.5), 0.0);
   152         assertEquals("1.0", 0.0, gte.evaluateFuzzy(1.0), 0.0);
   153         assertEquals("0.0", 1.0, gte.evaluateFuzzy(0.0), 0.0);
   154         
   155     }
   156     
   157     public void testSilly() {
   158         // (x1 and not x2) or x3
   159         Circuit c = Circuit.createOrCircuit(
   160                               Circuit.createAndCircuit(
   161                                   null,
   162                                   Circuit.createNotCircuit(null)),
   163                               null
   164                           );
   165         assertEquals("1 1 1", 1.0, c.evaluateFuzzy(1.0, 1.0, 1.0), 0.0);
   166         assertEquals("1 1 0", 0.0, c.evaluateFuzzy(1.0, 1.0, 0.0), 0.0);
   167         assertEquals("1 0 1", 1.0, c.evaluateFuzzy(1.0, 0.0, 1.0), 0.0);
   168         assertEquals("1 0 0", 1.0, c.evaluateFuzzy(1.0, 0.0, 0.0), 0.0);
   169         assertEquals("0 1 1", 1.0, c.evaluateFuzzy(0.0, 1.0, 1.0), 0.0);
   170         assertEquals("0 1 0", 0.0, c.evaluateFuzzy(0.0, 1.0, 0.0), 0.0);
   171         assertEquals("0 0 1", 1.0, c.evaluateFuzzy(0.0, 0.0, 1.0), 0.0);
   172         assertEquals("0 0 0", 0.0, c.evaluateFuzzy(0.0, 0.0, 0.0), 0.0);
   173     }
   174 }
   175 // END: apifest.day2.welltestedsolution.RealTest
   176