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] */
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");
32 xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
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));
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)",
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)",
57 /* end_of_traversal */
59 for (cpt=0; cpt< NB_ELEM; cpt++)
60 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
62 for (cpt=0; cpt< NB_ELEM; cpt++)
63 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
64 /* xbt_dynar_set(d,cpt,&cpt);*/
66 for (cpt=0; cpt< NB_ELEM; cpt++)
67 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
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)",
76 xbt_test_assert2(cpt == NB_ELEM,
77 "Cannot retrieve my %d values. Last got one is %d",
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)",
87 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
90 /* 5. Free the resources */
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));
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)",
106 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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));
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));
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)",
128 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
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)",
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)",
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);
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",
156 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
161 /*******************************************************************************/
162 /*******************************************************************************/
163 /*******************************************************************************/
164 XBT_TEST_UNIT("double",test_dynar_double,"Dynars of doubles") {
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");
178 xbt_test_add0("==== Push/shift 5000 doubles");
179 d=xbt_dynar_new(sizeof(double),NULL);
180 for (cpt=0; cpt< 5000; cpt++) {
182 xbt_dynar_push(d,&d1);
184 xbt_dynar_foreach(d,cursor,d2){
186 xbt_test_assert2(d1 == d2,
187 "The retrieved value is not the same than the injected one (%f!=%f)",
190 for (cpt=0; cpt< 5000; 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)",
201 xbt_test_add0("==== Unshift/pop 5000 doubles");
202 d=xbt_dynar_new(sizeof(double),NULL);
203 for (cpt=0; cpt< 5000; cpt++) {
205 xbt_dynar_unshift(d,&d1);
207 for (cpt=0; cpt< 5000; 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)",
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++) {
223 xbt_dynar_push(d,&d1);
225 for (cpt=0; cpt< 1000; cpt++) {
227 xbt_dynar_insert_at(d,2500,&d1);
230 for (cpt=0; cpt< 2500; 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)",
236 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
238 for (cpt=999; cpt>=0; 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)",
245 for (cpt=2500; cpt< 5000; 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)",
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++) {
260 xbt_dynar_push(d,&d1);
262 for (cpt=2000; cpt< 4000; cpt++) {
264 xbt_dynar_remove_at(d,2000,&d2);
265 xbt_test_assert2 (d1 == d2,
266 "Remove a bad value. Got %f, expected %f",
274 /* doxygen_string_cruft */
276 /* The function we will use to free the data */
277 static void free_string(void *d){
281 /*******************************************************************************/
282 /*******************************************************************************/
283 /*******************************************************************************/
284 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
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");
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);
306 xbt_dynar_push(d,&s1);
308 for (cpt=0; cpt< NB_ELEM; cpt++) {
309 sprintf(buf,"%d",cpt);
311 xbt_dynar_replace(d,cpt,&s1);
313 for (cpt=0; cpt< NB_ELEM; cpt++) {
314 sprintf(buf,"%d",cpt);
316 xbt_dynar_replace(d,cpt,&s1);
318 for (cpt=0; cpt< NB_ELEM; cpt++) {
319 sprintf(buf,"%d",cpt);
321 xbt_dynar_replace(d,cpt,&s1);
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)",
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);
340 xbt_dynar_unshift(d,&s1);
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)",
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)",
358 /* 4. Free the resources */
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);
368 xbt_dynar_push(d,&s1);
370 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
371 sprintf(buf,"%d",cpt);
373 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
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)",
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)",
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)",
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);
409 xbt_dynar_push(d,&s1);
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",
419 xbt_dynar_free(&d); /* end_of_doxygen */
423 /*******************************************************************************/
424 /*******************************************************************************/
425 /*******************************************************************************/
426 #include "xbt/synchro.h"
427 static void pusher_f(void *a) {
428 xbt_dynar_t d=(xbt_dynar_t)a;
430 for (i=0; i<500; i++) {
431 xbt_dynar_push(d,&i);
434 static void poper_f(void *a) {
435 xbt_dynar_t d=(xbt_dynar_t)a;
440 for (i=0; i<500; i++) {
442 xbt_dynar_pop(d,&data);
444 if (e.category == bound_error) {
455 XBT_TEST_UNIT("synchronized int",test_dynar_sync_int,"Synchronized dynars of integers") {
456 /* Vars_decl [doxygen cruft] */
458 xbt_thread_t pusher,poper;
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);
469 /*******************************/
470 /* GENERATED FILE, DO NOT EDIT */
471 /*******************************/