Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Window doesn't provide the function drand48(), remove useless break and add extern...
[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;
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 /* The function we will use to free the data */
279 static void free_string(void *d){
280   free(*(void**)d);
281 }
282
283 /*******************************************************************************/
284 /*******************************************************************************/
285 /*******************************************************************************/
286 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
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 *),&free_string);
295    xbt_dynar_foreach(d,iter,s1){
296      xbt_test_assert0(FALSE,
297                   "Damnit, there is something in the empty dynar");
298    }
299    xbt_dynar_free(&d);
300    xbt_dynar_free(&d);
301
302    xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
303    /* Populate_str [doxygen cruft] */
304    d=xbt_dynar_new(sizeof(char*),&free_string);
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**),&free_string);
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("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
367    d=xbt_dynar_new(sizeof(char*),&free_string);
368    for (cpt=0; cpt< NB_ELEM; cpt++) {
369      sprintf(buf,"%d",cpt);
370      s1=strdup(buf);
371      xbt_dynar_push(d,&s1);
372    }
373    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
374      sprintf(buf,"%d",cpt);
375      s1=strdup(buf);
376      xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
377    }
378
379    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
380      sprintf(buf,"%d",cpt);
381      xbt_dynar_shift(d,&s2);
382      xbt_test_assert2(!strcmp(buf,s2),
383            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
384                buf,s2);
385       free(s2);
386    }
387    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
388      sprintf(buf,"%d",cpt);
389      xbt_dynar_shift(d,&s2);
390      xbt_test_assert2 (!strcmp(buf,s2),
391            "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
392                buf,s2);
393      free(s2);
394    }
395    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
396      sprintf(buf,"%d",cpt);
397      xbt_dynar_shift(d,&s2);
398      xbt_test_assert2 (!strcmp(buf,s2),
399            "The retrieved value is not the same than the injected one at the end (%s!=%s)",
400                buf,s2);
401      free(s2);
402    }
403    xbt_dynar_free(&d);
404    xbt_dynar_free(&d);
405
406
407    xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
408    d=xbt_dynar_new(sizeof(char*),&free_string);
409    for (cpt=0; cpt< NB_ELEM; cpt++) {
410      sprintf(buf,"%d",cpt);
411      s1=strdup(buf);
412      xbt_dynar_push(d,&s1);
413    }
414    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
415      sprintf(buf,"%d",cpt);
416      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
417      xbt_test_assert2(!strcmp(buf,s2),
418                   "Remove a bad value. Got %s, expected %s",
419                   s2,buf);
420       free(s2);
421    }
422    xbt_dynar_free(&d); /* end_of_doxygen */
423 }
424
425
426 /*******************************************************************************/
427 /*******************************************************************************/
428 /*******************************************************************************/
429 #include "xbt/synchro.h"
430 static void pusher_f(void *a) {
431    xbt_dynar_t d=(xbt_dynar_t)a;
432    int i;
433    for (i=0; i<500; i++) {
434       xbt_dynar_push(d,&i);
435    }
436 }
437 static void poper_f(void *a) {
438    xbt_dynar_t d=(xbt_dynar_t)a;
439    int i;
440    int data;
441    xbt_ex_t e;
442    
443    for (i=0; i<500; i++) {
444       TRY {      
445          xbt_dynar_pop(d,&data);
446       } CATCH(e) {
447          if (e.category == bound_error) {
448             xbt_ex_free(e);
449             i--;
450          } else {
451             RETHROW;
452          }
453       }
454    }
455 }
456
457    
458 XBT_TEST_UNIT("synchronized int",test_dynar_sync_int,"Synchronized dynars of integers") {
459    /* Vars_decl [doxygen cruft] */
460    xbt_dynar_t d;
461    xbt_thread_t pusher,poper;
462    
463    xbt_test_add0("==== Have a pusher and a popper on the dynar");
464    d=xbt_dynar_new_sync(sizeof(int),NULL);
465    pusher = xbt_thread_create("pusher",pusher_f,d);
466    poper = xbt_thread_create("poper",poper_f,d);
467    xbt_thread_join(pusher);
468    xbt_thread_join(poper);
469    xbt_dynar_free(&d);
470 }
471
472 /*******************************/
473 /* GENERATED FILE, DO NOT EDIT */
474 /*******************************/
475