Logcat: Set data-text-lines dissectors for log
[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 gboolean failed = FALSE;
27
28 void
29 run_tests(void)
30 {
31     volatile unsigned int ex_thrown, finally_called;
32
33     /* check that the right catch, and the finally, are called, on exception */
34     ex_thrown = finally_called = 0;
35     TRY {
36         THROW(BoundsError);
37     }
38     CATCH(BoundsError) {
39         ex_thrown++;
40     }
41     CATCH(FragmentBoundsError) {
42         printf("01: Caught wrong exception: FragmentBoundsError\n");
43         failed = TRUE;
44     }
45     CATCH(ReportedBoundsError) {
46         printf("01: Caught wrong exception: ReportedBoundsError\n");
47         failed = TRUE;
48     }
49     CATCH_ALL {
50         printf("01: Caught wrong exception: %lu\n", exc->except_id.except_code);
51         failed = TRUE;
52     }
53     FINALLY {
54         finally_called ++;
55     }
56     ENDTRY;
57
58     if (ex_thrown != 1) {
59         printf("01: %u BoundsErrors (not 1) on caught exception\n", ex_thrown);
60         failed = TRUE;
61     }
62
63     if (finally_called != 1) {
64         printf("01: FINALLY called %u times (not 1) on caught exception\n", finally_called);
65         failed = TRUE;
66     }
67
68
69     /* check that no catch at all is called when there is no exn */
70     ex_thrown = finally_called = 0;
71     TRY {
72     }
73     CATCH(BoundsError) {
74         printf("02: Caught wrong exception: BoundsError\n");
75         failed = TRUE;
76     }
77     CATCH(FragmentBoundsError) {
78         printf("02: Caught wrong exception: FragmentBoundsError\n");
79         failed = TRUE;
80     }
81     CATCH(ReportedBoundsError) {
82         printf("02: Caught wrong exception: ReportedBoundsError\n");
83         failed = TRUE;
84     }
85     CATCH_ALL {
86         printf("02: Caught wrong exception: %lu\n", exc->except_id.except_code);
87         failed = TRUE;
88     }
89     FINALLY {
90         finally_called ++;
91     }
92     ENDTRY;
93
94     if (finally_called != 1) {
95         printf("02: FINALLY called %u times (not 1) on no exception\n", finally_called);
96         failed = TRUE;
97     }
98
99
100     /* check that finally is called on an uncaught exception */
101     ex_thrown = finally_called = 0;
102     TRY {
103         TRY {
104             THROW(BoundsError);
105         }
106         FINALLY {
107             finally_called ++;
108         }
109         ENDTRY;
110     }
111     CATCH(BoundsError) {
112         ex_thrown++;
113     }
114     ENDTRY;
115
116     if (finally_called != 1) {
117         printf("03: FINALLY called %u times (not 1) on uncaught exception\n", finally_called);
118         failed = TRUE;
119     }
120
121     if (ex_thrown != 1) {
122         printf("03: %u BoundsErrors (not 1) on uncaught exception\n", ex_thrown);
123         failed = TRUE;
124     }
125
126
127     /* check that finally is called on an rethrown exception */
128     ex_thrown = finally_called = 0;
129     TRY {
130         TRY {
131             THROW(BoundsError);
132         }
133         CATCH_ALL {
134             ex_thrown += 10;
135             RETHROW;
136         }
137         FINALLY {
138             finally_called += 10;
139         }
140         ENDTRY;
141     }
142     CATCH(BoundsError) {
143         ex_thrown ++;
144     }
145     FINALLY {
146         finally_called ++;
147     }
148     ENDTRY;
149
150     if (finally_called != 11) {
151         printf("04: finally_called = %u (not 11) on rethrown exception\n", finally_called);
152         failed = TRUE;
153     }
154
155     if (ex_thrown != 11) {
156         printf("04: %u BoundsErrors (not 11) on rethrown exception\n", ex_thrown);
157         failed = TRUE;
158     }
159
160
161     /* check that finally is called on an exception thrown from a CATCH block */
162     ex_thrown = finally_called = 0;
163     TRY {
164         TRY {
165             THROW(BoundsError);
166         }
167         CATCH_ALL {
168             if(ex_thrown > 0) {
169                 printf("05: Looping exception\n");
170                 failed = TRUE;
171             } else {
172                 ex_thrown += 10;
173                 THROW(BoundsError);
174             }
175         }
176         FINALLY {
177             finally_called += 10;
178         }
179         ENDTRY;
180     }
181     CATCH(BoundsError) {
182         ex_thrown ++;
183     }
184     FINALLY {
185         finally_called ++;
186     }
187     ENDTRY;
188
189     if (finally_called != 11) {
190         printf("05: finally_called = %u (not 11) on exception thrown from CATCH\n", finally_called);
191         failed = TRUE;
192     }
193
194     if (ex_thrown != 11) {
195         printf("05: %u BoundsErrors (not 11) on exception thrown from CATCH\n", ex_thrown);
196         failed = TRUE;
197     }
198
199     if(failed == FALSE )
200         printf("success\n");
201 }
202
203 int main(void)
204 {
205     except_init();
206     run_tests();
207     except_deinit();
208     exit(failed?1:0);
209 }