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