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
jtulach@52
     1
/*
jtulach@52
     2
 * The contents of this file are subject to the terms of the Common Development
jtulach@52
     3
 * and Distribution License (the License). You may not use this file except in
jtulach@52
     4
 * compliance with the License.
jtulach@52
     5
 *
jtulach@52
     6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
jtulach@52
     7
 * or http://www.netbeans.org/cddl.txt.
jtulach@52
     8
 *
jtulach@52
     9
 * When distributing Covered Code, include this CDDL Header Notice in each file
jtulach@52
    10
 * and include the License file at http://www.netbeans.org/cddl.txt.
jtulach@52
    11
 * If applicable, add the following below the CDDL Header, with the fields
jtulach@52
    12
 * enclosed by brackets [] replaced by your own identifying information:
jtulach@52
    13
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@52
    14
 *
jtulach@52
    15
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@52
    16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@52
    17
 * Microsystems, Inc. All Rights Reserved.
jtulach@52
    18
 */
jtulach@52
    19
jtulach@52
    20
package org.netbeans.apifest.boolcircuit;
jtulach@52
    21
jtulach@52
    22
import java.security.CodeSource;
jtulach@52
    23
import java.security.Permission;
jtulach@52
    24
import java.security.PermissionCollection;
jtulach@52
    25
import java.security.Policy;
jtulach@52
    26
import java.util.Collection;
jtulach@52
    27
import java.util.Collections;
jtulach@52
    28
import java.util.Enumeration;
jtulach@52
    29
import junit.framework.TestCase;
jtulach@52
    30
import junit.framework.*;
jtulach@52
    31
jtulach@52
    32
import static org.netbeans.apifest.boolcircuit.Circuit.*;
jtulach@52
    33
jtulach@52
    34
/** The initial quest for this APIFest is to create an API for boolean 
jtulach@52
    35
 * circuits. Such API shall be able to compose a boolean circuit from
jtulach@52
    36
 * basic elements and evaluate the result given initial values for 
jtulach@52
    37
 * input variables.
jtulach@52
    38
 * <p>
jtulach@52
    39
 * The basic elements include:
jtulach@52
    40
 * <ul>
jtulach@52
    41
 *   <li>negation - has one input and one output and changes 0 on input to 
jtulach@52
    42
 *          on output 1 and 1 to 0
jtulach@52
    43
 *   <li>and - has two inputs and one output. The output is 1 only if both 
jtulach@52
    44
 *          inputs are 1, otherwise it is 0
jtulach@52
    45
 *   <li>or - has two inputs and one output. The output is 1 always, except
jtulach@52
    46
 *          in the case when both inputs are 0
jtulach@52
    47
 * </ul>
jtulach@52
    48
 *
jtulach@52
    49
 * <p>
jtulach@52
    50
 * The boolean circuit can be used to represent boolean formulas and compute
jtulach@52
    51
 * the results for certain values of its inputs. The individual tasks described
jtulach@52
    52
 * as tests bellow.
jtulach@52
    53
 *
jtulach@52
    54
 * <p>
jtulach@52
    55
 * Links of interest:
jtulach@52
    56
 * <ul>
jtulach@52
    57
 *   <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
jtulach@52
    58
 *   <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
jtulach@52
    59
 * </ul>
jtulach@52
    60
 */
jtulach@52
    61
public class CircuitTest extends TestCase {
jtulach@52
    62
    
jtulach@52
    63
    
jtulach@52
    64
    
jtulach@52
    65
    static {
jtulach@52
    66
        // your code shall run without any permissions
jtulach@52
    67
    }
jtulach@52
    68
    
jtulach@52
    69
    public CircuitTest(String testName) {
jtulach@52
    70
        super(testName);
jtulach@52
    71
    }
jtulach@52
    72
jtulach@52
    73
    protected void setUp() throws Exception {
jtulach@52
    74
    }
jtulach@52
    75
jtulach@52
    76
    protected void tearDown() throws Exception {
jtulach@52
    77
    }
jtulach@52
    78
    
jtulach@52
    79
    
jtulach@52
    80
    /** 
jtulach@52
    81
     * Create a circuit to evaluate x1 and x2 and then
jtulach@52
    82
     * verify that its result is false for input (false, true) and
jtulach@52
    83
     * it is true for input (true, true).
jtulach@52
    84
     */
jtulach@52
    85
    public void testX1andX2() {
jtulach@52
    86
                
jtulach@52
    87
        assertFalse( AND.evaluate( false, true ));
jtulach@52
    88
        assertTrue( AND.evaluate( true, true ));
jtulach@52
    89
    }
jtulach@52
    90
    
jtulach@52
    91
    /** 
jtulach@52
    92
     * Create a circuit to evaluate (x1 and x2) or x3 and then
jtulach@52
    93
     * verify that its result is false for input (false, true, false) and
jtulach@52
    94
     * it is true for input (false, false, true).
jtulach@52
    95
     */
jtulach@52
    96
    public void testX1andX2orX3() {
jtulach@52
    97
        
jtulach@52
    98
        Circuit c = new Circuit() {
jtulach@52
    99
jtulach@52
   100
            @Override
jtulach@52
   101
            public boolean evaluate(boolean[] in) {
jtulach@52
   102
                if ( in.length != 3) {
jtulach@52
   103
                    throw new IllegalArgumentException( "Should have one parameter");
jtulach@52
   104
                }
jtulach@52
   105
                return OR.evaluate( AND.evaluate( in[0], in[1] ), in[2] );
jtulach@52
   106
            }
jtulach@52
   107
            
jtulach@52
   108
        };
jtulach@52
   109
        
jtulach@52
   110
        assertFalse( c.evaluate( false, true, false ) );
jtulach@52
   111
        assertTrue( c.evaluate(  false, false, true ) );
jtulach@52
   112
        
jtulach@52
   113
    }
jtulach@52
   114
    /** 
jtulach@52
   115
     * Create a circuit to evaluate (x1 or not(x1)) and then
jtulach@52
   116
     * verify that its result is true for all values of x1.
jtulach@52
   117
     */
jtulach@52
   118
    public void testAlwaysTrue() {    
jtulach@52
   119
        
jtulach@52
   120
        Circuit c = new Circuit() {
jtulach@52
   121
jtulach@52
   122
            @Override
jtulach@52
   123
            public boolean evaluate(boolean[] in) {
jtulach@52
   124
                if ( in.length != 1) {
jtulach@52
   125
                    throw new IllegalArgumentException( "Should have three parameters");
jtulach@52
   126
                }
jtulach@52
   127
                return OR.evaluate( in[0], NOT.evaluate( in[0] ) );
jtulach@52
   128
            }
jtulach@52
   129
            
jtulach@52
   130
        };
jtulach@52
   131
                
jtulach@52
   132
        assertTrue( c.evaluate( true ) );
jtulach@52
   133
        assertTrue( c.evaluate( false ) );
jtulach@52
   134
    }
jtulach@52
   135
    
jtulach@52
   136
}