Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include the original version of the libex library. Still to be fitted to our needs
[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
35 #include "ex_test_ts.h"
36 #include "xbt/ex.h"
37
38 TS_TEST(test_controlflow)
39 {
40     ex_t ex;
41     volatile int n;
42
43     ts_test_check(TS_CTX, "basic nested control flow");
44     n = 1;
45     ex_try {
46         if (n != 1)
47             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
48         n++;
49         ex_try {
50             if (n != 2)
51                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
52             n++;
53             ex_throw(0, 0, 0);
54         }
55         ex_catch (ex) {
56             if (n != 3)
57                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
58             n++;
59             ex_rethrow;
60         }
61         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
62     }
63     ex_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     ex_try {
77         ex_throw(1, 2, 3);
78     }
79     ex_catch (ex) {
80         ts_test_check(TS_CTX, "exception value passing");
81         if (ex.ex_class != (void *)1)
82             ts_test_fail(TS_CTX, "ex_class=0x%lx (!= 1)", (long)ex.ex_class);
83         if (ex.ex_object != (void *)2)
84             ts_test_fail(TS_CTX, "ex_object=0x%lx (!= 2)", (long)ex.ex_object);
85         if (ex.ex_value != (void *)3)
86             ts_test_fail(TS_CTX, "ex_value=0x%lx (!= 3)", (long)ex.ex_value);
87     }
88 }
89
90 TS_TEST(test_variables)
91 {
92     ex_t ex;
93     int r1, r2;
94     volatile int v1, v2;
95
96     r1 = r2 = v1 = v2 = 1234;
97     ex_try {
98         r2 = 5678;
99         v2 = 5678;
100         ex_throw(0, 0, 0);
101     }
102     ex_catch (ex) {
103         ts_test_check(TS_CTX, "variable preservation");
104         if (r1 != 1234)
105             ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
106         if (v1 != 1234)
107             ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1);
108         /* r2 is allowed to be destroyed because not volatile */
109         if (v2 != 5678)
110             ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2);
111     }
112 }
113
114 TS_TEST(test_defer)
115 {
116     ex_t ex;
117     volatile int i1 = 0;
118     volatile int i2 = 0;
119     volatile int i3 = 0;
120
121     ts_test_check(TS_CTX, "exception deferring");
122     if (ex_deferring)
123         ts_test_fail(TS_CTX, "unexpected deferring scope");
124     ex_try {
125         ex_defer {
126             if (!ex_deferring)
127                 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
128             ex_defer {
129                 i1 = 1;
130                 ex_throw(0, 0, 4711);
131                 i2 = 2;
132                 ex_throw(0, 0, 0);
133                 i3 = 3;
134                 ex_throw(0, 0, 0);
135             }
136             ex_throw(0, 0, 0);
137         }
138         ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
139     }
140     ex_catch (ex) {
141         if ((long)ex.ex_value != (long)4711)
142             ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", (long)ex.ex_value);
143     }
144     if (i1 != 1)
145         ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1);
146     if (i2 != 2)
147         ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2);
148     if (i3 != 3)
149         ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3);
150 }
151
152 TS_TEST(test_shield)
153 {
154     ex_t ex;
155
156     ts_test_check(TS_CTX, "exception shielding");
157     if (ex_shielding)
158         ts_test_fail(TS_CTX, "unexpected shielding scope");
159     if (ex_catching)
160         ts_test_fail(TS_CTX, "unexpected catching scope");
161     ex_try {
162         ex_shield {
163             if (!ex_shielding)
164                 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
165             ex_throw(0, 0, 0);
166         }
167         if (ex_shielding)
168             ts_test_fail(TS_CTX, "unexpected shielding scope");
169         if (!ex_catching)
170             ts_test_fail(TS_CTX, "unexpected non-catching scope");
171     }
172     ex_catch (ex) {
173         ts_test_fail(TS_CTX, "unexpected exception catched");
174         if (ex_catching)
175             ts_test_fail(TS_CTX, "unexpected catching scope");
176     }
177     if (ex_catching)
178         ts_test_fail(TS_CTX, "unexpected catching scope");
179 }
180
181 TS_TEST(test_cleanup)
182 {
183     ex_t ex;
184     volatile int v1;
185     int c;
186
187     ts_test_check(TS_CTX, "cleanup handling");
188
189     v1 = 1234;
190     c = 0;
191     ex_try {
192         v1 = 5678;
193         ex_throw(1, 2, 3);
194     }
195     ex_cleanup {
196         if (v1 != 5678)
197             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
198         c = 1;
199     }
200     ex_catch (ex) {
201         if (v1 != 5678)
202             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
203         if (!(ex.ex_class == (void *)1 && ex.ex_object == (void *)2 && ex.ex_value == (void *)3))
204             ts_test_fail(TS_CTX, "unexpected exception contents");
205     }
206     if (!c)
207         ts_test_fail(TS_CTX, "ex_cleanup not executed");
208 }
209
210 int main(int argc, char *argv[])
211 {
212     ts_suite_t *ts;
213     int n;
214
215     ts = ts_suite_new("OSSP ex (Exception Handling)");
216     ts_suite_test(ts, test_controlflow, "basic nested control flow");
217     ts_suite_test(ts, test_value,       "exception value passing");
218     ts_suite_test(ts, test_variables,   "variable value preservation");
219     ts_suite_test(ts, test_defer,       "exception deferring");
220     ts_suite_test(ts, test_shield,      "exception shielding");
221     ts_suite_test(ts, test_cleanup,     "cleanup handling");
222     n = ts_suite_run(ts);
223     ts_suite_free(ts);
224     return n;
225 }
226