samples/apifest1/day1/welltestedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/apifest1/day1/welltestedsolution/test/org/netbeans/apifest/boolcircuit/CircuitTest.java Sat Jun 14 09:52:45 2008 +0200
1.3 @@ -0,0 +1,165 @@
1.4 +/*
1.5 + * The contents of this file are subject to the terms of the Common Development
1.6 + * and Distribution License (the License). You may not use this file except in
1.7 + * compliance with the License.
1.8 + *
1.9 + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
1.10 + * or http://www.netbeans.org/cddl.txt.
1.11 + *
1.12 + * When distributing Covered Code, include this CDDL Header Notice in each file
1.13 + * and include the License file at http://www.netbeans.org/cddl.txt.
1.14 + * If applicable, add the following below the CDDL Header, with the fields
1.15 + * enclosed by brackets [] replaced by your own identifying information:
1.16 + * "Portions Copyrighted [year] [name of copyright owner]"
1.17 + *
1.18 + * The Original Software is NetBeans. The Initial Developer of the Original
1.19 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.20 + * Microsystems, Inc. All Rights Reserved.
1.21 + */
1.22 +
1.23 +package org.netbeans.apifest.boolcircuit;
1.24 +
1.25 +import java.security.CodeSource;
1.26 +import java.security.Permission;
1.27 +import java.security.PermissionCollection;
1.28 +import java.security.Policy;
1.29 +import java.util.Collection;
1.30 +import java.util.Collections;
1.31 +import java.util.Enumeration;
1.32 +import junit.framework.TestCase;
1.33 +import junit.framework.*;
1.34 +
1.35 +/** The initial quest for this APIFest is to create an API for boolean
1.36 + * circuits. Such API shall be able to compose a boolean circuit from
1.37 + * basic elements and evaluate the result given initial values for
1.38 + * input variables.
1.39 + * <p>
1.40 + * The basic elements include:
1.41 + * <ul>
1.42 + * <li>negation - has one input and one output and changes 0 on input to
1.43 + * on output 1 and 1 to 0
1.44 + * <li>and - has two inputs and one output. The output is 1 only if both
1.45 + * inputs are 1, otherwise it is 0
1.46 + * <li>or - has two inputs and one output. The output is 1 always, except
1.47 + * in the case when both inputs are 0
1.48 + * </ul>
1.49 + *
1.50 + * <p>
1.51 + * The boolean circuit can be used to represent boolean formulas and compute
1.52 + * the results for certain values of its inputs. The individual tasks described
1.53 + * as tests bellow.
1.54 + *
1.55 + * <p>
1.56 + * Links of interest:
1.57 + * <ul>
1.58 + * <li><a href="http://en.wikipedia.org/wiki/Truth_table">Truth table</a>
1.59 + * <li><a href="http://en.wikipedia.org/wiki/Tautology_(logic)">Taugology</a>
1.60 + * </ul>
1.61 + */
1.62 +public class CircuitTest extends TestCase {
1.63 + static {
1.64 + // your code shall run without any permissions
1.65 + }
1.66 +
1.67 + public CircuitTest(String testName) {
1.68 + super(testName);
1.69 + }
1.70 +
1.71 + protected void setUp() throws Exception {
1.72 + }
1.73 +
1.74 + protected void tearDown() throws Exception {
1.75 + }
1.76 +
1.77 +
1.78 + /**
1.79 + * Create a circuit to evaluate x1 and x2 and then
1.80 + * verify that its result is false for input (false, true) and
1.81 + * it is true for input (true, true).
1.82 + */
1.83 + public void testX1andX2() {
1.84 + assertFalse("x1 and x2 for (false, true)", Circuit.createAndCircuit(null, null).evaluate(false, true));
1.85 + assertTrue("x1 and x2 for (true, true)", Circuit.createAndCircuit(null, null).evaluate(true, true));
1.86 + }
1.87 +
1.88 + /**
1.89 + * Create a circuit to evaluate (x1 and x2) or x3 and then
1.90 + * verify that its result is false for input (false, true, false) and
1.91 + * it is true for input (false, false, true).
1.92 + */
1.93 + public void testX1andX2orX3() {
1.94 + Circuit c = Circuit.createOrCircuit(Circuit.createAndCircuit(null, null), null);
1.95 + assertFalse("(x1 and x2) or x3", c.evaluate(false, true, false));
1.96 + assertTrue("(x1 and x2) or x3", c.evaluate(false, false, true));
1.97 +
1.98 + assertTrue("(x1 and x2) or x3", c.evaluate(true, true, true));
1.99 + assertFalse("(x1 and x2) or x3", c.evaluate(true, false, false));
1.100 + }
1.101 + /**
1.102 + * Create a circuit to evaluate (x1 or not(x1)) and then
1.103 + * verify that its result is true for all values of x1.
1.104 + */
1.105 + public void testAlwaysTrue() {
1.106 + Circuit c = Circuit.createOrCircuit(null, Circuit.createNotCircuit(null));
1.107 + assertTrue("(x1 or not(x1)) for false", c.evaluate(false, false));
1.108 + assertTrue("(x1 or not(x1)) for false", c.evaluate(true, true));
1.109 + }
1.110 +
1.111 + public void testOr() {
1.112 + Circuit c = Circuit.createOrCircuit(null, null);
1.113 + try {
1.114 + c.evaluate();
1.115 + } catch (IllegalArgumentException iae) { // expected
1.116 + }
1.117 + try {
1.118 + c.evaluate(true);
1.119 + } catch (IllegalArgumentException iae) { // expected
1.120 + }
1.121 + try {
1.122 + c.evaluate(true, true, true);
1.123 + } catch (IllegalArgumentException iae) { // expected
1.124 + }
1.125 + assertFalse("(x1 or x2)", c.evaluate(false, false));
1.126 + assertTrue("(x1 or x2)", c.evaluate(true, false));
1.127 + assertTrue("(x1 or x2)", c.evaluate(false, true));
1.128 + assertTrue("(x1 or x2)", c.evaluate(true, true));
1.129 +
1.130 + }
1.131 +
1.132 + public void testAnd() {
1.133 + Circuit c = Circuit.createAndCircuit(null, null);
1.134 + try {
1.135 + c.evaluate();
1.136 + } catch (IllegalArgumentException iae) { // expected
1.137 + }
1.138 + try {
1.139 + c.evaluate(true);
1.140 + } catch (IllegalArgumentException iae) { // expected
1.141 + }
1.142 + try {
1.143 + c.evaluate(true, true, true);
1.144 + } catch (IllegalArgumentException iae) { // expected
1.145 + }
1.146 + assertFalse("(x1 or x2)", c.evaluate(false, false));
1.147 + assertFalse("(x1 or x2)", c.evaluate(true, false));
1.148 + assertFalse("(x1 or x2)", c.evaluate(false, true));
1.149 + assertTrue("(x1 or x2)", c.evaluate(true, true));
1.150 +
1.151 + }
1.152 +
1.153 + public void testNot() {
1.154 + Circuit c = Circuit.createNotCircuit(null);
1.155 + try {
1.156 + c.evaluate();
1.157 + } catch (IllegalArgumentException iae) { // expected
1.158 + }
1.159 + try {
1.160 + c.evaluate(true, true);
1.161 + } catch (IllegalArgumentException iae) { // expected
1.162 + }
1.163 + assertFalse("(x1 or x2)", c.evaluate(true));
1.164 + assertTrue("(x1 or x2)", c.evaluate(false));
1.165 +
1.166 + }
1.167 +
1.168 +}