samples/apifest1/day1/welltestedsolution/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 /** The initial quest for this APIFest is to create an API for boolean 
    33  * circuits. Such API shall be able to compose a boolean circuit from
    34  * basic elements and evaluate the result given initial values for 
    35  * input variables.
    36  * <p>
    37  * The basic elements include:
    38  * <ul>
    39  *   <li>negation - has one input and one output and changes 0 on input to 
    40  *          on output 1 and 1 to 0
    41  *   <li>and - has two inputs and one output. The output is 1 only if both 
    42  *          inputs are 1, otherwise it is 0
    43  *   <li>or - has two inputs and one output. The output is 1 always, except
    44  *          in the case when both inputs are 0
    45  * </ul>
    46  *
    47  * <p>
    48  * The boolean circuit can be used to represent boolean formulas and compute
    49  * the results for certain values of its inputs. The individual tasks described
    50  * as tests bellow.
    51  *
    52  * <p>
    53  * Links of interest:
    54  * <ul>
    55  *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
    56  *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
    57  * </ul>
    58  */
    59 public class CircuitTest extends TestCase {
    60     static {
    61         // your code shall run without any permissions
    62     }
    63     
    64     public CircuitTest(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     /** 
    76      * Create a circuit to evaluate x1 and x2 and then
    77      * verify that its result is false for input (false, true) and
    78      * it is true for input (true, true).
    79      */
    80     public void testX1andX2() {
    81         assertFalse("x1 and x2 for (false, true)", Circuit.createAndCircuit(null, null).evaluate(false, true));
    82         assertTrue("x1 and x2 for (true, true)", Circuit.createAndCircuit(null, null).evaluate(true, true));
    83     }
    84     
    85     /** 
    86      * Create a circuit to evaluate (x1 and x2) or x3 and then
    87      * verify that its result is false for input (false, true, false) and
    88      * it is true for input (false, false, true).
    89      */
    90     public void testX1andX2orX3() {
    91         Circuit c = Circuit.createOrCircuit(Circuit.createAndCircuit(null, null), null);
    92         assertFalse("(x1 and x2) or x3", c.evaluate(false, true, false));
    93         assertTrue("(x1 and x2) or x3", c.evaluate(false, false, true));
    94         
    95         assertTrue("(x1 and x2) or x3", c.evaluate(true, true, true));
    96         assertFalse("(x1 and x2) or x3", c.evaluate(true, false, false));
    97     }
    98     /** 
    99      * Create a circuit to evaluate (x1 or not(x1)) and then
   100      * verify that its result is true for all values of x1.
   101      */
   102     public void testAlwaysTrue() {
   103         Circuit c = Circuit.createOrCircuit(null, Circuit.createNotCircuit(null));
   104         assertTrue("(x1 or not(x1)) for false", c.evaluate(false, false));
   105         assertTrue("(x1 or not(x1)) for false", c.evaluate(true, true));
   106     }
   107     
   108     public void testOr() {
   109         Circuit c = Circuit.createOrCircuit(null, null);
   110         try {
   111             c.evaluate();
   112         } catch (IllegalArgumentException iae) { // expected
   113         }
   114         try {
   115             c.evaluate(true);
   116         } catch (IllegalArgumentException iae) { // expected
   117         }
   118         try {
   119             c.evaluate(true, true, true);
   120         } catch (IllegalArgumentException iae) { // expected
   121         }
   122         assertFalse("(x1 or x2)", c.evaluate(false, false));
   123         assertTrue("(x1 or x2)", c.evaluate(true, false));
   124         assertTrue("(x1 or x2)", c.evaluate(false, true));
   125         assertTrue("(x1 or x2)", c.evaluate(true, true));
   126         
   127     }
   128     
   129     public void testAnd() {
   130         Circuit c = Circuit.createAndCircuit(null, null);
   131         try {
   132             c.evaluate();
   133         } catch (IllegalArgumentException iae) { // expected
   134         }
   135         try {
   136             c.evaluate(true);
   137         } catch (IllegalArgumentException iae) { // expected
   138         }
   139         try {
   140             c.evaluate(true, true, true);
   141         } catch (IllegalArgumentException iae) { // expected
   142         }
   143         assertFalse("(x1 or x2)", c.evaluate(false, false));
   144         assertFalse("(x1 or x2)", c.evaluate(true, false));
   145         assertFalse("(x1 or x2)", c.evaluate(false, true));
   146         assertTrue("(x1 or x2)", c.evaluate(true, true));
   147         
   148     }
   149     
   150     public void testNot() {
   151         Circuit c = Circuit.createNotCircuit(null);
   152         try {
   153             c.evaluate();
   154         } catch (IllegalArgumentException iae) { // expected
   155         }
   156         try {
   157             c.evaluate(true, true);
   158         } catch (IllegalArgumentException iae) { // expected
   159         }
   160         assertFalse("(x1 or x2)", c.evaluate(true));
   161         assertTrue("(x1 or x2)", c.evaluate(false));
   162         
   163     }
   164     
   165 }