Logo AND Algorithmique Numérique Distribuée

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