source: src/tests/except-0.c@ 6ac5223

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6ac5223 was fcc88a4, checked in by Andrew Beach <ajbeach@…>, 8 years ago

That got the last case in except-2 working.

  • Property mode set to 100644
File size: 4.4 KB
Line 
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)
11struct signal_exit {
12 const char * area;
13};
14
15void ?{}(signal_exit * this, const char * area) {
16 this->area = area;
17}
18
19void ^?{}(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)
27struct yin;
28struct 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};
35struct yin {
36 struct yin_vtable const * parent;
37};
38void yin_msg(yin) {
39 return "in";
40}
41yin_vtable _yin_vtable_instance = {
42 &_exception_t_vtable_instance, sizeof(yin), ?{}, ^?{}, yin_msg
43}
44
45
46void terminate(exception * except_value) {
47 signal_exit a = {"terminate function"};
48 throw except_value;
49 printf("terminate returned\n");
50}
51
52void 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
59void 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
68void 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.
80void 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
89void 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:
101void 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:
117void fallback() {
118 try {
119 resume(2);
120 } catch (2) {
121 printf("fallback caught termination 2\n");
122 }
123}
124
125// Terminate Throw New Exception:
126void terminate_swap() {
127 signal_exit a = {"terminate_swap"};
128 try {
129 terminate(2);
130 } catch (2) {
131 terminate(3);
132 }
133}
134
135void 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:
145void resume_swap() {
146 signal_exit a = {"resume_swap"};
147 try {
148 resume(2);
149 } catchResume (2) {
150 resume(3);
151 }
152}
153
154void resume_swapped() {
155 try {
156 resume_swap();
157 } catchResume (3) {
158 printf("resume_swapped caught exception 3\n");
159 }
160}
161
162// Terminate Rethrow:
163void 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:
178void 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:
192void fum() {
193 // terminate block, call resume
194 try {
195 resume(3);
196 } catch (3) {
197 printf("fum caught exception 3\n");
198 }
199}
200
201void foe() {
202 // resume block, call terminate
203 try {
204 terminate(3);
205 } catchResume (3) {
206 printf("foe caught exception 3\n");
207 }
208}
209
210void 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
220void 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
231int 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}
Note: See TracBrowser for help on using the repository browser.