Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
follow the lastest macro renaming in xbt_ex
[simgrid.git] / testsuite / xbt / ex_test.c
1 /*
2 **  OSSP ex - Exception Handling
3 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
4 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
5 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
6 **
7 **  This file is part of OSSP ex, an exception handling library
8 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
9 **
10 **  Permission to use, copy, modify, and distribute this software for
11 **  any purpose with or without fee is hereby granted, provided that
12 **  the above copyright notice and this permission notice appear in all
13 **  copies.
14 **
15 **  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
16 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
19 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 **  SUCH DAMAGE.
27 **
28 **  ex_test.c: exception handling test suite
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <string.h>
35
36 #include "ex_test_ts.h"
37 #include "xbt/ex.h"
38
39 TS_TEST(test_controlflow)
40 {
41     ex_t ex;
42     volatile int n;
43
44     ts_test_check(TS_CTX, "basic nested control flow");
45     n = 1;
46     TRY {
47         if (n != 1)
48             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
49         n++;
50         TRY {
51             if (n != 2)
52                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
53             n++;
54             THROW(0,0,"something");
55         } CATCH (ex) {
56             if (n != 3)
57                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
58             n++;
59             RETHROW;
60         }
61         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
62     }
63     CATCH(ex) {
64         if (n != 4)
65             ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
66         n++;
67     }
68     if (n != 5)
69         ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n);
70 }
71
72 TS_TEST(test_value)
73 {
74     ex_t ex;
75
76     TRY {
77         THROW(1, 2, "toto");
78     } CATCH(ex) {
79         ts_test_check(TS_CTX, "exception value passing");
80         if (ex.category != 1)
81             ts_test_fail(TS_CTX, "category=%d (!= 1)", ex.category);
82         if (ex.value != 2)
83             ts_test_fail(TS_CTX, "value=%d (!= 2)", ex.value);
84         if (strcmp(ex.msg,"toto"))
85             ts_test_fail(TS_CTX, "message=%s (!= toto)", ex.msg);
86     }
87 }
88
89 TS_TEST(test_variables)
90 {
91     ex_t ex;
92     int r1, r2;
93     volatile int v1, v2;
94
95     r1 = r2 = v1 = v2 = 1234;
96     TRY {
97         r2 = 5678;
98         v2 = 5678;
99         THROW(0, 0, 0);
100     } CATCH(ex) {
101         ts_test_check(TS_CTX, "variable preservation");
102         if (r1 != 1234)
103             ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
104         if (v1 != 1234)
105             ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1);
106         /* r2 is allowed to be destroyed because not volatile */
107         if (v2 != 5678)
108             ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2);
109     }
110 }
111
112 TS_TEST(test_defer)
113 {
114     ex_t ex;
115     volatile int i1 = 0;
116     volatile int i2 = 0;
117     volatile int i3 = 0;
118
119     ts_test_check(TS_CTX, "exception deferring");
120     if (IS_DEFERRED)
121         ts_test_fail(TS_CTX, "unexpected deferring scope");
122     TRY {
123         DEFER {
124             if (!IS_DEFERRED)
125                 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
126             DEFER {
127                 i1 = 1;
128                 THROW(4711, 0, NULL);
129                 i2 = 2;
130                 THROW(0, 0, NULL);
131                 i3 = 3;
132                 THROW(0, 0, NULL);
133             }
134             THROW(0, 0, 0);
135         }
136         ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
137     }
138     CATCH(ex) {
139         if (ex.category != 4711)
140             ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", ex.value);
141     }
142     if (i1 != 1)
143         ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1);
144     if (i2 != 2)
145         ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2);
146     if (i3 != 3)
147         ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3);
148 }
149
150 TS_TEST(test_shield)
151 {
152     ex_t ex;
153
154     ts_test_check(TS_CTX, "exception shielding");
155     if (IS_SHIELDED)
156         ts_test_fail(TS_CTX, "unexpected shielding scope");
157     if (IS_CATCHED)
158         ts_test_fail(TS_CTX, "unexpected catching scope");
159     TRY {
160         SHIELD {
161             if (!IS_SHIELDED)
162                 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
163             THROW(0, 0, 0);
164         }
165         if (IS_SHIELDED)
166             ts_test_fail(TS_CTX, "unexpected shielding scope");
167         if (!IS_CATCHED)
168             ts_test_fail(TS_CTX, "unexpected non-catching scope");
169     } CATCH(ex) {
170         ts_test_fail(TS_CTX, "unexpected exception catched");
171         if (IS_CATCHED)
172             ts_test_fail(TS_CTX, "unexpected catching scope");
173     }
174     if (IS_CATCHED)
175         ts_test_fail(TS_CTX, "unexpected catching scope");
176 }
177
178 TS_TEST(test_cleanup)
179 {
180     ex_t ex;
181     volatile int v1;
182     int c;
183
184     ts_test_check(TS_CTX, "cleanup handling");
185
186     v1 = 1234;
187     c = 0;
188     TRY {
189         v1 = 5678;
190         THROW(1, 2, "blah");
191     } CLEANUP {
192         if (v1 != 5678)
193             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
194         c = 1;
195     } CATCH(ex) {
196         if (v1 != 5678)
197             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
198         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
199             ts_test_fail(TS_CTX, "unexpected exception contents");
200     }
201     if (!c)
202         ts_test_fail(TS_CTX, "ex_cleanup not executed");
203 }
204
205 int main(int argc, char *argv[])
206 {
207     ts_suite_t *ts;
208     int n;
209
210     ts = ts_suite_new("OSSP ex (Exception Handling)");
211     ts_suite_test(ts, test_controlflow, "basic nested control flow");
212     ts_suite_test(ts, test_value,       "exception value passing");
213     ts_suite_test(ts, test_variables,   "variable value preservation");
214     ts_suite_test(ts, test_defer,       "exception deferring");
215     ts_suite_test(ts, test_shield,      "exception shielding");
216     ts_suite_test(ts, test_cleanup,     "cleanup handling");
217     n = ts_suite_run(ts);
218     ts_suite_free(ts);
219     return n;
220 }
221
222
223 /*
224  * The following is the example included in the documentation. It's a good 
225  * idea to check its syntax even if we don't try to run it.
226  * And actually, it allows to put comments in the code despite doxygen.
227  */ 
228 static char *mallocex(int size) {
229   return NULL;
230 }
231 #define SMALLAMOUNT 10
232 #define TOOBIG 100000000
233
234 #if 0 /* this contains syntax errors, actually */
235 static void bad_example(void) {
236   struct {char*first;} *globalcontext;
237   ex_t ex;
238
239   /* BAD_EXAMPLE */
240   TRY {
241     char *cp1, *cp2, *cp3;
242     
243     cp1 = mallocex(SMALLAMOUNT);
244     globalcontext->first = cp1;
245     cp2 = mallocex(TOOBIG);
246     cp3 = mallocex(SMALLAMOUNT);
247     strcpy(cp1, "foo");
248     strcpy(cp2, "bar");
249   } CLEANUP {
250     if (cp3 != NULL) free(cp3);
251     if (cp2 != NULL) free(cp2);
252     if (cp1 != NULL) free(cp1);
253   } CATCH(ex) {
254     printf("cp3=%s", cp3);
255     RETHROW;
256   }
257   /* end_of_bad_example */
258 }
259 #endif
260
261 static void good_example(void) {
262   struct {char*first;} *globalcontext;
263   ex_t ex;
264
265   /* GOOD_EXAMPLE */
266   { /*01*/
267     char * volatile /*03*/ cp1 = NULL /*02*/;
268     char * volatile /*03*/ cp2 = NULL /*02*/;
269     char * volatile /*03*/ cp3 = NULL /*02*/;
270     TRY {
271       cp1 = mallocex(SMALLAMOUNT);
272       globalcontext->first = cp1;
273       cp1 = NULL /*05 give away*/;
274       cp2 = mallocex(TOOBIG);
275       cp3 = mallocex(SMALLAMOUNT);
276       strcpy(cp1, "foo");
277       strcpy(cp2, "bar");
278     } CLEANUP { /*04*/
279       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
280       if (cp3 != NULL)
281         free(cp3);
282       if (cp2 != NULL)
283         free(cp2);
284       /*05 cp1 was given away */
285     } CATCH(ex) {
286       /*05 global context untouched */
287       RETHROW;
288     }
289   }
290   /* end_of_good_example */
291 }