-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.c
228 lines (193 loc) · 3.93 KB
/
test.c
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
/* This tests individual exception handling scenarios. Not all possible
scenarios can be tested, this will have to do. */
#include "rocketcrash.h"
#include <assert.h> /* assert */
#include <stdio.h> /* printf */
#include <stdlib.h> /* EXIT_SUCCESS */
static int thrown;
static int caught;
static int finally_done;
static int checked;
static int end_catch;
static int end_finally;
static int bar_caught;
static int after_finally;
static void reset(void)
{
thrown = 0;
caught = 0;
finally_done = 0;
checked = 0;
end_catch = 0;
end_finally = 0;
bar_caught = 0;
after_finally = 0;
}
char const * const foo = "foo";
char const * const bar = "bar";
/* can be used as an expression that throws bar and returns foo */
static char const * whoops(void)
{
throw(bar);
return foo;
}
/* throw in try clause */
static void test01(void)
{
reset();
try {
throw(foo);
}
catch(foo) {
caught = 1;
}
finally {
finally_done = 1;
} endtry
assert(caught && finally_done);
}
/* throw in catch expression */
static void test02(void)
{
reset();
try {
try {
throw(foo);
}
catch(whoops()) {
caught = 1;
}
finally {
finally_done = 1;
} endtry
after_finally = 1;
}
catch(bar) {
checked = 1;
assert(!caught && finally_done && !after_finally);
} endtry
assert(checked);
}
/* throw in catch clause */
static void test03(void)
{
reset();
try {
try {
throw(foo);
}
catch(foo) {
caught = 1;
throw(bar);
end_catch = 1;
}
finally {
finally_done = 1;
} endtry
after_finally = 1;
}
catch(bar) {
checked = 1;
assert(caught && finally_done && !end_catch && !after_finally);
} endtry
assert(checked);
}
/* throw in finally clause */
static void test04(void)
{
reset();
try {
try {
} finally {
throw(foo);
end_finally = 1;
} endtry
}
catch(foo) {
checked = 1;
assert(!end_finally);
} endtry
assert(checked);
}
/* throw in try clause, two catch clauses */
static void test05(void)
{
reset();
try {
throw(foo);
}
catch(bar) {
bar_caught = 1;
}
catch(foo) {
caught = 1;
}
finally {
finally_done = 1;
} endtry
assert(caught && finally_done && !bar_caught);
}
/* uncaught nested throw */
static void test06(void)
{
reset();
try {
try {
throw(foo);
}
catch (bar) {
bar_caught = 1;
}
finally {
finally_done = 1;
} endtry
after_finally = 1;
}
catch(foo) {
caught = 1;
}
finally {
end_finally = 1;
} endtry
assert(caught && !bar_caught && finally_done && end_finally
&& !after_finally);
}
/* gcatch, complex expression (return val of assignment) */
static void test07(void)
{
int x = 1;
reset();
try {
throw(foo);
}
gcatch(foo, x = 0 ? 0 : 1) {
caught = 1;
} endtry
assert(caught);
}
/* ccatch, complex expression (return val of assignment) */
static void test08(void)
{
int x = 1;
reset();
try {
throw(foo);
}
ccatch(x = 0 ? 0 : 1) {
caught = 1;
} endtry
assert(caught);
}
int main(void)
{
test01(); printf("1"); fflush(stdout);
test02(); printf("2"); fflush(stdout);
test03(); printf("3"); fflush(stdout);
test04(); printf("4"); fflush(stdout);
test05(); printf("5"); fflush(stdout);
test06(); printf("6"); fflush(stdout);
test07(); printf("7"); fflush(stdout);
test08(); printf("8"); fflush(stdout);
printf("\nRocket Crash: All tests passed.\n");
return EXIT_SUCCESS;
}