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
|
package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Test;
import static org.junit.Assert.*;
public class Chapter01Test {
@Test
public void testSimpleProgram() {
Parser parser = new Parser("return 1;");
StopNode stop = parser.parse();
StartNode start = Parser.START;
ReturnNode ret = (ReturnNode)stop.in(0);
assertTrue(ret.ctrl() instanceof CProjNode);
Node expr = ret.expr();
if( expr instanceof ConstantNode con ) {
assertEquals(start,con.in(0));
assertEquals(TypeInteger.constant(1), con._type);
} else {
fail();
}
}
@Test
public void testZero() {
Parser parser = new Parser("return 0;");
parser.parse();
StartNode start = Parser.START;
for( Node use : start._outputs )
if( use instanceof ConstantNode con && con._type instanceof TypeInteger )
assertEquals(TypeInteger.constant(0),con._type);
}
@Test
public void testBad1() {
try {
new Parser("ret").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Undefined name 'ret'",e.getMessage());
}
}
@Test
public void testBad2() {
try {
new Parser("return 0123;").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage());
}
}
@Test
public void testNotBad3() {
// this test used to fail in chapter 1
assertEquals("return 12;", new Parser("return --12;").parse().print());
}
@Test
public void testBad4() {
try {
new Parser("return 100").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, expected ;: ",e.getMessage());
}
}
@Test
public void testNotBad5() {
// this test used to fail in chapter 1
assertEquals("return -100;", new Parser("return -100;").parse().print());
}
@Test
public void testBad6() {
try {
new Parser("return100").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Undefined name 'return100'",e.getMessage());
}
}
@Test
public void testBad7() {
try {
new Parser("return 1;}").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, unexpected }",e.getMessage());
}
}
}
|