Logo AND Algorithmique Numérique Distribuée

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