head 1.16; access; symbols EX_1_0_6:1.16 LMTP2NNTP_1_4_1:1.14 LMTP2NNTP_1_4_0:1.14 EX_1_0_5:1.14 LMTP2NNTP_1_3_0:1.13 LMTP2NNTP_1_3b2:1.13 LMTP2NNTP_1_3b1:1.13 LMTP2NNTP_1_3a3:1.13 LMTP2NNTP_1_3a2:1.13 LMTP2NNTP_1_3a1:1.13 EX_1_0_4:1.13 EX_1_0_3:1.13 OSSP_RC_0_7_3:1.12 OSSP_RC_0_7_2:1.12 OSSP_RC_0_7_1:1.12 OSSP_RC_ALPHA_06:1.12 OSSP_RC_EXBROKEN:1.12 LMTP2NNTP_1_2_0:1.12 LMTP2NNTP_1_2b4:1.12 LMTP2NNTP_1_2b3:1.12 LMTP2NNTP_1_2b2:1.12 LMTP2NNTP_1_2b1:1.12 LMTP2NNTP_1_2a8:1.12 LMTP2NNTP_1_2a7:1.12 EX_1_0_2:1.12 EX_1_0_1:1.11 LMTP2NNTP_1_2a6:1.10 LMTP2NNTP_1_2a5:1.10 EX_1_0_0:1.10 LMTP2NNTP_1_2a4:1.9 EX_0_9_1:1.9 LMTP2NNTP_1_2a3:1.8 EX_0_9_0:1.8; locks; strict; comment @ * @; 1.16 date 2007.10.12.19.59.57; author rse; state Exp; branches; next 1.15; commitid 9SN7ivPOxnsC7lBs; 1.15 date 2006.08.10.19.49.33; author rse; state Exp; branches; next 1.14; commitid o4A6XFppdzD0PkIr; 1.14 date 2005.01.30.13.39.07; author rse; state Exp; branches; next 1.13; 1.13 date 2004.03.25.19.01.44; author rse; state Exp; branches; next 1.12; 1.12 date 2003.01.30.11.04.42; author rse; state Exp; branches; next 1.11; 1.11 date 2003.01.06.15.31.24; author rse; state Exp; branches; next 1.10; 1.10 date 2002.03.30.18.56.02; author rse; state Exp; branches; next 1.9; 1.9 date 2002.03.07.21.30.10; author rse; state Exp; branches; next 1.8; 1.8 date 2002.01.30.10.40.07; author rse; state Exp; branches; next 1.7; 1.7 date 2002.01.30.10.33.36; author rse; state Exp; branches; next 1.6; 1.6 date 2002.01.26.22.45.37; author rse; state Exp; branches; next 1.5; 1.5 date 2002.01.26.22.42.46; author rse; state Exp; branches; next 1.4; 1.4 date 2002.01.26.22.35.02; author rse; state Exp; branches; next 1.3; 1.3 date 2002.01.26.22.30.19; author rse; state Exp; branches; next 1.2; 1.2 date 2002.01.25.22.23.17; author rse; state Exp; branches; next 1.1; 1.1 date 2002.01.25.15.25.51; author rse; state Exp; branches; next ; desc @@ 1.16 log @prepare for release @ text @/* ** OSSP ex - Exception Handling ** Copyright (c) 2002-2007 Ralf S. Engelschall ** Copyright (c) 2002-2007 The OSSP Project ** ** This file is part of OSSP ex, an exception handling library ** which can be found at http://www.ossp.org/pkg/lib/ex/. ** ** Permission to use, copy, modify, and distribute this software for ** any purpose with or without fee is hereby granted, provided that ** the above copyright notice and this permission notice appear in all ** copies. ** ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** ex_test.c: exception handling test suite */ #include #include #include #include "ts.h" #include "ex.h" TS_TEST(test_controlflow) { ex_t ex; volatile int n; ts_test_check(TS_CTX, "basic nested control flow"); n = 1; ex_try { if (n != 1) ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n); n++; ex_try { if (n != 2) ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n); n++; ex_throw(0, 0, 0); } ex_catch (ex) { if (n != 3) ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n); n++; ex_rethrow; } ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n); } ex_catch (ex) { if (n != 4) ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n); n++; } if (n != 5) ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n); } TS_TEST(test_value) { ex_t ex; ex_try { ex_throw(1, 2, 3); } ex_catch (ex) { ts_test_check(TS_CTX, "exception value passing"); if (ex.ex_class != (void *)1) ts_test_fail(TS_CTX, "ex_class=0x%lx (!= 1)", (long)ex.ex_class); if (ex.ex_object != (void *)2) ts_test_fail(TS_CTX, "ex_object=0x%lx (!= 2)", (long)ex.ex_object); if (ex.ex_value != (void *)3) ts_test_fail(TS_CTX, "ex_value=0x%lx (!= 3)", (long)ex.ex_value); } } TS_TEST(test_variables) { ex_t ex; int r1, r2; volatile int v1, v2; r1 = r2 = v1 = v2 = 1234; ex_try { r2 = 5678; v2 = 5678; ex_throw(0, 0, 0); } ex_catch (ex) { ts_test_check(TS_CTX, "variable preservation"); if (r1 != 1234) ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1); if (v1 != 1234) ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1); /* r2 is allowed to be destroyed because not volatile */ if (v2 != 5678) ts_test_fail(TS_CTX, "v2=%d (!= 5678)", v2); } } TS_TEST(test_defer) { ex_t ex; volatile int i1 = 0; volatile int i2 = 0; volatile int i3 = 0; ts_test_check(TS_CTX, "exception deferring"); if (ex_deferring) ts_test_fail(TS_CTX, "unexpected deferring scope"); ex_try { ex_defer { if (!ex_deferring) ts_test_fail(TS_CTX, "unexpected non-deferring scope"); ex_defer { i1 = 1; ex_throw(0, 0, 4711); i2 = 2; ex_throw(0, 0, 0); i3 = 3; ex_throw(0, 0, 0); } ex_throw(0, 0, 0); } ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing"); } ex_catch (ex) { if ((long)ex.ex_value != (long)4711) ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", (long)ex.ex_value); } if (i1 != 1) ts_test_fail(TS_CTX, "v.i1 not set (expected 1, got %d)", i1); if (i2 != 2) ts_test_fail(TS_CTX, "v.i2 not set (expected 2, got %d)", i2); if (i3 != 3) ts_test_fail(TS_CTX, "v.i3 not set (expected 3, got %d)", i3); } TS_TEST(test_shield) { ex_t ex; ts_test_check(TS_CTX, "exception shielding"); if (ex_shielding) ts_test_fail(TS_CTX, "unexpected shielding scope"); if (ex_catching) ts_test_fail(TS_CTX, "unexpected catching scope"); ex_try { ex_shield { if (!ex_shielding) ts_test_fail(TS_CTX, "unexpected non-shielding scope"); ex_throw(0, 0, 0); } if (ex_shielding) ts_test_fail(TS_CTX, "unexpected shielding scope"); if (!ex_catching) ts_test_fail(TS_CTX, "unexpected non-catching scope"); } ex_catch (ex) { ts_test_fail(TS_CTX, "unexpected exception catched"); if (ex_catching) ts_test_fail(TS_CTX, "unexpected catching scope"); } if (ex_catching) ts_test_fail(TS_CTX, "unexpected catching scope"); } TS_TEST(test_cleanup) { ex_t ex; volatile int v1; int c; ts_test_check(TS_CTX, "cleanup handling"); v1 = 1234; c = 0; ex_try { v1 = 5678; ex_throw(1, 2, 3); } ex_cleanup { if (v1 != 5678) ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1); c = 1; } ex_catch (ex) { if (v1 != 5678) ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1); if (!(ex.ex_class == (void *)1 && ex.ex_object == (void *)2 && ex.ex_value == (void *)3)) ts_test_fail(TS_CTX, "unexpected exception contents"); } if (!c) ts_test_fail(TS_CTX, "ex_cleanup not executed"); } int main(int argc, char *argv[]) { ts_suite_t *ts; int n; ts = ts_suite_new("OSSP ex (Exception Handling)"); ts_suite_test(ts, test_controlflow, "basic nested control flow"); ts_suite_test(ts, test_value, "exception value passing"); ts_suite_test(ts, test_variables, "variable value preservation"); ts_suite_test(ts, test_defer, "exception deferring"); ts_suite_test(ts, test_shield, "exception shielding"); ts_suite_test(ts, test_cleanup, "cleanup handling"); n = ts_suite_run(ts); ts_suite_free(ts); return n; } @ 1.15 log @cleanup source tree @ text @d3 2 a4 2 ** Copyright (c) 2002-2006 Ralf S. Engelschall ** Copyright (c) 2002-2006 The OSSP Project @ 1.14 log @Adjusted copyright messages to include new year 2005. @ text @d3 2 a4 3 ** Copyright (c) 2002-2005 Ralf S. Engelschall ** Copyright (c) 2002-2005 The OSSP Project ** Copyright (c) 2002-2005 Cable & Wireless @ 1.13 log @bump and adjust copyright @ text @d3 3 a5 3 ** Copyright (c) 2002-2004 Ralf S. Engelschall ** Copyright (c) 2002-2004 The OSSP Project ** Copyright (c) 2002-2004 Cable & Wireless @ 1.12 log @final polishing @ text @d3 3 a5 3 ** Copyright (c) 2002-2003 Ralf S. Engelschall ** Copyright (c) 2002-2003 The OSSP Project ** Copyright (c) 2002-2003 Cable & Wireless Germany @ 1.11 log @- consistently use "Exception Handling" - strip trailing whitespaces - adjust copyright for new year 2003 - consistently use OSSP ASCII-art @ text @d5 1 a5 1 ** Copyright (c) 2002-2003 Cable & Wireless Deutschland @ 1.10 log @bump version and fix URLs @ text @d2 4 a5 4 ** OSSP ex - Exception Handling Library ** Copyright (c) 2002 Ralf S. Engelschall ** Copyright (c) 2002 The OSSP Project ** Copyright (c) 2002 Cable & Wireless Deutschland @ 1.9 log @Implement one of the coolest things since sliced bread: deferred exceptions. This allows one to more conviniently program the allocation (and the freeing in case of an error) of multiple (independent) resources. @ text @d8 1 a8 1 ** which can be found at http://www.ossp.org/pkg/ex/. @ 1.8 log @resolve name space mapping conflict @ text @d114 38 d219 1 @ 1.7 log @add ex_cleanup clause to ex_try/ex_catch, modelled after Java's "finally" @ text @d147 1 a147 1 int cleanup; d152 1 a152 1 cleanup = 0; d160 1 a160 1 cleanup = 1; d168 1 a168 1 if (!cleanup) @ 1.6 log @add ex_catching to test suite @ text @d143 29 d182 1 @ 1.5 log @- rename ex_shielded to ex_shielding - do not provide namespace mapping for ex_shielding - add ex_catching for testing whether someone is trying to catch exceptions @ text @d121 2 d131 2 d136 2 d139 2 @ 1.4 log @fix license messages @ text @d119 2 a120 2 if (ex_shielded) ts_test_fail(TS_CTX, "unexpected shielded scope"); d123 2 a124 2 if (!ex_shielded) ts_test_fail(TS_CTX, "unexpected non-shielded scope"); d127 2 a128 2 if (ex_shielded) ts_test_fail(TS_CTX, "unexpected shielded scope"); @ 1.3 log @first cut for a real test suite @ text @d10 4 a13 4 ** This program is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License ** as published by the Free Software Foundation; either version ** 2.0 of the License, or (at your option) any later version. d15 12 a26 9 ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this file; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 ** USA, or contact the OSSP project . @ 1.2 log @many many polishing and extending @ text @d1 26 d32 1 d35 4 a38 1 /* === foo.h === */ d40 28 a67 6 typedef enum { FOO_OK = 0, FOO_ERR_1, FOO_ERR_2, FOO_ERR_3 } foo_rc_t; d69 3 a71 1 #define FOO_TEXAS d73 13 a85 14 #ifdef FOO_TEXAS #include "ex.h" #define FOO_ERR(err) \ (ex_throw(FOO_CLASS, NULL, FOO_ERR_##err), FOO_ERR_##err) #else #define FOO_ERR(err) \ (FOO_ERR_##err) #endif #define FOO_EX2RC(e) \ ((foo_rc_t)((e)->ex_value)) #define FOO_CLASS (&foo_class) extern int foo_class; d87 1 a87 7 foo_rc_t foo_func(void); /* === foo.c === */ int foo_class; static int foo_sub(void) d89 20 a108 4 fprintf(stderr, "mark4\n"); if (random() % 2) return FOO_OK; return FOO_ERR(1); d111 1 a111 1 foo_rc_t foo_func(void) d113 17 a129 5 fprintf(stderr, "mark3\n"); foo_sub(); if (random() % 2) return FOO_OK; return FOO_ERR(2); a131 2 /* === test.c === */ d134 2 a135 2 srandom((unsigned int)time(NULL)); ex_t e; d137 8 a144 43 fprintf(stderr, "main-0\n"); for (;;) { fprintf(stderr, "main-1\n"); try { fprintf(stderr, "main-2\n"); ex_shield { foo_func(); } fprintf(stderr, "main-3\n"); for (;;) { fprintf(stderr, "main-4\n"); try { fprintf(stderr, "main-5\n"); foo_func(); fprintf(stderr, "main-6\n"); } catch (e) { fprintf(stderr, "main-7\n"); fprintf(stderr, " %s@@%s:%d class=0x%lx object=0x%lx value=0x%lx\n", e.ex_func, e.ex_file, e.ex_line, (long)e.ex_class, (long)e.ex_object, (long)e.ex_value); if (e.ex_class == FOO_CLASS && (foo_rc_t)(e.ex_value) == FOO_ERR_1) fprintf(stderr, " exception handling\n"); else { fprintf(stderr, " exception rethrowing\n"); ex_rethrow; } fprintf(stderr, "main-8\n"); } fprintf(stderr, "main-9\n"); } fprintf(stderr, "main-10\n"); } catch (e) { fprintf(stderr, "main-11\n"); fprintf(stderr, " %s@@%s:%d class=0x%lx object=0x%lx value=0x%lx\n", e.ex_func, e.ex_file, e.ex_line, (long)e.ex_class, (long)e.ex_object, (long)e.ex_value); fprintf(stderr, "main-12\n"); if (e.ex_class == FOO_CLASS && (foo_rc_t)(e.ex_value) == FOO_ERR_2) break; } } fprintf(stderr, "main-0\n"); return 0; @ 1.1 log @Welcome OSSP ex, another new-born OSSP library. This is the first cut for a C exception handling library. Read ex.pod for details... @ text @d63 1 a63 1 d69 3 a71 1 foo_func(); d101 2 @