Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fit libex into SimGrid (with an axe, I admit)
[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     sg_try {
47         if (n != 1)
48             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
49         n++;
50         sg_try {
51             if (n != 2)
52                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
53             n++;
54             sg_throw(0,0,"something");
55         }
56         sg_catch (ex) {
57             if (n != 3)
58                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
59             n++;
60             sg_rethrow;
61         }
62         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
63     }
64     sg_catch (ex) {
65         if (n != 4)
66             ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
67         n++;
68     }
69     if (n != 5)
70         ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n);
71 }
72
73 TS_TEST(test_value)
74 {
75     ex_t ex;
76
77     sg_try {
78         sg_throw(1, 2, "toto");
79     }
80     sg_catch (ex) {
81         ts_test_check(TS_CTX, "exception value passing");
82         if (ex.code != 1)
83             ts_test_fail(TS_CTX, "code=%d (!= 1)", ex.code);
84         if (ex.value != 2)
85             ts_test_fail(TS_CTX, "value=%d (!= 2)", ex.value);
86         if (strcmp(ex.msg,"toto"))
87             ts_test_fail(TS_CTX, "message=%s (!= toto)", ex.msg);
88     }
89 }
90
91 TS_TEST(test_variables)
92 {
93     ex_t ex;
94     int r1, r2;
95     volatile int v1, v2;
96
97     r1 = r2 = v1 = v2 = 1234;
98     sg_try {
99         r2 = 5678;
100         v2 = 5678;
101         sg_throw(0, 0, 0);
102     }
103     sg_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_defer)
116 {
117     ex_t ex;
118     volatile int i1 = 0;
119     volatile int i2 = 0;
120     volatile int i3 = 0;
121
122     ts_test_check(TS_CTX, "exception deferring");
123     if (sg_deferring)
124         ts_test_fail(TS_CTX, "unexpected deferring scope");
125     sg_try {
126         sg_defer {
127             if (!sg_deferring)
128                 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
129             sg_defer {
130                 i1 = 1;
131                 sg_throw(4711, 0, NULL);
132                 i2 = 2;
133                 sg_throw(0, 0, NULL);
134                 i3 = 3;
135                 sg_throw(0, 0, NULL);
136             }
137             sg_throw(0, 0, 0);
138         }
139         ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
140     }
141     sg_catch (ex) {
142         if (ex.code != 4711)
143             ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", ex.value);
144     }
145     if (i1 != 1)
146         ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1);
147     if (i2 != 2)
148         ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2);
149     if (i3 != 3)
150         ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3);
151 }
152
153 TS_TEST(test_shield)
154 {
155     ex_t ex;
156
157     ts_test_check(TS_CTX, "exception shielding");
158     if (sg_shielding)
159         ts_test_fail(TS_CTX, "unexpected shielding scope");
160     if (sg_catching)
161         ts_test_fail(TS_CTX, "unexpected catching scope");
162     sg_try {
163         sg_shield {
164             if (!sg_shielding)
165                 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
166             sg_throw(0, 0, 0);
167         }
168         if (sg_shielding)
169             ts_test_fail(TS_CTX, "unexpected shielding scope");
170         if (!sg_catching)
171             ts_test_fail(TS_CTX, "unexpected non-catching scope");
172     }
173     sg_catch (ex) {
174         ts_test_fail(TS_CTX, "unexpected exception catched");
175         if (sg_catching)
176             ts_test_fail(TS_CTX, "unexpected catching scope");
177     }
178     if (sg_catching)
179         ts_test_fail(TS_CTX, "unexpected catching scope");
180 }
181
182 TS_TEST(test_cleanup)
183 {
184     ex_t ex;
185     volatile int v1;
186     int c;
187
188     ts_test_check(TS_CTX, "cleanup handling");
189
190     v1 = 1234;
191     c = 0;
192     sg_try {
193         v1 = 5678;
194         sg_throw(1, 2, "blah");
195     }
196     sg_cleanup {
197         if (v1 != 5678)
198             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
199         c = 1;
200     }
201     sg_catch (ex) {
202         if (v1 != 5678)
203             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
204         if (!(ex.code == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
205             ts_test_fail(TS_CTX, "unexpected exception contents");
206     }
207     if (!c)
208         ts_test_fail(TS_CTX, "ex_cleanup not executed");
209 }
210
211 int main(int argc, char *argv[])
212 {
213     ts_suite_t *ts;
214     int n;
215
216     ts = ts_suite_new("OSSP ex (Exception Handling)");
217     ts_suite_test(ts, test_controlflow, "basic nested control flow");
218     ts_suite_test(ts, test_value,       "exception value passing");
219     ts_suite_test(ts, test_variables,   "variable value preservation");
220     ts_suite_test(ts, test_defer,       "exception deferring");
221     ts_suite_test(ts, test_shield,      "exception shielding");
222     ts_suite_test(ts, test_cleanup,     "cleanup handling");
223     n = ts_suite_run(ts);
224     ts_suite_free(ts);
225     return n;
226 }
227