update epan/dissectors/pidl/drsuapi/drsuapi.idl from samba
[wireshark-sm.git] / epan / exntest.c
blob0fe81645ed804cfed27bfa47850d85199c56f63d
1 /* Standalone program to test functionality of exceptions.
3 * Copyright (c) 2004 MX Telecom Ltd. <richardv@mxtelecom.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
9 #include <config.h>
11 #include <stdio.h>
12 #include <glib.h>
13 #include "exceptions.h"
15 bool failed;
17 static void
18 finally_called_uncaught_exception(volatile unsigned int* called)
20 TRY {
21 THROW(BoundsError);
23 FINALLY {
24 (*called)++;
26 ENDTRY;
29 static void
30 finally_called_rethrown_exception(volatile unsigned int* thrown, volatile unsigned int* called)
32 TRY {
33 THROW(BoundsError);
35 CATCH_ALL {
36 (*thrown) += 10;
37 RETHROW;
39 FINALLY {
40 (*called) += 10;
42 ENDTRY;
45 static void
46 finally_called_exception_from_catch(volatile unsigned int* thrown, volatile unsigned int* called)
48 TRY {
49 THROW(BoundsError);
51 CATCH_ALL {
52 if((*thrown) > 0) {
53 printf("05: Looping exception\n");
54 failed = true;
55 } else {
56 (*thrown) += 10;
57 THROW(BoundsError);
60 FINALLY {
61 (*called) += 10;
63 ENDTRY;
66 static void
67 run_tests(void)
69 volatile unsigned int ex_thrown, finally_called;
71 /* check that the right catch, and the finally, are called, on exception */
72 ex_thrown = finally_called = 0;
73 TRY {
74 THROW(BoundsError);
76 CATCH(BoundsError) {
77 ex_thrown++;
79 CATCH(FragmentBoundsError) {
80 printf("01: Caught wrong exception: FragmentBoundsError\n");
81 failed = true;
83 CATCH(ReportedBoundsError) {
84 printf("01: Caught wrong exception: ReportedBoundsError\n");
85 failed = true;
87 CATCH_ALL {
88 printf("01: Caught wrong exception: %lu\n", exc->except_id.except_code);
89 failed = true;
91 FINALLY {
92 finally_called ++;
94 ENDTRY;
96 if (ex_thrown != 1) {
97 printf("01: %u BoundsErrors (not 1) on caught exception\n", ex_thrown);
98 failed = true;
101 if (finally_called != 1) {
102 printf("01: FINALLY called %u times (not 1) on caught exception\n", finally_called);
103 failed = true;
107 /* check that no catch at all is called when there is no exn */
108 ex_thrown = finally_called = 0;
109 TRY {
111 CATCH(BoundsError) {
112 printf("02: Caught wrong exception: BoundsError\n");
113 failed = true;
115 CATCH(FragmentBoundsError) {
116 printf("02: Caught wrong exception: FragmentBoundsError\n");
117 failed = true;
119 CATCH(ReportedBoundsError) {
120 printf("02: Caught wrong exception: ReportedBoundsError\n");
121 failed = true;
123 CATCH_ALL {
124 printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
125 failed = true;
127 FINALLY {
128 finally_called ++;
130 ENDTRY;
132 if (finally_called != 1) {
133 printf("02: FINALLY called %u times (not 1) on no exception\n", finally_called);
134 failed = true;
137 /* check that finally is called on an uncaught exception */
138 ex_thrown = finally_called = 0;
139 TRY {
140 finally_called_uncaught_exception(&finally_called);
142 CATCH(BoundsError) {
143 ex_thrown++;
145 ENDTRY;
147 if (finally_called != 1) {
148 printf("03: FINALLY called %u times (not 1) on uncaught exception\n", finally_called);
149 failed = true;
152 if (ex_thrown != 1) {
153 printf("03: %u BoundsErrors (not 1) on uncaught exception\n", ex_thrown);
154 failed = true;
158 /* check that finally is called on an rethrown exception */
159 ex_thrown = finally_called = 0;
160 TRY {
161 finally_called_rethrown_exception(&ex_thrown, &finally_called);
163 CATCH(BoundsError) {
164 ex_thrown ++;
166 FINALLY {
167 finally_called ++;
169 ENDTRY;
171 if (finally_called != 11) {
172 printf("04: finally_called = %u (not 11) on rethrown exception\n", finally_called);
173 failed = true;
176 if (ex_thrown != 11) {
177 printf("04: %u BoundsErrors (not 11) on rethrown exception\n", ex_thrown);
178 failed = true;
182 /* check that finally is called on an exception thrown from a CATCH block */
183 ex_thrown = finally_called = 0;
184 TRY {
185 finally_called_exception_from_catch(&ex_thrown, &finally_called);
187 CATCH(BoundsError) {
188 ex_thrown ++;
190 FINALLY {
191 finally_called ++;
193 ENDTRY;
195 if (finally_called != 11) {
196 printf("05: finally_called = %u (not 11) on exception thrown from CATCH\n", finally_called);
197 failed = true;
200 if (ex_thrown != 11) {
201 printf("05: %u BoundsErrors (not 11) on exception thrown from CATCH\n", ex_thrown);
202 failed = true;
205 if(failed == false )
206 printf("success\n");
209 int main(void)
211 except_init();
212 run_tests();
213 except_deinit();
214 exit(failed?1:0);
218 * Editor modelines - https://www.wireshark.org/tools/modelines.html
220 * Local variables:
221 * c-basic-offset: 4
222 * tab-width: 8
223 * indent-tabs-mode: nil
224 * End:
226 * vi: set shiftwidth=4 tabstop=8 expandtab:
227 * :indentSize=4:tabSize=8:noTabs=true: