-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign4Test.java
More file actions
270 lines (216 loc) · 6.98 KB
/
Copy pathAssign4Test.java
File metadata and controls
270 lines (216 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import java.util.StringTokenizer;
import junit.framework.TestCase;
import java.io.*;
import java.util.concurrent.ExecutionException;
public class Assign4Test extends TestCase {
public Assign4Test (String name) {
super(name);
}
/** The following 9 check methods create an interpreter object with the specified String as the program, invoke the
* respective evaluation method (valueValue, valueName, valueNeed, etc.), and check that the result matches the
* (given) expected output.
*/
private void evalCheck(String name, String answer, String program) {
Interpreter interp = new Interpreter(new StringReader(program));
assertEquals("by-value-value " + name, answer, interp.eval().toString());
}
public void testNumberP() {
try {
String output = "number?";
String input = "number?";
evalCheck("numberP", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("numberP threw " + e);
}
} //end of func
public void testMathOp() {
try {
String output = "18";
String input = "2 * 3 + 12";
evalCheck("mathOp", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("mathOp threw " + e);
}
} //end of func
public void testIfExpr1() {
try {
String output = "18";
String input = "if (4 > 0) then 18 else 0";
evalCheck("IfExpr1", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("mathOp threw " + e);
}
} //end of func
public void testIfExpr2() {
try {
String output = "18";
String input = "if (4 > 0) then (5)(5) else 18";
evalCheck("IfExpr2", output, input );
fail("IfExpr2 did not abort");
} catch (EvalException e) {
} catch (Exception e) {
e.printStackTrace();
fail("IfExpr2 threw " + e);
}
} //end of func
public void testParseException() {
try {
String output = "haha";
String input = " 1 +";
evalCheck("parseException", output, input );
fail("parseException did not throw ParseException exception");
} catch (ParseException e) {
//e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
fail("parseException threw " + e);
}
} //end of func
public void testEvalException() {
try {
String output = "mojo";
String input = "1 + number?";
evalCheck("evalException", output, input );
fail("evalException did not throw EvalException exception");
} catch (EvalException e) {
//e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
fail("evalException threw " + e);
}
} //end of func
public void testAppend() {
try {
String output = "(1 2 3 1 2 3)";
String input = "let Y := map f to let g := map x to f(map z1,z2 to (x(x))(z1,z2)); in g(g); APPEND := map ap to map x,y to if x = empty then y else cons(first(x), ap(rest(x), y)); l := cons(1,cons(2,cons(3,empty))); in (Y(APPEND))(l,l)";
evalCheck("append", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("append threw " + e);
}
} //end of func
public void testLetRec() {
try {
String output = "(1 2 3 1 2 3)";
String input = "letrec append := map x,y to if x = empty then y else cons(first(x), append(rest(x), y)); " +
"in let l := cons(1,cons(2,cons(3,empty))); in append(l,l)";
evalCheck("letRec", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("letRec threw " + e);
}
} //end of func
public void testEmptyBlock() {
try {
String output = "0";
String input = "{ }";
evalCheck("emptyBlock", output, input );
fail("emptyBlock did not throw ParseException exception");
} catch (ParseException e) {
//e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
fail("emptyBlock threw " + e);
}
} //end of func
public void testBlock() {
try {
String output = "1";
String input = "{3; 2; 1}";
evalCheck("block", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("block threw " + e);
}
} //end of func
public void testDupVar() {
try {
String output = "ha!";
String input = "let x:=3; x:=4; in x";
evalCheck("dupVar", output, input );
fail("dupVar did not throw SyntaxException exception");
} catch (SyntaxException e) {
//e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
fail("dupVar threw " + e);
}
} //end of func
public void testRefApp() {
try {
String output = "(ref 17)";
String input = "let x := ref 10; in {x <- 17; x}";
evalCheck("refApp", output, input); // mutation is lost in nameXXX because the rhs of x is re-evaluated
} catch (Exception e) {
e.printStackTrace();
fail("refApp threw " + e);
}
} //end of func
public void testRefref() {
try {
String output = "(ref (ref 4))";
String input = "let x:= ref 4; " +
"in let y:= ref x; in y";
evalCheck("refref", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("refref threw " + e);
}
} //end of func
public void testRefP() {
try {
String output = "true";
String input = "ref?(ref 4)";
evalCheck("refref", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("refref threw " + e);
}
} //end of func
public void testBangApp() {
try {
String output = "10";
String input = "let x := ref 10; in !x";
evalCheck("bangApp", output, input );
} catch (Exception e) {
e.printStackTrace();
fail("bangApp threw " + e);
}
} //end of func
public void testComplexBang() {
try {
String output = "3";
String input = "let x:= ref 3; in let y:= ref x; in !!y";
evalCheck("complexBang", output, input);
} catch (Exception e) {
e.printStackTrace();
fail("complexBang threw " + e);
}
}
public void testComplexBlock() {
try {
String output = "ruh roh";
String input = "{let x := ref 10; in x <- 17; x";
evalCheck("complexBlock", output, input);
fail("complexBlock didn't throw a ParseException");
} catch (ParseException e) {
} catch (Exception e) {
e.printStackTrace();
fail("complexBlock threw " + e);
}
}
public void testProblem2() {
try {
String output = "(100)";
String input = "letrec maplist := map f,s to if empty?(s) then empty else cons(f(first(s)), maplist(f, rest(s)));" +
"in maplist(map x to x*x, maplist(map x to 1000/x, cons(100, empty)))";
evalCheck("Problem2", output, input);
} catch (Exception e) {
e.printStackTrace();
fail("Problem2 threw " + e);
}
}
}