samples/apifest1/day2/subclassingsolution/test/org/netbeans/apifest/boolcircuit/RealTest.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
     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 
    32 /** This file contains the APIFest quest for day 2. Simply, turn the 
    33  * boolean circuit into circuit that can compute with double values from 0 to 1.
    34  * <p>
    35  * This means that where ever a boolean was used to represent input or 
    36  * output values, one can now use any double number from >= 0 and <= 1.
    37  * Still, to support backward compatibility, the operations with booleans
    38  * has to be kept available and have to work. In fact False shall be 
    39  * treated as 0 and True as 1.
    40  * <p>
    41  * The basic elements has to be modified to work on doubles in the following
    42  * way:
    43  * <ul>
    44  *   <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
    45  *   <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
    46  *             and(false,true)=0*1=0=false
    47  *   <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
    48  *             or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
    49  *             or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
    50  * </ul>
    51  * <p>
    52  * However as the circuits with doubles are more rich than plain boolean circuits,
    53  * there is additional requirement to allow any user of your API to write its 
    54  * own "element" type. This is all going to be exercise in the tests bellow
    55  * which you are supposed to implement.
    56  */
    57 public class RealTest extends TestCase {
    58     static {
    59         // your code shall run without any permissions
    60     }
    61     
    62     public RealTest(String testName) {
    63         super(testName);
    64     }
    65 
    66     protected void setUp() throws Exception {
    67     }
    68 
    69     protected void tearDown() throws Exception {
    70     }
    71     
    72     
    73     
    74     /** First of all create a circuit which will evaluate
    75      * expression (X1 and X2) or not(x1). Hold the circuit
    76      * in some variable.
    77      *
    78      * Feed this circuit with x1=true, x2=false, assert result is false
    79      *
    80      * Feed the same circuit with x1=false, x2=true, assert result is true
    81      *
    82      * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    83      *
    84      * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    85      *
    86      * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
    87      */
    88     public void testX1andX2orNotX1() {
    89         FuzzyCircuit c = new CircuitSupport( 2 ) {
    90 
    91             @Override
    92             public boolean ev(boolean[] in) {                
    93                 return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
    94             }
    95             
    96             @Override
    97             public double ev(double[] in) {
    98                 return OR.evaluate( AND.evaluate( in[0], in[1] ), NOT.evaluate( in[0] ) );
    99             }
   100             
   101         };
   102         
   103         assertFalse( c.evaluate(true, false) );
   104         assertTrue( c.evaluate(false, true) );
   105         assertEquals(c.evaluate(0.0, 1.0), 1.0, 0.0001);
   106         assertEquals(c.evaluate(0.5, 0.5), 0.625, 0.0001);
   107         try {
   108             c.evaluate(0.0, 2.0);
   109         }
   110         catch ( IllegalArgumentException e ) {
   111             return;
   112         }
   113         fail();
   114     }
   115     
   116     /** Ensure that one variable cannot be filled with two different values.
   117      * Create a circuit for x1 and x1. Make sure that for any usage of your
   118      * API that would not lead to x1 * x1 result, an exception is thrown.
   119      * For example if there was a way to feed the circuit with two different 
   120      * values 0.3 and 0.5 an exception is thrown indicating that this is 
   121      * improper use of the circuit.
   122      */
   123     public void testImproperUseOfTheCircuit() {
   124         FuzzyCircuit c = new CircuitSupport( 1 ) {
   125 
   126             @Override
   127             public boolean ev(boolean[] in) {                
   128                 return AND.evaluate( in[0], in[0] );
   129             }
   130             
   131             @Override
   132             public double ev(double[] in) {
   133                 return AND.evaluate( in[0], in[0] );
   134             }
   135             
   136         };
   137         
   138         // Empty array        
   139         IllegalArgumentException ex = null;        
   140         try {
   141             c.evaluate(new boolean[]{} );
   142         } catch ( IllegalArgumentException e) {
   143             ex = e ;
   144         }
   145         if ( ex == null ) {
   146             fail();
   147         }
   148         
   149         // Other sizes
   150         int MAX = 4;    // :-) This test obviously requires a nonsese. 
   151                         // For testing nonsese Integer.MAX_VALUE
   152         for( int i = 2; i < MAX ; i++ ) {
   153             double a[] = new double[i];
   154             ex = null; 
   155             try {
   156                 c.evaluate( a );
   157             }
   158             catch ( IllegalArgumentException e) {
   159                 ex = e ;
   160             }
   161             if ( ex == null ) {
   162                 fail();
   163             }
   164         }
   165         
   166     }
   167     
   168     /** Write your own element type called "gte" that will have two inputs and one output.
   169      * The output value will be 1 if x1 >= x2 and 0 otherwise. 
   170      * 
   171      * Create 
   172      * circuit for following expression: (x1 and not(x1)) gte x1
   173      *
   174      * Feed the circuit with 0.5 and verify the result is 0
   175      *
   176      * Feed the same circuit with 1 and verify the result is 0
   177      *
   178      * Feed the same circuit with 0 and verify the result is 1
   179      */
   180     public void testGreaterThanEqualElement() {
   181         final FuzzyCircuit gte = new CircuitSupport( 2 ) {
   182 
   183             // Assumes true > false
   184             @Override
   185             public boolean ev(boolean[] in) {                
   186                 return in[0] || ( !in[0] && in[1] ); // May be
   187             }
   188             
   189             @Override
   190             public double ev(double[] in) {
   191                 return in[0] >= in[1] ? 1.0 : 0.0;
   192             }
   193                                   
   194         };
   195         
   196         FuzzyCircuit c = new CircuitSupport( 1 ) {
   197 
   198             // Assumes true > false
   199             @Override
   200             public boolean ev(boolean[] in) {                
   201                 return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
   202             }
   203             
   204             @Override
   205             public double ev(double[] in) {
   206                 return gte.evaluate( AND.evaluate( in[0], NOT.evaluate( in[0] ) ), in[0]);
   207             }
   208                                   
   209         };
   210         
   211         
   212         assertEquals(c.evaluate(0.5), 0.0, 0.0001);
   213         assertEquals(c.evaluate(1.0), 0.0, 0.0001);
   214         assertEquals(c.evaluate(0.0), 1.0, 0.0001);
   215         
   216     }
   217     
   218        
   219     private abstract static class CircuitSupport extends FuzzyCircuit {
   220     
   221         private int paramCount;
   222         
   223         CircuitSupport( int paramCount ) {
   224             this.paramCount = paramCount;
   225         }
   226                 
   227         abstract boolean ev( boolean in[] );
   228         abstract double ev( double in[] );
   229         
   230         public boolean evaluate(boolean[] in) {
   231             checkParams( paramCount, in );
   232             return ev( in );
   233         }
   234                         
   235         public double evaluate(double[] in) {
   236             checkParams( paramCount, in );
   237             return ev( in );
   238         }
   239         
   240     }
   241     
   242 }