samples/apifest1/day2/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/apifest1/day2/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java Sat Jun 14 09:52:45 2008 +0200
1.3 @@ -0,0 +1,153 @@
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.Arrays;
1.30 +import java.util.Collection;
1.31 +import java.util.Collections;
1.32 +import java.util.Enumeration;
1.33 +import java.util.Stack;
1.34 +import junit.framework.TestCase;
1.35 +import junit.framework.*;
1.36 +
1.37 +/** This file contains the APIFest quest for day 2. Simply, turn the
1.38 + * boolean circuit into circuit that can compute with double values from 0 to 1.
1.39 + * <p>
1.40 + * This means that where ever a boolean was used to represent input or
1.41 + * output values, one can now use any double number from >= 0 and <= 1.
1.42 + * Still, to support backward compatibility, the operations with booleans
1.43 + * has to be kept available and have to work. In fact False shall be
1.44 + * treated as 0 and True as 1.
1.45 + * <p>
1.46 + * The basic elements has to be modified to work on doubles in the following
1.47 + * way:
1.48 + * <ul>
1.49 + * <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
1.50 + * <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
1.51 + * and(false,true)=0*1=0=false
1.52 + * <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
1.53 + * or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
1.54 + * or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
1.55 + * </ul>
1.56 + * <p>
1.57 + * However as the circuits with doubles are more rich than plain boolean circuits,
1.58 + * there is additional requirement to allow any user of your API to write its
1.59 + * own "element" type. This is all going to be exercise in the tests bellow
1.60 + * which you are supposed to implement.
1.61 + */
1.62 +public class RealTest extends TestCase {
1.63 + static {
1.64 + // your code shall run without any permissions
1.65 + }
1.66 +
1.67 + public RealTest(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 + /** First of all create a circuit which will evaluate
1.79 + * expression (X1 and X2) or not(x1). Hold the circuit
1.80 + * in some variable.
1.81 + *
1.82 + * Feed this circuit with x1=true, x2=false, assert result is false
1.83 + *
1.84 + * Feed the same circuit with x1=false, x2=true, assert result is true
1.85 + *
1.86 + * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
1.87 + *
1.88 + * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
1.89 + *
1.90 + * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
1.91 + */
1.92 + public void testX1andX2orNotX1() {
1.93 + Circuit2 trivX1 = CircuitFactory.getTrivialCircuit(0);
1.94 + Circuit2 trivX2 = CircuitFactory.getTrivialCircuit(1);
1.95 + Circuit2 cAnd = CircuitFactory.join(trivX1, trivX2, Operation.AND);
1.96 + Circuit2 cNeg = CircuitFactory.join(trivX1, trivX2, Operation.NEG);
1.97 + Circuit2 cOr = CircuitFactory.join(CircuitFactory.getBasicCircuit (Operation.AND), CircuitFactory.getBasicCircuit(Operation.NEG), Operation.OR);
1.98 +
1.99 + Stack<Character> s = new Stack<Character> ();
1.100 + s.addAll(Arrays.asList('1', '1', '0', '1'));
1.101 +
1.102 + assertEquals ("Feed this circuit with x1=true, x2=false, assert result is false", '0', cOr.evaluate(s));
1.103 + assertEquals ("Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0", 1.0, cOr.evaluate(0.0,1.0));
1.104 + assertEquals ("Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625", 0.625, cOr.evaluate(0.5,0.5));
1.105 + try {
1.106 + assertEquals ("Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception", 0, cOr.evaluate(0.0,2.0));
1.107 + fail ("Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception");
1.108 + } catch (Exception x) {
1.109 + }
1.110 + }
1.111 +
1.112 + /** Ensure that one variable cannot be filled with two different values.
1.113 + * Create a circuit for x1 and x1. Make sure that for any usage of your
1.114 + * API that would not lead to x1 * x1 result, an exception is thrown.
1.115 + * For example if there was a way to feed the circuit with two different
1.116 + * values 0.3 and 0.5 an exception is thrown indicating that this is
1.117 + * improper use of the circuit.
1.118 + */
1.119 + public void testImproperUseOfTheCircuit() {
1.120 + Circuit2 trivX1 = CircuitFactory.getTrivialCircuit(0);
1.121 + assertEquals ("0.25", 0.25, CircuitFactory.join(trivX1, trivX1, Operation.AND).evaluate(0.5, 2));
1.122 + }
1.123 +
1.124 + /** Write your own element type called "gte" that will have two inputs and one output.
1.125 + * The output value will be 1 if x1 >= x2 and 0 otherwise.
1.126 + *
1.127 + * Create
1.128 + * circuit for following expression: (x1 and not(x1)) gte x1
1.129 + *
1.130 + * Feed the circuit with 0.5 and verify the result is 0
1.131 + *
1.132 + * Feed the same circuit with 1 and verify the result is 0
1.133 + *
1.134 + * Feed the same circuit with 0 and verify the result is 1
1.135 + */
1.136 + public void testGreaterThanEqualElement() {
1.137 +// fail("task3");
1.138 + Operation gte = new Operation () {
1.139 +
1.140 + public char evaluate(char i1, char i2) throws IllegalArgumentException {
1.141 + assert false : "Not supported";
1.142 + return 'x';
1.143 + }
1.144 +
1.145 + public double evaluate(double i1, double i2) throws IllegalArgumentException {
1.146 + if (i1 < 0 || i1 > 1) {
1.147 + throw new IllegalArgumentException ("Invalid input parameter: " + i1);
1.148 + }
1.149 + if (i2 < 0 || i2 > 1) {
1.150 + throw new IllegalArgumentException ("Invalid input parameter: " + i2);
1.151 + }
1.152 + return i1 >= i2 ? 1 : 0;
1.153 + }
1.154 + };
1.155 + }
1.156 +}