Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a newline at the end of the file to make gcc happy
[simgrid.git] / src / ex_unit.c
1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
4
5 #include "xbt.h"
6 /*******************************/
7 /* GENERATED FILE, DO NOT EDIT */
8 /*******************************/
9
10 # 317 "xbt/ex.c" 
11 #include "xbt/ex.h"
12
13
14 XBT_TEST_UNIT("controlflow",test_controlflow, "basic nested control flow") {
15     xbt_ex_t ex;
16     volatile int n=1;
17
18     xbt_test_add0("basic nested control flow");
19
20     TRY {
21         if (n != 1)
22             xbt_test_fail1("M1: n=%d (!= 1)", n);
23         n++;
24         TRY {
25             if (n != 2)
26                 xbt_test_fail1("M2: n=%d (!= 2)", n);
27             n++;
28             THROW0(unknown_error,0,"something");
29         } CATCH (ex) {
30             if (n != 3)
31                 xbt_test_fail1("M3: n=%d (!= 3)", n);
32             n++;
33             xbt_ex_free(ex);
34         }
35         n++;
36         TRY {
37             if (n != 5)
38                 xbt_test_fail1("M2: n=%d (!= 5)", n);
39             n++;
40             THROW0(unknown_error,0,"something");
41         } CATCH (ex) {
42             if (n != 6)
43                 xbt_test_fail1("M3: n=%d (!= 6)", n);
44             n++;
45             RETHROW;
46             n++;
47         }
48         xbt_test_fail1("MX: n=%d (shouldn't reach this point)", n);
49     }
50     CATCH(ex) {
51         if (n != 7)
52             xbt_test_fail1("M4: n=%d (!= 7)", n);
53         n++;
54         xbt_ex_free(ex);
55     }
56     if (n != 8)
57         xbt_test_fail1("M5: n=%d (!= 8)", n);
58 }
59
60 XBT_TEST_UNIT("value",test_value,"exception value passing") {
61     xbt_ex_t ex;
62
63     TRY {
64         THROW0(unknown_error, 2, "toto");
65     } CATCH(ex) {
66         xbt_test_add0("exception value passing");
67         if (ex.category != unknown_error)
68             xbt_test_fail1("category=%d (!= 1)", ex.category);
69         if (ex.value != 2)
70             xbt_test_fail1("value=%d (!= 2)", ex.value);
71         if (strcmp(ex.msg,"toto"))
72             xbt_test_fail1("message=%s (!= toto)", ex.msg);
73         xbt_ex_free(ex);
74     }
75 }
76
77 XBT_TEST_UNIT("variables",test_variables,"variable value preservation") {
78     xbt_ex_t ex;
79     int r1, r2;
80     volatile int v1, v2;
81
82     r1 = r2 = v1 = v2 = 1234;
83     TRY {
84         r2 = 5678;
85         v2 = 5678;
86         THROW0(unknown_error, 0, "toto");
87     } CATCH(ex) {
88         xbt_test_add0("variable preservation");
89         if (r1 != 1234)
90             xbt_test_fail1("r1=%d (!= 1234)", r1);
91         if (v1 != 1234)
92             xbt_test_fail1("v1=%d (!= 1234)", v1);
93         /* r2 is allowed to be destroyed because not volatile */
94         if (v2 != 5678)
95             xbt_test_fail1("v2=%d (!= 5678)", v2);
96         xbt_ex_free(ex);
97     }
98 }
99
100 XBT_TEST_UNIT("cleanup",test_cleanup,"cleanup handling") {
101     xbt_ex_t ex;
102     volatile int v1;
103     int c;
104
105     xbt_test_add0("cleanup handling");
106
107     v1 = 1234;
108     c = 0;
109     TRY {
110         v1 = 5678;
111         THROW0(1, 2, "blah");
112     } CLEANUP {
113         if (v1 != 5678)
114             xbt_test_fail1("v1 = %d (!= 5678)", v1);
115         c = 1;
116     } CATCH(ex) {
117         if (v1 != 5678)
118             xbt_test_fail1("v1 = %d (!= 5678)", v1);
119         if (!(ex.category == 1 && ex.value == 2 && !strcmp(ex.msg,"blah")))
120             xbt_test_fail0("unexpected exception contents");
121         xbt_ex_free(ex);
122     }
123     if (!c)
124         xbt_test_fail0("xbt_ex_free not executed");
125 }
126
127
128 /*
129  * The following is the example included in the documentation. It's a good 
130  * idea to check its syntax even if we don't try to run it.
131  * And actually, it allows to put comments in the code despite doxygen.
132  */ 
133 static char *mallocex(int size) {
134   return NULL;
135 }
136 #define SMALLAMOUNT 10
137 #define TOOBIG 100000000
138
139 #if 0 /* this contains syntax errors, actually */
140 static void bad_example(void) {
141   struct {char*first;} *globalcontext;
142   ex_t ex;
143
144   /* BAD_EXAMPLE */
145   TRY {
146     char *cp1, *cp2, *cp3;
147     
148     cp1 = mallocex(SMALLAMOUNT);
149     globalcontext->first = cp1;
150     cp2 = mallocex(TOOBIG);
151     cp3 = mallocex(SMALLAMOUNT);
152     strcpy(cp1, "foo");
153     strcpy(cp2, "bar");
154   } CLEANUP {
155     if (cp3 != NULL) free(cp3);
156     if (cp2 != NULL) free(cp2);
157     if (cp1 != NULL) free(cp1);
158   } CATCH(ex) {
159     printf("cp3=%s", cp3);
160     RETHROW;
161   }
162   /* end_of_bad_example */
163 }
164 #endif
165 typedef struct {char *first;} global_context_t;
166    
167 static void good_example(void) {
168   global_context_t *global_context=malloc(sizeof(global_context_t));
169   xbt_ex_t ex;
170
171   /* GOOD_EXAMPLE */
172   { /*01*/
173     char * volatile /*03*/ cp1 = NULL /*02*/;
174     char * volatile /*03*/ cp2 = NULL /*02*/;
175     char * volatile /*03*/ cp3 = NULL /*02*/;
176     TRY {
177       cp1 = mallocex(SMALLAMOUNT);
178       global_context->first = cp1;
179       cp1 = NULL /*05 give away*/;
180       cp2 = mallocex(TOOBIG);
181       cp3 = mallocex(SMALLAMOUNT);
182       strcpy(cp1, "foo");
183       strcpy(cp2, "bar");
184     } CLEANUP { /*04*/
185       printf("cp3=%s", cp3 == NULL /*02*/ ? "" : cp3);
186       if (cp3 != NULL)
187         free(cp3);
188       if (cp2 != NULL)
189         free(cp2);
190       /*05 cp1 was given away */
191     } CATCH(ex) {
192       /*05 global context untouched */
193       RETHROW;
194     }
195   }
196   /* end_of_good_example */
197 }
198 /*******************************/
199 /* GENERATED FILE, DO NOT EDIT */
200 /*******************************/
201