Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
change mismatch_error to not_found_error where appropriate
[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 #include "xbt/log.h"
39
40 XBT_LOG_NEW_CATEGORY(test,"This test");
41
42 TS_TEST(test_controlflow)
43 {
44     xbt_ex_t ex;
45     volatile int n;
46
47     ts_test_check(TS_CTX, "basic nested control flow");
48     n = 1;
49     TRY {
50         if (n != 1)
51             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
52         n++;
53         TRY {
54             if (n != 2)
55                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
56             n++;
57             THROW0(unknown_error,0,"something");
58         } CATCH (ex) {
59             if (n != 3)
60                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
61             n++;
62             RETHROW;
63         }
64         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
65     }
66     CATCH(ex) {
67         if (n != 4)
68             ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
69         n++;
70     }
71     if (n != 5)
72         ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n);
73 }
74
75 TS_TEST(test_value)
76 {
77     xbt_ex_t ex;
78
79     TRY {
80         THROW0(unknown_error, 2, "toto");
81     } CATCH(ex) {
82         ts_test_check(TS_CTX, "exception value passing");
83         if (ex.category != unknown_error)
84             ts_test_fail(TS_CTX, "category=%d (!= 1)", ex.category);
85         if (ex.value != 2)
86             ts_test_fail(TS_CTX, "value=%d (!= 2)", ex.value);
87         if (strcmp(ex.msg,"toto"))
88             ts_test_fail(TS_CTX, "message=%s (!= toto)", ex.msg);
89     }
90 }
91
92 TS_TEST(test_variables)
93 {
94     xbt_ex_t ex;
95     int r1, r2;
96     volatile int v1, v2;
97
98     r1 = r2 = v1 = v2 = 1234;
99     TRY {
100         r2 = 5678;
101         v2 = 5678;
102         THROW0(unknown_error, 0, "toto");
103     } CATCH(ex) {
104         ts_test_check(TS_CTX, "variable preservation");
105         if (r1 != 1234)
106             ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
107         if (v1 != 1234)
108             ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1);
109         /* r2 is allowed to be destroyed because not volatile */
110         if (v2 != 5678)
111             ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2);
112     }
113 }
114
115 TS_TEST(test_cleanup)
116 {
117     xbt_ex_t ex;
118     volatile int v1;
119     int c;
120
121     ts_test_check(TS_CTX, "cleanup handling");
122
123     v1 = 1234;
124     c = 0;
125     TRY {
126         v1 = 5678;
127         THROW0(1, 2, "blah");
128     } CLEANUP {
129         if (v1 != 5678)
130             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
131         c = 1;
132     } CATCH(ex) {
133         if (v1 != 5678)
134             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
135         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
136             ts_test_fail(TS_CTX, "unexpected exception contents");
137     }
138     if (!c)
139         ts_test_fail(TS_CTX, "ex_cleanup not executed");
140 }
141
142 int main(int argc, char *argv[])
143 {
144     ts_suite_t *ts;
145     int n;
146
147     ts = ts_suite_new("OSSP ex (Exception Handling)");
148     ts_suite_test(ts, test_controlflow, "basic nested control flow");
149     ts_suite_test(ts, test_value,       "exception value passing");
150     ts_suite_test(ts, test_variables,   "variable value preservation");
151     ts_suite_test(ts, test_cleanup,     "cleanup handling");
152     n = ts_suite_run(ts);
153     ts_suite_free(ts);
154     return n;
155 }
156
157
158 /*
159  * The following is the example included in the documentation. It's a good 
160  * idea to check its syntax even if we don't try to run it.
161  * And actually, it allows to put comments in the code despite doxygen.
162  */ 
163 static char *mallocex(int size) {
164   return NULL;
165 }
166 #define SMALLAMOUNT 10
167 #define TOOBIG 100000000
168
169 #if 0 /* this contains syntax errors, actually */
170 static void bad_example(void) {
171   struct {char*first;} *globalcontext;
172   ex_t ex;
173
174   /* BAD_EXAMPLE */
175   TRY {
176     char *cp1, *cp2, *cp3;
177     
178     cp1 = mallocex(SMALLAMOUNT);
179     globalcontext->first = cp1;
180     cp2 = mallocex(TOOBIG);
181     cp3 = mallocex(SMALLAMOUNT);
182     strcpy(cp1, "foo");
183     strcpy(cp2, "bar");
184   } CLEANUP {
185     if (cp3 != NULL) free(cp3);
186     if (cp2 != NULL) free(cp2);
187     if (cp1 != NULL) free(cp1);
188   } CATCH(ex) {
189     printf("cp3=%s", cp3);
190     RETHROW;
191   }
192   /* end_of_bad_example */
193 }
194 #endif
195
196 static void good_example(void) {
197   struct {char*first;} *globalcontext;
198   xbt_ex_t ex;
199
200   /* GOOD_EXAMPLE */
201   { /*01*/
202     char * volatile /*03*/ cp1 = NULL /*02*/;
203     char * volatile /*03*/ cp2 = NULL /*02*/;
204     char * volatile /*03*/ cp3 = NULL /*02*/;
205     TRY {
206       cp1 = mallocex(SMALLAMOUNT);
207       globalcontext->first = cp1;
208       cp1 = NULL /*05 give away*/;
209       cp2 = mallocex(TOOBIG);
210       cp3 = mallocex(SMALLAMOUNT);
211       strcpy(cp1, "foo");
212       strcpy(cp2, "bar");
213     } CLEANUP { /*04*/
214       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
215       if (cp3 != NULL)
216         free(cp3);
217       if (cp2 != NULL)
218         free(cp2);
219       /*05 cp1 was given away */
220     } CATCH(ex) {
221       /*05 global context untouched */
222       RETHROW;
223     }
224   }
225   /* end_of_good_example */
226 }