Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
follow the lastest macro renaming in xbt_ex
[simgrid.git] / testsuite / xbt / ex_test.c
index f0491ef..f242e23 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <string.h>
 
 #include "ex_test_ts.h"
 #include "xbt/ex.h"
@@ -42,25 +43,24 @@ TS_TEST(test_controlflow)
 
     ts_test_check(TS_CTX, "basic nested control flow");
     n = 1;
-    ex_try {
+    TRY {
         if (n != 1)
             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
         n++;
-        ex_try {
+        TRY {
             if (n != 2)
                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
             n++;
-            ex_throw(0, 0, 0);
-        }
-        ex_catch (ex) {
+            THROW(0,0,"something");
+        } CATCH (ex) {
             if (n != 3)
                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
             n++;
-            ex_rethrow;
+            RETHROW;
         }
         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
     }
-    ex_catch (ex) {
+    CATCH(ex) {
         if (n != 4)
             ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
         n++;
@@ -73,17 +73,16 @@ TS_TEST(test_value)
 {
     ex_t ex;
 
-    ex_try {
-        ex_throw(1, 2, 3);
-    }
-    ex_catch (ex) {
+    TRY {
+        THROW(1, 2, "toto");
+    } 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);
+        if (ex.category != 1)
+            ts_test_fail(TS_CTX, "category=%d (!= 1)", ex.category);
+        if (ex.value != 2)
+            ts_test_fail(TS_CTX, "value=%d (!= 2)", ex.value);
+        if (strcmp(ex.msg,"toto"))
+            ts_test_fail(TS_CTX, "message=%s (!= toto)", ex.msg);
     }
 }
 
@@ -94,12 +93,11 @@ TS_TEST(test_variables)
     volatile int v1, v2;
 
     r1 = r2 = v1 = v2 = 1234;
-    ex_try {
+    TRY {
         r2 = 5678;
         v2 = 5678;
-        ex_throw(0, 0, 0);
-    }
-    ex_catch (ex) {
+        THROW(0, 0, 0);
+    } CATCH(ex) {
         ts_test_check(TS_CTX, "variable preservation");
         if (r1 != 1234)
             ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
@@ -119,27 +117,27 @@ TS_TEST(test_defer)
     volatile int i3 = 0;
 
     ts_test_check(TS_CTX, "exception deferring");
-    if (ex_deferring)
+    if (IS_DEFERRED)
         ts_test_fail(TS_CTX, "unexpected deferring scope");
-    ex_try {
-        ex_defer {
-            if (!ex_deferring)
+    TRY {
+        DEFER {
+            if (!IS_DEFERRED)
                 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
-            ex_defer {
+            DEFER {
                 i1 = 1;
-                ex_throw(0, 0, 4711);
+                THROW(4711, 0, NULL);
                 i2 = 2;
-                ex_throw(0, 0, 0);
+                THROW(0, 0, NULL);
                 i3 = 3;
-                ex_throw(0, 0, 0);
+                THROW(0, 0, NULL);
             }
-            ex_throw(0, 0, 0);
+            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);
+    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);
@@ -154,27 +152,26 @@ TS_TEST(test_shield)
     ex_t ex;
 
     ts_test_check(TS_CTX, "exception shielding");
-    if (ex_shielding)
+    if (IS_SHIELDED)
         ts_test_fail(TS_CTX, "unexpected shielding scope");
-    if (ex_catching)
+    if (IS_CATCHED)
         ts_test_fail(TS_CTX, "unexpected catching scope");
-    ex_try {
-        ex_shield {
-            if (!ex_shielding)
+    TRY {
+        SHIELD {
+            if (!IS_SHIELDED)
                 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
-            ex_throw(0, 0, 0);
+            THROW(0, 0, 0);
         }
-        if (ex_shielding)
+        if (IS_SHIELDED)
             ts_test_fail(TS_CTX, "unexpected shielding scope");
-        if (!ex_catching)
+        if (!IS_CATCHED)
             ts_test_fail(TS_CTX, "unexpected non-catching scope");
-    }
-    ex_catch (ex) {
+    } CATCH(ex) {
         ts_test_fail(TS_CTX, "unexpected exception catched");
-        if (ex_catching)
+        if (IS_CATCHED)
             ts_test_fail(TS_CTX, "unexpected catching scope");
     }
-    if (ex_catching)
+    if (IS_CATCHED)
         ts_test_fail(TS_CTX, "unexpected catching scope");
 }
 
@@ -188,19 +185,17 @@ TS_TEST(test_cleanup)
 
     v1 = 1234;
     c = 0;
-    ex_try {
+    TRY {
         v1 = 5678;
-        ex_throw(1, 2, 3);
-    }
-    ex_cleanup {
+        THROW(1, 2, "blah");
+    } CLEANUP {
         if (v1 != 5678)
             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
         c = 1;
-    }
-    ex_catch (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))
+        if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
             ts_test_fail(TS_CTX, "unexpected exception contents");
     }
     if (!c)
@@ -224,3 +219,73 @@ int main(int argc, char *argv[])
     return n;
 }
 
+
+/*
+ * 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;
+  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 */
+}