From Colin O'Flynn:
[obnox/wireshark/wip.git] / epan / exntest.c
1 /* Standalone program to test functionality of exceptions.
2  *
3  * $Id$
4  *
5  * Copyright (c) 2004 MX Telecom Ltd. <richardv@mxtelecom.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  */
22
23 #include <stdio.h>
24 #include <glib.h>
25 #include <config.h>
26 #include "exceptions.h"
27
28 gboolean failed = FALSE;
29
30 void
31 run_tests(void)
32 {
33     volatile unsigned int ex_thrown, finally_called;
34
35     /* check that the right catch, and the finally, are called, on exception */
36     ex_thrown = finally_called = 0;
37     TRY {
38         THROW(BoundsError);
39     }
40     CATCH(BoundsError) {
41         ex_thrown++;
42     }
43     CATCH(ReportedBoundsError) {
44         printf("01: Caught wrong exception: ReportedBoundsError\n");
45         failed = TRUE;
46     }
47     CATCH_ALL {
48         printf("01: Caught wrong exception: %lu\n", exc->except_id.except_code);
49         failed = TRUE;
50     }
51     FINALLY {
52         finally_called ++;
53     }
54     ENDTRY;
55
56     if (ex_thrown != 1) {
57         printf("01: %u BoundsErrors (not 1) on caught exception\n", ex_thrown);
58         failed = TRUE;
59     }
60
61     if (finally_called != 1) {
62         printf("01: FINALLY called %u times (not 1) on caught exception\n", finally_called);
63         failed = TRUE;
64     }
65
66
67     /* check that no catch at all is called when there is no exn */
68     ex_thrown = finally_called = 0;
69     TRY {
70     }
71     CATCH(BoundsError) {
72         printf("02: Caught wrong exception: BoundsError\n");
73         failed = TRUE;
74     }
75     CATCH(ReportedBoundsError) {
76         printf("02: Caught wrong exception: ReportedBoundsError\n");
77         failed = TRUE;
78     }
79     CATCH_ALL {
80         printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
81         failed = TRUE;
82     }
83     FINALLY {
84         finally_called ++;
85     }
86     ENDTRY;
87
88     if (finally_called != 1) {
89         printf("02: FINALLY called %u times (not 1) on no exception\n", finally_called);
90         failed = TRUE;
91     }
92
93
94     /* check that finally is called on an uncaught exception */
95     ex_thrown = finally_called = 0;
96     TRY {
97         TRY {
98             THROW(BoundsError);
99         }
100         FINALLY {
101             finally_called ++;
102         }
103         ENDTRY;
104     }
105     CATCH(BoundsError) {
106         ex_thrown++;
107     }
108     ENDTRY;
109
110     if (finally_called != 1) {
111         printf("03: FINALLY called %u times (not 1) on uncaught exception\n", finally_called);
112         failed = TRUE;
113     }
114
115     if (ex_thrown != 1) {
116         printf("03: %u BoundsErrors (not 1) on uncaught exception\n", ex_thrown);
117         failed = TRUE;
118     }
119
120
121     /* check that finally is called on an rethrown exception */
122     ex_thrown = finally_called = 0;
123     TRY {
124         TRY {
125             THROW(BoundsError);
126         }
127         CATCH_ALL {
128             ex_thrown += 10;
129             RETHROW;
130         }
131         FINALLY {
132             finally_called += 10;
133         }
134         ENDTRY;
135     }
136     CATCH(BoundsError) {
137         ex_thrown ++;
138     }
139     FINALLY {
140         finally_called ++;
141     }
142     ENDTRY;
143
144     if (finally_called != 11) {
145         printf("04: finally_called = %u (not 11) on rethrown exception\n", finally_called);
146         failed = TRUE;
147     }
148
149     if (ex_thrown != 11) {
150         printf("04: %u BoundsErrors (not 11) on rethrown exception\n", ex_thrown);
151         failed = TRUE;
152     }
153
154
155     /* check that finally is called on an exception thrown from a CATCH block */
156     ex_thrown = finally_called = 0;
157     TRY {
158         TRY {
159             THROW(BoundsError);
160         }
161         CATCH_ALL {
162             if(ex_thrown > 0) {
163                 printf("05: Looping exception\n");
164                 failed = TRUE;
165             } else {
166                 ex_thrown += 10;
167                 THROW(BoundsError);
168             }
169         }
170         FINALLY {
171             finally_called += 10;
172         }
173         ENDTRY;
174     }
175     CATCH(BoundsError) {
176         ex_thrown ++;
177     }
178     FINALLY {
179         finally_called ++;
180     }
181     ENDTRY;
182
183     if (finally_called != 11) {
184         printf("05: finally_called = %u (not 11) on exception thrown from CATCH\n", finally_called);
185         failed = TRUE;
186     }
187
188     if (ex_thrown != 11) {
189         printf("05: %u BoundsErrors (not 11) on exception thrown from CATCH\n", ex_thrown);
190         failed = TRUE;
191     }
192
193     if(failed == FALSE )
194         printf("success\n");
195 }
196
197 int main(void)
198 {
199     except_init();
200     run_tests();
201     except_deinit();
202     exit(failed?1:0);
203 }