-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDeclTester.java
More file actions
95 lines (77 loc) · 2.57 KB
/
CDeclTester.java
File metadata and controls
95 lines (77 loc) · 2.57 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
import CDecl.CDeclParser;
import org.junit.Assert;
import org.junit.Test;
import java.text.ParseException;
public class CDeclTester {
private static class MyTest {
String input;
String answer;
public MyTest(String input, String answer) {
this.input = input;
this.answer = answer;
}
}
@Test
public void simpleTests() {
MyTest[] tests = {
new MyTest("int a;", "int a;\n"),
new MyTest("bool b;", "bool b;\n"),
new MyTest("int a,b;", "int a,b;\n"),
new MyTest("int a,b,c;", "int a,b,c;\n"),
new MyTest("int a; int b;", "int a;\nint b;\n"),
};
launchTests(tests);
}
@Test
public void pointerTests() {
MyTest[] tests = {
new MyTest("int *a;", "int *a;\n"),
new MyTest("bool **b;", "bool **b;\n"),
new MyTest("int a,*b;", "int a,*b;\n"),
new MyTest("int **a,***b,c;", "int **a,***b,c;\n"),
new MyTest("int ****a; int b;", "int ****a;\nint b;\n"),
};
launchTests(tests);
}
@Test
public void whitespacesTests() {
MyTest[] tests = {
new MyTest("int a;", "int a;\n"),
new MyTest("int a;\n float b;", "int a;\nfloat b;\n"),
new MyTest("double * * * a;", "double ***a;\n"),
};
launchTests(tests);
}
@Test
public void errorTests() {
MyTest[] tests = {
new MyTest("inta;", ""),
new MyTest("int a", ""),
new MyTest("int a b", ""),
new MyTest("bol a;", ""),
new MyTest("*int a;", ""),
};
launchErrorTests(tests);
}
private void launchTests(MyTest[] tests){
for (MyTest test : tests) {
try {
String res = new CDeclParser().parse(test.input).val;
if (!res.equals(test.answer)){
Assert.fail("Test: "+test.input+"\nExpected: "+test.answer+" but given: "+ res);
}
} catch (ParseException e) {
Assert.fail("Unexpected exception:" + e.getLocalizedMessage());
}
}
}
private void launchErrorTests(MyTest[] tests){
for (MyTest test : tests) {
try {
String res = new CDeclParser().parse(test.input).val;
Assert.fail("Test: "+test.input+"\nExpecting exception");
} catch (ParseException e) {
}
}
}
}