1*93aeb8c3SIngo Weinhold #include <stdio.h>
2*93aeb8c3SIngo Weinhold
3*93aeb8c3SIngo Weinhold #include <string>
4*93aeb8c3SIngo Weinhold
5*93aeb8c3SIngo Weinhold #include "exceptions.h"
6*93aeb8c3SIngo Weinhold
7*93aeb8c3SIngo Weinhold using std::string;
8*93aeb8c3SIngo Weinhold
9*93aeb8c3SIngo Weinhold static const char *kCaughtNothing = "nothing";
10*93aeb8c3SIngo Weinhold static const char *kCaughtGeneric = "generic";
11*93aeb8c3SIngo Weinhold static const char *kCaughtBase = "ExceptionBase";
12*93aeb8c3SIngo Weinhold static const char *kCaughtA = "ExceptionA";
13*93aeb8c3SIngo Weinhold static const char *kCaughtB = "ExceptionB";
14*93aeb8c3SIngo Weinhold static const char *kCaughtVirtualBase = "VirtualExceptionBase";
15*93aeb8c3SIngo Weinhold static const char *kCaughtVirtualA = "VirtualExceptionA";
16*93aeb8c3SIngo Weinhold static const char *kCaughtVirtualB = "VirtualExceptionB";
17*93aeb8c3SIngo Weinhold static const char *kCaughtInt = "VirtualInt";
18*93aeb8c3SIngo Weinhold
19*93aeb8c3SIngo Weinhold
20*93aeb8c3SIngo Weinhold static string
catchBase(void (* function)())21*93aeb8c3SIngo Weinhold catchBase(void (*function)())
22*93aeb8c3SIngo Weinhold {
23*93aeb8c3SIngo Weinhold try {
24*93aeb8c3SIngo Weinhold (*function)();
25*93aeb8c3SIngo Weinhold } catch (ExceptionBase exception) {
26*93aeb8c3SIngo Weinhold return kCaughtBase;
27*93aeb8c3SIngo Weinhold } catch (...) {
28*93aeb8c3SIngo Weinhold return kCaughtGeneric;
29*93aeb8c3SIngo Weinhold }
30*93aeb8c3SIngo Weinhold return kCaughtNothing;
31*93aeb8c3SIngo Weinhold }
32*93aeb8c3SIngo Weinhold
33*93aeb8c3SIngo Weinhold
34*93aeb8c3SIngo Weinhold static string
catchA(void (* function)())35*93aeb8c3SIngo Weinhold catchA(void (*function)())
36*93aeb8c3SIngo Weinhold {
37*93aeb8c3SIngo Weinhold try {
38*93aeb8c3SIngo Weinhold (*function)();
39*93aeb8c3SIngo Weinhold } catch (ExceptionA exception) {
40*93aeb8c3SIngo Weinhold return kCaughtA;
41*93aeb8c3SIngo Weinhold } catch (...) {
42*93aeb8c3SIngo Weinhold return kCaughtGeneric;
43*93aeb8c3SIngo Weinhold }
44*93aeb8c3SIngo Weinhold return kCaughtNothing;
45*93aeb8c3SIngo Weinhold }
46*93aeb8c3SIngo Weinhold
47*93aeb8c3SIngo Weinhold
48*93aeb8c3SIngo Weinhold static string
catchB(void (* function)())49*93aeb8c3SIngo Weinhold catchB(void (*function)())
50*93aeb8c3SIngo Weinhold {
51*93aeb8c3SIngo Weinhold try {
52*93aeb8c3SIngo Weinhold (*function)();
53*93aeb8c3SIngo Weinhold } catch (ExceptionB exception) {
54*93aeb8c3SIngo Weinhold return kCaughtB;
55*93aeb8c3SIngo Weinhold } catch (...) {
56*93aeb8c3SIngo Weinhold return kCaughtGeneric;
57*93aeb8c3SIngo Weinhold }
58*93aeb8c3SIngo Weinhold return kCaughtNothing;
59*93aeb8c3SIngo Weinhold }
60*93aeb8c3SIngo Weinhold
61*93aeb8c3SIngo Weinhold
62*93aeb8c3SIngo Weinhold static string
catchVirtualBase(void (* function)())63*93aeb8c3SIngo Weinhold catchVirtualBase(void (*function)())
64*93aeb8c3SIngo Weinhold {
65*93aeb8c3SIngo Weinhold try {
66*93aeb8c3SIngo Weinhold (*function)();
67*93aeb8c3SIngo Weinhold } catch (VirtualExceptionBase exception) {
68*93aeb8c3SIngo Weinhold return kCaughtVirtualBase;
69*93aeb8c3SIngo Weinhold } catch (...) {
70*93aeb8c3SIngo Weinhold return kCaughtGeneric;
71*93aeb8c3SIngo Weinhold }
72*93aeb8c3SIngo Weinhold return kCaughtNothing;
73*93aeb8c3SIngo Weinhold }
74*93aeb8c3SIngo Weinhold
75*93aeb8c3SIngo Weinhold
76*93aeb8c3SIngo Weinhold static string
catchVirtualA(void (* function)())77*93aeb8c3SIngo Weinhold catchVirtualA(void (*function)())
78*93aeb8c3SIngo Weinhold {
79*93aeb8c3SIngo Weinhold try {
80*93aeb8c3SIngo Weinhold (*function)();
81*93aeb8c3SIngo Weinhold } catch (VirtualExceptionA exception) {
82*93aeb8c3SIngo Weinhold return kCaughtVirtualA;
83*93aeb8c3SIngo Weinhold } catch (...) {
84*93aeb8c3SIngo Weinhold return kCaughtGeneric;
85*93aeb8c3SIngo Weinhold }
86*93aeb8c3SIngo Weinhold return kCaughtNothing;
87*93aeb8c3SIngo Weinhold }
88*93aeb8c3SIngo Weinhold
89*93aeb8c3SIngo Weinhold
90*93aeb8c3SIngo Weinhold static string
catchVirtualB(void (* function)())91*93aeb8c3SIngo Weinhold catchVirtualB(void (*function)())
92*93aeb8c3SIngo Weinhold {
93*93aeb8c3SIngo Weinhold try {
94*93aeb8c3SIngo Weinhold (*function)();
95*93aeb8c3SIngo Weinhold } catch (VirtualExceptionB exception) {
96*93aeb8c3SIngo Weinhold return kCaughtVirtualB;
97*93aeb8c3SIngo Weinhold } catch (...) {
98*93aeb8c3SIngo Weinhold return kCaughtGeneric;
99*93aeb8c3SIngo Weinhold }
100*93aeb8c3SIngo Weinhold return kCaughtNothing;
101*93aeb8c3SIngo Weinhold }
102*93aeb8c3SIngo Weinhold
103*93aeb8c3SIngo Weinhold
104*93aeb8c3SIngo Weinhold static string
catchInt(void (* function)())105*93aeb8c3SIngo Weinhold catchInt(void (*function)())
106*93aeb8c3SIngo Weinhold {
107*93aeb8c3SIngo Weinhold try {
108*93aeb8c3SIngo Weinhold (*function)();
109*93aeb8c3SIngo Weinhold } catch (int exception) {
110*93aeb8c3SIngo Weinhold return kCaughtInt;
111*93aeb8c3SIngo Weinhold } catch (...) {
112*93aeb8c3SIngo Weinhold return kCaughtGeneric;
113*93aeb8c3SIngo Weinhold }
114*93aeb8c3SIngo Weinhold return kCaughtNothing;
115*93aeb8c3SIngo Weinhold }
116*93aeb8c3SIngo Weinhold
117*93aeb8c3SIngo Weinhold static string
catchAny(void (* function)())118*93aeb8c3SIngo Weinhold catchAny(void (*function)())
119*93aeb8c3SIngo Weinhold {
120*93aeb8c3SIngo Weinhold try {
121*93aeb8c3SIngo Weinhold (*function)();
122*93aeb8c3SIngo Weinhold } catch (int exception) {
123*93aeb8c3SIngo Weinhold return kCaughtInt;
124*93aeb8c3SIngo Weinhold } catch (VirtualExceptionA exception) {
125*93aeb8c3SIngo Weinhold return kCaughtVirtualA;
126*93aeb8c3SIngo Weinhold } catch (VirtualExceptionB exception) {
127*93aeb8c3SIngo Weinhold return kCaughtVirtualB;
128*93aeb8c3SIngo Weinhold } catch (VirtualExceptionBase exception) {
129*93aeb8c3SIngo Weinhold return kCaughtVirtualBase;
130*93aeb8c3SIngo Weinhold } catch (ExceptionA exception) {
131*93aeb8c3SIngo Weinhold return kCaughtA;
132*93aeb8c3SIngo Weinhold } catch (ExceptionB exception) {
133*93aeb8c3SIngo Weinhold return kCaughtB;
134*93aeb8c3SIngo Weinhold } catch (ExceptionBase exception) {
135*93aeb8c3SIngo Weinhold return kCaughtBase;
136*93aeb8c3SIngo Weinhold } catch (...) {
137*93aeb8c3SIngo Weinhold return kCaughtGeneric;
138*93aeb8c3SIngo Weinhold }
139*93aeb8c3SIngo Weinhold return kCaughtNothing;
140*93aeb8c3SIngo Weinhold }
141*93aeb8c3SIngo Weinhold
142*93aeb8c3SIngo Weinhold static void
test(string (* catcher)(void (*)()),void (* thrower)(),const char * expected)143*93aeb8c3SIngo Weinhold test(string (*catcher)(void (*)()), void (*thrower)(), const char *expected)
144*93aeb8c3SIngo Weinhold {
145*93aeb8c3SIngo Weinhold string caught((*catcher)(thrower));
146*93aeb8c3SIngo Weinhold if (caught != expected) {
147*93aeb8c3SIngo Weinhold printf("ERROR: expected exception: %s, but caught: %s\n", expected,
148*93aeb8c3SIngo Weinhold caught.c_str());
149*93aeb8c3SIngo Weinhold }
150*93aeb8c3SIngo Weinhold }
151*93aeb8c3SIngo Weinhold
152*93aeb8c3SIngo Weinhold int
main()153*93aeb8c3SIngo Weinhold main()
154*93aeb8c3SIngo Weinhold {
155*93aeb8c3SIngo Weinhold test(catchBase, throwBase, kCaughtBase);
156*93aeb8c3SIngo Weinhold test(catchBase, throwA, kCaughtBase);
157*93aeb8c3SIngo Weinhold test(catchBase, throwB, kCaughtBase);
158*93aeb8c3SIngo Weinhold test(catchBase, throwVirtualBase, kCaughtBase);
159*93aeb8c3SIngo Weinhold test(catchBase, throwVirtualA, kCaughtBase);
160*93aeb8c3SIngo Weinhold test(catchBase, throwVirtualB, kCaughtBase);
161*93aeb8c3SIngo Weinhold test(catchBase, throwInt, kCaughtGeneric);
162*93aeb8c3SIngo Weinhold
163*93aeb8c3SIngo Weinhold test(catchA, throwBase, kCaughtGeneric);
164*93aeb8c3SIngo Weinhold test(catchA, throwA, kCaughtA);
165*93aeb8c3SIngo Weinhold test(catchA, throwB, kCaughtGeneric);
166*93aeb8c3SIngo Weinhold test(catchA, throwVirtualBase, kCaughtGeneric);
167*93aeb8c3SIngo Weinhold test(catchA, throwVirtualA, kCaughtGeneric);
168*93aeb8c3SIngo Weinhold test(catchA, throwVirtualB, kCaughtGeneric);
169*93aeb8c3SIngo Weinhold test(catchA, throwInt, kCaughtGeneric);
170*93aeb8c3SIngo Weinhold
171*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwBase, kCaughtGeneric);
172*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwA, kCaughtGeneric);
173*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwB, kCaughtGeneric);
174*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwVirtualBase, kCaughtVirtualBase);
175*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwVirtualA, kCaughtVirtualBase);
176*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwVirtualB, kCaughtVirtualBase);
177*93aeb8c3SIngo Weinhold test(catchVirtualBase, throwInt, kCaughtGeneric);
178*93aeb8c3SIngo Weinhold
179*93aeb8c3SIngo Weinhold test(catchVirtualA, throwBase, kCaughtGeneric);
180*93aeb8c3SIngo Weinhold test(catchVirtualA, throwA, kCaughtGeneric);
181*93aeb8c3SIngo Weinhold test(catchVirtualA, throwB, kCaughtGeneric);
182*93aeb8c3SIngo Weinhold test(catchVirtualA, throwVirtualBase, kCaughtGeneric);
183*93aeb8c3SIngo Weinhold test(catchVirtualA, throwVirtualA, kCaughtVirtualA);
184*93aeb8c3SIngo Weinhold test(catchVirtualA, throwVirtualB, kCaughtGeneric);
185*93aeb8c3SIngo Weinhold test(catchVirtualA, throwInt, kCaughtGeneric);
186*93aeb8c3SIngo Weinhold
187*93aeb8c3SIngo Weinhold test(catchInt, throwBase, kCaughtGeneric);
188*93aeb8c3SIngo Weinhold test(catchInt, throwA, kCaughtGeneric);
189*93aeb8c3SIngo Weinhold test(catchInt, throwB, kCaughtGeneric);
190*93aeb8c3SIngo Weinhold test(catchInt, throwVirtualBase, kCaughtGeneric);
191*93aeb8c3SIngo Weinhold test(catchInt, throwVirtualA, kCaughtGeneric);
192*93aeb8c3SIngo Weinhold test(catchInt, throwVirtualB, kCaughtGeneric);
193*93aeb8c3SIngo Weinhold test(catchInt, throwInt, kCaughtInt);
194*93aeb8c3SIngo Weinhold
195*93aeb8c3SIngo Weinhold test(catchAny, throwBase, kCaughtBase);
196*93aeb8c3SIngo Weinhold test(catchAny, throwA, kCaughtA);
197*93aeb8c3SIngo Weinhold test(catchAny, throwB, kCaughtB);
198*93aeb8c3SIngo Weinhold test(catchAny, throwVirtualBase, kCaughtVirtualBase);
199*93aeb8c3SIngo Weinhold test(catchAny, throwVirtualA, kCaughtVirtualA);
200*93aeb8c3SIngo Weinhold test(catchAny, throwVirtualB, kCaughtVirtualB);
201*93aeb8c3SIngo Weinhold test(catchAny, throwInt, kCaughtInt);
202*93aeb8c3SIngo Weinhold
203*93aeb8c3SIngo Weinhold return 0;
204*93aeb8c3SIngo Weinhold }
205