-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompilerTest.java
1111 lines (944 loc) · 33.5 KB
/
CompilerTest.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nl.cos;
import nl.cos.exceptions.CompilerException;
import nl.cos.io.JasminBytecode;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
/**
* This file shows a few different ways you can do automated tests:
*
* - checkByteCode(): Shows how to compile a file and check that the bytecode that was
* generated matches your expectations.
* - checkByteCode2(): Same as above, but now the output is stored in a file.
* - syntaxErrorsAreFound(): Checks that a file with syntax errors stops compilation.
* - checkOutputFile(): This test shows how to compile a file, run it and check if the output
* matches your expectations.
* - checkOutputString(): Same as above, but now the source code is coming from a string within
* the test.
*
* Not shown is a test where the file contains no syntax errors, but the checker should find some
* error. You can of course add that yourself.
*/
class CompilerTest extends TestBase {
@Test
@DisplayName("GoodWeather - Output string")
void checkOutputString() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-\"Hello World\";", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"Hello World"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Output number")
void checkOutputNumber() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-12345;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"12345"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Output boolean")
void checkOutputBoolean() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-ival;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Output null")
void checkOutputNull() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-nil;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"null"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Output float")
void checkOutputFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-1234.1234;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"1234.1234"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Basic addition")
void checkAddition() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5+4;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"9"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Basic subtraction")
void checkDivision() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5-4;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"1"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Basic modulus")
void checkModulus() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-3%2;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"1"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Basic multiplication")
void checkMultiplication() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7*3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"21"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Basic division")
void checkSubtraction() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-8/2;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"4"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Math priority operations")
void checkMathPriorityOperations() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7+2*4/2-1;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"10"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Math priority operations with brackets")
void checkMathPriorityOperationsBrackets() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-(7+2)*4/2-1;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"17"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Float addition")
void checkFloatAddition() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7.4+8.5;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"15.9"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Float subtraction")
void checkFloatDivision() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-8.2-5.6;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"2.6"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Float modulus")
void checkFloatModulus() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-8.0%2.0;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"0.0"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Float multiplication")
void checkFloatMultiplication() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5.8*2.5;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"14.5"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Float division")
void checkFloatSubtraction() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-8.4/3.5;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"2.3999999"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Math float priority operations")
void checkMatFloatPriorityOperations() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-8.3+5.6*4.2/2.6-3.1;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"14.246153"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Math float priority operations with brackets")
void checkMathPriorityOperationsFloatBrackets() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-(8.3+5.6)*4.2/2.6-3.1;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"19.353846"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Division by zero")
void checkDivisionByZero() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("$()<-6/0;", "ArcTest");
});
assertEquals("Cannot do division by zero!", e.getMessage());
}
@Test
@DisplayName("BadWeather - Different number types in math")
void checkDifferentNumbersInMath() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("$()<-5+6.5;", "ArcTest");
});
assertEquals("Can only do operation on the same type", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Boolean equal on numbers")
void checkBoolEqual() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5==5;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean equal on float")
void checkBoolEqualFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7.2==7.2;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean equal on booleans")
void checkBoolEqualBooleans() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-ival==ival;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean not equal on numbers")
void checkBoolNotEqual() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5~=5;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean not equal on float")
void checkBoolNotEqualFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7.2~=7.2;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean not equal on booleans")
void checkBoolNotEqualBooleans() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-ival~=ival;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater on ints")
void checkBoolGrater() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-4>3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater on ints - false")
void checkBoolGraterFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-2>3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater on floats")
void checkBoolGraterFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-4.2>4.1;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater on floats - false")
void checkBoolGraterFloatFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7.2>7.3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less on ints")
void checkBoolLess() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-3<6;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less on ints - false")
void checkBoolLessFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-7<3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less on floats")
void checkBoolLessFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-3.2<3.6;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less on floats - false")
void checkBoolLessFloatFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5.6<5.3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater-equal on ints")
void checkBoolGraterE() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-4>=3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater-equal on ints - false")
void checkBoolGraterEFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-2>=3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater-equal on floats")
void checkBoolGraterEFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-4.6>=4.4;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean grater-equal on floats - false")
void checkBoolGraterEFloatFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-1.5>=1.6;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less-equal on ints")
void checkBoolLessE() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-2<=3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less-equal on ints - false")
void checkBoolLessEFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-6<=3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less-equal on floats")
void checkBoolLessEFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-3.5<=3.6;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean less-equal on floats - false")
void checkBoolLessEFloatFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-5.4<=5.3;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Boolean compare on incompatible types")
void checkBoolCompareIncompatible() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("$()<-5.4>6;", "ArcTest");
});
assertEquals("Can only do operation on the same type", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Boolean OR")
void checkBoolOr() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-val||val;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean OR - 2nd")
void checkBoolOr2() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-ival||val;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean OR - false")
void checkBoolOrFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-ival||ival;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Boolean OR with numbers")
void checkBoolOrWithNumbers() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("$()<-5||val;", "ArcTest");
});
assertEquals("Can only do boolean logical compare on boolean values!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Boolean AND")
void checkBoolAnd() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-val&&val;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Boolean AND - false")
void checkBoolAndFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("$()<-val&&ival;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Boolean AND with numbers")
void checkBoolAndWithNumbers() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("$()<-5&&val;", "ArcTest");
});
assertEquals("Can only do boolean logical compare on boolean values!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Declare int variable")
void checkVariableDeclarationInt() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:num;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"ldc 0",
"istore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("GoodWeather - Declare float variable")
void checkVariableDeclarationFloat() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:frac;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"ldc 0.0",
"fstore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("GoodWeather - Declare string variable")
void checkVariableDeclarationString() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:text;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"aconst_null",
"astore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("GoodWeather - Declare boolean variable")
void checkVariableDeclarationBool() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:logic;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"ldc 0",
"istore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("GoodWeather - Declare multiple variables")
void checkMultipleVarDeclare() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:text;b:num;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"aconst_null",
"astore 2",
"ldc 0",
"istore 3",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("BadWeather - Declare the same variable twice")
void checkDeclareSameVariable() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:num;a:num;", "ArcTest");
});
assertEquals("Variable 'a' already exists in current scope and cannot be added!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Variable declare and init")
void checkVarDecAndInit() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:frac<-2.3;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"ldc 2.3",
"fstore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("BadWeather - Declare and init var with wrong type")
void checkDeclareAndInitVarWithWrongType() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:logic<-9.5;", "ArcTest");
});
assertEquals("Cannot assign FLOAT to BOOLEAN", e.getMessage());
}
@Test
@DisplayName("BadWeather - Init var that doesn't exit")
void checkInitNonExistingVar() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a<-9.5;", "ArcTest");
});
assertEquals("Could not resolve variable 'a'!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Variable declare and later init")
void checkVarDecAndInitLater() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:num;a<-6;", "ArcTest");
assertNotNull(code);
assertArrayEquals(new String[] {
".bytecode 49.0",
".class public ArcTest",
".super java/lang/Object",
"",
".method public static main([Ljava/lang/String;)V",
".limit stack 99",
".limit locals 99",
"ldc 0",
"istore 2",
"ldc 6",
"istore 2",
"return",
".end method",
}, code.getLines().toArray());
}
@Test
@DisplayName("GoodWeather - Resolve variable value")
void checkResolveVariableValue() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:frac;a<-6.12;$()<-a;", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"6.12"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Store incompatible number in var")
void checkIncompatibleNumInVar() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a<-9.5;", "ArcTest");
});
assertEquals("Could not resolve variable 'a'!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - If statement true branch")
void checkIfStatementTrue() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("is(val) -> yes {$() <- val;} no {$() <- ival;}", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"true"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - If statement false branch")
void checkIfStatementFalse() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("is(ival) -> yes {$() <- val;} no {$() <- ival;}", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"false"}, output.toArray());
}
@Test
@DisplayName("BadWeather - If statement has a non-boolean condition")
void checkIfStatementNonBool() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("is(6+2) -> yes {$() <- val;}", "ArcTest");
});
assertEquals("Condition statement expected a boolean!", e.getMessage());
}
@Test
@DisplayName("BadWeather - Variable not in scope")
void checkVarNotInScope() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("is(val) -> yes {a:num <- 6;} a<-7;", "ArcTest");
});
assertEquals("Could not resolve variable 'a'!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Variable in outer scope")
void checkVarInOuterScope() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("a:num <- 5; $()<-a;is(val) -> yes {a<-10; $()<-a;}", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"5","10"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Loop")
void checkLoop() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("index:num <- 1; rep(index <= 5) {$()<-index; index <- index + 1;}", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[] {"1","2","3","4","5"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Function returns when type is void")
void checkFuncVoidReturn() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func<-():nil=>{()<-1;};", "ArcTest");
});
assertEquals("Cannot return from a function with a return type of NIL", e.getMessage());
}
@Test
@DisplayName("BadWeather - Function return when mismatch")
void checkFuncMismatchReturn() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func<-():num=>{()<-4.6;};", "ArcTest");
});
assertEquals("Function expected to return INT, but found FLOAT", e.getMessage());
}
@Test
@DisplayName("BadWeather - Function does not return when expected")
void checkFuncNoReturn() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func<-():num=>{$()<-1;};", "ArcTest");
});
assertEquals("Function is supposed to return INT, but does not", e.getMessage());
}
@Test
@DisplayName("BadWeather - Assign function to an other type variable")
void checkFuncToNonFuncVariable() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:logic<-():num=>{()<-1;};", "ArcTest");
});
assertEquals("Cannot assign function to non-function variable", e.getMessage());
}
@Test
@DisplayName("BadWeather - Assign function to an other type variable")
void checkForUninitFunc() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func; @a();", "ArcTest");
});
assertEquals("Cannot execute function variable 'a', as it is not initialized", e.getMessage());
}
@Test
@DisplayName("BadWeather - Access var in func from outer scope")
void checkFuncReachOuterVar() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("b:logic<-val;a:func<-():logic=>{()<-b;};", "ArcTest");
});
assertEquals("Could not resolve variable 'b'!", e.getMessage());
}
@Test
@DisplayName("BadWeather - Function returns Function with signature")
void checkFuncRetFuncWithSign() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func<-():func[num,num]#logic=>{()<-val;};", "ArcTest");
});
assertEquals("Cannot return a Function Parameter", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Function normal execution")
void checkFunctionCall() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("f:func<-(n:num):num=>{()<-n+1;};$()<-@f(4);", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[]{"5"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Function declare and then init, then call")
void checkFuncDecInitCall() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("f:func;f<-(n:num):num=>{()<-n+1;};$()<-@f(9);", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[]{"10"}, output.toArray());
}
@Test
@DisplayName("GoodWeather - Function inside a function")
void checkFuncInFunc() throws Exception {
Compiler c = new Compiler();
JasminBytecode code = c.compileString("x:func<-(n:num):nil=>{addOne:func<-(t:num):num=>{()<-t+1;};$()<-@addOne(n);};@x(5);", "ArcTest");
assertNotNull(code);
List<String> output = runCode(code);
assertArrayEquals(new String[]{"6"}, output.toArray());
}
@Test
@DisplayName("BadWeather - Redeclare function parameter")
void checkFuncParamRedeclare() {
Exception e = assertThrows(CompilerException.class,
()->{
Compiler c = new Compiler();
c.compileString("a:func<-(n:num):logic=>{n:frac<-3.2;()<-val;};", "ArcTest");
});
assertEquals("Variable 'n' already exists in current scope and cannot be added!", e.getMessage());
}
@Test
@DisplayName("GoodWeather - Pass function as parameter")
void checkFuncAsParameter() throws Exception {
Compiler c = new Compiler();