jtulach@52: /*
jtulach@52: * The contents of this file are subject to the terms of the Common Development
jtulach@52: * and Distribution License (the License). You may not use this file except in
jtulach@52: * compliance with the License.
jtulach@52: *
jtulach@52: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
jtulach@52: * or http://www.netbeans.org/cddl.txt.
jtulach@52: *
jtulach@52: * When distributing Covered Code, include this CDDL Header Notice in each file
jtulach@52: * and include the License file at http://www.netbeans.org/cddl.txt.
jtulach@52: * If applicable, add the following below the CDDL Header, with the fields
jtulach@52: * enclosed by brackets [] replaced by your own identifying information:
jtulach@52: * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@52: *
jtulach@52: * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@52: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@52: * Microsystems, Inc. All Rights Reserved.
jtulach@52: */
jtulach@52:
jtulach@52: package org.netbeans.apifest.boolcircuit;
jtulach@52:
jtulach@52: import java.util.Arrays;
jtulach@52: import java.util.Stack;
jtulach@52: import junit.framework.TestCase;
jtulach@52:
jtulach@52: /** The initial quest for this APIFest is to create an API for boolean
jtulach@52: * circuits. Such API shall be able to compose a boolean circuit from
jtulach@52: * basic elements and evaluate the result given initial values for
jtulach@52: * input variables.
jtulach@52: *
jtulach@52: * The basic elements include:
jtulach@52: *
jtulach@52: * - negation - has one input and one output and changes 0 on input to
jtulach@52: * on output 1 and 1 to 0
jtulach@52: *
- and - has two inputs and one output. The output is 1 only if both
jtulach@52: * inputs are 1, otherwise it is 0
jtulach@52: *
- or - has two inputs and one output. The output is 1 always, except
jtulach@52: * in the case when both inputs are 0
jtulach@52: *
jtulach@52: *
jtulach@52: *
jtulach@52: * The boolean circuit can be used to represent boolean formulas and compute
jtulach@52: * the results for certain values of its inputs. The individual tasks described
jtulach@52: * as tests bellow.
jtulach@52: *
jtulach@52: *
jtulach@52: * Links of interest:
jtulach@52: *
jtulach@52: */
jtulach@52: public class CircuitTest extends TestCase {
jtulach@52: static {
jtulach@52: // your code shall run without any permissions
jtulach@52: }
jtulach@52:
jtulach@52: public CircuitTest(String testName) {
jtulach@52: super(testName);
jtulach@52: }
jtulach@52:
jtulach@52: protected void setUp() throws Exception {
jtulach@52: }
jtulach@52:
jtulach@52: protected void tearDown() throws Exception {
jtulach@52: }
jtulach@52:
jtulach@52: // BEGIN: apifest.day1.stackbasedsolution.CircuitTest
jtulach@52: /**
jtulach@52: * Create a circuit to evaluate x1 and x2 and then
jtulach@52: * verify that its result is false for input (false, true) and
jtulach@52: * it is true for input (true, true).
jtulach@52: */
jtulach@52: public void testX1andX2() {
jtulach@52: Stack s = new Stack ();
jtulach@52: s.addAll(Arrays.asList('1', '1'));
jtulach@132: assertEquals("'1' for '11' input.", '1',
jtulach@132: CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
jtulach@52: s.addAll(Arrays.asList('1', '0'));
jtulach@132: assertEquals("'0' for '10' input.", '0',
jtulach@132: CircuitFactory.getBasicCircuit(Operation.AND).evaluate(s));
jtulach@52: }
jtulach@52:
jtulach@52: /**
jtulach@52: * Create a circuit to evaluate (x1 and x2) or x3 and then
jtulach@52: * verify that its result is false for input (false, true, false) and
jtulach@52: * it is true for input (false, false, true).
jtulach@52: */
jtulach@52: public void testX1andX2orX3() {
jtulach@52: Stack s = new Stack ();
jtulach@52: s.addAll(Arrays.asList('0', '1', '0'));
jtulach@132: assertEquals("'0' for '010' input.", '0',
jtulach@132: CircuitFactory.join(CircuitFactory.getTrivialCircuit(),
jtulach@132: CircuitFactory.getBasicCircuit(Operation.OR),
jtulach@132: Operation.AND).evaluate(s)
jtulach@132: );
jtulach@52: s.addAll(Arrays.asList('0', '0', '1'));
jtulach@132: assertEquals("'1' for '001' input.", '1',
jtulach@132: CircuitFactory.join(CircuitFactory.getTrivialCircuit(),
jtulach@132: CircuitFactory.getBasicCircuit(Operation.OR),
jtulach@132: Operation.AND).evaluate(s)
jtulach@132: );
jtulach@52: }
jtulach@52: /**
jtulach@52: * Create a circuit to evaluate (x1 or not(x1)) and then
jtulach@52: * verify that its result is true for all values of x1.
jtulach@52: */
jtulach@52: public void testAlwaysTrue() {
jtulach@132: Circuit alwaysTrue = CircuitFactory.join(
jtulach@132: CircuitFactory.getTrivialCircuit(),
jtulach@132: CircuitFactory.getBasicCircuit(Operation.NEG),
jtulach@132: Operation.OR
jtulach@132: );
jtulach@52: Stack s = new Stack ();
jtulach@52: s.addAll(Arrays.asList('0', '0'));
jtulach@52: assertEquals ("'1' for '00'", '1', alwaysTrue.evaluate(s));
jtulach@52: s.addAll(Arrays.asList('1', '1'));
jtulach@52: assertEquals ("'1' for '11'", '1', alwaysTrue.evaluate(s));
jtulach@52: }
jtulach@52: // END: apifest.day1.stackbasedsolution.CircuitTest
jtulach@52: }