samples/apifest1/day2/alwayscreatenewcircuit/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     /** First of all create a circuit which will evaluate
    74      * expression (X1 and X2) or not(x1). Hold the circuit
    75      * in some variable.
    76      *
    77      * Feed this circuit with x1=true, x2=false, assert result is false
    78      *
    79      * Feed the same circuit with x1=false, x2=true, assert result is true
    80      *
    81      * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
    82      *
    83      * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
    84      *
    85      * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
    86      */
    87     public void testX1andX2orNotX1() {
    88         Circuit.Input x1 = Circuit.Input.valueOf(true);
    89         Circuit.Input x2 = Circuit.Input.valueOf(false);
    90         Circuit c = Circuit.or(Circuit.and(x1,x2), Circuit.negate(x1));
    91         assertFalse(c.output());
    92         
    93         x1.setValue(false);
    94         x2.setValue(true);
    95         assertTrue(c.output());
    96         
    97         x1.setValue(0.0);
    98         x2.setValue(1.0);
    99         assertTrue(c.output());
   100 
   101         x1.setValue(0.5);
   102         x2.setValue(0.5);
   103         assertEquals(0.625, c.value());
   104         
   105         try {
   106             x1.setValue(1.0);
   107             x2.setValue(2.0);
   108             c.value();
   109             fail();
   110         } catch(IllegalArgumentException iae) {
   111             
   112         }
   113     }
   114     
   115     /** Ensure that one variable cannot be filled with two different values.
   116      * Create a circuit for x1 and x1. Make sure that for any usage of your
   117      * API that would not lead to x1 * x1 result, an exception is thrown.
   118      * For example if there was a way to feed the circuit with two different 
   119      * values 0.3 and 0.5 an exception is thrown indicating that this is 
   120      * improper use of the circuit.
   121      */
   122     public void testImproperUseOfTheCircuit() {
   123         assertTrue(true);
   124     }
   125     
   126     /** Write your own element type called "gte" that will have two inputs and one output.
   127      * The output value will be 1 if x1 >= x2 and 0 otherwise. 
   128      * 
   129      * Create 
   130      * circuit for following expression: (x1 and not(x1)) gte x1
   131      *
   132      * Feed the circuit with 0.5 and verify the result is 0
   133      *
   134      * Feed the same circuit with 1 and verify the result is 0
   135      *
   136      * Feed the same circuit with 0 and verify the result is 1
   137      */
   138     public void testGreaterThanEqualElement() {
   139         class Comp extends Circuit {
   140             Circuit i1;  
   141             Circuit i2;
   142             Comp(Circuit i1,  Circuit i2) {
   143                 this.i1 = i1;
   144                 this.i2 = i2;
   145             }
   146             
   147             public double value() {
   148                 double x1 = i1.value();
   149                 double x2 = i2.value();
   150                 return (x1 >= x2) ? 1 : 0;
   151             }                        
   152         }
   153         
   154         
   155         
   156         Circuit.Input x1 = Circuit.Input.valueOf(0.5);
   157         
   158         Circuit c1 = Circuit.and(x1, Circuit.negate(x1));
   159         Circuit c2 = Circuit.negate(Circuit.negate(x1));        
   160         
   161         Circuit comp =  new Comp(c1, c2);
   162         assertEquals(0.0, comp.value());
   163         
   164         x1.setValue(1);
   165         assertEquals(0.0, comp.value());
   166         
   167         x1.setValue(0);
   168         assertEquals(1.0, comp.value());
   169         
   170     }
   171 }