-
Notifications
You must be signed in to change notification settings - Fork 900
Expand file tree
/
Copy pathTestScripts.java
More file actions
395 lines (334 loc) · 12.2 KB
/
TestScripts.java
File metadata and controls
395 lines (334 loc) · 12.2 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package com.googlecode.aviator.scripts;
import static com.googlecode.aviator.TestUtils.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.junit.Before;
import org.junit.Test;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.TestUtils;
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
import com.googlecode.aviator.exception.StandardError;
import com.googlecode.aviator.runtime.JavaMethodReflectionFunctionMissing;
import com.googlecode.aviator.runtime.module.IoModule;
import com.googlecode.aviator.runtime.type.Range;
import com.googlecode.aviator.utils.Env;
import com.googlecode.aviator.utils.Reflector;
public class TestScripts {
protected AviatorEvaluatorInstance instance;
@Before
public void setup() throws Exception {
this.instance = AviatorEvaluator.newInstance();
this.instance.addStaticFunctions("j", TestUtils.class);
this.instance.setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
}
public Object testScript(final String name, final Object... args) {
try {
System.out.println("Testing script " + name + " with args: " + Arrays.toString(args));
final String file = TestScripts.class.getResource("/scripts/" + name).getFile();
this.instance.validate(IoModule.slurp(file));
Expression exp = this.instance.compileScript(file, true);
return exp.execute(AviatorEvaluator.newEnv(args));
} catch (Throwable t) {
Reflector.sneakyThrow(t);
}
return null;
}
public Object testLib(final String name, final Object... args) {
try {
System.out.println("Testing lib " + name + " with args: " + Arrays.toString(args));
final String file = TestScripts.class.getResource("/lib/test_" + name + ".av").getFile();
this.instance.validate(IoModule.slurp(file));
Expression exp = this.instance.compileScript(file, true);
return exp.execute(AviatorEvaluator.newEnv(args));
} catch (Throwable t) {
Reflector.sneakyThrow(t);
}
return null;
}
@Test
public void testLibs() {
testLib("aviator");
testLib("math");
testLib("map");
testLib("var");
}
@Test
public void testLambda() {
testScript("lambda.av");
}
@Test
public void testArray() {
testScript("array.av");
}
@Test
public void testUseStatement() {
Env env = (Env) testScript("use1.av", "Long", Long.class);
assertNotNull(env);
List<String> symbols = env.getImportedSymbols();
assertNotNull(symbols);
assertEquals(2, symbols.size());
assertTrue(symbols.contains("com.googlecode.aviator.runtime.type.AviatorObject"));
assertTrue(symbols.contains("java.util.List"));
testScript("use2.av");
testScript("use3.av");
testScript("use4.av");
try {
testScript("use5.av");
fail();
} catch (ExpressionSyntaxErrorException e) {
assertTrue(e.getMessage().contains("expect variable name or * to use at"));
}
}
@Test
public void testOverloadFunction() {
testScript("overload_function.av");
}
@Test
public void testVariadicFunctions() {
testScript("variadic_function.av");
}
@Test(expected = ExpressionSyntaxErrorException.class)
public void testVariadicFunctionsWrongPosition() {
testScript("variadic_function2.av");
}
@Test(expected = ExpressionSyntaxErrorException.class)
public void testVariadicFunctionsTwoArgs() {
testScript("variadic_function3.av");
}
@Test
public void testMisc() {
assertEquals("aviator execute 1 + 2 = 3.", testScript("string_interpolation.av"));
assertEquals("aviator execute 1 + 2 = 3.", testScript("string_interpolation.av"));
}
@Test
public void testMultilineString() {
assertEquals("SELECT u.id, u.name\n" + " FROM USER u\n" + " WHERE u.id = 1",
testScript("string.av"));
}
@Test
public void testRange() {
assertTrue(testScript("range.av") instanceof Range);
}
@Test
public void testStringSeq() {
assertEquals("hello world", testScript("string_seq.av"));
}
@Test
public void testSeqEntry() {
assertEquals(3, testScript("seq_entry.av"));
}
@Test
public void testNew() {
assertEquals(99, testScript("new.av"));
}
@Test
public void testScopes() {
assertEquals(2, testScript("scope1.av"));
assertEquals(199, testScript("scope2.av"));
}
@Test
public void testComments() {
assertEquals(6, testScript("comments.av"));
}
@Test
public void testIfElse() {
assertEquals(1, testScript("if_else1.av"));
assertEquals(2, testScript("if_else2.av"));
assertEquals(3, testScript("if_else3.av"));
assertEquals(4, testScript("if_else4.av"));
assertEquals(5, testScript("if_else5.av"));
try {
testScript("if_else6.av");
fail();
} catch (ExpressionSyntaxErrorException e) {
}
assertEquals("a is less than 10.", testScript("if_elsif1.av", "a", 2));
assertEquals("a is greater than 10.", testScript("if_elsif1.av", "a", 11));
assertEquals("a is greater than 100.", testScript("if_elsif1.av", "a", 101));
assertEquals("a is less than 10.", testScript("if_elsif2.av", "a", 2));
assertEquals("a is greater than 10.", testScript("if_elsif2.av", "a", 11));
assertEquals("a is greater than 100.", testScript("if_elsif2.av", "a", 101));
assertEquals("a is less than 10.", testScript("if_elsif3.av", "a", 8));
assertEquals("a is greater than 10.", testScript("if_elsif3.av", "a", 12));
assertEquals("statement after if", testScript("if_elsif3.av", "a", 112));
assertEquals(7, testScript("if_else7.av"));
assertEquals(8, testScript("if_else8.av"));
assertEquals(null, testScript("if_else9.av"));
assertEquals(10, testScript("if_else10.av"));
assertEquals("b is greater than 100.", testScript("if_else_scope.av", "b", 101));
assertEquals("b is greater than 10.", testScript("if_else_scope.av", "b", 11));
assertEquals("b is 1.", testScript("if_else_scope.av", "b", 1));
}
private int testSum(final int n) {
int sum = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
for (int z = 0; z < n; z++) {
sum += x + y + z;
}
}
}
return sum;
}
@Test
public void testForLoop() {
assertEquals(9, testScript("for1.av"));
assertEquals(0, testScript("for2.av", "a", 1));
assertEquals(2, testScript("for2.av", "a", 3));
assertEquals(99, testScript("for2.av", "a", 100));
assertEquals(null, testScript("for2.av", "a", -1));
assertEquals(null, testScript("for2.av", "a", 0));
assertEquals(45, testScript("for3.av", "a", 10));
assertEquals(5050, testScript("for3.av", "a", 101));
assertEquals(testSum(10), testScript("for4.av", "a", 10));
assertEquals(testSum(50), testScript("for4.av", "a", 50));
{
// break statement
assertEquals(3, testScript("for_break1.av", "a", 5));
assertEquals(3, testScript("for_break1.av", "a", 101));
int[] z = (int[]) testScript("for_break2.av", "a", 5);
assertEquals(4, z[0]);
assertEquals(3, z[1]);
assertEquals(12, z[2]);
z = (int[]) testScript("for_break2.av", "a", 101);
assertEquals(4, z[0]);
assertEquals(3, z[1]);
assertEquals(12, z[2]);
assertEquals(9, testScript("for_break3.av", "a", 5));
assertEquals(9, testScript("for_break3.av", "a", 101));
assertEquals(4, testScript("for_break4.av", "a", 5));
assertEquals(100, testScript("for_break4.av", "a", 101));
assertEquals(4, testScript("for_break5.av", "a", 5));
assertEquals(100, testScript("for_break5.av", "a", 101));
assertEquals(3, testScript("for_break6.av", "a", 101));
}
{
// continue statement
assertEquals(5050 - 45, testScript("for_continue1.av", "a", 101));
assertEquals(10, testScript("for_continue1.av", "a", 11));
assertEquals(21, testScript("for_continue1.av", "a", 12));
assertEquals(4, testScript("for_continue2.av", "a", 5));
assertEquals(100, testScript("for_continue2.av", "a", 101));
assertEquals(4, testScript("for_continue3.av", "a", 5));
assertEquals(100, testScript("for_continue3.av", "a", 101));
assertEquals(108, testScript("for_continue4.av", "a", 101));
assertEquals(108, testScript("for_continue4.av", "a", 51));
assertEquals(10, testScript("for_continue4.av", "a", 11));
assertEquals(21, testScript("for_continue4.av", "a", 12));
assertEquals(10, testScript("for_continue5.av", "a", 11));
assertEquals(21, testScript("for_continue5.av", "a", 12));
assertEquals(41, testScript("for_continue6.av"));
}
{
// return statement
assertEquals(0, testScript("for_return1.av"));
assertEquals(99, testScript("for_return2.av"));
assertEquals(56, testScript("for_return3.av"));
assertEquals(6, testScript("for_return4.av"));
}
{
// for statement values
assertEquals(9, testScript("for5.av"));
}
testScript("for6.av");
testScript("for7.av");
{
// for null sequence
assertEquals(10, testScript("for_null.av"));
assertEquals(13, testScript("for_null.av", "a", new int[] {1, 2}));
}
}
@Test
public void testWhileLoop() {
assertEquals(10, testScript("while1.av"));
assertEquals(10, testScript("while2.av"));
assertEquals(10, testScript("while3.av"));
assertEquals(10, testScript("while4.av"));
final Object[] tuple = (Object[]) testScript("while5.av", "a", 1, "b", 10);
assertEquals(2, tuple.length);
assertEquals(6, tuple[0]);
assertEquals(5, tuple[1]);
for (int i = 0; i < 10; i++) {
final long r = (long) testScript("while6.av");
System.out.println("rand_int(10)=" + r);
assertTrue(r >= 5);
}
testScript("while7.av");
assertEquals(10, testScript("while8.av"));
}
@Test
public void testLet() {
assertEquals(1, testScript("let1.av"));
assertEquals(1, testScript("let2.av"));
assertEquals(null, testScript("let3.av"));
assertEquals(9, testScript("let4.av"));
assertEquals(9, testScript("let5.av"));
}
private final Random rand = new Random();
public int[] genRandomIntArray(final int size) {
int[] a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = this.rand.nextInt();
}
return a;
}
public void assertSortArray(final int[] a, final int size) {
assertNotNull(a);
assertEquals(size, a.length);
int prev = a[0];
for (int i = 0; i < a.length; i++) {
assertTrue(prev <= a[i]);
prev = a[i];
}
}
@Test
public void testFunctions() {
// test qsort
for (int i = 10; i < 100; i++) {
int[] a = genRandomIntArray(i);
assertSortArray((int[]) testScript("qsort.av", "a", a), i);
}
// selection sort
for (int i = 10; i < 100; i++) {
int[] a = genRandomIntArray(i);
assertSortArray((int[]) testScript("selection_sort.av", "a", a), i);
}
// test fibonacci
assertEquals(0, testScript("fibonacci.av", "n", 0));
assertEquals(1, testScript("fibonacci.av", "n", 1));
assertEquals(1, testScript("fibonacci.av", "n", 2));
assertEquals(55, testScript("fibonacci.av", "n", 10));
assertEquals(610, testScript("fibonacci.av", "n", 15));
assertEquals(6765, testScript("fibonacci.av", "n", 20));
testScript("unpacking_arguments.av");
}
@Test
public void testTryCatch() {
Exception e = (Exception) testScript("try_catch1.av");
assertTrue(e instanceof StandardError);
assertEquals("1", ((Throwable) e).getMessage());
assertEquals(1, testScript("try_catch2.av"));
e = (Exception) testScript("try_catch3.av");
assertTrue(e instanceof ClassCastException);
e = (Exception) testScript("try_catch4.av");
assertTrue(e instanceof IOException);
assertEquals(3, testScript("try_catch5.av"));
assertEquals(1, testScript("try_catch6.av"));
assertEquals(2, testScript("try_catch7.av"));
}
@Test
public void testToMap() {
testScript("toMap.av");
}
@Test
public void testPartition() {
testScript("partition.av");
}
}