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
31 #include "xbt/cunit.h"
35 XBT_LOG_NEW_CATEGORY(test,"This test");
37 XBT_TEST_UNIT(test_expected_failure) {
38 xbt_test0("Skipped test");
41 xbt_test0("EXPECTED FAILURE");
42 xbt_test_expect_failure();
43 xbt_test_log2("%s %s","Test","log");
44 xbt_test_fail0("EXPECTED FAILURE");
47 XBT_TEST_UNIT(test_controlflow) {
51 xbt_test0("basic nested control flow");
55 xbt_test_fail1("M1: n=%d (!= 1)", n);
59 xbt_test_fail1("M2: n=%d (!= 2)", n);
61 THROW0(unknown_error,0,"something");
64 xbt_test_fail1("M3: n=%d (!= 1)", n);
68 xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
72 xbt_test_fail1("M4: n=%d (!= 4)", n);
77 xbt_test_fail1("M5: n=%d (!= 5)", n);
80 XBT_TEST_UNIT(test_value) {
84 THROW0(unknown_error, 2, "toto");
86 xbt_test0("exception value passing");
87 if (ex.category != unknown_error)
88 xbt_test_fail1("category=%d (!= 1)", ex.category);
90 xbt_test_fail1("value=%d (!= 2)", ex.value);
91 if (strcmp(ex.msg,"toto"))
92 xbt_test_fail1("message=%s (!= toto)", ex.msg);
97 XBT_TEST_UNIT(test_variables) {
102 r1 = r2 = v1 = v2 = 1234;
106 THROW0(unknown_error, 0, "toto");
108 xbt_test0("variable preservation");
110 xbt_test_fail1("r1=%d (!= 1234)", r1);
112 xbt_test_fail1("v1=%d (!= 1234)", v1);
113 /* r2 is allowed to be destroyed because not volatile */
115 xbt_test_fail1("v2=%d (!= 5678)", v2);
120 XBT_TEST_UNIT(test_cleanup) {
125 xbt_test0("cleanup handling");
131 THROW0(1, 2, "blah");
134 xbt_test_fail1("v1 = %d (!= 5678)", v1);
138 xbt_test_fail1("v1 = %d (!= 5678)", v1);
139 if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
140 xbt_test_fail0("unexpected exception contents");
144 xbt_test_fail0("xbt_ex_free not executed");
147 int main(int argc, char *argv[]) {
148 xbt_test_suite_t suite;
150 suite = xbt_test_suite_new("Testsuite Autotest");
151 xbt_test_suite_push(suite, test_expected_failure, "expected failures");
153 suite = xbt_test_suite_new("Exception Handling");
154 xbt_test_suite_push(suite, test_controlflow, "basic nested control flow");
155 xbt_test_suite_push(suite, test_value, "exception value passing");
156 xbt_test_suite_push(suite, test_variables, "variable value preservation");
157 xbt_test_suite_push(suite, test_cleanup, "cleanup handling");
159 return xbt_test_run();
164 * The following is the example included in the documentation. It's a good
165 * idea to check its syntax even if we don't try to run it.
166 * And actually, it allows to put comments in the code despite doxygen.
168 static char *mallocex(int size) {
171 #define SMALLAMOUNT 10
172 #define TOOBIG 100000000
174 #if 0 /* this contains syntax errors, actually */
175 static void bad_example(void) {
176 struct {char*first;} *globalcontext;
181 char *cp1, *cp2, *cp3;
183 cp1 = mallocex(SMALLAMOUNT);
184 globalcontext->first = cp1;
185 cp2 = mallocex(TOOBIG);
186 cp3 = mallocex(SMALLAMOUNT);
190 if (cp3 != NULL) free(cp3);
191 if (cp2 != NULL) free(cp2);
192 if (cp1 != NULL) free(cp1);
194 printf("cp3=%s", cp3);
197 /* end_of_bad_example */
201 static void good_example(void) {
202 struct {char*first;} *globalcontext;
207 char * volatile /*03*/ cp1 = NULL /*02*/;
208 char * volatile /*03*/ cp2 = NULL /*02*/;
209 char * volatile /*03*/ cp3 = NULL /*02*/;
211 cp1 = mallocex(SMALLAMOUNT);
212 globalcontext->first = cp1;
213 cp1 = NULL /*05 give away*/;
214 cp2 = mallocex(TOOBIG);
215 cp3 = mallocex(SMALLAMOUNT);
219 printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
224 /*05 cp1 was given away */
226 /*05 global context untouched */
230 /* end_of_good_example */