lua: fix reload with -Xlua_script
[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 DIAG_OFF(shadow)
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(FragmentBoundsError) {
44         printf("01: Caught wrong exception: FragmentBoundsError\n");
45         failed = TRUE;
46     }
47     CATCH(ReportedBoundsError) {
48         printf("01: Caught wrong exception: ReportedBoundsError\n");
49         failed = TRUE;
50     }
51     CATCH_ALL {
52         printf("01: Caught wrong exception: %lu\n", exc->except_id.except_code);
53         failed = TRUE;
54     }
55     FINALLY {
56         finally_called ++;
57     }
58     ENDTRY;
59
60     if (ex_thrown != 1) {
61         printf("01: %u BoundsErrors (not 1) on caught exception\n", ex_thrown);
62         failed = TRUE;
63     }
64
65     if (finally_called != 1) {
66         printf("01: FINALLY called %u times (not 1) on caught exception\n", finally_called);
67         failed = TRUE;
68     }
69
70
71     /* check that no catch at all is called when there is no exn */
72     ex_thrown = finally_called = 0;
73     TRY {
74     }
75     CATCH(BoundsError) {
76         printf("02: Caught wrong exception: BoundsError\n");
77         failed = TRUE;
78     }
79     CATCH(FragmentBoundsError) {
80         printf("02: Caught wrong exception: FragmentBoundsError\n");
81         failed = TRUE;
82     }
83     CATCH(ReportedBoundsError) {
84         printf("02: Caught wrong exception: ReportedBoundsError\n");
85         failed = TRUE;
86     }
87     CATCH_ALL {
88         printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
89         failed = TRUE;
90     }
91     FINALLY {
92         finally_called ++;
93     }
94     ENDTRY;
95
96     if (finally_called != 1) {
97         printf("02: FINALLY called %u times (not 1) on no exception\n", finally_called);
98         failed = TRUE;
99     }
100
101     /* check that finally is called on an uncaught exception */
102     ex_thrown = finally_called = 0;
103     TRY {
104         TRY {
105             THROW(BoundsError);
106         }
107         FINALLY {
108             finally_called ++;
109         }
110         ENDTRY;
111     }
112     CATCH(BoundsError) {
113         ex_thrown++;
114     }
115     ENDTRY;
116
117     if (finally_called != 1) {
118         printf("03: FINALLY called %u times (not 1) on uncaught exception\n", finally_called);
119         failed = TRUE;
120     }
121
122     if (ex_thrown != 1) {
123         printf("03: %u BoundsErrors (not 1) on uncaught exception\n", ex_thrown);
124         failed = TRUE;
125     }
126
127
128     /* check that finally is called on an rethrown exception */
129     ex_thrown = finally_called = 0;
130     TRY {
131         TRY {
132             THROW(BoundsError);
133         }
134         CATCH_ALL {
135             ex_thrown += 10;
136             RETHROW;
137         }
138         FINALLY {
139             finally_called += 10;
140         }
141         ENDTRY;
142     }
143     CATCH(BoundsError) {
144         ex_thrown ++;
145     }
146     FINALLY {
147         finally_called ++;
148     }
149     ENDTRY;
150
151     if (finally_called != 11) {
152         printf("04: finally_called = %u (not 11) on rethrown exception\n", finally_called);
153         failed = TRUE;
154     }
155
156     if (ex_thrown != 11) {
157         printf("04: %u BoundsErrors (not 11) on rethrown exception\n", ex_thrown);
158         failed = TRUE;
159     }
160
161
162     /* check that finally is called on an exception thrown from a CATCH block */
163     ex_thrown = finally_called = 0;
164     TRY {
165         TRY {
166             THROW(BoundsError);
167         }
168         CATCH_ALL {
169             if(ex_thrown > 0) {
170                 printf("05: Looping exception\n");
171                 failed = TRUE;
172             } else {
173                 ex_thrown += 10;
174                 THROW(BoundsError);
175             }
176         }
177         FINALLY {
178             finally_called += 10;
179         }
180         ENDTRY;
181     }
182     CATCH(BoundsError) {
183         ex_thrown ++;
184     }
185     FINALLY {
186         finally_called ++;
187     }
188     ENDTRY;
189
190     if (finally_called != 11) {
191         printf("05: finally_called = %u (not 11) on exception thrown from CATCH\n", finally_called);
192         failed = TRUE;
193     }
194
195     if (ex_thrown != 11) {
196         printf("05: %u BoundsErrors (not 11) on exception thrown from CATCH\n", ex_thrown);
197         failed = TRUE;
198     }
199
200     if(failed == FALSE )
201         printf("success\n");
202 }
203 DIAG_ON(shadow)
204
205 int main(void)
206 {
207     except_init();
208     run_tests();
209     except_deinit();
210     exit(failed?1:0);
211 }
212
213 /*
214  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
215  *
216  * Local variables:
217  * c-basic-offset: 4
218  * tab-width: 8
219  * indent-tabs-mode: nil
220  * End:
221  *
222  * vi: set shiftwidth=4 tabstop=8 expandtab:
223  * :indentSize=4:tabSize=8:noTabs=true:
224  */