samples/apifest1/day2/subclassingsolution/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
     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 import static org.netbeans.apifest.boolcircuit.Circuit.*;
    33 
    34 /** The initial quest for this APIFest is to create an API for boolean 
    35  * circuits. Such API shall be able to compose a boolean circuit from
    36  * basic elements and evaluate the result given initial values for 
    37  * input variables.
    38  * <p>
    39  * The basic elements include:
    40  * <ul>
    41  *   <li>negation - has one input and one output and changes 0 on input to 
    42  *          on output 1 and 1 to 0
    43  *   <li>and - has two inputs and one output. The output is 1 only if both 
    44  *          inputs are 1, otherwise it is 0
    45  *   <li>or - has two inputs and one output. The output is 1 always, except
    46  *          in the case when both inputs are 0
    47  * </ul>
    48  *
    49  * <p>
    50  * The boolean circuit can be used to represent boolean formulas and compute
    51  * the results for certain values of its inputs. The individual tasks described
    52  * as tests bellow.
    53  *
    54  * <p>
    55  * Links of interest:
    56  * <ul>
    57  *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    58  *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    59  * </ul>
    60  */
    61 public class CircuitTest extends TestCase {
    62     
    63     
    64     
    65     static {
    66         // your code shall run without any permissions
    67     }
    68     
    69     public CircuitTest(String testName) {
    70         super(testName);
    71     }
    72 
    73     protected void setUp() throws Exception {
    74     }
    75 
    76     protected void tearDown() throws Exception {
    77     }
    78     
    79     
    80     /** 
    81      * Create a circuit to evaluate x1 and x2 and then
    82      * verify that its result is false for input (false, true) and
    83      * it is true for input (true, true).
    84      */
    85     public void testX1andX2() {
    86                 
    87         assertFalse( AND.evaluate( false, true ));
    88         assertTrue( AND.evaluate( true, true ));
    89     }
    90     
    91     /** 
    92      * Create a circuit to evaluate (x1 and x2) or x3 and then
    93      * verify that its result is false for input (false, true, false) and
    94      * it is true for input (false, false, true).
    95      */
    96     public void testX1andX2orX3() {
    97         
    98         Circuit c = new Circuit() {
    99 
   100             @Override
   101             public boolean evaluate(boolean[] in) {
   102                 if ( in.length != 3) {
   103                     throw new IllegalArgumentException( "Should have one parameter");
   104                 }
   105                 return OR.evaluate( AND.evaluate( in[0], in[1] ), in[2] );
   106             }
   107             
   108         };
   109         
   110         assertFalse( c.evaluate( false, true, false ) );
   111         assertTrue( c.evaluate(  false, false, true ) );
   112         
   113     }
   114     /** 
   115      * Create a circuit to evaluate (x1 or not(x1)) and then
   116      * verify that its result is true for all values of x1.
   117      */
   118     public void testAlwaysTrue() {    
   119         
   120         Circuit c = new Circuit() {
   121 
   122             @Override
   123             public boolean evaluate(boolean[] in) {
   124                 if ( in.length != 1) {
   125                     throw new IllegalArgumentException( "Should have three parameters");
   126                 }
   127                 return OR.evaluate( in[0], NOT.evaluate( in[0] ) );
   128             }
   129             
   130         };
   131                 
   132         assertTrue( c.evaluate( true ) );
   133         assertTrue( c.evaluate( false ) );
   134     }
   135     
   136 }