Logo AND Algorithmique Numérique Distribuée

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