|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+import org.junit.Before;
|
|
|
2
|
+import org.junit.Ignore;
|
|
|
3
|
+import org.junit.Rule;
|
|
|
4
|
+import org.junit.Test;
|
|
|
5
|
+import org.junit.rules.ExpectedException;
|
|
|
6
|
+
|
|
|
7
|
+import java.util.Arrays;
|
|
|
8
|
+import java.util.Collections;
|
|
|
9
|
+
|
|
|
10
|
+import static org.junit.Assert.assertEquals;
|
|
|
11
|
+
|
|
|
12
|
+/*
|
|
|
13
|
+ * version: 1.2.0
|
|
|
14
|
+ */
|
|
|
15
|
+public class ForthEvaluatorTest {
|
|
|
16
|
+
|
|
|
17
|
+ @Rule
|
|
|
18
|
+ public ExpectedException expectedException = ExpectedException.none();
|
|
|
19
|
+
|
|
|
20
|
+ private ForthEvaluator forthEvaluator;
|
|
|
21
|
+
|
|
|
22
|
+ @Before
|
|
|
23
|
+ public void setUp() {
|
|
|
24
|
+ forthEvaluator = new ForthEvaluator();
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
|
27
|
+ @Test
|
|
|
28
|
+ public void testEmptyProgramResultsInEmptyStack() {
|
|
|
29
|
+ assertEquals(
|
|
|
30
|
+ Collections.emptyList(),
|
|
|
31
|
+ forthEvaluator.evaluateProgram(Collections.emptyList()));
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ @Ignore("Remove to run test")
|
|
|
35
|
+ @Test
|
|
|
36
|
+ public void testNumbersAreJustPushedOntoTheStack() {
|
|
|
37
|
+ assertEquals(
|
|
|
38
|
+ Arrays.asList(1, 2, 3, 4, 5),
|
|
|
39
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 4 5")));
|
|
|
40
|
+ }
|
|
|
41
|
+
|
|
|
42
|
+ @Ignore("Remove to run test")
|
|
|
43
|
+ @Test
|
|
|
44
|
+ public void testTwoNumbersCanBeAdded() {
|
|
|
45
|
+ assertEquals(
|
|
|
46
|
+ Collections.singletonList(3),
|
|
|
47
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 +")));
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ @Ignore("Remove to run test")
|
|
|
51
|
+ @Test
|
|
|
52
|
+ public void testErrorIfAdditionAttemptedWithNothingOnTheStack() {
|
|
|
53
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
54
|
+ expectedException.expectMessage("Addition requires that the stack contain at least 2 values");
|
|
|
55
|
+
|
|
|
56
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("+"));
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ @Ignore("Remove to run test")
|
|
|
60
|
+ @Test
|
|
|
61
|
+ public void testErrorIfAdditionAttemptedWithOneNumberOnTheStack() {
|
|
|
62
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
63
|
+ expectedException.expectMessage("Addition requires that the stack contain at least 2 values");
|
|
|
64
|
+
|
|
|
65
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 +"));
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ @Ignore("Remove to run test")
|
|
|
69
|
+ @Test
|
|
|
70
|
+ public void testTwoNumbersCanBeSubtracted() {
|
|
|
71
|
+ assertEquals(
|
|
|
72
|
+ Collections.singletonList(-1),
|
|
|
73
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("3 4 -")));
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ @Ignore("Remove to run test")
|
|
|
77
|
+ @Test
|
|
|
78
|
+ public void testErrorIfSubtractionAttemptedWithNothingOnTheStack() {
|
|
|
79
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
80
|
+ expectedException.expectMessage("Subtraction requires that the stack contain at least 2 values");
|
|
|
81
|
+
|
|
|
82
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("-"));
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ @Ignore("Remove to run test")
|
|
|
86
|
+ @Test
|
|
|
87
|
+ public void testErrorIfSubtractionAttemptedWithOneNumberOnTheStack() {
|
|
|
88
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
89
|
+ expectedException.expectMessage("Subtraction requires that the stack contain at least 2 values");
|
|
|
90
|
+
|
|
|
91
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 -"));
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ @Ignore("Remove to run test")
|
|
|
95
|
+ @Test
|
|
|
96
|
+ public void testTwoNumbersCanBeMultiplied() {
|
|
|
97
|
+ assertEquals(
|
|
|
98
|
+ Collections.singletonList(8),
|
|
|
99
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("2 4 *")));
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ @Ignore("Remove to run test")
|
|
|
103
|
+ @Test
|
|
|
104
|
+ public void testErrorIfMultiplicationAttemptedWithNothingOnTheStack() {
|
|
|
105
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
106
|
+ expectedException.expectMessage("Multiplication requires that the stack contain at least 2 values");
|
|
|
107
|
+
|
|
|
108
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("*"));
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ @Ignore("Remove to run test")
|
|
|
112
|
+ @Test
|
|
|
113
|
+ public void testErrorIfMultiplicationAttemptedWithOneNumberOnTheStack() {
|
|
|
114
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
115
|
+ expectedException.expectMessage("Multiplication requires that the stack contain at least 2 values");
|
|
|
116
|
+
|
|
|
117
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 *"));
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ @Ignore("Remove to run test")
|
|
|
121
|
+ @Test
|
|
|
122
|
+ public void testTwoNumbersCanBeDivided() {
|
|
|
123
|
+ assertEquals(
|
|
|
124
|
+ Collections.singletonList(4),
|
|
|
125
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("12 3 /")));
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ @Ignore("Remove to run test")
|
|
|
129
|
+ @Test
|
|
|
130
|
+ public void testThatIntegerDivisionIsUsed() {
|
|
|
131
|
+ assertEquals(
|
|
|
132
|
+ Collections.singletonList(2),
|
|
|
133
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("8 3 /")));
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ @Ignore("Remove to run test")
|
|
|
137
|
+ @Test
|
|
|
138
|
+ public void testErrorIfDividingByZero() {
|
|
|
139
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
140
|
+ expectedException.expectMessage("Division by 0 is not allowed");
|
|
|
141
|
+
|
|
|
142
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("4 0 /"));
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ @Ignore("Remove to run test")
|
|
|
146
|
+ @Test
|
|
|
147
|
+ public void testErrorIfDivisionAttemptedWithNothingOnTheStack() {
|
|
|
148
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
149
|
+ expectedException.expectMessage("Division requires that the stack contain at least 2 values");
|
|
|
150
|
+
|
|
|
151
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("/"));
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ @Ignore("Remove to run test")
|
|
|
155
|
+ @Test
|
|
|
156
|
+ public void testErrorIfDivisionAttemptedWithOneNumberOnTheStack() {
|
|
|
157
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
158
|
+ expectedException.expectMessage("Division requires that the stack contain at least 2 values");
|
|
|
159
|
+
|
|
|
160
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 /"));
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
|
163
|
+ @Ignore("Remove to run test")
|
|
|
164
|
+ @Test
|
|
|
165
|
+ public void testCombinedAdditionAndSubtraction() {
|
|
|
166
|
+ assertEquals(
|
|
|
167
|
+ Collections.singletonList(-1),
|
|
|
168
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 + 4 -")));
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ @Ignore("Remove to run test")
|
|
|
172
|
+ @Test
|
|
|
173
|
+ public void testCombinedMultiplicationAndDivision() {
|
|
|
174
|
+ assertEquals(
|
|
|
175
|
+ Collections.singletonList(2),
|
|
|
176
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("2 4 * 3 /")));
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ @Ignore("Remove to run test")
|
|
|
180
|
+ @Test
|
|
|
181
|
+ public void testDupCopiesTheTopValueOnTheStack() {
|
|
|
182
|
+ assertEquals(
|
|
|
183
|
+ Arrays.asList(1, 1),
|
|
|
184
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 DUP")));
|
|
|
185
|
+ }
|
|
|
186
|
+
|
|
|
187
|
+ @Ignore("Remove to run test")
|
|
|
188
|
+ @Test
|
|
|
189
|
+ public void testDupParsingIsCaseInsensitive() {
|
|
|
190
|
+ assertEquals(
|
|
|
191
|
+ Arrays.asList(1, 2, 2),
|
|
|
192
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 Dup")));
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ @Ignore("Remove to run test")
|
|
|
196
|
+ @Test
|
|
|
197
|
+ public void testErrorIfDuplicatingAttemptedWithNothingOnTheStack() {
|
|
|
198
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
199
|
+ expectedException.expectMessage("Duplicating requires that the stack contain at least 1 value");
|
|
|
200
|
+
|
|
|
201
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("dup"));
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ @Ignore("Remove to run test")
|
|
|
205
|
+ @Test
|
|
|
206
|
+ public void testDropRemovesTheTopValueOnTheStackIfItIsTheOnlyOne() {
|
|
|
207
|
+ assertEquals(
|
|
|
208
|
+ Collections.emptyList(),
|
|
|
209
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 drop")));
|
|
|
210
|
+ }
|
|
|
211
|
+
|
|
|
212
|
+ @Ignore("Remove to run test")
|
|
|
213
|
+ @Test
|
|
|
214
|
+ public void testDropRemovesTheTopValueOnTheStackIfItIsNotTheOnlyOne() {
|
|
|
215
|
+ assertEquals(
|
|
|
216
|
+ Collections.singletonList(1),
|
|
|
217
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 drop")));
|
|
|
218
|
+ }
|
|
|
219
|
+
|
|
|
220
|
+ @Ignore("Remove to run test")
|
|
|
221
|
+ @Test
|
|
|
222
|
+ public void testErrorIfDroppingAttemptedWithNothingOnTheStack() {
|
|
|
223
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
224
|
+ expectedException.expectMessage("Dropping requires that the stack contain at least 1 value");
|
|
|
225
|
+
|
|
|
226
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("drop"));
|
|
|
227
|
+ }
|
|
|
228
|
+
|
|
|
229
|
+ @Ignore("Remove to run test")
|
|
|
230
|
+ @Test
|
|
|
231
|
+ public void testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreTheOnlyOnes() {
|
|
|
232
|
+ assertEquals(
|
|
|
233
|
+ Arrays.asList(2, 1),
|
|
|
234
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 swap")));
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ @Ignore("Remove to run test")
|
|
|
238
|
+ @Test
|
|
|
239
|
+ public void testSwapSwapsTheTopTwosValueOnTheStackIfTheyAreNotTheOnlyOnes() {
|
|
|
240
|
+ assertEquals(
|
|
|
241
|
+ Arrays.asList(1, 3, 2),
|
|
|
242
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 swap")));
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+ @Ignore("Remove to run test")
|
|
|
246
|
+ @Test
|
|
|
247
|
+ public void testErrorIfSwappingAttemptedWithNothingOnTheStack() {
|
|
|
248
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
249
|
+ expectedException.expectMessage("Swapping requires that the stack contain at least 2 values");
|
|
|
250
|
+
|
|
|
251
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("swap"));
|
|
|
252
|
+ }
|
|
|
253
|
+
|
|
|
254
|
+ @Ignore("Remove to run test")
|
|
|
255
|
+ @Test
|
|
|
256
|
+ public void testErrorIfSwappingAttemptedWithOneNumberOnTheStack() {
|
|
|
257
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
258
|
+ expectedException.expectMessage("Swapping requires that the stack contain at least 2 values");
|
|
|
259
|
+
|
|
|
260
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 swap"));
|
|
|
261
|
+ }
|
|
|
262
|
+
|
|
|
263
|
+ @Ignore("Remove to run test")
|
|
|
264
|
+ @Test
|
|
|
265
|
+ public void testOverCopiesTheSecondElementIfThereAreOnlyTwo() {
|
|
|
266
|
+ assertEquals(
|
|
|
267
|
+ Arrays.asList(1, 2, 1),
|
|
|
268
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 over")));
|
|
|
269
|
+ }
|
|
|
270
|
+
|
|
|
271
|
+ @Ignore("Remove to run test")
|
|
|
272
|
+ @Test
|
|
|
273
|
+ public void testOverCopiesTheSecondElementIfThereAreMoreThanTwo() {
|
|
|
274
|
+ assertEquals(
|
|
|
275
|
+ Arrays.asList(1, 2, 3, 2),
|
|
|
276
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 2 3 over")));
|
|
|
277
|
+ }
|
|
|
278
|
+
|
|
|
279
|
+ @Ignore("Remove to run test")
|
|
|
280
|
+ @Test
|
|
|
281
|
+ public void testErrorIfOveringAttemptedWithNothingOnTheStack() {
|
|
|
282
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
283
|
+ expectedException.expectMessage("Overing requires that the stack contain at least 2 values");
|
|
|
284
|
+
|
|
|
285
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("over"));
|
|
|
286
|
+ }
|
|
|
287
|
+
|
|
|
288
|
+ @Ignore("Remove to run test")
|
|
|
289
|
+ @Test
|
|
|
290
|
+ public void testErrorIfOveringAttemptedWithOneNumberOnTheStack() {
|
|
|
291
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
292
|
+ expectedException.expectMessage("Overing requires that the stack contain at least 2 values");
|
|
|
293
|
+
|
|
|
294
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("1 over"));
|
|
|
295
|
+ }
|
|
|
296
|
+
|
|
|
297
|
+ @Ignore("Remove to run test")
|
|
|
298
|
+ @Test
|
|
|
299
|
+ public void testUserDefinedOperatorsCanConsistOfBuiltInOperators() {
|
|
|
300
|
+ assertEquals(
|
|
|
301
|
+ Arrays.asList(1, 1, 1),
|
|
|
302
|
+ forthEvaluator.evaluateProgram(Arrays.asList(": dup-twice dup dup ;", "1 dup-twice")));
|
|
|
303
|
+ }
|
|
|
304
|
+
|
|
|
305
|
+ @Ignore("Remove to run test")
|
|
|
306
|
+ @Test
|
|
|
307
|
+ public void testUserDefinedOperatorsAreEvaluatedInTheCorrectOrder() {
|
|
|
308
|
+ assertEquals(
|
|
|
309
|
+ Arrays.asList(1, 2, 3),
|
|
|
310
|
+ forthEvaluator.evaluateProgram(Arrays.asList(": countup 1 2 3 ;", "countup")));
|
|
|
311
|
+ }
|
|
|
312
|
+
|
|
|
313
|
+ @Ignore("Remove to run test")
|
|
|
314
|
+ @Test
|
|
|
315
|
+ public void testCanRedefineAUserDefinedOperator() {
|
|
|
316
|
+ assertEquals(
|
|
|
317
|
+ Arrays.asList(1, 1, 1),
|
|
|
318
|
+ forthEvaluator.evaluateProgram(Arrays.asList(": foo dup ;", ": foo dup dup ;", "1 foo")));
|
|
|
319
|
+ }
|
|
|
320
|
+
|
|
|
321
|
+ @Ignore("Remove to run test")
|
|
|
322
|
+ @Test
|
|
|
323
|
+ public void testCanOverrideBuiltInWordOperators() {
|
|
|
324
|
+ assertEquals(
|
|
|
325
|
+ Arrays.asList(1, 1),
|
|
|
326
|
+ forthEvaluator.evaluateProgram(Arrays.asList(": swap dup ;", "1 swap")));
|
|
|
327
|
+ }
|
|
|
328
|
+
|
|
|
329
|
+ @Ignore("Remove to run test")
|
|
|
330
|
+ @Test
|
|
|
331
|
+ public void testCanOverrideBuiltInArithmeticOperators() {
|
|
|
332
|
+ assertEquals(
|
|
|
333
|
+ Collections.singletonList(12),
|
|
|
334
|
+ forthEvaluator.evaluateProgram(Arrays.asList(": + * ;", "3 4 +")));
|
|
|
335
|
+ }
|
|
|
336
|
+
|
|
|
337
|
+ @Ignore("Remove to run test")
|
|
|
338
|
+ @Test
|
|
|
339
|
+ public void testCannotRedefineNumbers() {
|
|
|
340
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
341
|
+ expectedException.expectMessage("Cannot redefine numbers");
|
|
|
342
|
+
|
|
|
343
|
+ forthEvaluator.evaluateProgram(Collections.singletonList(": 1 2 ;"));
|
|
|
344
|
+ }
|
|
|
345
|
+
|
|
|
346
|
+ @Ignore("Remove to run test")
|
|
|
347
|
+ @Test
|
|
|
348
|
+ public void testErrorIfEvaluatingAnUndefinedOperator() {
|
|
|
349
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
350
|
+ expectedException.expectMessage("No definition available for operator \"foo\"");
|
|
|
351
|
+
|
|
|
352
|
+ forthEvaluator.evaluateProgram(Collections.singletonList("foo"));
|
|
|
353
|
+ }
|
|
|
354
|
+
|
|
|
355
|
+}
|