Have GTPv2 pass its "instance ID" to "private extension" subdissectors rather than...
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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(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
102     /* check that finally is called on an uncaught exception */
103     ex_thrown = finally_called = 0;
104     TRY {
105         TRY {
106             THROW(BoundsError);
107         }
108         FINALLY {
109             finally_called ++;
110         }
111         ENDTRY;
112     }
113     CATCH(BoundsError) {
114         ex_thrown++;
115     }
116     ENDTRY;
117
118     if (finally_called != 1) {
119         printf("03: FINALLY called %u times (not 1) on uncaught exception\n", finally_called);
120         failed = TRUE;
121     }
122
123     if (ex_thrown != 1) {
124         printf("03: %u BoundsErrors (not 1) on uncaught exception\n", ex_thrown);
125         failed = TRUE;
126     }
127
128
129     /* check that finally is called on an rethrown exception */
130     ex_thrown = finally_called = 0;
131     TRY {
132         TRY {
133             THROW(BoundsError);
134         }
135         CATCH_ALL {
136             ex_thrown += 10;
137             RETHROW;
138         }
139         FINALLY {
140             finally_called += 10;
141         }
142         ENDTRY;
143     }
144     CATCH(BoundsError) {
145         ex_thrown ++;
146     }
147     FINALLY {
148         finally_called ++;
149     }
150     ENDTRY;
151
152     if (finally_called != 11) {
153         printf("04: finally_called = %u (not 11) on rethrown exception\n", finally_called);
154         failed = TRUE;
155     }
156
157     if (ex_thrown != 11) {
158         printf("04: %u BoundsErrors (not 11) on rethrown exception\n", ex_thrown);
159         failed = TRUE;
160     }
161
162
163     /* check that finally is called on an exception thrown from a CATCH block */
164     ex_thrown = finally_called = 0;
165     TRY {
166         TRY {
167             THROW(BoundsError);
168         }
169         CATCH_ALL {
170             if(ex_thrown > 0) {
171                 printf("05: Looping exception\n");
172                 failed = TRUE;
173             } else {
174                 ex_thrown += 10;
175                 THROW(BoundsError);
176             }
177         }
178         FINALLY {
179             finally_called += 10;
180         }
181         ENDTRY;
182     }
183     CATCH(BoundsError) {
184         ex_thrown ++;
185     }
186     FINALLY {
187         finally_called ++;
188     }
189     ENDTRY;
190
191     if (finally_called != 11) {
192         printf("05: finally_called = %u (not 11) on exception thrown from CATCH\n", finally_called);
193         failed = TRUE;
194     }
195
196     if (ex_thrown != 11) {
197         printf("05: %u BoundsErrors (not 11) on exception thrown from CATCH\n", ex_thrown);
198         failed = TRUE;
199     }
200
201     if(failed == FALSE )
202         printf("success\n");
203 }
204
205 int main(void)
206 {
207     except_init();
208     run_tests();
209     except_deinit();
210     exit(failed?1:0);
211 }