1 | // Draft of tests for exception handling. |
---|
2 | // Outdated: The integer constant exceptions need to be replaced with virtual |
---|
3 | // exceptions for the new system. |
---|
4 | |
---|
5 | // ERROR: exceptions do not interact with ^?{} properly. |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <stdbool.h> |
---|
9 | |
---|
10 | // Local type to mark exits from scopes. (see ERROR) |
---|
11 | struct signal_exit { |
---|
12 | const char * area; |
---|
13 | }; |
---|
14 | |
---|
15 | void ?{}(signal_exit * this, const char * area) { |
---|
16 | this->area = area; |
---|
17 | } |
---|
18 | |
---|
19 | void ^?{}(signal_exit * this) { |
---|
20 | printf("Exiting: %s\n", this->area); |
---|
21 | // sout | "Exiting:" | this->area | endl; |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | // Local Exception Types and manual vtable types. |
---|
26 | //#define TRIVIAL_EXCEPTION(name) //TRIVAL_EXCEPTION(yin) |
---|
27 | struct yin; |
---|
28 | struct yin_vtable { |
---|
29 | struct exception_t_vtable const * parent; |
---|
30 | size_t size; |
---|
31 | void (*copy)(yin *this, yin * other); |
---|
32 | void (*free)(yin *this); |
---|
33 | const char (*msg)(yin *this); |
---|
34 | }; |
---|
35 | struct yin { |
---|
36 | struct yin_vtable const * parent; |
---|
37 | }; |
---|
38 | void yin_msg(yin) { |
---|
39 | return "in"; |
---|
40 | } |
---|
41 | yin_vtable _yin_vtable_instance = { |
---|
42 | &_exception_t_vtable_instance, sizeof(yin), ?{}, ^?{}, yin_msg |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | void terminate(exception * except_value) { |
---|
47 | signal_exit a = {"terminate function"}; |
---|
48 | throw except_value; |
---|
49 | printf("terminate returned\n"); |
---|
50 | } |
---|
51 | |
---|
52 | void resume(exception * except_value) { |
---|
53 | signal_exit a = {"resume function"}; |
---|
54 | throwResume except_value; |
---|
55 | printf("resume returned\n"); |
---|
56 | } |
---|
57 | |
---|
58 | // Termination Test: Two handlers: no catch, catch |
---|
59 | void bar() { |
---|
60 | signal_exit a = {"bar function"}; |
---|
61 | try { |
---|
62 | terminate(4); |
---|
63 | } catch (3) { |
---|
64 | printf("bar caught exception 3.\n"); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | void foo() { |
---|
69 | signal_exit a = {"foo function"}; |
---|
70 | try { |
---|
71 | bar(); |
---|
72 | } catch (4) { |
---|
73 | printf("foo caught exception 4.\n"); |
---|
74 | } catch (2) { |
---|
75 | printf("foo caught exception 2.\n"); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | // Resumption Two Handler Test: no catch, catch. |
---|
80 | void beta() { |
---|
81 | signal_exit a = {"beta function"}; |
---|
82 | try { |
---|
83 | resume(4); |
---|
84 | } catchResume (3) { |
---|
85 | printf("beta caught exception 3\n"); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void alpha() { |
---|
90 | signal_exit a = {"alpha function"}; |
---|
91 | try { |
---|
92 | beta(); |
---|
93 | } catchResume (2) { |
---|
94 | printf("alpha caught exception 2\n"); |
---|
95 | } catchResume (4) { |
---|
96 | printf("alpha caught exception 4\n"); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | // Finally Test: |
---|
101 | void farewell(bool jump) { |
---|
102 | try { |
---|
103 | if (jump) { |
---|
104 | printf("jump out of farewell\n"); |
---|
105 | goto endoffunction; |
---|
106 | } else { |
---|
107 | printf("walk out of farewell\n"); |
---|
108 | } |
---|
109 | } finally { |
---|
110 | printf("See you next time\n"); |
---|
111 | } |
---|
112 | endoffunction: |
---|
113 | printf("leaving farewell\n"); |
---|
114 | } |
---|
115 | |
---|
116 | // Resume-to-Terminate Test: |
---|
117 | void fallback() { |
---|
118 | try { |
---|
119 | resume(2); |
---|
120 | } catch (2) { |
---|
121 | printf("fallback caught termination 2\n"); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | // Terminate Throw New Exception: |
---|
126 | void terminate_swap() { |
---|
127 | signal_exit a = {"terminate_swap"}; |
---|
128 | try { |
---|
129 | terminate(2); |
---|
130 | } catch (2) { |
---|
131 | terminate(3); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | void terminate_swapped() { |
---|
136 | signal_exit a = {"terminate_swapped"}; |
---|
137 | try { |
---|
138 | terminate_swap(); |
---|
139 | } catch (3) { |
---|
140 | printf("terminate_swapped caught exception 3\n"); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | // Resume Throw New Exception: |
---|
145 | void resume_swap() { |
---|
146 | signal_exit a = {"resume_swap"}; |
---|
147 | try { |
---|
148 | resume(2); |
---|
149 | } catchResume (2) { |
---|
150 | resume(3); |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | void resume_swapped() { |
---|
155 | try { |
---|
156 | resume_swap(); |
---|
157 | } catchResume (3) { |
---|
158 | printf("resume_swapped caught exception 3\n"); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | // Terminate Rethrow: |
---|
163 | void reterminate() { |
---|
164 | try { |
---|
165 | try { |
---|
166 | terminate(2); |
---|
167 | } catch (2) { |
---|
168 | printf("reterminate 2 caught and " |
---|
169 | "will rethrow exception 2\n"); |
---|
170 | throw; |
---|
171 | } |
---|
172 | } catch (2) { |
---|
173 | printf("reterminate 1 caught exception 2\n"); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | // Resume Rethrow: |
---|
178 | void reresume() { |
---|
179 | try { |
---|
180 | try { |
---|
181 | resume(2); |
---|
182 | } catchResume (2) { |
---|
183 | printf("reresume 2 caught and rethrows exception 2\n"); |
---|
184 | throwResume; |
---|
185 | } |
---|
186 | } catchResume (2) { |
---|
187 | printf("reresume 1 caught exception 2\n"); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // Terminate-Resume interaction: |
---|
192 | void fum() { |
---|
193 | // terminate block, call resume |
---|
194 | try { |
---|
195 | resume(3); |
---|
196 | } catch (3) { |
---|
197 | printf("fum caught exception 3\n"); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | void foe() { |
---|
202 | // resume block, call terminate |
---|
203 | try { |
---|
204 | terminate(3); |
---|
205 | } catchResume (3) { |
---|
206 | printf("foe caught exception 3\n"); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | void fy() { |
---|
211 | // terminate block calls fum, call foe |
---|
212 | try { |
---|
213 | foe(); |
---|
214 | } catch (3) { |
---|
215 | printf("fy caught exception 3\n"); |
---|
216 | fum(); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | void fee() { |
---|
221 | // resume block, call fy |
---|
222 | try { |
---|
223 | fy(); |
---|
224 | } catchResume (3) { |
---|
225 | printf("fee caught exception 3\n"); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | // main: choose which tests to run |
---|
231 | int main(int argc, char * argv[]) { |
---|
232 | signal_exit a = {"main function"}; |
---|
233 | |
---|
234 | foo(); printf("\n"); |
---|
235 | alpha(); printf("\n"); |
---|
236 | farewell(false); printf("\n"); |
---|
237 | farewell(true); printf("\n"); |
---|
238 | fallback(); printf("\n"); |
---|
239 | terminate_swapped(); printf("\n"); |
---|
240 | resume_swapped(); printf("\n"); |
---|
241 | reterminate(); printf("\n"); |
---|
242 | reresume(); printf("\n"); |
---|
243 | fee(); printf("\n"); |
---|
244 | // Uncaught termination test. |
---|
245 | terminate(7); |
---|
246 | } |
---|