X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/4cc287260f3405196110824b01eeb9b53cb0ea65..0b5e43957fe16281ccee60ccccf064a0d4d0fec0:/testsuite/xbt/ex_test.c diff --git a/testsuite/xbt/ex_test.c b/testsuite/xbt/ex_test.c index 7302ad3388..78b2f5797d 100644 --- a/testsuite/xbt/ex_test.c +++ b/testsuite/xbt/ex_test.c @@ -28,200 +28,204 @@ ** ex_test.c: exception handling test suite */ -#include -#include -#include -#include - -#include "ex_test_ts.h" +#include "xbt/cunit.h" #include "xbt/ex.h" +#include "xbt/log.h" + +XBT_LOG_NEW_CATEGORY(test,"This test"); + +XBT_TEST_UNIT(test_expected_failure) { + xbt_test0("Skipped test"); + xbt_test_skip(); + + xbt_test0("EXPECTED FAILURE"); + xbt_test_expect_failure(); + xbt_test_log2("%s %s","Test","log"); + xbt_test_fail0("EXPECTED FAILURE"); +} + +XBT_TEST_UNIT(test_controlflow) { + xbt_ex_t ex; + volatile int n=1; -TS_TEST(test_controlflow) -{ - ex_t ex; - volatile int n; + xbt_test0("basic nested control flow"); - ts_test_check(TS_CTX, "basic nested control flow"); - n = 1; - xbt_try { + TRY { if (n != 1) - ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n); + xbt_test_fail1("M1: n=%d (!= 1)", n); n++; - xbt_try { + TRY { if (n != 2) - ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n); + xbt_test_fail1("M2: n=%d (!= 2)", n); n++; - xbt_throw(0,0,"something"); - } - xbt_catch (ex) { + THROW0(unknown_error,0,"something"); + } CATCH (ex) { if (n != 3) - ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n); + xbt_test_fail1("M3: n=%d (!= 1)", n); n++; - xbt_rethrow; + RETHROW; } - ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n); + xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n); } - xbt_catch (ex) { + CATCH(ex) { if (n != 4) - ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n); + xbt_test_fail1("M4: n=%d (!= 4)", n); n++; + xbt_ex_free(ex); } if (n != 5) - ts_test_fail(TS_CTX, "M5: n=%d (!= 5)", n); + xbt_test_fail1("M5: n=%d (!= 5)", n); } -TS_TEST(test_value) -{ - ex_t ex; +XBT_TEST_UNIT(test_value) { + xbt_ex_t ex; - xbt_try { - xbt_throw(1, 2, "toto"); - } - xbt_catch (ex) { - ts_test_check(TS_CTX, "exception value passing"); - if (ex.category != 1) - ts_test_fail(TS_CTX, "category=%d (!= 1)", ex.category); + TRY { + THROW0(unknown_error, 2, "toto"); + } CATCH(ex) { + xbt_test0("exception value passing"); + if (ex.category != unknown_error) + xbt_test_fail1("category=%d (!= 1)", ex.category); if (ex.value != 2) - ts_test_fail(TS_CTX, "value=%d (!= 2)", ex.value); + xbt_test_fail1("value=%d (!= 2)", ex.value); if (strcmp(ex.msg,"toto")) - ts_test_fail(TS_CTX, "message=%s (!= toto)", ex.msg); + xbt_test_fail1("message=%s (!= toto)", ex.msg); + xbt_ex_free(ex); } } -TS_TEST(test_variables) -{ - ex_t ex; +XBT_TEST_UNIT(test_variables) { + xbt_ex_t ex; int r1, r2; volatile int v1, v2; r1 = r2 = v1 = v2 = 1234; - xbt_try { + TRY { r2 = 5678; v2 = 5678; - xbt_throw(0, 0, 0); - } - xbt_catch (ex) { - ts_test_check(TS_CTX, "variable preservation"); + THROW0(unknown_error, 0, "toto"); + } CATCH(ex) { + xbt_test0("variable preservation"); if (r1 != 1234) - ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1); + xbt_test_fail1("r1=%d (!= 1234)", r1); if (v1 != 1234) - ts_test_fail(TS_CTX, "v1=%d (!= 1234)", v1); + xbt_test_fail1("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); + xbt_test_fail1("v2=%d (!= 5678)", v2); + xbt_ex_free(ex); } } -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 (xbt_deferring) - ts_test_fail(TS_CTX, "unexpected deferring scope"); - xbt_try { - xbt_defer { - if (!xbt_deferring) - ts_test_fail(TS_CTX, "unexpected non-deferring scope"); - xbt_defer { - i1 = 1; - xbt_throw(4711, 0, NULL); - i2 = 2; - xbt_throw(0, 0, NULL); - i3 = 3; - xbt_throw(0, 0, NULL); - } - xbt_throw(0, 0, 0); - } - ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing"); - } - xbt_catch (ex) { - if (ex.category != 4711) - ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", 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 (xbt_shielding) - ts_test_fail(TS_CTX, "unexpected shielding scope"); - if (xbt_catching) - ts_test_fail(TS_CTX, "unexpected catching scope"); - xbt_try { - xbt_shield { - if (!xbt_shielding) - ts_test_fail(TS_CTX, "unexpected non-shielding scope"); - xbt_throw(0, 0, 0); - } - if (xbt_shielding) - ts_test_fail(TS_CTX, "unexpected shielding scope"); - if (!xbt_catching) - ts_test_fail(TS_CTX, "unexpected non-catching scope"); - } - xbt_catch (ex) { - ts_test_fail(TS_CTX, "unexpected exception catched"); - if (xbt_catching) - ts_test_fail(TS_CTX, "unexpected catching scope"); - } - if (xbt_catching) - ts_test_fail(TS_CTX, "unexpected catching scope"); -} - -TS_TEST(test_cleanup) -{ - ex_t ex; +XBT_TEST_UNIT(test_cleanup) { + xbt_ex_t ex; volatile int v1; int c; - ts_test_check(TS_CTX, "cleanup handling"); + xbt_test0("cleanup handling"); v1 = 1234; c = 0; - xbt_try { + TRY { v1 = 5678; - xbt_throw(1, 2, "blah"); - } - xbt_cleanup { + THROW0(1, 2, "blah"); + } CLEANUP { if (v1 != 5678) - ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1); + xbt_test_fail1("v1 = %d (!= 5678)", v1); c = 1; - } - xbt_catch (ex) { + } CATCH(ex) { if (v1 != 5678) - ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1); + xbt_test_fail1("v1 = %d (!= 5678)", v1); if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah"))) - ts_test_fail(TS_CTX, "unexpected exception contents"); + xbt_test_fail0("unexpected exception contents"); + xbt_ex_free(ex); } if (!c) - ts_test_fail(TS_CTX, "ex_cleanup not executed"); + xbt_test_fail0("xbt_ex_free 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; +int main(int argc, char *argv[]) { + xbt_test_suite_t suite; + + suite = xbt_test_suite_new("Testsuite Autotest"); + xbt_test_suite_push(suite, test_expected_failure, "expected failures"); + + suite = xbt_test_suite_new("Exception Handling"); + xbt_test_suite_push(suite, test_controlflow, "basic nested control flow"); + xbt_test_suite_push(suite, test_value, "exception value passing"); + xbt_test_suite_push(suite, test_variables, "variable value preservation"); + xbt_test_suite_push(suite, test_cleanup, "cleanup handling"); + + return xbt_test_run(); } + +/* + * The following is the example included in the documentation. It's a good + * idea to check its syntax even if we don't try to run it. + * And actually, it allows to put comments in the code despite doxygen. + */ +static char *mallocex(int size) { + return NULL; +} +#define SMALLAMOUNT 10 +#define TOOBIG 100000000 + +#if 0 /* this contains syntax errors, actually */ +static void bad_example(void) { + struct {char*first;} *globalcontext; + ex_t ex; + + /* BAD_EXAMPLE */ + TRY { + char *cp1, *cp2, *cp3; + + cp1 = mallocex(SMALLAMOUNT); + globalcontext->first = cp1; + cp2 = mallocex(TOOBIG); + cp3 = mallocex(SMALLAMOUNT); + strcpy(cp1, "foo"); + strcpy(cp2, "bar"); + } CLEANUP { + if (cp3 != NULL) free(cp3); + if (cp2 != NULL) free(cp2); + if (cp1 != NULL) free(cp1); + } CATCH(ex) { + printf("cp3=%s", cp3); + RETHROW; + } + /* end_of_bad_example */ +} +#endif + +static void good_example(void) { + struct {char*first;} *globalcontext; + xbt_ex_t ex; + + /* GOOD_EXAMPLE */ + { /*01*/ + char * volatile /*03*/ cp1 = NULL /*02*/; + char * volatile /*03*/ cp2 = NULL /*02*/; + char * volatile /*03*/ cp3 = NULL /*02*/; + TRY { + cp1 = mallocex(SMALLAMOUNT); + globalcontext->first = cp1; + cp1 = NULL /*05 give away*/; + cp2 = mallocex(TOOBIG); + cp3 = mallocex(SMALLAMOUNT); + strcpy(cp1, "foo"); + strcpy(cp2, "bar"); + } CLEANUP { /*04*/ + printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3); + if (cp3 != NULL) + free(cp3); + if (cp2 != NULL) + free(cp2); + /*05 cp1 was given away */ + } CATCH(ex) { + /*05 global context untouched */ + RETHROW; + } + } + /* end_of_good_example */ +}