--- ex_test.c 2002/01/25 22:23:17 1.2
+++ ex_test.c 2002/01/26 22:30:19 1.3
@@ -1,108 +1,146 @@
+/*
+** OSSP ex - Exception Handling Library
+** Copyright (c) 2002 Ralf S. Engelschall <rse@engelschall.com>
+** Copyright (c) 2002 The OSSP Project <http://www.ossp.org/>
+** Copyright (c) 2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
+**
+** This file is part of OSSP ex, an exception handling library
+** which can be found at http://www.ossp.org/pkg/ex/.
+**
+** 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.
+**
+** 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 <ossp@ossp.org>.
+**
+** ex_test.c: exception handling test suite
+*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include "ts.h"
#include "ex.h"
-/* === foo.h === */
-
-typedef enum {
- FOO_OK = 0,
- FOO_ERR_1,
- FOO_ERR_2,
- FOO_ERR_3
-} foo_rc_t;
-
-#define FOO_TEXAS
-
-#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;
+TS_TEST(test_controlflow)
+{
+ ex_t ex;
+ volatile int n;
-foo_rc_t foo_func(void);
+ 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);
+}
-/* === foo.c === */
+TS_TEST(test_value)
+{
+ ex_t ex;
-int foo_class;
+ 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);
+ }
+}
-static int foo_sub(void)
+TS_TEST(test_variables)
{
- fprintf(stderr, "mark4\n");
- if (random() % 2)
- return FOO_OK;
- return FOO_ERR(1);
+ 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);
+ }
}
-foo_rc_t foo_func(void)
+TS_TEST(test_shield)
{
- fprintf(stderr, "mark3\n");
- foo_sub();
- if (random() % 2)
- return FOO_OK;
- return FOO_ERR(2);
-}
+ ex_t ex;
-/* === test.c === */
+ ts_test_check(TS_CTX, "exception shielding");
+ if (ex_shielded)
+ ts_test_fail(TS_CTX, "unexpected shielded scope");
+ ex_try {
+ ex_shield {
+ if (!ex_shielded)
+ ts_test_fail(TS_CTX, "unexpected non-shielded scope");
+ ex_throw(0, 0, 0);
+ }
+ if (ex_shielded)
+ ts_test_fail(TS_CTX, "unexpected shielded scope");
+ }
+ ex_catch (ex) {
+ ts_test_fail(TS_CTX, "unexpected exception catched");
+ }
+}
int main(int argc, char *argv[])
{
- srandom((unsigned int)time(NULL));
- ex_t e;
+ ts_suite_t *ts;
+ int n;
- 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;
+ 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_shield, "exception shielding");
+ n = ts_suite_run(ts);
+ ts_suite_free(ts);
+ return n;
}
|