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/>
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/.
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
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
28 ** ex_test.c: exception handling test suite
36 #include "ex_test_ts.h"
39 TS_TEST(test_controlflow)
44 ts_test_check(TS_CTX, "basic nested control flow");
48 ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
52 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
54 THROW(0,0,"something");
57 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
61 ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
65 ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
69 ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n);
79 ts_test_check(TS_CTX, "exception value passing");
81 ts_test_fail(TS_CTX, "category=%d (!= 1)", ex.category);
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);
89 TS_TEST(test_variables)
95 r1 = r2 = v1 = v2 = 1234;
101 ts_test_check(TS_CTX, "variable preservation");
103 ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
105 ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1);
106 /* r2 is allowed to be destroyed because not volatile */
108 ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2);
119 ts_test_check(TS_CTX, "exception deferring");
121 ts_test_fail(TS_CTX, "unexpected deferring scope");
125 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
128 THROW(4711, 0, NULL);
136 ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
139 if (ex.category != 4711)
140 ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", ex.value);
143 ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1);
145 ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2);
147 ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3);
154 ts_test_check(TS_CTX, "exception shielding");
156 ts_test_fail(TS_CTX, "unexpected shielding scope");
158 ts_test_fail(TS_CTX, "unexpected catching scope");
162 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
166 ts_test_fail(TS_CTX, "unexpected shielding scope");
168 ts_test_fail(TS_CTX, "unexpected non-catching scope");
170 ts_test_fail(TS_CTX, "unexpected exception catched");
172 ts_test_fail(TS_CTX, "unexpected catching scope");
175 ts_test_fail(TS_CTX, "unexpected catching scope");
178 TS_TEST(test_cleanup)
184 ts_test_check(TS_CTX, "cleanup handling");
193 ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
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");
202 ts_test_fail(TS_CTX, "ex_cleanup not executed");
205 int main(int argc, char *argv[])
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);
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.
228 static char *mallocex(int size) {
231 #define SMALLAMOUNT 10
232 #define TOOBIG 100000000
234 #if 0 /* this contains syntax errors, actually */
235 static void bad_example(void) {
236 struct {char*first;} *globalcontext;
241 char *cp1, *cp2, *cp3;
243 cp1 = mallocex(SMALLAMOUNT);
244 globalcontext->first = cp1;
245 cp2 = mallocex(TOOBIG);
246 cp3 = mallocex(SMALLAMOUNT);
250 if (cp3 != NULL) free(cp3);
251 if (cp2 != NULL) free(cp2);
252 if (cp1 != NULL) free(cp1);
254 printf("cp3=%s", cp3);
257 /* end_of_bad_example */
261 static void good_example(void) {
262 struct {char*first;} *globalcontext;
267 char * volatile /*03*/ cp1 = NULL /*02*/;
268 char * volatile /*03*/ cp2 = NULL /*02*/;
269 char * volatile /*03*/ cp3 = NULL /*02*/;
271 cp1 = mallocex(SMALLAMOUNT);
272 globalcontext->first = cp1;
273 cp1 = NULL /*05 give away*/;
274 cp2 = mallocex(TOOBIG);
275 cp3 = mallocex(SMALLAMOUNT);
279 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
284 /*05 cp1 was given away */
286 /*05 global context untouched */
290 /* end_of_good_example */