-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeCheckerTest.java
691 lines (530 loc) · 21.6 KB
/
TypeCheckerTest.java
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
package cop5556sp18;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556sp18.Parser;
import cop5556sp18.Scanner;
import cop5556sp18.AST.ASTVisitor;
import cop5556sp18.AST.Program;
import cop5556sp18.TypeChecker.SemanticException;
public class TypeCheckerTest {
/*
* set Junit to be able to catch exceptions
*/
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* Prints objects in a way that is easy to turn on and off
*/
static final boolean doPrint = true;
private void show(Object input) {
if (doPrint) {
System.out.println(input.toString());
}
}
/**
* Scans, parses, and type checks the input string
*
* @param input
* @throws Exception
*/
void typeCheck(String input) throws Exception {
show(input);
// instantiate a Scanner and scan input
Scanner scanner = new Scanner(input).scan();
show(scanner);
// instantiate a Parser and parse input to obtain and AST
Program ast = new Parser(scanner).parse();
show(ast);
// instantiate a TypeChecker and visit the ast to perform type checking and
// decorate the AST.
ASTVisitor v = new TypeChecker();
ast.visit(v, null);
}
/**
* Simple test case with an almost empty program.
*
* @throws Exception
*/
@Test
public void emptyProg() throws Exception {
String input = "emptyProg{}";
typeCheck(input);
}
@Test
public void expression1() throws Exception {
String input = "prog {show 3+4;}";
typeCheck(input);
}
@Test
public void expression2_fail() throws Exception {
String input = "prog { float a; b:=5;}";
thrown.expect(SemanticException.class);
try {
typeCheck(input);
} catch (SemanticException e) {
show(e);
throw e;
}
}
/*
*ExpressionPredefinedName.type � integer
*/@Test
public void simpleImage() throws Exception {
String input = "X{ image im[1,2]; }";
typeCheck(input);
}
@Test
public void simpleImageFail() throws Exception {
String input = "X{ int im;image im[1.0, 2]; }";
thrown.expect(SemanticException.class);
try {
typeCheck(input);
} catch (SemanticException e) {
show(e);
throw e;
}
}
/*
*ExpressionPredefinedName.type � integer
*/
@Test
public void nestedDec1() throws Exception {
String input = "X{ int x; int y; while (x == y) {int x;}; }";
typeCheck(input);
}
@Test
public void nestedDec2() throws Exception {
String input = "X{ int x; int z; while (x == y) {int x;}; }";
thrown.expect(SemanticException.class);
try {
typeCheck(input);
} catch (SemanticException e) {
show(e);
throw e;
}
}
/*
*ExpressionPredefinedName.type � integer
*/
@Test
public void nestedDec3() throws Exception {
String input = "X{ int x; int y; if (x == y) { show x;}; }";
typeCheck(input);
}
/*
*ExpressionPredefinedName.type � integer
*/
@Test
public void nestedDec4() throws Exception {
String input = "X{ int x; int y; while (x == y) { int z;}; show z;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidFunctionAppWithArg2() throws Exception {
String input = "prog {float var2;var2 := abs(1.0); var2 := sin(1.0); "
+ "var2 := cos(1.0); var2 := atan(1.0); var2 := float(1.0); var2 := log(1.0);}";
typeCheck(input);
}
@Test
public void testinvalidDeclarations1() throws Exception {
String input = "prog{int var1; float var2; boolean var3; image var4; filename var5; image var6[500,500];if(true){int var1; float var2; boolean var3; image var4; filename var5; image var6[500,500];};float var1;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
// typeCheck(input);
}
@Test
public void testinvalidDeclarations2() throws Exception {
String input = "prog{image var1[1.0, 500];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidExpressionBinary1() throws Exception {
String input = "prog{ int var1; boolean var2;var1 := 1 + 2; var1 := 1 - 2; "
+ "var1 := 1 * 2; var1 := 1 / 2; var1 := 1 ** 2; var1 := 1 % 2; "
+ "var1 := 1 & 2; var1 := 1 | 2; var2 := 1 == 2; var2 := 1 != 2; "
+ "var2 := 1 > 2; var2 := 1 >= 2; var2 := 1 < 2; var2 := 1 <= 2;}";
typeCheck(input);
}
@Test
public void testvalidExpressionBinary2() throws Exception {
String input = "prog{ float var1; boolean var2;var1 := 1.0 + 2.0; var1 := 1.0 - 2.0; "
+ "var1 := 1.0 * 2.0; var1 := 1.0 / 2.0; var1 := 1.0 ** 2.0; var2 := 1.0 == 2.0; "
+ "var2 := 1.0 != 2.0; var2 := 1.0 > 2.0; var2 := 1.0 >= 2.0; var2 := 1.0 < 2.0; "
+ "var2 := 1.0 <= 2.0;}";
typeCheck(input);
}
@Test
public void testvalidExpressionBinary4() throws Exception {
String input = "prog{ float var2;var2 := 1 + 2.0; var2 := 1 - 2.0; var2 := 1 * 2.0; "
+ "var2 := 1 / 2.0; var2 := 1 ** 2.0; var2 := 1.0 + 2; var2 := 1.0 - 2; "
+ "var2 := 1.0 * 2; var2 := 1.0 / 2; var2 := 1.0 ** 2;}";
typeCheck(input);
}
//check
@Test
public void testvalidFunctionAppWithPixel2() throws Exception {
String input = "prog {float var1; var1 := polar_a[1,1]; var1 := polar_r[1,1];}";
typeCheck(input);
}
@Test
public void testvalidExpressionPixel1() throws Exception {
String input = "prog{ image var1; int var2; var2 := var1[0,0];}";
typeCheck(input);
}
@Test
public void testinvalidStatementInput1() throws Exception {
String input = "prog{input var from @1; int var;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidStatementInput2() throws Exception {
String input = "prog{if(true){int var;}; if(true){input var from @1;};}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidStatementInput3() throws Exception {
String input = "prog{if(true){int var;}; input var from @1;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidStatementShow1() throws Exception {
String input = "prog{boolean a; int b; float c; image d;show a; show b; show c; show d;}";
typeCheck(input);
}
//check
@Test
public void testinvalidStatementWrite2() throws Exception {
String input = "prog{filename f1; write image1 to f1;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidPixelSelector1() throws Exception {
String input = "prog{image var1; red( var1[1,1]) := 5; red( var1[1.0,1.0]) := 5;}";
typeCheck(input);
}
//check
@Test
public void testvalidStatementInput1() throws Exception {
String input = "prog{int var; int var2;input var from @1; input var from @var2; input var from @<<1,2,3,4>>;}";
typeCheck(input);
}
@Test
public void testvalidStatementWhile1() throws Exception {
String input = "prog{boolean a; boolean b; while(a & b){};}";
typeCheck(input);
}
@Test
public void testinvalidFunctionAppWithArg2() throws Exception {
String input = "prog {float var1;var1 := abs(1);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; } }
//check
@Test
public void testScope1() throws Exception {
String input = "p{int var; if(true) {float var; var := 5.0;}; var := 5;}";
typeCheck(input);
}
///* \
@Test
public void testinvalidStatementInput4() throws Exception {
String input = "prog{float var; input var from @var;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testExpressionFunctionAppWithPixel3() throws Exception {
String input = "prog { int var2; var2 := cart_x[1,6.5];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testExpressionFunctionAppWithPixel4() throws Exception {
String input = "prog { int var2; var2 := cart_x[1.0,6];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testExpressionFunctionAppWithPixel5() throws Exception {
String input = "prog { float var2; var2 := polar_a[1.0,6];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testExpressionFunctionAppWithPixel6() throws Exception {
String input = "prog { float var2; var2 := polar_a[1,6.5];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidStatementWrite1() throws Exception {
String input = "prog{image image1; filename f1; write image1 to f1;}";
typeCheck(input);
}
@Test
public void testinvalidStatementAssign1() throws Exception {
String input = "prog{int var1; var1 := 1.0; float var2; var2 := 1;boolean var3; var3 := 1;filename f1; f1 := 1;image var4; var4 := 1;image var5[500,500]; var5 := 1;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidStatementAssign2() throws Exception {
String input = "prog{image var; var[0,0] := 1.0;alpha(var[0,0]) := 1.0; red(var[0,0]) := 1.0; green(var[0,0]) := 1.0; blue(var[0,0]) := 1.0;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg1() throws Exception {
String input = "prog {int var1;var1 := abs(1.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg3() throws Exception {
String input = "prog {int var1;var1 := red(1.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg4() throws Exception {
String input = "prog {float var1;var1 := sin(1);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg5() throws Exception {
String input = "prog {int var1;var1 := width(1);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg6() throws Exception {
String input = "prog {int var1;var1 := cart_x(1.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg7() throws Exception {
String input = "prog {float var1;var1 := polar_a(1);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg8() throws Exception {
String input = "prog {int var1;var1 := cart_y(1);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidFunctionAppWithArg9() throws Exception {
String input = "prog {float var1;var1 := polar_r(1.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidExpressionBinary3() throws Exception {
String input = "prog{ boolean var1;var1 := true & false; var1 := true | false; "
+ "var1 := true == false; var1 := true != false; var1 := true > false; "
+ "var1 := true >= false; var1 := true < false; var1 := true <= false;}";
typeCheck(input);
}
@Test
public void testvalidFunctionAppWithPixel1() throws Exception {
String input = "prog {int var1; var1 := cart_x[1.0,1.0]; var1 := cart_y[1.0,1.0];}";
typeCheck(input);
}
@Test
public void testvalidDeclarations1() throws Exception {
String input = "prog{int var1; float var2; boolean var3; image var4; "
+ "filename var5; image var6[500,500];}";
typeCheck(input);
}
@Test
public void testvalidDeclarations2() throws Exception {
String input = "prog{int var1; float var2; boolean var3; image var4; "
+ "filename var5; image var6[500,500];if(true){int var1; float var2; "
+ "boolean var3; image var4; filename var5; image var6[500,500];};}";
typeCheck(input);
}
@Test
public void testvalidDeclarations3() throws Exception {
String input = "prog{if(false){int var1; float var2; boolean var3; "
+ "image var4; filename var5; image var6[500,500];};"
+ "if(true){int var1; float var2; boolean var3; image var4; filename var5; "
+ "image var6[500,500];};}";
typeCheck(input);
}
@Test
public void testinvalidStatementSleep1() throws Exception {
String input = "prog{sleep 1.0;}";
thrown.expect(SemanticException.class);
try {
typeCheck(input);
} catch (SemanticException e) {
show(e);
throw e;
}
}
@Test
public void testvalidUnary1() throws Exception {
String input = "prog{ int a; float b; boolean c;a := +a; a := -a; a := !a; "
+ "b := +b; b := -b; b := !b; c := +c; c := -c; c := !c;}";
typeCheck(input);
}
@Test
public void testvalidUnary2() throws Exception {
String input = "prog{ filename a; image b;a := +a; a := -a; a := !a; "
+ "b := +b; b := -b; b := !b;}";
typeCheck(input);
}
@Test
public void testvalidStatementSleep1() throws Exception {
String input = "prog{sleep 1;}";
typeCheck(input);}
@Test
public void testvalidStatementAssign1() throws Exception {
String input = "prog{int var1; var1 := 1; float var2; var2 := 1.0;"
+ "boolean var3; var3 := true;filename f1; filename f2; f1 := f2;image var4; "
+ "image var5[500,500]; var4 := var5;}";
typeCheck(input);
}
@Test
public void testvalidStatementAssign2() throws Exception {
String input = "prog{image var; var[0,0] := 1; alpha(var[0,0]) := 1; "
+ "red(var[0,0]) := 1; green(var[0,0]) := 1; blue(var[0,0]) := 1;}";
typeCheck(input);
}
// */
@Test
public void testvalidFunctionAppWithArg3() throws Exception {
String input = "prog {int var1; float var2; image var3;var1 := width(var3); var1 := height(var3); var2 := float(1); var1 := int(1.0);}";
typeCheck(input);
}
@Test
public void testinvalidExpressionBinary1() throws Exception {
String input = "prog{ show (1.0 % 2.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidExpressionBinary3() throws Exception {
String input = "prog{ show (1.0 % 2);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidExpressionBinary6() throws Exception {
String input = "prog{ show (1 % 2.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidExpressionBinary7() throws Exception {
String input = "prog{ show (1 & 2.0); show (1 | 2.0);}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidDeclarations3() throws Exception {
String input = "prog{image var1[500, 1.0];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidExpressionPixel1() throws Exception {
String input = "prog{ int var2; var2 := var1[0,0];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidExpressionPixel2() throws Exception {
String input = "prog{ int var1; int var2; var2 := var1[0,0];}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidLhsSample1() throws Exception {
String input = "prog{red (var1[0,0]) := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidLhsSample2() throws Exception {
String input = "prog{int var1; red( var1[0,0]) := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidStatementIf1() throws Exception {
String input = "prog{boolean a; boolean b; if(a & b){};}";
typeCheck(input);
}
@Test
public void testinvalidLhsPixel1() throws Exception {
String input = "prog{var1[0,0] := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidLhsPixel2() throws Exception {
String input = "prog{int var1; var1[0,0] := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidExpressionConditional1() throws Exception {
String input = "prog{boolean cond;int var1; var1 := cond ? var1+ 1 : var1;float var2; var2 := cond ? var2 + 1.0 : var2;}";
typeCheck(input);
}
@Test
public void testinvalidPixelSelector1() throws Exception {
String input = "prog{image var1; red( var1[0,0.0]) := 5;}";
thrown.expect(SemanticException.class);
try {
typeCheck(input);
}
catch (SemanticException e) { show(e); throw e; } }
@Test
public void testinvalidPixelSelector2() throws Exception {
String input = "prog{image var1; red( var1[0.0,0]) := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testinvalidPixelSelector3() throws Exception {
String input = "prog{image var1; red( var1[true,false]) := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
// typeCheck(input);
}
@Test
public void testScope2() throws Exception {
String input = "p{int var; if(true) {float var; var := 5.0;}; var := 5.0;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testScope4() throws Exception {
String input = "p{int var; if(true) {float var; var := 5;}; var := 5.0;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testScope3() throws Exception {
String input = "p{int var; if(true) {float var; var := 5;}; var := 5;}";
thrown.expect(SemanticException.class);
try { typeCheck(input); } catch (SemanticException e) { show(e); throw e; }
}
@Test
public void testvalidFunctionAppWithArg1() throws Exception {
String input = "prog {int var1; var1 := abs(1); var1 := red(1); var1 := green(1); "
+ "var1 := blue(1); var1 := int(1); var1 := alpha(1);}";
typeCheck(input);
}
}