Logo AND Algorithmique Numérique Distribuée

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