Logo AND Algorithmique Numérique Distribuée

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