1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
16 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
18 XBT_TEST_UNIT("int",test_dynar_int,"Dynars of integers") {
19 /* Vars_decl [doxygen cruft] */
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");
33 xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
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));
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)",
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)",
58 /* end_of_traversal */
60 for (cpt=0; cpt< NB_ELEM; cpt++)
61 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
63 for (cpt=0; cpt< NB_ELEM; cpt++)
64 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
65 /* xbt_dynar_set(d,cpt,&cpt);*/
67 for (cpt=0; cpt< NB_ELEM; cpt++)
68 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
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)",
77 xbt_test_assert2(cpt == NB_ELEM,
78 "Cannot retrieve my %d values. Last got one is %d",
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)",
88 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
91 /* 5. Free the resources */
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));
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)",
107 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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));
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));
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)",
129 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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)",
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)",
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);
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",
157 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
162 /*******************************************************************************/
163 /*******************************************************************************/
164 /*******************************************************************************/
165 XBT_TEST_UNIT("double",test_dynar_double,"Dynars of doubles") {
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");
180 xbt_test_add0("==== Push/shift 5000 doubles");
181 d=xbt_dynar_new(sizeof(double),NULL);
182 for (cpt=0; cpt< 5000; cpt++) {
184 xbt_dynar_push(d,&d1);
186 xbt_dynar_foreach(d,cursor,d2){
188 xbt_test_assert2(d1 == d2,
189 "The retrieved value is not the same than the injected one (%f!=%f)",
192 for (cpt=0; cpt< 5000; 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)",
203 xbt_test_add0("==== Unshift/pop 5000 doubles");
204 d=xbt_dynar_new(sizeof(double),NULL);
205 for (cpt=0; cpt< 5000; cpt++) {
207 xbt_dynar_unshift(d,&d1);
209 for (cpt=0; cpt< 5000; 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)",
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++) {
225 xbt_dynar_push(d,&d1);
227 for (cpt=0; cpt< 1000; cpt++) {
229 xbt_dynar_insert_at(d,2500,&d1);
232 for (cpt=0; cpt< 2500; 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)",
238 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
240 for (cpt=999; cpt>=0; 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)",
247 for (cpt=2500; cpt< 5000; 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)",
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++) {
262 xbt_dynar_push(d,&d1);
264 for (cpt=2000; cpt< 4000; cpt++) {
266 xbt_dynar_remove_at(d,2000,&d2);
267 xbt_test_assert2 (d1 == d2,
268 "Remove a bad value. Got %f, expected %f",
276 /* doxygen_string_cruft */
278 /* The function we will use to free the data */
279 static void free_string(void *d){
283 /*******************************************************************************/
284 /*******************************************************************************/
285 /*******************************************************************************/
286 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
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");
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);
309 xbt_dynar_push(d,&s1);
311 for (cpt=0; cpt< NB_ELEM; cpt++) {
312 sprintf(buf,"%d",cpt);
314 xbt_dynar_replace(d,cpt,&s1);
316 for (cpt=0; cpt< NB_ELEM; cpt++) {
317 sprintf(buf,"%d",cpt);
319 xbt_dynar_replace(d,cpt,&s1);
321 for (cpt=0; cpt< NB_ELEM; cpt++) {
322 sprintf(buf,"%d",cpt);
324 xbt_dynar_replace(d,cpt,&s1);
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)",
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);
343 xbt_dynar_unshift(d,&s1);
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)",
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)",
361 /* 4. Free the resources */
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);
371 xbt_dynar_push(d,&s1);
373 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
374 sprintf(buf,"%d",cpt);
376 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
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)",
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)",
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)",
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);
412 xbt_dynar_push(d,&s1);
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",
422 xbt_dynar_free(&d); /* end_of_doxygen */
426 /*******************************************************************************/
427 /*******************************************************************************/
428 /*******************************************************************************/
429 #include "xbt/synchro.h"
430 static void pusher_f(void *a) {
431 xbt_dynar_t d=(xbt_dynar_t)a;
433 for (i=0; i<500; i++) {
434 xbt_dynar_push(d,&i);
437 static void poper_f(void *a) {
438 xbt_dynar_t d=(xbt_dynar_t)a;
443 for (i=0; i<500; i++) {
445 xbt_dynar_pop(d,&data);
447 if (e.category == bound_error) {
458 XBT_TEST_UNIT("synchronized int",test_dynar_sync_int,"Synchronized dynars of integers") {
459 /* Vars_decl [doxygen cruft] */
461 xbt_thread_t pusher,poper;
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);
472 /*******************************/
473 /* GENERATED FILE, DO NOT EDIT */
474 /*******************************/