Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve documentation and put the examples in another file, so that we can include...
[simgrid.git] / testsuite / xbt / ex_test.c
index 66b63f7..0053bee 100644 (file)
@@ -43,25 +43,25 @@ TS_TEST(test_controlflow)
 
     ts_test_check(TS_CTX, "basic nested control flow");
     n = 1;
-    sg_try {
+    xbt_try {
         if (n != 1)
             ts_test_fail(TS_CTX, "M1: n=%d (!= 1)", n);
         n++;
-        sg_try {
+        xbt_try {
             if (n != 2)
                 ts_test_fail(TS_CTX, "M2: n=%d (!= 2)", n);
             n++;
-            sg_throw(0,0,"something");
+            xbt_throw(0,0,"something");
         }
-        sg_catch (ex) {
+        xbt_catch (ex) {
             if (n != 3)
                 ts_test_fail(TS_CTX, "M3: n=%d (!= 1)", n);
             n++;
-            sg_rethrow;
+            xbt_rethrow;
         }
         ts_test_fail(TS_CTX, "MX: n=%d (expected: not reached)", n);
     }
-    sg_catch (ex) {
+    xbt_catch (ex) {
         if (n != 4)
             ts_test_fail(TS_CTX, "M4: n=%d (!= 4)", n);
         n++;
@@ -74,13 +74,13 @@ TS_TEST(test_value)
 {
     ex_t ex;
 
-    sg_try {
-        sg_throw(1, 2, "toto");
+    xbt_try {
+        xbt_throw(1, 2, "toto");
     }
-    sg_catch (ex) {
+    xbt_catch (ex) {
         ts_test_check(TS_CTX, "exception value passing");
-        if (ex.code != 1)
-            ts_test_fail(TS_CTX, "code=%d (!= 1)", ex.code);
+        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"))
@@ -95,12 +95,12 @@ TS_TEST(test_variables)
     volatile int v1, v2;
 
     r1 = r2 = v1 = v2 = 1234;
-    sg_try {
+    xbt_try {
         r2 = 5678;
         v2 = 5678;
-        sg_throw(0, 0, 0);
+        xbt_throw(0, 0, 0);
     }
-    sg_catch (ex) {
+    xbt_catch (ex) {
         ts_test_check(TS_CTX, "variable preservation");
         if (r1 != 1234)
             ts_test_fail(TS_CTX, "r1=%d (!= 1234)", r1);
@@ -120,26 +120,26 @@ TS_TEST(test_defer)
     volatile int i3 = 0;
 
     ts_test_check(TS_CTX, "exception deferring");
-    if (sg_deferring)
+    if (xbt_deferring)
         ts_test_fail(TS_CTX, "unexpected deferring scope");
-    sg_try {
-        sg_defer {
-            if (!sg_deferring)
+    xbt_try {
+        xbt_defer {
+            if (!xbt_deferring)
                 ts_test_fail(TS_CTX, "unexpected non-deferring scope");
-            sg_defer {
+            xbt_defer {
                 i1 = 1;
-                sg_throw(4711, 0, NULL);
+                xbt_throw(4711, 0, NULL);
                 i2 = 2;
-                sg_throw(0, 0, NULL);
+                xbt_throw(0, 0, NULL);
                 i3 = 3;
-                sg_throw(0, 0, NULL);
+                xbt_throw(0, 0, NULL);
             }
-            sg_throw(0, 0, 0);
+            xbt_throw(0, 0, 0);
         }
         ts_test_fail(TS_CTX, "unexpected not occurred deferred throwing");
     }
-    sg_catch (ex) {
-        if (ex.code != 4711)
+    xbt_catch (ex) {
+        if (ex.category != 4711)
             ts_test_fail(TS_CTX, "caught exception with value %d, expected 4711", ex.value);
     }
     if (i1 != 1)
@@ -155,27 +155,27 @@ TS_TEST(test_shield)
     ex_t ex;
 
     ts_test_check(TS_CTX, "exception shielding");
-    if (sg_shielding)
+    if (xbt_shielding)
         ts_test_fail(TS_CTX, "unexpected shielding scope");
-    if (sg_catching)
+    if (xbt_catching)
         ts_test_fail(TS_CTX, "unexpected catching scope");
-    sg_try {
-        sg_shield {
-            if (!sg_shielding)
+    xbt_try {
+        xbt_shield {
+            if (!xbt_shielding)
                 ts_test_fail(TS_CTX, "unexpected non-shielding scope");
-            sg_throw(0, 0, 0);
+            xbt_throw(0, 0, 0);
         }
-        if (sg_shielding)
+        if (xbt_shielding)
             ts_test_fail(TS_CTX, "unexpected shielding scope");
-        if (!sg_catching)
+        if (!xbt_catching)
             ts_test_fail(TS_CTX, "unexpected non-catching scope");
     }
-    sg_catch (ex) {
+    xbt_catch (ex) {
         ts_test_fail(TS_CTX, "unexpected exception catched");
-        if (sg_catching)
+        if (xbt_catching)
             ts_test_fail(TS_CTX, "unexpected catching scope");
     }
-    if (sg_catching)
+    if (xbt_catching)
         ts_test_fail(TS_CTX, "unexpected catching scope");
 }
 
@@ -189,19 +189,19 @@ TS_TEST(test_cleanup)
 
     v1 = 1234;
     c = 0;
-    sg_try {
+    xbt_try {
         v1 = 5678;
-        sg_throw(1, 2, "blah");
+        xbt_throw(1, 2, "blah");
     }
-    sg_cleanup {
+    xbt_cleanup {
         if (v1 != 5678)
             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
         c = 1;
     }
-    sg_catch (ex) {
+    xbt_catch (ex) {
         if (v1 != 5678)
             ts_test_fail(TS_CTX, "v1 = %d (!= 5678)", v1);
-        if (!(ex.code == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
+        if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
             ts_test_fail(TS_CTX, "unexpected exception contents");
     }
     if (!c)
@@ -225,3 +225,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 */
+  xbt_try {
+    char *cp1, *cp2, *cp3;
+    
+    cp1 = mallocex(SMALLAMOUNT);
+    globalcontext->first = cp1;
+    cp2 = mallocex(TOOBIG);
+    cp3 = mallocex(SMALLAMOUNT);
+    strcpy(cp1, "foo");
+    strcpy(cp2, "bar");
+  } xbt_cleanup {
+    if (cp3 != NULL) free(cp3);
+    if (cp2 != NULL) free(cp2);
+    if (cp1 != NULL) free(cp1);
+  } xbt_catch(ex) {
+    printf("cp3=%s", cp3);
+    xbt_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*/;
+    xbt_try {
+      cp1 = mallocex(SMALLAMOUNT);
+      globalcontext->first = cp1;
+      cp1 = NULL /*05 give away*/;
+      cp2 = mallocex(TOOBIG);
+      cp3 = mallocex(SMALLAMOUNT);
+      strcpy(cp1, "foo");
+      strcpy(cp2, "bar");
+    } xbt_cleanup { /*04*/
+      printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
+      if (cp3 != NULL)
+       free(cp3);
+      if (cp2 != NULL)
+       free(cp2);
+      /*05 cp1 was given away */
+    } xbt_catch(ex) {
+      /*05 global context untouched */
+      xbt_rethrow;
+    }
+  }
+  /* end_of_good_example */
+}