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 "xbt/cunit.h"
40 XBT_LOG_NEW_CATEGORY(test,"This test");
42 XBT_TEST_UNIT(test_expected_failure) {
43 xbt_test0("Skipped test");
46 xbt_test0("EXPECTED FAILURE");
47 xbt_test_expect_failure();
48 xbt_test_log2("%s %s","Test","log");
49 xbt_test_fail0("EXPECTED FAILURE");
52 XBT_TEST_UNIT(test_controlflow) {
56 xbt_test0("basic nested control flow");
60 xbt_test_fail1("M1: n=%d (!= 1)", n);
64 xbt_test_fail1("M2: n=%d (!= 2)", n);
66 THROW0(unknown_error,0,"something");
69 xbt_test_fail1("M3: n=%d (!= 1)", n);
73 xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
77 xbt_test_fail1("M4: n=%d (!= 4)", n);
82 xbt_test_fail1("M5: n=%d (!= 5)", n);
85 XBT_TEST_UNIT(test_value) {
89 THROW0(unknown_error, 2, "toto");
91 xbt_test0("exception value passing");
92 if (ex.category != unknown_error)
93 xbt_test_fail1("category=%d (!= 1)", ex.category);
95 xbt_test_fail1("value=%d (!= 2)", ex.value);
96 if (strcmp(ex.msg,"toto"))
97 xbt_test_fail1("message=%s (!= toto)", ex.msg);
102 XBT_TEST_UNIT(test_variables) {
107 r1 = r2 = v1 = v2 = 1234;
111 THROW0(unknown_error, 0, "toto");
113 xbt_test0("variable preservation");
115 xbt_test_fail1("r1=%d (!= 1234)", r1);
117 xbt_test_fail1("v1=%d (!= 1234)", v1);
118 /* r2 is allowed to be destroyed because not volatile */
120 xbt_test_fail1("v2=%d (!= 5678)", v2);
125 XBT_TEST_UNIT(test_cleanup) {
130 xbt_test0("cleanup handling");
136 THROW0(1, 2, "blah");
139 xbt_test_fail1("v1 = %d (!= 5678)", v1);
143 xbt_test_fail1("v1 = %d (!= 5678)", v1);
144 if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
145 xbt_test_fail0("unexpected exception contents");
149 xbt_test_fail0("xbt_ex_free not executed");
152 int main(int argc, char *argv[]) {
153 xbt_test_suite_t suite;
155 suite = xbt_test_suite_new("Testsuite Autotest");
156 xbt_test_suite_push(suite, test_expected_failure, "expected failures");
158 suite = xbt_test_suite_new("Exception Handling");
159 xbt_test_suite_push(suite, test_controlflow, "basic nested control flow");
160 xbt_test_suite_push(suite, test_value, "exception value passing");
161 xbt_test_suite_push(suite, test_variables, "variable value preservation");
162 xbt_test_suite_push(suite, test_cleanup, "cleanup handling");
164 return xbt_test_run();
169 * The following is the example included in the documentation. It's a good
170 * idea to check its syntax even if we don't try to run it.
171 * And actually, it allows to put comments in the code despite doxygen.
173 static char *mallocex(int size) {
176 #define SMALLAMOUNT 10
177 #define TOOBIG 100000000
179 #if 0 /* this contains syntax errors, actually */
180 static void bad_example(void) {
181 struct {char*first;} *globalcontext;
186 char *cp1, *cp2, *cp3;
188 cp1 = mallocex(SMALLAMOUNT);
189 globalcontext->first = cp1;
190 cp2 = mallocex(TOOBIG);
191 cp3 = mallocex(SMALLAMOUNT);
195 if (cp3 != NULL) free(cp3);
196 if (cp2 != NULL) free(cp2);
197 if (cp1 != NULL) free(cp1);
199 printf("cp3=%s", cp3);
202 /* end_of_bad_example */
206 static void good_example(void) {
207 struct {char*first;} *globalcontext;
212 char * volatile /*03*/ cp1 = NULL /*02*/;
213 char * volatile /*03*/ cp2 = NULL /*02*/;
214 char * volatile /*03*/ cp3 = NULL /*02*/;
216 cp1 = mallocex(SMALLAMOUNT);
217 globalcontext->first = cp1;
218 cp1 = NULL /*05 give away*/;
219 cp2 = mallocex(TOOBIG);
220 cp3 = mallocex(SMALLAMOUNT);
224 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
229 /*05 cp1 was given away */
231 /*05 global context untouched */
235 /* end_of_good_example */