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
35 #include "ex_test_ts.h"
38 TS_TEST(test_controlflow)
43 ts_test_check(TS_CTX, "basic nested control flow");
47 ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
51 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
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);
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);
90 TS_TEST(test_variables)
96 r1 = r2 = v1 = v2 = 1234;
103 ts_test_check(TS_CTX, "variable preservation");
105 ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
107 ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1);
108 /* r2 is allowed to be destroyed because not volatile */
110 ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2);
121 ts_test_check(TS_CTX, "exception deferring");
123 ts_test_fail(TS_CTX, "unexpected deferring scope");
127 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
130 ex_throw(0, 0, 4711);
138 ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
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);
145 ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1);
147 ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2);
149 ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3);
156 ts_test_check(TS_CTX, "exception shielding");
158 ts_test_fail(TS_CTX, "unexpected shielding scope");
160 ts_test_fail(TS_CTX, "unexpected catching scope");
164 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
168 ts_test_fail(TS_CTX, "unexpected shielding scope");
170 ts_test_fail(TS_CTX, "unexpected non-catching scope");
173 ts_test_fail(TS_CTX, "unexpected exception catched");
175 ts_test_fail(TS_CTX, "unexpected catching scope");
178 ts_test_fail(TS_CTX, "unexpected catching scope");
181 TS_TEST(test_cleanup)
187 ts_test_check(TS_CTX, "cleanup handling");
197 ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
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");
207 ts_test_fail(TS_CTX, "ex_cleanup not executed");
210 int main(int argc, char *argv[])
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);