Skip to content

Commit 109e39f

Browse files
committed
test: add control flow test
1 parent 349e1fb commit 109e39f

File tree

4 files changed

+298
-0
lines changed

4 files changed

+298
-0
lines changed

tests/control_flow.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use assert_cmd::Command;
2+
use predicates::prelude::predicate;
3+
4+
#[test]
5+
fn control_flow() -> Result<(), Box<dyn std::error::Error>> {
6+
let mut cmd = Command::cargo_bin("cmjava")?;
7+
8+
cmd.arg("tests/data/control_flow/Main.class");
9+
cmd.assert()
10+
.success()
11+
.stdout(predicate::str::contains("i % 10 == 0\n".repeat(10)))
12+
.stdout(predicate::str::contains("a / 2 == 5\n"))
13+
.stdout(predicate::str::contains("l / 2 != 5\n"))
14+
.stdout(predicate::str::contains("d:\n15\n"))
15+
.stdout(predicate::str::contains("f > 10"));
16+
17+
Ok(())
18+
}

tests/data/control_flow/Main.class

1.1 KB
Binary file not shown.

tests/data/control_flow/Main.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.cmjava2023;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
for (int i = 0; i < 100; i++) {
6+
if (i % 10 == 0) {
7+
System.out.println("i % 10 == 0");
8+
}
9+
}
10+
11+
int a = 10;
12+
if (a / 2 == 5) {
13+
System.out.println("a / 2 == 5");
14+
} else {
15+
System.out.println("a / 2 != 5");
16+
}
17+
18+
long l = 100;
19+
if (l / 2 == 5) {
20+
System.out.println("l / 2 == 5");
21+
} else {
22+
System.out.println("l / 2 != 5");
23+
}
24+
25+
double d = 10.0;
26+
while (d < 15.0) {
27+
d += 1.0;
28+
}
29+
System.out.println("d:");
30+
System.out.println(d);
31+
32+
float f = 12.0f;
33+
if (f < 10) {
34+
System.out.println("f < 10");
35+
} else if (f > 10) {
36+
System.out.println("f > 10");
37+
} else {
38+
System.out.println("f == 10");
39+
}
40+
}
41+
}

tests/data/control_flow/Main.javap

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
Classfile /home/florian/Documents/fh/WS23_24/Compilerbau/vm/main/tests/data/control_flow/Main.class
2+
Last modified 19.12.2023; size 1127 bytes
3+
MD5 checksum 197352d71ec064ad9cd95b0b7235fece
4+
Compiled from "Main.java"
5+
public class org.cmjava2023.Main
6+
minor version: 0
7+
major version: 52
8+
flags: ACC_PUBLIC, ACC_SUPER
9+
Constant pool:
10+
#1 = Methodref #27.#51 // java/lang/Object."<init>":()V
11+
#2 = Fieldref #52.#53 // java/lang/System.out:Ljava/io/PrintStream;
12+
#3 = String #54 // i % 10 == 0
13+
#4 = Methodref #55.#56 // java/io/PrintStream.println:(Ljava/lang/String;)V
14+
#5 = String #57 // a / 2 == 5
15+
#6 = String #58 // a / 2 != 5
16+
#7 = Long 100l
17+
#9 = Long 2l
18+
#11 = Long 5l
19+
#13 = String #59 // l / 2 == 5
20+
#14 = String #60 // l / 2 != 5
21+
#15 = Double 10.0d
22+
#17 = Double 15.0d
23+
#19 = String #61 // d:
24+
#20 = Methodref #55.#62 // java/io/PrintStream.println:(D)V
25+
#21 = Float 12.0f
26+
#22 = Float 10.0f
27+
#23 = String #63 // f < 10
28+
#24 = String #64 // f > 10
29+
#25 = String #65 // f == 10
30+
#26 = Class #66 // org/cmjava2023/Main
31+
#27 = Class #67 // java/lang/Object
32+
#28 = Utf8 <init>
33+
#29 = Utf8 ()V
34+
#30 = Utf8 Code
35+
#31 = Utf8 LineNumberTable
36+
#32 = Utf8 LocalVariableTable
37+
#33 = Utf8 this
38+
#34 = Utf8 Lorg/cmjava2023/Main;
39+
#35 = Utf8 main
40+
#36 = Utf8 ([Ljava/lang/String;)V
41+
#37 = Utf8 i
42+
#38 = Utf8 I
43+
#39 = Utf8 args
44+
#40 = Utf8 [Ljava/lang/String;
45+
#41 = Utf8 a
46+
#42 = Utf8 l
47+
#43 = Utf8 J
48+
#44 = Utf8 d
49+
#45 = Utf8 D
50+
#46 = Utf8 f
51+
#47 = Utf8 F
52+
#48 = Utf8 StackMapTable
53+
#49 = Utf8 SourceFile
54+
#50 = Utf8 Main.java
55+
#51 = NameAndType #28:#29 // "<init>":()V
56+
#52 = Class #68 // java/lang/System
57+
#53 = NameAndType #69:#70 // out:Ljava/io/PrintStream;
58+
#54 = Utf8 i % 10 == 0
59+
#55 = Class #71 // java/io/PrintStream
60+
#56 = NameAndType #72:#73 // println:(Ljava/lang/String;)V
61+
#57 = Utf8 a / 2 == 5
62+
#58 = Utf8 a / 2 != 5
63+
#59 = Utf8 l / 2 == 5
64+
#60 = Utf8 l / 2 != 5
65+
#61 = Utf8 d:
66+
#62 = NameAndType #72:#74 // println:(D)V
67+
#63 = Utf8 f < 10
68+
#64 = Utf8 f > 10
69+
#65 = Utf8 f == 10
70+
#66 = Utf8 org/cmjava2023/Main
71+
#67 = Utf8 java/lang/Object
72+
#68 = Utf8 java/lang/System
73+
#69 = Utf8 out
74+
#70 = Utf8 Ljava/io/PrintStream;
75+
#71 = Utf8 java/io/PrintStream
76+
#72 = Utf8 println
77+
#73 = Utf8 (Ljava/lang/String;)V
78+
#74 = Utf8 (D)V
79+
{
80+
public org.cmjava2023.Main();
81+
descriptor: ()V
82+
flags: ACC_PUBLIC
83+
Code:
84+
stack=1, locals=1, args_size=1
85+
0: aload_0
86+
1: invokespecial #1 // Method java/lang/Object."<init>":()V
87+
4: return
88+
LineNumberTable:
89+
line 3: 0
90+
LocalVariableTable:
91+
Start Length Slot Name Signature
92+
0 5 0 this Lorg/cmjava2023/Main;
93+
94+
public static void main(java.lang.String[]);
95+
descriptor: ([Ljava/lang/String;)V
96+
flags: ACC_PUBLIC, ACC_STATIC
97+
Code:
98+
stack=4, locals=7, args_size=1
99+
0: iconst_0
100+
1: istore_1
101+
2: iload_1
102+
3: bipush 100
103+
5: if_icmpge 29
104+
8: iload_1
105+
9: bipush 10
106+
11: irem
107+
12: ifne 23
108+
15: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
109+
18: ldc #3 // String i % 10 == 0
110+
20: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
111+
23: iinc 1, 1
112+
26: goto 2
113+
29: bipush 10
114+
31: istore_1
115+
32: iload_1
116+
33: iconst_2
117+
34: idiv
118+
35: iconst_5
119+
36: if_icmpne 50
120+
39: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
121+
42: ldc #5 // String a / 2 == 5
122+
44: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
123+
47: goto 58
124+
50: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
125+
53: ldc #6 // String a / 2 != 5
126+
55: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
127+
58: ldc2_w #7 // long 100l
128+
61: lstore_2
129+
62: lload_2
130+
63: ldc2_w #9 // long 2l
131+
66: ldiv
132+
67: ldc2_w #11 // long 5l
133+
70: lcmp
134+
71: ifne 85
135+
74: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
136+
77: ldc #13 // String l / 2 == 5
137+
79: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
138+
82: goto 93
139+
85: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
140+
88: ldc #14 // String l / 2 != 5
141+
90: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
142+
93: ldc2_w #15 // double 10.0d
143+
96: dstore 4
144+
98: dload 4
145+
100: ldc2_w #17 // double 15.0d
146+
103: dcmpg
147+
104: ifge 116
148+
107: dload 4
149+
109: dconst_1
150+
110: dadd
151+
111: dstore 4
152+
113: goto 98
153+
116: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
154+
119: ldc #19 // String d:
155+
121: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
156+
124: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
157+
127: dload 4
158+
129: invokevirtual #20 // Method java/io/PrintStream.println:(D)V
159+
132: ldc #21 // float 12.0f
160+
134: fstore 6
161+
136: fload 6
162+
138: ldc #22 // float 10.0f
163+
140: fcmpg
164+
141: ifge 155
165+
144: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
166+
147: ldc #23 // String f < 10
167+
149: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
168+
152: goto 182
169+
155: fload 6
170+
157: ldc #22 // float 10.0f
171+
159: fcmpl
172+
160: ifle 174
173+
163: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
174+
166: ldc #24 // String f > 10
175+
168: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
176+
171: goto 182
177+
174: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
178+
177: ldc #25 // String f == 10
179+
179: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
180+
182: return
181+
LineNumberTable:
182+
line 5: 0
183+
line 6: 8
184+
line 7: 15
185+
line 5: 23
186+
line 11: 29
187+
line 12: 32
188+
line 13: 39
189+
line 15: 50
190+
line 18: 58
191+
line 19: 62
192+
line 20: 74
193+
line 22: 85
194+
line 25: 93
195+
line 26: 98
196+
line 27: 107
197+
line 29: 116
198+
line 30: 124
199+
line 32: 132
200+
line 33: 136
201+
line 34: 144
202+
line 35: 155
203+
line 36: 163
204+
line 38: 174
205+
line 40: 182
206+
LocalVariableTable:
207+
Start Length Slot Name Signature
208+
2 27 1 i I
209+
0 183 0 args [Ljava/lang/String;
210+
32 151 1 a I
211+
62 121 2 l J
212+
98 85 4 d D
213+
136 47 6 f F
214+
StackMapTable: number_of_entries = 12
215+
frame_type = 252 /* append */
216+
offset_delta = 2
217+
locals = [ int ]
218+
frame_type = 20 /* same */
219+
frame_type = 250 /* chop */
220+
offset_delta = 5
221+
frame_type = 252 /* append */
222+
offset_delta = 20
223+
locals = [ int ]
224+
frame_type = 7 /* same */
225+
frame_type = 252 /* append */
226+
offset_delta = 26
227+
locals = [ long ]
228+
frame_type = 7 /* same */
229+
frame_type = 252 /* append */
230+
offset_delta = 4
231+
locals = [ double ]
232+
frame_type = 17 /* same */
233+
frame_type = 252 /* append */
234+
offset_delta = 38
235+
locals = [ float ]
236+
frame_type = 18 /* same */
237+
frame_type = 7 /* same */
238+
}
239+
SourceFile: "Main.java"

0 commit comments

Comments
 (0)