Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renaming some tracing functions to make easy to remember what they do
[simgrid.git] / src / dynar_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 # 691 "/home/navarrop/Developments/simgrid/src/xbt/dynar.c" 
12
13 #define NB_ELEM 5000
14
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
16 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
17
18 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
19 {
20   /* Vars_decl [doxygen cruft] */
21   xbt_dynar_t d;
22   int i, cpt;
23   unsigned int cursor;
24   int *iptr;
25
26   xbt_test_add0("==== Traverse the empty dynar");
27   d = xbt_dynar_new(sizeof(int), NULL);
28   xbt_dynar_foreach(d, cursor, i) {
29     xbt_assert0(0, "Damnit, there is something in the empty dynar");
30   }
31   xbt_dynar_free(&d);
32   xbt_dynar_free(&d);
33
34   xbt_test_add1
35     ("==== Push %d int, set them again 3 times, traverse them, shift them",
36      NB_ELEM);
37   /* Populate_ints [doxygen cruft] */
38   /* 1. Populate the dynar */
39   d = xbt_dynar_new(sizeof(int), NULL);
40   for (cpt = 0; cpt < NB_ELEM; cpt++) {
41     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
42     /* xbt_dynar_push(d,&cpt);       This would also work */
43     xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
44   }
45
46   /* 2. Traverse manually the dynar */
47   for (cursor = 0; cursor < NB_ELEM; cursor++) {
48     iptr = xbt_dynar_get_ptr(d, cursor);
49     xbt_test_assert2(cursor == *iptr,
50                      "The retrieved value is not the same than the injected one (%d!=%d)",
51                      cursor, cpt);
52   }
53
54   /* 3. Traverse the dynar using the neat macro to that extend */
55   xbt_dynar_foreach(d, cursor, cpt) {
56     xbt_test_assert2(cursor == cpt,
57                      "The retrieved value is not the same than the injected one (%d!=%d)",
58                      cursor, cpt);
59   }
60   /* end_of_traversal */
61
62   for (cpt = 0; cpt < NB_ELEM; cpt++)
63     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
64
65   for (cpt = 0; cpt < NB_ELEM; cpt++)
66     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
67   /*     xbt_dynar_set(d,cpt,&cpt); */
68
69   for (cpt = 0; cpt < NB_ELEM; cpt++)
70     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
71
72   cpt = 0;
73   xbt_dynar_foreach(d, cursor, i) {
74     xbt_test_assert2(i == cpt,
75                      "The retrieved value is not the same than the injected one (%d!=%d)",
76                      i, cpt);
77     cpt++;
78   }
79   xbt_test_assert2(cpt == NB_ELEM,
80                    "Cannot retrieve my %d values. Last got one is %d",
81                    NB_ELEM, cpt);
82
83   /* shifting [doxygen cruft] */
84   /* 4. Shift all the values */
85   for (cpt = 0; cpt < NB_ELEM; cpt++) {
86     xbt_dynar_shift(d, &i);
87     xbt_test_assert2(i == cpt,
88                      "The retrieved value is not the same than the injected one (%d!=%d)",
89                      i, cpt);
90     xbt_test_log2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
91   }
92
93   /* 5. Free the resources */
94   xbt_dynar_free(&d);
95   xbt_dynar_free(&d);
96
97
98   xbt_test_add1("==== Unshift/pop %d int", NB_ELEM);
99   d = xbt_dynar_new(sizeof(int), NULL);
100   for (cpt = 0; cpt < NB_ELEM; cpt++) {
101     xbt_dynar_unshift(d, &cpt);
102     DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
103   }
104   for (cpt = 0; cpt < NB_ELEM; cpt++) {
105     i = xbt_dynar_pop_as(d, int);
106     xbt_test_assert2(i == cpt,
107                      "The retrieved value is not the same than the injected one (%d!=%d)",
108                      i, cpt);
109     xbt_test_log2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
110   }
111   xbt_dynar_free(&d);
112   xbt_dynar_free(&d);
113
114
115   xbt_test_add1
116     ("==== Push %d int, insert 1000 int in the middle, shift everything",
117      NB_ELEM);
118   d = xbt_dynar_new(sizeof(int), NULL);
119   for (cpt = 0; cpt < NB_ELEM; cpt++) {
120     xbt_dynar_push_as(d, int, cpt);
121     DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
122   }
123   for (cpt = 0; cpt < 1000; cpt++) {
124     xbt_dynar_insert_at_as(d, 2500, int, cpt);
125     DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
126   }
127
128   for (cpt = 0; cpt < 2500; cpt++) {
129     xbt_dynar_shift(d, &i);
130     xbt_test_assert2(i == cpt,
131                      "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
132                      i, cpt);
133     DEBUG2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
134   }
135   for (cpt = 999; cpt >= 0; cpt--) {
136     xbt_dynar_shift(d, &i);
137     xbt_test_assert2(i == cpt,
138                      "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
139                      i, cpt);
140   }
141   for (cpt = 2500; cpt < NB_ELEM; cpt++) {
142     xbt_dynar_shift(d, &i);
143     xbt_test_assert2(i == cpt,
144                      "The retrieved value is not the same than the injected one at the end (%d!=%d)",
145                      i, cpt);
146   }
147   xbt_dynar_free(&d);
148   xbt_dynar_free(&d);
149
150
151   xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest", NB_ELEM);
152   d = xbt_dynar_new(sizeof(int), NULL);
153   for (cpt = 0; cpt < NB_ELEM; cpt++)
154     xbt_dynar_push_as(d, int, cpt);
155
156   for (cpt = 2000; cpt < 4000; cpt++) {
157     xbt_dynar_remove_at(d, 2000, &i);
158     xbt_test_assert2(i == cpt,
159                      "Remove a bad value. Got %d, expected %d", i, cpt);
160     DEBUG2("remove %d, length=%lu", cpt, xbt_dynar_length(d));
161   }
162   xbt_dynar_free(&d);
163   xbt_dynar_free(&d);
164 }
165
166 /*******************************************************************************/
167 /*******************************************************************************/
168 /*******************************************************************************/
169 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
170 {
171   xbt_dynar_t d;
172   int cpt;
173   unsigned int cursor;
174   double d1, d2;
175
176   xbt_test_add0("==== Traverse the empty dynar");
177   d = xbt_dynar_new(sizeof(int), NULL);
178   xbt_dynar_foreach(d, cursor, cpt) {
179     xbt_test_assert0(FALSE, "Damnit, there is something in the empty dynar");
180   }
181   xbt_dynar_free(&d);
182   xbt_dynar_free(&d);
183
184   xbt_test_add0("==== Push/shift 5000 doubles");
185   d = xbt_dynar_new(sizeof(double), NULL);
186   for (cpt = 0; cpt < 5000; cpt++) {
187     d1 = (double) cpt;
188     xbt_dynar_push(d, &d1);
189   }
190   xbt_dynar_foreach(d, cursor, d2) {
191     d1 = (double) cursor;
192     xbt_test_assert2(d1 == d2,
193                      "The retrieved value is not the same than the injected one (%f!=%f)",
194                      d1, d2);
195   }
196   for (cpt = 0; cpt < 5000; cpt++) {
197     d1 = (double) cpt;
198     xbt_dynar_shift(d, &d2);
199     xbt_test_assert2(d1 == d2,
200                      "The retrieved value is not the same than the injected one (%f!=%f)",
201                      d1, d2);
202   }
203   xbt_dynar_free(&d);
204   xbt_dynar_free(&d);
205
206
207   xbt_test_add0("==== Unshift/pop 5000 doubles");
208   d = xbt_dynar_new(sizeof(double), NULL);
209   for (cpt = 0; cpt < 5000; cpt++) {
210     d1 = (double) cpt;
211     xbt_dynar_unshift(d, &d1);
212   }
213   for (cpt = 0; cpt < 5000; cpt++) {
214     d1 = (double) cpt;
215     xbt_dynar_pop(d, &d2);
216     xbt_test_assert2(d1 == d2,
217                      "The retrieved value is not the same than the injected one (%f!=%f)",
218                      d1, d2);
219   }
220   xbt_dynar_free(&d);
221   xbt_dynar_free(&d);
222
223
224
225   xbt_test_add0
226     ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
227   d = xbt_dynar_new(sizeof(double), NULL);
228   for (cpt = 0; cpt < 5000; cpt++) {
229     d1 = (double) cpt;
230     xbt_dynar_push(d, &d1);
231   }
232   for (cpt = 0; cpt < 1000; cpt++) {
233     d1 = (double) cpt;
234     xbt_dynar_insert_at(d, 2500, &d1);
235   }
236
237   for (cpt = 0; cpt < 2500; cpt++) {
238     d1 = (double) cpt;
239     xbt_dynar_shift(d, &d2);
240     xbt_test_assert2(d1 == d2,
241                      "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
242                      d1, d2);
243     DEBUG2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
244   }
245   for (cpt = 999; cpt >= 0; cpt--) {
246     d1 = (double) cpt;
247     xbt_dynar_shift(d, &d2);
248     xbt_test_assert2(d1 == d2,
249                      "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
250                      d1, d2);
251   }
252   for (cpt = 2500; cpt < 5000; cpt++) {
253     d1 = (double) cpt;
254     xbt_dynar_shift(d, &d2);
255     xbt_test_assert2(d1 == d2,
256                      "The retrieved value is not the same than the injected one at the end (%f!=%f)",
257                      d1, d2);
258   }
259   xbt_dynar_free(&d);
260   xbt_dynar_free(&d);
261
262
263   xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
264   d = xbt_dynar_new(sizeof(double), NULL);
265   for (cpt = 0; cpt < 5000; cpt++) {
266     d1 = (double) cpt;
267     xbt_dynar_push(d, &d1);
268   }
269   for (cpt = 2000; cpt < 4000; cpt++) {
270     d1 = (double) cpt;
271     xbt_dynar_remove_at(d, 2000, &d2);
272     xbt_test_assert2(d1 == d2,
273                      "Remove a bad value. Got %f, expected %f", d2, d1);
274   }
275   xbt_dynar_free(&d);
276   xbt_dynar_free(&d);
277 }
278
279
280 /* doxygen_string_cruft */
281
282 /*******************************************************************************/
283 /*******************************************************************************/
284 /*******************************************************************************/
285 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
286 {
287   xbt_dynar_t d;
288   int cpt;
289   unsigned int iter;
290   char buf[1024];
291   char *s1, *s2;
292
293   xbt_test_add0("==== Traverse the empty dynar");
294   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
295   xbt_dynar_foreach(d, iter, s1) {
296     xbt_test_assert0(FALSE, "Damnit, there is something in the empty dynar");
297   }
298   xbt_dynar_free(&d);
299   xbt_dynar_free(&d);
300
301   xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",
302                 NB_ELEM);
303   /* Populate_str [doxygen cruft] */
304   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
305   /* 1. Populate the dynar */
306   for (cpt = 0; cpt < NB_ELEM; cpt++) {
307     sprintf(buf, "%d", cpt);
308     s1 = strdup(buf);
309     xbt_dynar_push(d, &s1);
310   }
311   for (cpt = 0; cpt < NB_ELEM; cpt++) {
312     sprintf(buf, "%d", cpt);
313     s1 = strdup(buf);
314     xbt_dynar_replace(d, cpt, &s1);
315   }
316   for (cpt = 0; cpt < NB_ELEM; cpt++) {
317     sprintf(buf, "%d", cpt);
318     s1 = strdup(buf);
319     xbt_dynar_replace(d, cpt, &s1);
320   }
321   for (cpt = 0; cpt < NB_ELEM; cpt++) {
322     sprintf(buf, "%d", cpt);
323     s1 = strdup(buf);
324     xbt_dynar_replace(d, cpt, &s1);
325   }
326   for (cpt = 0; cpt < NB_ELEM; cpt++) {
327     sprintf(buf, "%d", cpt);
328     xbt_dynar_shift(d, &s2);
329     xbt_test_assert2(!strcmp(buf, s2),
330                      "The retrieved value is not the same than the injected one (%s!=%s)",
331                      buf, s2);
332     free(s2);
333   }
334   xbt_dynar_free(&d);
335   xbt_dynar_free(&d);
336
337
338   xbt_test_add1("==== Unshift, traverse and pop %d strings", NB_ELEM);
339   d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
340   for (cpt = 0; cpt < NB_ELEM; cpt++) {
341     sprintf(buf, "%d", cpt);
342     s1 = strdup(buf);
343     xbt_dynar_unshift(d, &s1);
344   }
345   /* 2. Traverse the dynar with the macro */
346   xbt_dynar_foreach(d, iter, s1) {
347     sprintf(buf, "%d", NB_ELEM - iter - 1);
348     xbt_test_assert2(!strcmp(buf, s1),
349                      "The retrieved value is not the same than the injected one (%s!=%s)",
350                      buf, s1);
351   }
352   /* 3. Traverse the dynar with the macro */
353   for (cpt = 0; cpt < NB_ELEM; cpt++) {
354     sprintf(buf, "%d", cpt);
355     xbt_dynar_pop(d, &s2);
356     xbt_test_assert2(!strcmp(buf, s2),
357                      "The retrieved value is not the same than the injected one (%s!=%s)",
358                      buf, s2);
359     free(s2);
360   }
361   /* 4. Free the resources */
362   xbt_dynar_free(&d);
363   xbt_dynar_free(&d);
364
365
366   xbt_test_add2
367     ("==== Push %d strings, insert %d strings in the middle, shift everything",
368      NB_ELEM, NB_ELEM / 5);
369   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
370   for (cpt = 0; cpt < NB_ELEM; cpt++) {
371     sprintf(buf, "%d", cpt);
372     s1 = strdup(buf);
373     xbt_dynar_push(d, &s1);
374   }
375   for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
376     sprintf(buf, "%d", cpt);
377     s1 = strdup(buf);
378     xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
379   }
380
381   for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
382     sprintf(buf, "%d", cpt);
383     xbt_dynar_shift(d, &s2);
384     xbt_test_assert2(!strcmp(buf, s2),
385                      "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
386                      buf, s2);
387     free(s2);
388   }
389   for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
390     sprintf(buf, "%d", cpt);
391     xbt_dynar_shift(d, &s2);
392     xbt_test_assert2(!strcmp(buf, s2),
393                      "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
394                      buf, s2);
395     free(s2);
396   }
397   for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
398     sprintf(buf, "%d", cpt);
399     xbt_dynar_shift(d, &s2);
400     xbt_test_assert2(!strcmp(buf, s2),
401                      "The retrieved value is not the same than the injected one at the end (%s!=%s)",
402                      buf, s2);
403     free(s2);
404   }
405   xbt_dynar_free(&d);
406   xbt_dynar_free(&d);
407
408
409   xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest", NB_ELEM,
410                 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
411   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
412   for (cpt = 0; cpt < NB_ELEM; cpt++) {
413     sprintf(buf, "%d", cpt);
414     s1 = strdup(buf);
415     xbt_dynar_push(d, &s1);
416   }
417   for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
418     sprintf(buf, "%d", cpt);
419     xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
420     xbt_test_assert2(!strcmp(buf, s2),
421                      "Remove a bad value. Got %s, expected %s", s2, buf);
422     free(s2);
423   }
424   xbt_dynar_free(&d);           /* end_of_doxygen */
425 }
426
427
428 /*******************************************************************************/
429 /*******************************************************************************/
430 /*******************************************************************************/
431 #include "xbt/synchro.h"
432 static void pusher_f(void *a)
433 {
434   xbt_dynar_t d = (xbt_dynar_t) a;
435   int i;
436   for (i = 0; i < 500; i++) {
437     xbt_dynar_push(d, &i);
438   }
439 }
440
441 static void poper_f(void *a)
442 {
443   xbt_dynar_t d = (xbt_dynar_t) a;
444   int i;
445   int data;
446   xbt_ex_t e;
447
448   for (i = 0; i < 500; i++) {
449     TRY {
450       xbt_dynar_pop(d, &data);
451     }
452     CATCH(e) {
453       if (e.category == bound_error) {
454         xbt_ex_free(e);
455         i--;
456       } else {
457         RETHROW;
458       }
459     }
460   }
461 }
462
463
464 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int,"Synchronized dynars of integers")
465 {
466   /* Vars_decl [doxygen cruft] */
467   xbt_dynar_t d;
468   xbt_thread_t pusher, poper;
469
470   xbt_test_add0("==== Have a pusher and a popper on the dynar");
471   d = xbt_dynar_new_sync(sizeof(int), NULL);
472   pusher = xbt_thread_create("pusher", pusher_f, d,0/*not joinable*/);
473   poper = xbt_thread_create("poper", poper_f, d,0/*not joinable*/);
474   xbt_thread_join(pusher);
475   xbt_thread_join(poper);
476   xbt_dynar_free(&d);
477 }
478
479 /*******************************/
480 /* GENERATED FILE, DO NOT EDIT */
481 /*******************************/
482