-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeGenTest.java
More file actions
730 lines (647 loc) · 29.4 KB
/
CodeGenTest.java
File metadata and controls
730 lines (647 loc) · 29.4 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
/**
* Starter code with JUnit tests for code generation used in the class project in COP5556 Programming Language Principles
* at the University of Florida, Spring 2018.
*
* This software is solely for the educational benefit of students
* enrolled in the course during the Spring 2018 semester.
*
* This software, and any software derived from it, may not be shared with others or posted to public web sites,
* either during the course or afterwards.
*
* @Beverly A. Sanders, 2018
*/
package cop5556sp18;
import static org.junit.Assert.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5556sp18.CodeGenUtils.DynamicClassLoader;
import cop5556sp18.AST.Program;
public class CodeGenTest {
//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 integerLit() throws Exception {
String prog = "integerLit";
String input = prog + "{show 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;3;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void floatLit() throws Exception {
String prog = "floatLit";
String input = prog + "{show 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;4.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void booleanLit() throws Exception {
String prog = "booleanLit";
String input = prog + "{show true;} ";
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 BinaryExpressionLit() throws Exception {
String prog = "binaryExpressionLit";
String input = prog + "{show (0+2.5)+(3*1);} ";
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;5.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit2() throws Exception {
String prog = "binaryExpressionLit2";
String input = prog + "{show (2+1)*(3-1.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;4.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit4() throws Exception {
String prog = "binaryExpressionLit4";
String input = prog + "{show 2&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;2;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit3() throws Exception {
String prog = "binaryExpressionLit3";
String input = prog + "{show (2+14.9)>(14.8+2.1);} ";
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 BinaryExpressionLit9() throws Exception {
String prog = "binaryExpressionLit9";
String input = prog + "{show 4.5*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;14.400001;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit5() throws Exception {
String prog = "binaryExpressionLit5";
String input = prog + "{show (true&false)&(false&true);} ";
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 BinaryExpressionLit6() throws Exception {
String prog = "binaryExpressionLit6";
String input = prog + "{show (true&false)|(false|true);} ";
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 BinaryExpressionLit7() throws Exception {
String prog = "binaryExpressionLit7";
String input = prog + "{show 2|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;3;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit8() throws Exception {
String prog = "binaryExpressionLit8";
String input = prog + "{show (1*2.5)**(3*1);} ";
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;15.625;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void BinaryExpressionLit10() throws Exception {
String prog = "binaryExpressionLit10";
String input = prog + "{show (1*2)**(3*1);} ";
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;8;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void UnaryExpressionLit() throws Exception {
String prog = "unaryExpressionLit";
String input = prog + "{show +(2-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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void UnaryExpressionLit2() throws Exception {
String prog = "unaryExpressionLit2";
String input = prog + "{show -(2.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;-0.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void UnaryExpressionLit3() throws Exception {
String prog = "unaryExpressionLit3";
String input = prog + "{show !(1+1);} ";
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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void UnaryExpressionLit4() throws Exception {
String prog = "unaryExpressionLit4";
String input = prog + "{show !(true&false);} ";
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 SleepExpressionLit() throws Exception {
String prog = "sleepExpressionLit";
String input = prog + "{sleep 500;} ";
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 FunctionExpressionWithArgLit() throws Exception {
String prog = "functionExpressionWithArgLit";
String input = prog + "{show sin(0.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;0.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit2() throws Exception {
String prog = "functionExpressionWithArgLit2";
String input = prog + "{show cos(0.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;1.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit3() throws Exception {
String prog = "functionExpressionWithArgLit3";
String input = prog + "{show atan(0.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;0.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit4() throws Exception {
String prog = "functionExpressionWithArgLit4";
String input = prog + "{show log(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;0.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit5() throws Exception {
String prog = "functionExpressionWithArgLit5";
String input = prog + "{show abs(-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;1.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit6() throws Exception {
String prog = "functionExpressionWithArgLit6";
String input = prog + "{show abs(-1);} ";
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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit7() throws Exception {
String prog = "functionExpressionWithArgLit7";
String input = prog + "{show float(-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;-1.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit8() throws Exception {
String prog = "functionExpressionWithArgLit8";
String input = prog + "{show alpha(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;0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionExpressionWithArgLit9() throws Exception {
String prog = "functionExpressionWithArgLit9";
String input = prog + "{show 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;-1;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionPredefinedNameLit() throws Exception {
String prog = "functionPredefinedNameLit";
String input = prog + "{show Z;} ";
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;255;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void FunctionPredefinedNameLit2() throws Exception {
String prog = "functionPredefinedNameLit2";
String input = prog + "{show default_width;} ";
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;1024;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void AssignmentTest() throws Exception {
String prog = "AssignmentTest";
String input = prog + "{int a; a := 2; show a; float b; b := 2.5; 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;2;2.5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void AssignmentTest2() throws Exception {
String prog = "AssignmentTest2";
String input = prog + "{float a; a := 2.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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void AssignmentTest3() throws Exception {
String prog = "AssignmentTest3";
String input = prog + "{boolean a; a := true;} ";
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 InputTest() throws Exception {
String prog = "InputTest";
String input = prog + "{filename f; input f from @ 0; filename g; g := f;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG"}; //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 ImageTest() throws Exception {
String prog = "ImageTest";
String input = prog + "{image i[400, 600]; show width(i);} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
//waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;400;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void ImageTest2() throws Exception {
String prog = "ImageTest2";
String input = prog + "{image i;image f; f := i;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
//waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void ImageTest3() throws Exception {
String prog = "ImageTest3";
String input = prog + "{image i;show i;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
//waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void ImageTest4() throws Exception {
String prog = "ImageTest4";
String input = prog + "{image i[400,600]; input i from @ 0; image f; f := i; show f;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG"}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void expressionFunctionAppWithPixelTest() throws Exception {
String prog = "ExpressionFunctionAppWithPixelTest";
String input = prog + "{show cart_x[5.0, 0.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;5;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void expressionFunctionAppWithPixelTest2() throws Exception {
String prog = "ExpressionFunctionAppWithPixelTest2";
String input = prog + "{show cart_y[5.0, 0.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;0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void expressionFunctionAppWithPixelTest3() throws Exception {
String prog = "ExpressionFunctionAppWithPixelTest3";
String input = prog + "{show polar_a[1, 1];} ";
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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void expressionFunctionAppWithPixelTest4() throws Exception {
String prog = "ExpressionFunctionAppWithPixelTest4";
String input = prog + "{show polar_r[3, 4];} ";
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;5.0;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void ifStatementTest() throws Exception {
String prog = "IfStatementTest";
String input = prog + "{int i; i := 0; if (i == 0){i := 1; show i;};} ";
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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void whileStatementTest() throws Exception {
String prog = "whileStatementTest";
String input = prog + "{int i; i := 0; while(i < 2){show 3; i := i + 1;}; show i;} ";
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;3;2;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void pixelConstructorTest() throws Exception {
String prog = "PixelConstructorTest";
String input = prog + "{show <<2+0, 2-0, 1+1, 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;33686018;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void pixelSelectorTest() throws Exception {
String prog = "PixelSelectorTest";
String input = prog + "{image i; input i from @ 0; show i[300, 400];} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG"}; //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;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void lhsPixelTest() throws Exception {
String prog = "lhsPixelTest";
String input = prog + "{image i; input i from @ 0; i[300, 400] := 40; show i;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG"}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void writeTest() throws Exception {
String prog = "writeTest";
String input = prog + "{image i; input i from @ 0; filename f; input f from @ 1; write i to f;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG", "C:\\Users\\wema6001\\Pictures\\Anus.PNG"}; //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 lhsPixelSampleTest() throws Exception {
String prog = "lhsPixelSampleTest";
String input = prog + "{image i; input i from @ 0; green(i[300, 400]) := 40; show i;} ";
byte[] bytecode = genCode(input);
String[] commandLineArgs = {"C:\\Users\\wema6001\\Pictures\\Cloud.PNG"}; //create command line argument array to initialize params, none in this case
runCode(prog, bytecode, commandLineArgs);
waitForKey();
show("Log:\n"+RuntimeLog.globalLog);
assertEquals("entering main;leaving main;",RuntimeLog.globalLog.toString());
}
@Test
public void conditionalExpressionTest() throws Exception {
String prog = "conditionalExpressionTest";
String input = prog + "{show 4 > 4 ? 6: 4;} ";
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;4;leaving main;",RuntimeLog.globalLog.toString());
}
}