-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenTest.java
More file actions
847 lines (740 loc) · 35.6 KB
/
Copy pathCodeGenTest.java
File metadata and controls
847 lines (740 loc) · 35.6 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
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
package *******;
import static org.junit.Assert.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556sp18.RuntimeLog;
import cop5556sp18.CodeGenUtils.DynamicClassLoader;
import cop5556sp18.AST.Program;
public class CodeGenTest implements ImageFiles{
//determines whether show prints anything
static boolean doPrint = true;
static void show(Object s) {
if (doPrint) {
System.out.println(s);
}
}
//determines whether a classfile is created
static boolean doCreateFile = false;
@Rule
public ExpectedException thrown = ExpectedException.none();
//values passed to CodeGenerator constructor to control grading and debugging output
private boolean devel = true; //if true, print devel output
private boolean grade = true; //if true, print grade output
// private boolean devel = false;
// private boolean grade = false;
//sets the default width and height of newly created images. Should be small enough to fit on screen.
public static final int defaultWidth = 1024;
public static final int defaultHeight = 1024;
/**
* Generates bytecode for given input.
* Throws exceptions for Lexical, Syntax, and Type checking errors
*
* @param input String containing source code
* @return Generated bytecode
* @throws Exception
*/
byte[] genCode(String input) throws Exception {
//scan, parse, and type check
Scanner scanner = new Scanner(input);
show(input);
scanner.scan();
Parser parser = new Parser(scanner);
Program program = parser.parse();
TypeChecker v = new TypeChecker();
program.visit(v, null);
// show(program); //It may be useful useful to show this here if code generation fails
//generate code
CodeGenerator cv = new CodeGenerator(devel, grade, null, defaultWidth, defaultHeight);
byte[] bytecode = (byte[]) program.visit(cv, null);
show(program); //doing it here shows the values filled in during code gen
//display the generated bytecode
show(CodeGenUtils.bytecodeToString(bytecode));
//write byte code to file
if (doCreateFile) {
String name = ((Program) program).progName;
String classFileName = "bin/" + name + ".class";
OutputStream output = new FileOutputStream(classFileName);
output.write(bytecode);
output.close();
System.out.println("wrote classfile to " + classFileName);
}
//return generated classfile as byte array
return bytecode;
}
/**
* Run main method in given class
*
* @param className
* @param bytecode
* @param commandLineArgs String array containing command line arguments, empty array if none
* @throws +
* @throws Throwable
*/
void runCode(String className, byte[] bytecode, String[] commandLineArgs) throws Exception {
RuntimeLog.initLog(); //initialize log used for grading.
DynamicClassLoader loader = new DynamicClassLoader(Thread.currentThread().getContextClassLoader());
Class<?> testClass = loader.define(className, bytecode);
@SuppressWarnings("rawtypes")
Class[] argTypes = {commandLineArgs.getClass()};
Method m = testClass.getMethod("main", argTypes );
show("Output from " + m + ":"); //print name of method to be executed
Object passedArgs[] = {commandLineArgs}; //create array containing params, in this case a single array.
try {
m.invoke(null, passedArgs);
}
catch (Exception e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
Exception ec = (Exception) e.getCause();
throw ec;
}
throw e;
}
}
/**
* When invoked from JUnit, Frames containing images will be shown and then immediately deleted.
* To prevent this behavior, waitForKey will pause until a key is pressed.
*
* @throws IOException
*/
void waitForKey() throws IOException {
System.out.println("enter any char to exit");
System.in.read();
}
/**
* When invoked from JUnit, Frames containing images will be shown and then immediately deleted.
* To prevent this behavior, keepFrame will keep the frame visible for 5000 milliseconds.
*
* @throws Exception
*/
void keepFrame() throws Exception {
Thread.sleep(5000);
}
/**
* Since we are not doing any optimization, the compiler will
* still create a class with a main method and the JUnit test will
* execute it.
*
* The only thing it will do is append the "entering main" and "leaving main" messages to the log.
*
* @throws Exception
*/
@Test
public void emptyProg() throws Exception {
String prog = "emptyProg";
String input = prog + "{}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testadd() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 1 + 3.0; show 1.0 + 2.0; show 3.0 +1;show 2 +2;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;4.0;3.0;4.0;4;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testsub() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 1 - 3.0; show 1.0 - 2.0; show 3.0 - 1;show 2 - 2;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;-2.0;-1.0;2.0;0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void unaryOpNot1() throws Exception {
String prog = "Prog";
String input = prog + "{show !false; show !true; show !(true | false); show !(true & false);} ";//how 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;-2.0;-1.0;2.0;0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void unaryOpNot2() throws Exception {
String prog = "Prog";
String input = prog + "{show !1; show !-1; show !0; show !-2;} ";//how 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;-2;0;-1;1;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testdiv() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 6.0 / 3; show 2.0 / 1.0; show 3.0 / 1;show 2 / 2;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;2.0;2.0;3.0;1;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBinary4() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 9.4 + 4; show 9.5 - 4; show 9.0 * 4; show 9.0 / 4; show 9.0 ** 4;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;13.4;5.5;36.0;2.25;6561.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBinary3() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 9 + 4.5;show 9 - 4.5;show 9 * 4.5;show 8 / 4.2;show 9 ** 4.0;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;13.5;4.5;40.5;1.904762;6561.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBinary2() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 5.5 + 4.5 ; show 9.5 - 4.5; show 9.0 * 4.5; show 15.0 / 2.5; show 9.1 ** 4.1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;10.0;5.0;40.5;6.0;8552.039;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBinary1() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 9 % 4;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;1;5;36;2;6561;1;0;13;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testmul() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 1 * 3.0; show 1.0 * 2.0; show 3.0 * 1;show 2 * 2;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;3.0;2.0;3.0;4;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testpower() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 3 ** 3.0; show 4.0 ** 2.0; show 3.0 ** 3;show 2 ** 2;}";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;27.0;16.0;27.0;4;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void test1Prog() throws Exception {
String prog = "emptyProg";
String input = prog + "{show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;5;36;2;6561;1;0;13;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBool() throws Exception {
String prog = "Prog";
String input = prog + "{boolean c; boolean d; d:=false; c := true; show !c; show !d; }";//;show 9 - 4;show 9 * 4;show 9 / 4;show 9 ** 4;show 9 % 4;show 9 & 4;show 9 | 4;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n "+RuntimeLog.globalLog);
assertEquals("entering main;false;true;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void assignImageWithDifferentSize() throws Exception {
String prog = "Prog";
String input = prog + "{image y;\n show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void declareAssignBoolean() throws Exception {
String prog = "Prog";
String input = prog + "{ boolean y; y := true; show y; y := false; show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;true;false;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void integerLit() throws Exception {
String prog = "prog";
String input = prog + "{ int var1; float var2; image var3;var1 := width(var3); var1 := height(var3); var2 := float(1); var1 := int(1.0);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void assignFile() throws Exception {
String prog = "Prog";
String input = prog + "{ filename f1;\n filename f2; \n input f1 from @ 0 ;\n f2 := f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1};//"C:\\Users\\Surbhi\\eclipse-workspace\\cop5556sp18\\src\\cop5556sp18\\test_file.txt"}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
//assertEquals("entering main;3.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void unaryOpMinus() throws Exception {
String prog = "Prog";
String input = prog + "{show -3; show -4.5; show -3-4.5;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;-3;-4.5;-7.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void readImageFromCommandLineAndScale() throws Exception {
String prog = "Prog";
String input = prog + "{image y[300,400];\n input y from @ 0 ;\n show y;sleep(4000);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
// BufferedImage refImage0 = RuntimeImageSupport.readFromFile(file3);
// BufferedImage loggedImage0 = RuntimeLog.globalImageLog.get(0);
}
@Test
public void declareAssignFloat() throws Exception {
String prog = "Prog";
String input = prog + "{float y; y := 6.6; show y; y := -0.5; show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;6.6;-0.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void assignImage1() throws Exception {
String prog = "Prog";
String input = prog + "{image x; input x from @ 0 ; show x; image y; y := x; show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
// BufferedImage refImage0 = RuntimeImageSupport.readFromFile(file1);
// BufferedImage loggedImage0 = RuntimeLog.globalImageLog.get(0);
}
@Test
public void assignImage2() throws Exception {
String prog = "Prog";
String input = prog + "{image y[1000,1000]; image copy[1000,1000]; input y from @ 0 ; show y; copy := y; show copy;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
// BufferedImage refImage0 = RuntimeImageSupport.readFromFile(file1);
// BufferedImage loggedImage0 = RuntimeLog.globalImageLog.get(0);
}
@Test
public void testExpressionFuncArg1() throws Exception {
String prog = "Prog";
String input = prog + "{image b[512,256]; show width(b); show height(b);\nimage c;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;512;256;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionFuncArg2() throws Exception {
String prog = "Prog";
String input = prog + "{show abs(1); show abs(-1); show abs(5.3); show abs(-5.3); show abs(1-5.3);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;1;1;5.3;5.3;4.3;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionFuncArg3() throws Exception {
String prog = "Prog";
String input = prog + "{int a; a := 123456789;\n show red(a); show green(a); show blue(a);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;91;205;21;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionFuncArg4() throws Exception {
String prog = "Prog";
String input = prog + "{int a; a := 123456789; show alpha(a);\n a := -1; show alpha(a);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;7;255;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionFuncArg5() throws Exception {
String prog = "Prog";
String input = prog + "{float a; a := float(-3.7); show a; a := float(4); show a;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;-3.7;4.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionFuncArg6() throws Exception {
String prog = "Prog";
String input = prog + "{int a; a := int(-3.7); show a; a := int(4); show a;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;-3;4;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testAssociativityPrecedence4() throws Exception {
String prog = "Prog";
String input = prog + "{show 4 ** 3 ** 2;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;262144;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void declareAssignInteger() throws Exception {
String prog = "Prog";
String input = prog + "{int y; y := 55; show y; y := -234; show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;55;-234;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void sometest() throws Exception {
String prog = "Prog";
String input = prog + "{show sin(10.0);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;55;-234;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void createNonDefaultSizeImage() throws Exception {
String prog = "Prog";
String input = prog + "{image y[512,256];\n show y;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
// BufferedImage refImage0 = RuntimeImageSupport.readFromFile(file1);
// BufferedImage loggedImage0 = RuntimeLog.globalImageLog.get(0);
}
@Test
public void readFromCommandLine() throws Exception {
String prog = "Prog";
String input = prog + "{int x; input x from @ 0 ; show x;\nfloat y; input y from @ 1; show y;\nboolean z; input z from @ 2; show z;\ninput z from @ 3; show z;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"3","5.0","true","false"}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;3;5.0;true;false;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void readImageFromCommandLine() throws Exception {
String prog = "Prog";
String input = prog + "{image y; input y from @ 0 ; show y; sleep(2000);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
//BufferedImage refImage0 = RuntimeImageSupport.readFromFile(file1);
//BufferedImage loggedImage0 = RuntimeLog.globalImageLog.get(0);
}
@Test
public void imagetest() throws Exception {
String prog = "prog";
String input = prog + "{image y; image copy[128,256]; input y from @ 0 ; show y; copy := y; show copy;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
keepFrame();
}
@Test
public void checkif() throws Exception {
String prog = "Prog";
String input = prog + "{int a; a :=2; int b ;b:=5; if(b>a){show a+b;};}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;7;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkif2() throws Exception {
String prog = "Prog";
String input = prog + "{float a; a :=5.0; float b ;b:=5.0; if(b <= a){show a+b;};}";// if (a < b) {show b;};}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;7.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkif3() throws Exception {
String prog = "Prog";
String input = prog + "{boolean a; a := true; boolean b; b := true; if(a&b) {show b;};}";// if (a < b) {show b;};}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;true;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkwhile1() throws Exception {
String prog = "Prog";
String input = prog + "{boolean a; a := true; boolean b; b := true; while(a & b){ b := false; show b;};}";// if (a < b) {show b;};}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;false;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkConditional() throws Exception {
String prog = "Prog";
String input = prog + "{boolean cond; cond:= true & true; int var1; var := 2; var1 := cond ? var1+ 1 : var1; show var1;}"; //float var2; var2 := 3.0; var2 := cond ? var2 + 1.0 : var2;}";// if (a < b) {show b;};}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;true;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkWrite() throws Exception {
String prog = "Prog";
String input = prog + "{image image1; input image1 from @ 0; show image1; sleep(4000); filename f1; }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkvalidFunctionAppWithPixel2() throws Exception {
String prog = "Prog";
String input = prog + "{float var1; var1 := polar_a[1,1];show var1; var1 := polar_r[1,1]; show var1; }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;0.7853982;1.4142135;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkvalidFunctionAppWithPixel1() throws Exception {
String prog = "Prog";
String input = prog + "{int var1; var1 := cart_x[2.0,0.0];show var1; var1 := cart_y[2.0,90.0]; show var1; }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;2;1;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkExpressionPixel() throws Exception {
String prog = "Prog";
String input = prog + "{image var1; int var2; var2 := var1[0,0]; }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkvalidFunctionAppWithPixel3() throws Exception {
String prog = "Prog";
String input = prog + "{int var1; var1 := cart_x[2.0,0.0];show var1; var1 := cart_y[2.0,90.0]; show var1; }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;2;1;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void checkPixelConstructor() throws Exception {
String prog = "Prog";
String input = prog + "{image im[1024,1024];input im from @ 0;int x; x:=0; int y; y:=0; im[x,y] := <<0,0,0,0>>; show im; sleep(4000); }";//write image1 to f1;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void makeRedImage() throws Exception {
String prog = "Prog";
String input = prog + "{"
+ "image im[256,256];\n"
+ "int x;\n"
+ "int y; \n"
+ "x := 0; \n"
+ "y := 0; \n"
+ "while (x < width(im)){ \n"
+ "y := 0;\n "
+ "while (y < height(im)) { \n"
+ "im[x,y] := <<255,255,0,0>>; \n"
+ "y := y + 1; \n"
+ "};\n"
+ "x := x +1;};\n"
+ "show im;\n}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
keepFrame();
}
@Test
public void testExpressionPixelConstructor1() throws Exception {
String prog = "prog";
String input = prog + "{show <<0,0,0,0>>; show <<255,255,255,255>>;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
//assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void polarR2() throws Exception { //arrayoutofboundexception 1
String prog = "Prog";
String input = prog + "{ image im[1024,1024];"
//+ "input im from @ 0;"
+ "int x;\n"
+ "x :=0; \n"
+ "show width(im);"
+ "while (x < width(im)) { \n"
+ "int y; \n"
+ "y := 0;"
+ "while (y < height (im)) {\n"
+ "float p;\n"
+ "p := polar_r[x,y];\n"
+ "int r;\n"
+ "r := int(p)%Z;\n"
+ "im[x,y] := <<Z,0,0,r>>; \n"
+ "y := y+1; \n"
+ "};\n"
+ "x := x +1;};\n"
+ "show im;\n"
+ "}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
keepFrame();
System.out.println("pixel(0,0,0,1)=" + RuntimePixelOps.makePixel(0,0,0,1));
}
@Test
public void demo1() throws Exception {
String prog = "prog";
String input = prog + "{image h;input h from @0;show h; sleep(4000); image g[width(h),height(h)];int x;x:=0;while(x<width(g)){int y;y:=0;while(y<height(g)){g[x,y]:=h[y,x];y:=y+1;};x:=x+1;};show g;sleep(4000);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void samples() throws Exception {
String prog = "prog";
String input = prog + "{image bird; input bird from @ 0;show bird;sleep(4000);image bird2[width(bird),height(bird)];int x;x:=0;while(x<width(bird2)) {int y;y:=0;while(y<height(bird2)) {blue(bird2[x,y]):=red(bird[x,y]);green(bird2[x,y]):=blue(bird[x,y]);red(bird2[x,y]):=green(bird[x,y]);alpha(bird2[x,y]):=Z;y:=y+1;};x:=x+1;};show bird2;sleep(4000);}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void writeRedImage() throws Exception { //color blue coming instead of red
String prog = "prog";
String input = prog + "{image im[256,256]; \nfilename f; \ninput f from @0; \nint x;\n int y; \nx := 0; \ny := 0; \nwhile (x < width(im)){ \n y := 0; while (y < height(im)){\nim[x,y] := <<255,255,0,0>>; \nint z; z := im[x,y];y := y + 1; \n};\nx := x + 1;};\nwrite im to f;\n}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {RedImage}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void writeModifiedImage() throws Exception {
String prog = "prog";
String input = prog + "{image im; \ninput im from @0; \nfilename f; \ninput f from @1; \nint x;\n int y; \nx := 0; \ny := 0; \nwhile (x < width(im)){ \n y := 0; while (y < height(im)){\nim[x,y] := <<15,255,0,0>>; \nint z; z := im[x,y];y := y + 1; \n};\nx := x + 1;};\nwrite im to f;\n}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file3, check}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testBlockScope2() throws Exception {
String prog = "prog";
String input = prog + "{ if(true){ int x; }; int x; x := 5; show x;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {};
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void writeTransparentImage() throws Exception {
String prog = "prog";
String input = prog + "{ image im[256,256]; \nfilename f; \ninput f from @0; \nint x;\n int y; \nx := 0; \ny := 0; \nwhile (x < width(im)){ \n y := 0; while (y < height(im)){\nim[x,y] := <<15,255,0,0>>; \nint z; z := im[x,y];y := y + 1; \n};\nx := x + 1;};\nwrite im to f;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {transparent}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void writeImageToFile() throws Exception {
String prog = "prog";
String input = prog + "{image y;\n filename f;\n input y from @ 0 ; input f from @1; \n show y; write y to f;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {file1, file}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void testExpressionPixelConstructor2() throws Exception {
String prog = "prog";
String input = prog + "{show <<25,137,10,67>>; show <<1000,-50,1000,-10>>;}";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
show("Log:\n"+RuntimeLog.globalLog);
//assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
}