samples/apifest1/day2/subclassingsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
changeset 52 4257f4cf226b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/samples/apifest1/day2/subclassingsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java	Sat Jun 14 09:52:45 2008 +0200
     1.3 @@ -0,0 +1,242 @@
     1.4 +/*
     1.5 + * The contents of this file are subject to the terms of the Common Development
     1.6 + * and Distribution License (the License). You may not use this file except in
     1.7 + * compliance with the License.
     1.8 + *
     1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
    1.10 + * or http://www.netbeans.org/cddl.txt.
    1.11 + *
    1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
    1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
    1.14 + * If applicable, add the following below the CDDL Header, with the fields
    1.15 + * enclosed by brackets [] replaced by your own identifying information:
    1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.17 + *
    1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
    1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    1.20 + * Microsystems, Inc. All Rights Reserved.
    1.21 + */
    1.22 +
    1.23 +package org.netbeans.apifest.boolcircuit;
    1.24 +
    1.25 +import java.security.CodeSource;
    1.26 +import java.security.Permission;
    1.27 +import java.security.PermissionCollection;
    1.28 +import java.security.Policy;
    1.29 +import java.util.Collection;
    1.30 +import java.util.Collections;
    1.31 +import java.util.Enumeration;
    1.32 +import junit.framework.TestCase;
    1.33 +import junit.framework.*;
    1.34 +
    1.35 +/** This file contains the APIFest quest for day 2. Simply, turn the 
    1.36 + * boolean circuit into circuit that can compute with double values from 0 to 1.
    1.37 + * <p>
    1.38 + * This means that where ever a boolean was used to represent input or 
    1.39 + * output values, one can now use any double number from >= 0 and <= 1.
    1.40 + * Still, to support backward compatibility, the operations with booleans
    1.41 + * has to be kept available and have to work. In fact False shall be 
    1.42 + * treated as 0 and True as 1.
    1.43 + * <p>
    1.44 + * The basic elements has to be modified to work on doubles in the following
    1.45 + * way:
    1.46 + * <ul>
    1.47 + *   <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
    1.48 + *   <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
    1.49 + *             and(false,true)=0*1=0=false
    1.50 + *   <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
    1.51 + *             or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
    1.52 + *             or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
    1.53 + * </ul>
    1.54 + * <p>
    1.55 + * However as the circuits with doubles are more rich than plain boolean circuits,
    1.56 + * there is additional requirement to allow any user of your API to write its 
    1.57 + * own "element" type. This is all going to be exercise in the tests bellow
    1.58 + * which you are supposed to implement.
    1.59 + */
    1.60 +public class RealTest extends TestCase {
    1.61 +    static {
    1.62 +        // your code shall run without any permissions
    1.63 +    }
    1.64 +    
    1.65 +    public RealTest(String testName) {
    1.66 +        super(testName);
    1.67 +    }
    1.68 +
    1.69 +    protected void setUp() throws Exception {
    1.70 +    }
    1.71 +
    1.72 +    protected void tearDown() throws Exception {
    1.73 +    }
    1.74 +    
    1.75 +    
    1.76 +    
    1.77 +    /** First of all create a circuit which will evaluate
    1.78 +     * expression (X1 and X2) or not(x1). Hold the circuit
    1.79 +     * in some variable.
    1.80 +     *
    1.81 +     * Feed this circuit with x1=true, x2=false, assert result is false
    1.82 +     *
    1.83 +     * Feed the same circuit with x1=false, x2=true, assert result is true
    1.84 +     *
    1.85 +     * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    1.86 +     *
    1.87 +     * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    1.88 +     *
    1.89 +     * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
    1.90 +     */
    1.91 +    public void testX1andX2orNotX1() {
    1.92 +        FuzzyCircuit c = new CircuitSupport( 2 ) {
    1.93 +
    1.94 +            @Override
    1.95 +            public boolean ev(boolean[] in) {                
    1.96 +                return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
    1.97 +            }
    1.98 +            
    1.99 +            @Override
   1.100 +            public double ev(double[] in) {
   1.101 +                return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
   1.102 +            }
   1.103 +            
   1.104 +        };
   1.105 +        
   1.106 +        assertFalse( c.evaluate(true, false) );
   1.107 +        assertTrue( c.evaluate(false, true) );
   1.108 +        assertEquals(c.evaluate(0.0, 1.0), 1.0, 0.0001);
   1.109 +        assertEquals(c.evaluate(0.5, 0.5), 0.625, 0.0001);
   1.110 +        try {
   1.111 +            c.evaluate(0.0, 2.0);
   1.112 +        }
   1.113 +        catch ( IllegalArgumentException e ) {
   1.114 +            return;
   1.115 +        }
   1.116 +        fail();
   1.117 +    }
   1.118 +    
   1.119 +    /** Ensure that one variable cannot be filled with two different values.
   1.120 +     * Create a circuit for x1 and x1. Make sure that for any usage of your
   1.121 +     * API that would not lead to x1 * x1 result, an exception is thrown.
   1.122 +     * For example if there was a way to feed the circuit with two different 
   1.123 +     * values 0.3 and 0.5 an exception is thrown indicating that this is 
   1.124 +     * improper use of the circuit.
   1.125 +     */
   1.126 +    public void testImproperUseOfTheCircuit() {
   1.127 +        FuzzyCircuit c = new CircuitSupport( 1 ) {
   1.128 +
   1.129 +            @Override
   1.130 +            public boolean ev(boolean[] in) {                
   1.131 +                return AND.evaluate( in[0], in[0] );
   1.132 +            }
   1.133 +            
   1.134 +            @Override
   1.135 +            public double ev(double[] in) {
   1.136 +                return AND.evaluate( in[0], in[0] );
   1.137 +            }
   1.138 +            
   1.139 +        };
   1.140 +        
   1.141 +        // Empty array        
   1.142 +        IllegalArgumentException ex = null;        
   1.143 +        try {
   1.144 +            c.evaluate(new boolean[]{} );
   1.145 +        } catch ( IllegalArgumentException e) {
   1.146 +            ex = e ;
   1.147 +        }
   1.148 +        if ( ex == null ) {
   1.149 +            fail();
   1.150 +        }
   1.151 +        
   1.152 +        // Other sizes
   1.153 +        int MAX = 4;    // :-) This test obviously requires a nonsese. 
   1.154 +                        // For testing nonsese Integer.MAX_VALUE
   1.155 +        for( int i = 2; i < MAX ; i++ ) {
   1.156 +            double a[] = new double[i];
   1.157 +            ex = null; 
   1.158 +            try {
   1.159 +                c.evaluate( a );
   1.160 +            }
   1.161 +            catch ( IllegalArgumentException e) {
   1.162 +                ex = e ;
   1.163 +            }
   1.164 +            if ( ex == null ) {
   1.165 +                fail();
   1.166 +            }
   1.167 +        }
   1.168 +        
   1.169 +    }
   1.170 +    
   1.171 +    /** Write your own element type called "gte" that will have two inputs and one output.
   1.172 +     * The output value will be 1 if x1 >= x2 and 0 otherwise. 
   1.173 +     * 
   1.174 +     * Create 
   1.175 +     * circuit for following expression: (x1 and not(x1)) gte x1
   1.176 +     *
   1.177 +     * Feed the circuit with 0.5 and verify the result is 0
   1.178 +     *
   1.179 +     * Feed the same circuit with 1 and verify the result is 0
   1.180 +     *
   1.181 +     * Feed the same circuit with 0 and verify the result is 1
   1.182 +     */
   1.183 +    public void testGreaterThanEqualElement() {
   1.184 +        final FuzzyCircuit gte = new CircuitSupport( 2 ) {
   1.185 +
   1.186 +            // Assumes true > false
   1.187 +            @Override
   1.188 +            public boolean ev(boolean[] in) {                
   1.189 +                return in[0] || ( !in[0] && in[1] ); // May be
   1.190 +            }
   1.191 +            
   1.192 +            @Override
   1.193 +            public double ev(double[] in) {
   1.194 +                return in[0] >= in[1] ? 1.0 : 0.0;
   1.195 +            }
   1.196 +                                  
   1.197 +        };
   1.198 +        
   1.199 +        FuzzyCircuit c = new CircuitSupport( 1 ) {
   1.200 +
   1.201 +            // Assumes true > false
   1.202 +            @Override
   1.203 +            public boolean ev(boolean[] in) {                
   1.204 +                return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
   1.205 +            }
   1.206 +            
   1.207 +            @Override
   1.208 +            public double ev(double[] in) {
   1.209 +                return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
   1.210 +            }
   1.211 +                                  
   1.212 +        };
   1.213 +        
   1.214 +        
   1.215 +        assertEquals(c.evaluate(0.5), 0.0, 0.0001);
   1.216 +        assertEquals(c.evaluate(1.0), 0.0, 0.0001);
   1.217 +        assertEquals(c.evaluate(0.0), 1.0, 0.0001);
   1.218 +        
   1.219 +    }
   1.220 +    
   1.221 +       
   1.222 +    private abstract static class CircuitSupport extends FuzzyCircuit {
   1.223 +    
   1.224 +        private int paramCount;
   1.225 +        
   1.226 +        CircuitSupport( int paramCount ) {
   1.227 +            this.paramCount = paramCount;
   1.228 +        }
   1.229 +                
   1.230 +        abstract boolean ev( boolean in[] );
   1.231 +        abstract double ev( double in[] );
   1.232 +        
   1.233 +        public boolean evaluate(boolean[] in) {
   1.234 +            checkParams( paramCount, in );
   1.235 +            return ev( in );
   1.236 +        }
   1.237 +                        
   1.238 +        public double evaluate(double[] in) {
   1.239 +            checkParams( paramCount, in );
   1.240 +            return ev( in );
   1.241 +        }
   1.242 +        
   1.243 +    }
   1.244 +    
   1.245 +}