Skip to content

Commit 7af2c63

Browse files
committed
Change RegressionTest to a full junit3 TestCase
1 parent 7538e37 commit 7af2c63

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<classpathentry excluding="example/" kind="src" path=""/>
44
<classpathentry kind="src" path="example"/>
55
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/>
67
<classpathentry kind="output" path="bin"/>
78
</classpath>

expr/RegressionTest.java

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
package expr;
22

3-
/**
4-
* Test for bugs in the whole package.
5-
*/
6-
public class RegressionTest {
3+
import junit.framework.TestCase;
74

8-
public static void main(String[] args) {
9-
Variable.make("pi").setValue(Math.PI);
5+
public class RegressionTest extends TestCase {
6+
private static void expect(double expected, String input) {
7+
Expr expr;
8+
try {
9+
expr = Parser.parse(input);
10+
} catch (SyntaxException e) {
11+
throw new Error(e.explain());
12+
}
1013

14+
assertEquals(expected,expr.value(),0.00001);
15+
}
16+
17+
public void testOperators() {
1118
expect(9, "3^2");
1219
expect(256, "2^2^3");
1320
expect(6, "3*2");
1421
expect(1.5, "3/2");
1522
expect(5, "3+2");
1623
expect(1, "3-2");
1724
expect(-3, "-3");
25+
}
26+
27+
public void testComparisions() {
1828
expect(1, "2<3");
1929
expect(0, "2<2");
2030
expect(0, "3<2");
@@ -30,7 +40,10 @@ public static void main(String[] args) {
3040
expect(1, "3>=2");
3141
expect(0, "2>3");
3242
expect(0, "2>2");
33-
expect(1, "3>2");
43+
expect(1, "3>2");
44+
}
45+
46+
public void testLogic() {
3447
expect(1, "(1 and 1)");
3548
expect(0, "(1 and 0)");
3649
expect(0, "(0 and 1)");
@@ -39,6 +52,9 @@ public static void main(String[] args) {
3952
expect(1, "(1 or 0)");
4053
expect(1, "(0 or 1)");
4154
expect(0, "(0 or 0)");
55+
}
56+
57+
public void testFunctions() {
4258
expect(2, "abs(-2)");
4359
expect(2, "abs(2)");
4460
expect(0, "acos(1)");
@@ -61,26 +77,18 @@ public static void main(String[] args) {
6177
expect(2, "min(2, 3)");
6278
expect(137, "if(0, 42, 137)");
6379
expect(42, "if(1, 42, 137)");
80+
}
6481

82+
public void testPower() {
6583
expect(-3.0 * Math.pow(1.01, 100.1), " -3 * 1.01^100.1 ");
84+
}
6685

86+
public void testSubstitution() {
6787
Variable x = Variable.make("x");
6888
x.setValue(-40.0);
6989
expect(-171.375208, "-0.00504238 * x^2 + 2.34528 * x - 69.4962");
7090

71-
{
72-
boolean caught = false;
73-
Parser p = new Parser();
74-
p.allow(x); // or p.allow(null);
75-
try {
76-
p.parseString("whoo");
77-
} catch (SyntaxException se) {
78-
caught = true;
79-
}
80-
if (!caught)
81-
throw new Error("Test failed: unknown variable allowed");
82-
}
83-
91+
Variable.make("pi").setValue(Math.PI);
8492
x.setValue(1.1);
8593

8694
expect(137, "137");
@@ -102,24 +110,21 @@ public static void main(String[] args) {
102110
expect(-2.2199999999999998, "-2*(x-3)^2+5");
103111
expect(1.2000000000000002, "2*abs(x+1)-3");
104112
expect(2.7910571473905725, "sqrt(9-x^2)");
105-
106-
System.out.println("All tests passed.");
107113
}
108-
109-
private static void expect(double expected, String input) {
110-
Expr expr;
114+
public void testUnallowedAnyVariable() {
115+
Parser p = new Parser();
116+
p.allow(null);
111117
try {
112-
expr = Parser.parse(input);
113-
} catch (SyntaxException e) {
114-
throw new Error(e.explain());
115-
}
116-
117-
double result = expr.value();
118-
if (result != expected) {
119-
throw new Error("Bad result: " + result
120-
+ " instead of the expected " + expected + " in \"" + input
121-
+ "\"");
122-
}
118+
p.parseString("x");
119+
fail();
120+
} catch (SyntaxException se) {}
121+
}
122+
public void UnallowedVariable() {
123+
Parser p = new Parser();
124+
p.allow(Variable.make("x"));
125+
try {
126+
p.parseString("w");
127+
fail();
128+
} catch (SyntaxException se) {}
123129
}
124-
125130
}

0 commit comments

Comments
 (0)