(Temporarily) Allow DCE/RPC dissector table to have duplicates.
[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 <stdio.h>
22 #include <glib.h>
23 #include <config.h>
24 #include "exceptions.h"
25
26 #include <wsutil/ws_diag_control.h>
27
28 gboolean failed = FALSE;
29
30 DIAG_OFF(shadow)
31 void
32 run_tests(void)
33 {
34     volatile unsigned int ex_thrown, finally_called;
35
36     /* check that the right catch, and the finally, are called, on exception */
37     ex_thrown = finally_called = 0;
38     TRY {
39         THROW(BoundsError);
40     }
41     CATCH(BoundsError) {
42         ex_thrown++;
43     }
44     CATCH(FragmentBoundsError) {
45         printf("01: Caught wrong exception: FragmentBoundsError\n");
46         failed = TRUE;
47     }
48     CATCH(ReportedBoundsError) {
49         printf("01: Caught wrong exception: ReportedBoundsError\n");
50         failed = TRUE;
51     }
52     CATCH_ALL {
53         printf("01: Caught wrong exception: %lu\n", exc->except_id.except_code);
54         failed = TRUE;
55     }
56     FINALLY {
57         finally_called ++;
58     }
59     ENDTRY;
60
61     if (ex_thrown != 1) {
62         printf("01: %u BoundsErrors (not 1) on caught exception\n", ex_thrown);
63         failed = TRUE;
64     }
65
66     if (finally_called != 1) {
67         printf("01: FINALLY called %u times (not 1) on caught exception\n", finally_called);
68         failed = TRUE;
69     }
70
71
72     /* check that no catch at all is called when there is no exn */
73     ex_thrown = finally_called = 0;
74     TRY {
75     }
76     CATCH(BoundsError) {
77         printf("02: Caught wrong exception: BoundsError\n");
78         failed = TRUE;
79     }
80     CATCH(FragmentBoundsError) {
81         printf("02: Caught wrong exception: FragmentBoundsError\n");
82         failed = TRUE;
83     }
84     CATCH(ReportedBoundsError) {
85         printf("02: Caught wrong exception: ReportedBoundsError\n");
86         failed = TRUE;
87     }
88     CATCH_ALL {
89         printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
90         failed = TRUE;
91     }
92     FINALLY {
93         finally_called ++;
94     }
95     ENDTRY;
96
97     if (finally_called != 1) {
98         printf("02: FINALLY called %u times (not 1) on no exception\n", finally_called);
99         failed = TRUE;
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 DIAG_ON(shadow)
205
206 int main(void)
207 {
208     except_init();
209     run_tests();
210     except_deinit();
211     exit(failed?1:0);
212 }
213
214 /*
215  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
216  *
217  * Local variables:
218  * c-basic-offset: 4
219  * tab-width: 8
220  * indent-tabs-mode: nil
221  * End:
222  *
223  * vi: set shiftwidth=4 tabstop=8 expandtab:
224  * :indentSize=4:tabSize=8:noTabs=true:
225  */