samples/apifest1/day2/stackbasedsolution/test/org/netbeans/apifest/boolcircuit/RealTest.java
Adding samples from API fest to the repository, including pieces of their code in the document, not just links
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.
6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7 * or http://www.netbeans.org/cddl.txt.
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]"
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.
20 package org.netbeans.apifest.boolcircuit;
22 import java.security.CodeSource;
23 import java.security.Permission;
24 import java.security.PermissionCollection;
25 import java.security.Policy;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.Stack;
31 import junit.framework.TestCase;
32 import junit.framework.*;
34 /** This file contains the APIFest quest for day 2. Simply, turn the
35 * boolean circuit into circuit that can compute with double values from 0 to 1.
37 * This means that where ever a boolean was used to represent input or
38 * output values, one can now use any double number from >= 0 and <= 1.
39 * Still, to support backward compatibility, the operations with booleans
40 * has to be kept available and have to work. In fact False shall be
41 * treated as 0 and True as 1.
43 * The basic elements has to be modified to work on doubles in the following
46 * <li>negation - neg(x) = 1 - x, this is correct extension as neg(false)=neg(0)=1-0=1=true
47 * <li>and - and(x,y) = x * y, again this is fine as and(true,true)=1*1=true and also
48 * and(false,true)=0*1=0=false
49 * <li>or - or(x,y) = 1 - (1 - x) * (1 - y) and this is also ok as
50 * or(false,false) = 1 - (1 - 0) * (1 - 0) = 1 - 1 = 0 = false
51 * or(true,false) = 1 - (1 - 1) * (1 - 0) = 1 - 0 * 1 = 1 = true
54 * However as the circuits with doubles are more rich than plain boolean circuits,
55 * there is additional requirement to allow any user of your API to write its
56 * own "element" type. This is all going to be exercise in the tests bellow
57 * which you are supposed to implement.
59 public class RealTest extends TestCase {
61 // your code shall run without any permissions
64 public RealTest(String testName) {
68 protected void setUp() throws Exception {
71 protected void tearDown() throws Exception {
75 /** First of all create a circuit which will evaluate
76 * expression (X1 and X2) or not(x1). Hold the circuit
79 * Feed this circuit with x1=true, x2=false, assert result is false
81 * Feed the same circuit with x1=false, x2=true, assert result is true
83 * Feed the same circuit with x1=0.0, x2=1.0, assert result is 1.0
85 * Feed the same circuit with x1=0.5, x2=0.5, assert result is 0.625
87 * Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception
89 public void testX1andX2orNotX1() {
90 Circuit2 trivX1 = CircuitFactory.getTrivialCircuit(0);
91 Circuit2 trivX2 = CircuitFactory.getTrivialCircuit(1);
92 Circuit2 cAnd = CircuitFactory.join(trivX1, trivX2, Operation.AND);
93 Circuit2 cNeg = CircuitFactory.join(trivX1, trivX2, Operation.NEG);
94 Circuit2 cOr = CircuitFactory.join(CircuitFactory.getBasicCircuit (Operation.AND), CircuitFactory.getBasicCircuit(Operation.NEG), Operation.OR);
96 Stack<Character> s = new Stack<Character> ();
97 s.addAll(Arrays.asList('1', '1', '0', '1'));
99 assertEquals ("Feed this circuit with x1=true, x2=false, assert result is false", '0', cOr.evaluate(s));
100 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));
101 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));
103 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));
104 fail ("Feed the same circuit with x1=0.0, x2=2.0, make sure it throws an exception");
105 } catch (Exception x) {
109 /** Ensure that one variable cannot be filled with two different values.
110 * Create a circuit for x1 and x1. Make sure that for any usage of your
111 * API that would not lead to x1 * x1 result, an exception is thrown.
112 * For example if there was a way to feed the circuit with two different
113 * values 0.3 and 0.5 an exception is thrown indicating that this is
114 * improper use of the circuit.
116 public void testImproperUseOfTheCircuit() {
117 Circuit2 trivX1 = CircuitFactory.getTrivialCircuit(0);
118 assertEquals ("0.25", 0.25, CircuitFactory.join(trivX1, trivX1, Operation.AND).evaluate(0.5, 2));
121 /** Write your own element type called "gte" that will have two inputs and one output.
122 * The output value will be 1 if x1 >= x2 and 0 otherwise.
125 * circuit for following expression: (x1 and not(x1)) gte x1
127 * Feed the circuit with 0.5 and verify the result is 0
129 * Feed the same circuit with 1 and verify the result is 0
131 * Feed the same circuit with 0 and verify the result is 1
133 public void testGreaterThanEqualElement() {
135 Operation gte = new Operation () {
137 public char evaluate(char i1, char i2) throws IllegalArgumentException {
138 assert false : "Not supported";
142 public double evaluate(double i1, double i2) throws IllegalArgumentException {
143 if (i1 < 0 || i1 > 1) {
144 throw new IllegalArgumentException ("Invalid input parameter: " + i1);
146 if (i2 < 0 || i2 > 1) {
147 throw new IllegalArgumentException ("Invalid input parameter: " + i2);
149 return i1 >= i2 ? 1 : 0;