-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.sh
executable file
·155 lines (136 loc) · 2.47 KB
/
test.sh
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
#!/bin/bash -e
function test_result {
input="$1"
expected="$2"
expected_stdout="$3"
testname="$4"
printf "$expected_stdout" > expected.stdout
./compilium --target-os `uname` <<< "$input" > out.S || { \
echo "$input" > failcase.c; \
echo "Compilation failed."; \
exit 1; }
gcc out.S
actual=0
./a.out > out.stdout || actual=$?
if [ $expected = $actual ]; then
diff -u expected.stdout out.stdout \
&& echo "PASS $testname returns $expected" \
|| { echo "FAIL $testname: stdout diff"; exit 1; }
else
echo "FAIL $testname: expected $expected but got $actual"; echo $input > failcase.c; exit 1;
fi
}
function test_expr_result {
test_result "int main(){return $1;}" "$2" "" "$1"
}
function test_stmt_result {
test_result "int main(){$1}" "$2" "" "$1"
}
function test_src_result {
test_result "$1" "$2" "$3" "$1"
}
# nested func call with args
test_src_result "`cat << EOS
int g() {
int a;
a = 3;
int b;
b = 5;
return a + b;
}
int f() {
int v;
v = 2;
int r;
r = g();
return v + r;
}
int main() {
return f();
}
EOS
`" 10 ''
# func args should be visible
test_src_result "`cat << EOS
int sum(int a, int b) {
return a + b;
}
int main() {
return sum(3, 5);
}
EOS
`" 8 ''
# same symbols in diffrent scope shadows previous one
test_src_result "`cat << EOS
int main() {
int result;
result = 1;
int duplicated_var;
duplicated_var = 2;
result = result * duplicated_var;
if(1) {
int duplicated_var;
duplicated_var = 3;
result = result * duplicated_var;
}
result = result * duplicated_var;
return result;
}
EOS
`" 12 ''
# for stmt
test_src_result "`cat << EOS
int main() {
int i;
int sum;
sum = 0;
for(i = 0; i <= 10; i = i + 1) {
sum = sum + i;
}
return sum;
}
EOS
`" 55 ''
# return with no expression
test_src_result "`cat << EOS
void func_returns_void() {
return;
}
int main() {
func_returns_void();
return 3;
}
EOS
`" 3 ''
test_src_result "`cat << EOS
int three() {
return 3;
}
int main() {
return three();
}
EOS
`" 3 ''
test_src_result "`cat << EOS
int puts(char *s);
int main() {
puts("Hello, world!");
return 0;
}
EOS
`" 0 'Hello, world!\n'
test_src_result "`cat << EOS
int putchar(int c);
int main() {
putchar('C');
return 0;
}
EOS
`" 0 'C'
test_stmt_result 'int a; int b; int c; a = 3; b = 5; c = 7; return a + b + c;' 15
# Non-printable
test_expr_result ' 0 ' 0
# Unary Prefix
test_expr_result '+ +1' 1
test_expr_result '- -17' 17
echo "All tests passed."