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