Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b7e9209810b752130b5ecfbc427ebe07e5377379
[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 # 623 "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,"Dyars 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 XBT_TEST_UNIT("double",test_dynar_double,"Dyars of doubles") {
163    xbt_dynar_t d;
164    int cpt,cursor;
165    double d1,d2;
166    
167    xbt_test_add0("==== Traverse the empty dynar");
168    d=xbt_dynar_new(sizeof(int),NULL);
169    xbt_dynar_foreach(d,cursor,cpt){
170      xbt_test_assert0(FALSE,
171              "Damnit, there is something in the empty dynar");
172    }
173    xbt_dynar_free(&d);
174    xbt_dynar_free(&d);
175
176    xbt_test_add0("==== Push/shift 5000 doubles");
177    d=xbt_dynar_new(sizeof(double),NULL);
178    for (cpt=0; cpt< 5000; cpt++) {
179      d1=(double)cpt;
180      xbt_dynar_push(d,&d1);
181    }
182    xbt_dynar_foreach(d,cursor,d2){
183      d1=(double)cursor;
184      xbt_test_assert2(d1 == d2,
185            "The retrieved value is not the same than the injected one (%f!=%f)",
186                   d1,d2);
187    }
188    for (cpt=0; cpt< 5000; cpt++) {
189      d1=(double)cpt;
190      xbt_dynar_shift(d,&d2);
191      xbt_test_assert2(d1 == d2,
192            "The retrieved value is not the same than the injected one (%f!=%f)",
193                   d1,d2);
194    }
195    xbt_dynar_free(&d);
196    xbt_dynar_free(&d);
197
198
199    xbt_test_add0("==== Unshift/pop 5000 doubles");
200    d=xbt_dynar_new(sizeof(double),NULL);
201    for (cpt=0; cpt< 5000; cpt++) {
202      d1=(double)cpt;
203      xbt_dynar_unshift(d,&d1);
204    }
205    for (cpt=0; cpt< 5000; cpt++) {
206      d1=(double)cpt;
207      xbt_dynar_pop(d,&d2);
208      xbt_test_assert2 (d1 == d2,
209            "The retrieved value is not the same than the injected one (%f!=%f)",
210                    d1,d2);
211    }
212    xbt_dynar_free(&d);
213    xbt_dynar_free(&d);
214
215
216
217    xbt_test_add0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
218    d=xbt_dynar_new(sizeof(double),NULL);
219    for (cpt=0; cpt< 5000; cpt++) {
220      d1=(double)cpt;
221      xbt_dynar_push(d,&d1);
222    }
223    for (cpt=0; cpt< 1000; cpt++) {
224      d1=(double)cpt;
225      xbt_dynar_insert_at(d,2500,&d1);
226    }
227
228    for (cpt=0; cpt< 2500; cpt++) {
229      d1=(double)cpt;
230      xbt_dynar_shift(d,&d2);
231      xbt_test_assert2(d1 == d2,
232            "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
233                   d1,d2);
234      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
235    }
236    for (cpt=999; cpt>=0; cpt--) {
237      d1=(double)cpt;
238      xbt_dynar_shift(d,&d2);
239      xbt_test_assert2 (d1 == d2,
240            "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
241                    d1,d2);
242    }
243    for (cpt=2500; cpt< 5000; cpt++) {
244      d1=(double)cpt;
245      xbt_dynar_shift(d,&d2);
246      xbt_test_assert2 (d1 == d2,
247            "The retrieved value is not the same than the injected one at the end (%f!=%f)",
248                    d1,d2);
249    }
250    xbt_dynar_free(&d);
251    xbt_dynar_free(&d);
252
253
254    xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
255    d=xbt_dynar_new(sizeof(double),NULL);
256    for (cpt=0; cpt< 5000; cpt++) {
257      d1=(double)cpt;
258      xbt_dynar_push(d,&d1);
259    }
260    for (cpt=2000; cpt< 4000; cpt++) {
261      d1=(double)cpt;
262      xbt_dynar_remove_at(d,2000,&d2);
263      xbt_test_assert2 (d1 == d2,
264            "Remove a bad value. Got %f, expected %f",
265                d2,d1);
266    }
267    xbt_dynar_free(&d);
268    xbt_dynar_free(&d);
269 }
270
271
272 /* doxygen_string_cruft */
273
274 /* The function we will use to free the data */
275 static void free_string(void *d){
276   free(*(void**)d);
277 }
278
279 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
280    xbt_dynar_t d;
281    int cpt;
282    char buf[1024];
283    char *s1,*s2;
284    
285    xbt_test_add0("==== Traverse the empty dynar");
286    d=xbt_dynar_new(sizeof(char *),&free_string);
287    xbt_dynar_foreach(d,cpt,s1){
288      xbt_test_assert0(FALSE,
289                   "Damnit, there is something in the empty dynar");
290    }
291    xbt_dynar_free(&d);
292    xbt_dynar_free(&d);
293
294    xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
295    /* Populate_str [doxygen cruft] */
296    d=xbt_dynar_new(sizeof(char*),&free_string);
297    /* 1. Populate the dynar */
298    for (cpt=0; cpt< NB_ELEM; cpt++) {
299      sprintf(buf,"%d",cpt);
300      s1=strdup(buf);
301      xbt_dynar_push(d,&s1);
302    }
303    for (cpt=0; cpt< NB_ELEM; cpt++) {
304      sprintf(buf,"%d",cpt);
305      s1=strdup(buf);
306      xbt_dynar_replace(d,cpt,&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      xbt_dynar_shift(d,&s2);
321      xbt_test_assert2 (!strcmp(buf,s2),
322             "The retrieved value is not the same than the injected one (%s!=%s)",
323                    buf,s2);
324      free(s2);
325    }
326    xbt_dynar_free(&d);
327    xbt_dynar_free(&d);
328
329
330    xbt_test_add1("==== Unshift, traverse and pop %d strings",NB_ELEM);
331    d=xbt_dynar_new(sizeof(char**),&free_string);
332    for (cpt=0; cpt< NB_ELEM; cpt++) {
333      sprintf(buf,"%d",cpt);
334      s1=strdup(buf);
335      xbt_dynar_unshift(d,&s1);
336    }
337    /* 2. Traverse the dynar with the macro */
338    xbt_dynar_foreach(d,cpt,s1) {
339      sprintf(buf,"%d",NB_ELEM - cpt -1);
340      xbt_test_assert2 (!strcmp(buf,s1),
341            "The retrieved value is not the same than the injected one (%s!=%s)",
342                buf,s1);
343    }
344    /* 3. Traverse the dynar with the macro */
345    for (cpt=0; cpt< NB_ELEM; cpt++) {
346      sprintf(buf,"%d",cpt);
347      xbt_dynar_pop(d,&s2);
348      xbt_test_assert2 (!strcmp(buf,s2),
349            "The retrieved value is not the same than the injected one (%s!=%s)",
350                buf,s2);
351      free(s2);
352    }
353    /* 4. Free the resources */
354    xbt_dynar_free(&d);
355    xbt_dynar_free(&d);
356
357
358    xbt_test_add2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
359    d=xbt_dynar_new(sizeof(char*),&free_string);
360    for (cpt=0; cpt< NB_ELEM; cpt++) {
361      sprintf(buf,"%d",cpt);
362      s1=strdup(buf);
363      xbt_dynar_push(d,&s1);
364    }
365    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
366      sprintf(buf,"%d",cpt);
367      s1=strdup(buf);
368      xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
369    }
370
371    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
372      sprintf(buf,"%d",cpt);
373      xbt_dynar_shift(d,&s2);
374      xbt_test_assert2(!strcmp(buf,s2),
375            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
376                buf,s2);
377       free(s2);
378    }
379    for (cpt=(NB_ELEM/5)-1; cpt>=0; 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 in the middle (%s!=%s)",
384                buf,s2);
385      free(s2);
386    }
387    for (cpt=NB_ELEM/2; cpt< NB_ELEM; 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 at the end (%s!=%s)",
392                buf,s2);
393      free(s2);
394    }
395    xbt_dynar_free(&d);
396    xbt_dynar_free(&d);
397
398
399    xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
400    d=xbt_dynar_new(sizeof(char*),&free_string);
401    for (cpt=0; cpt< NB_ELEM; cpt++) {
402      sprintf(buf,"%d",cpt);
403      s1=strdup(buf);
404      xbt_dynar_push(d,&s1);
405    }
406    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
407      sprintf(buf,"%d",cpt);
408      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
409      xbt_test_assert2(!strcmp(buf,s2),
410                   "Remove a bad value. Got %s, expected %s",
411                   s2,buf);
412       free(s2);
413    }
414    xbt_dynar_free(&d); /* end_of_doxygen */
415 }
416 /*******************************/
417 /* GENERATED FILE, DO NOT EDIT */
418 /*******************************/
419