1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
11 # 691 "/home/navarrop/Developments/simgrid/src/xbt/dynar.c"
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")
20 /* Vars_decl [doxygen cruft] */
26 xbt_test_add0("==== Traverse the empty dynar");
27 d = xbt_dynar_new(sizeof(int), NULL);
28 xbt_dynar_foreach(d, cursor, i) {
29 xbt_assert0(0, "Damnit, there is something in the empty dynar");
35 ("==== Push %d int, set them again 3 times, traverse them, shift them",
37 /* Populate_ints [doxygen cruft] */
38 /* 1. Populate the dynar */
39 d = xbt_dynar_new(sizeof(int), NULL);
40 for (cpt = 0; cpt < NB_ELEM; cpt++) {
41 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
42 /* xbt_dynar_push(d,&cpt); This would also work */
43 xbt_test_log2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
46 /* 2. Traverse manually the dynar */
47 for (cursor = 0; cursor < NB_ELEM; cursor++) {
48 iptr = xbt_dynar_get_ptr(d, cursor);
49 xbt_test_assert2(cursor == *iptr,
50 "The retrieved value is not the same than the injected one (%d!=%d)",
54 /* 3. Traverse the dynar using the neat macro to that extend */
55 xbt_dynar_foreach(d, cursor, cpt) {
56 xbt_test_assert2(cursor == cpt,
57 "The retrieved value is not the same than the injected one (%d!=%d)",
60 /* end_of_traversal */
62 for (cpt = 0; cpt < NB_ELEM; cpt++)
63 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
65 for (cpt = 0; cpt < NB_ELEM; cpt++)
66 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
67 /* xbt_dynar_set(d,cpt,&cpt); */
69 for (cpt = 0; cpt < NB_ELEM; cpt++)
70 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
73 xbt_dynar_foreach(d, cursor, i) {
74 xbt_test_assert2(i == cpt,
75 "The retrieved value is not the same than the injected one (%d!=%d)",
79 xbt_test_assert2(cpt == NB_ELEM,
80 "Cannot retrieve my %d values. Last got one is %d",
83 /* shifting [doxygen cruft] */
84 /* 4. Shift all the values */
85 for (cpt = 0; cpt < NB_ELEM; cpt++) {
86 xbt_dynar_shift(d, &i);
87 xbt_test_assert2(i == cpt,
88 "The retrieved value is not the same than the injected one (%d!=%d)",
90 xbt_test_log2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
93 /* 5. Free the resources */
98 xbt_test_add1("==== Unshift/pop %d int", NB_ELEM);
99 d = xbt_dynar_new(sizeof(int), NULL);
100 for (cpt = 0; cpt < NB_ELEM; cpt++) {
101 xbt_dynar_unshift(d, &cpt);
102 DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
104 for (cpt = 0; cpt < NB_ELEM; cpt++) {
105 i = xbt_dynar_pop_as(d, int);
106 xbt_test_assert2(i == cpt,
107 "The retrieved value is not the same than the injected one (%d!=%d)",
109 xbt_test_log2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
116 ("==== Push %d int, insert 1000 int in the middle, shift everything",
118 d = xbt_dynar_new(sizeof(int), NULL);
119 for (cpt = 0; cpt < NB_ELEM; cpt++) {
120 xbt_dynar_push_as(d, int, cpt);
121 DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
123 for (cpt = 0; cpt < 1000; cpt++) {
124 xbt_dynar_insert_at_as(d, 2500, int, cpt);
125 DEBUG2("Push %d, length=%lu", cpt, xbt_dynar_length(d));
128 for (cpt = 0; cpt < 2500; cpt++) {
129 xbt_dynar_shift(d, &i);
130 xbt_test_assert2(i == cpt,
131 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
133 DEBUG2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
135 for (cpt = 999; cpt >= 0; 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 in the middle (%d!=%d)",
141 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
142 xbt_dynar_shift(d, &i);
143 xbt_test_assert2(i == cpt,
144 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
151 xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest", NB_ELEM);
152 d = xbt_dynar_new(sizeof(int), NULL);
153 for (cpt = 0; cpt < NB_ELEM; cpt++)
154 xbt_dynar_push_as(d, int, cpt);
156 for (cpt = 2000; cpt < 4000; cpt++) {
157 xbt_dynar_remove_at(d, 2000, &i);
158 xbt_test_assert2(i == cpt,
159 "Remove a bad value. Got %d, expected %d", i, cpt);
160 DEBUG2("remove %d, length=%lu", cpt, xbt_dynar_length(d));
166 /*******************************************************************************/
167 /*******************************************************************************/
168 /*******************************************************************************/
169 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
176 xbt_test_add0("==== Traverse the empty dynar");
177 d = xbt_dynar_new(sizeof(int), NULL);
178 xbt_dynar_foreach(d, cursor, cpt) {
179 xbt_test_assert0(FALSE, "Damnit, there is something in the empty dynar");
184 xbt_test_add0("==== Push/shift 5000 doubles");
185 d = xbt_dynar_new(sizeof(double), NULL);
186 for (cpt = 0; cpt < 5000; cpt++) {
188 xbt_dynar_push(d, &d1);
190 xbt_dynar_foreach(d, cursor, d2) {
191 d1 = (double) cursor;
192 xbt_test_assert2(d1 == d2,
193 "The retrieved value is not the same than the injected one (%f!=%f)",
196 for (cpt = 0; cpt < 5000; cpt++) {
198 xbt_dynar_shift(d, &d2);
199 xbt_test_assert2(d1 == d2,
200 "The retrieved value is not the same than the injected one (%f!=%f)",
207 xbt_test_add0("==== Unshift/pop 5000 doubles");
208 d = xbt_dynar_new(sizeof(double), NULL);
209 for (cpt = 0; cpt < 5000; cpt++) {
211 xbt_dynar_unshift(d, &d1);
213 for (cpt = 0; cpt < 5000; cpt++) {
215 xbt_dynar_pop(d, &d2);
216 xbt_test_assert2(d1 == d2,
217 "The retrieved value is not the same than the injected one (%f!=%f)",
226 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
227 d = xbt_dynar_new(sizeof(double), NULL);
228 for (cpt = 0; cpt < 5000; cpt++) {
230 xbt_dynar_push(d, &d1);
232 for (cpt = 0; cpt < 1000; cpt++) {
234 xbt_dynar_insert_at(d, 2500, &d1);
237 for (cpt = 0; cpt < 2500; cpt++) {
239 xbt_dynar_shift(d, &d2);
240 xbt_test_assert2(d1 == d2,
241 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
243 DEBUG2("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
245 for (cpt = 999; cpt >= 0; 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 in the middle (%f!=%f)",
252 for (cpt = 2500; cpt < 5000; cpt++) {
254 xbt_dynar_shift(d, &d2);
255 xbt_test_assert2(d1 == d2,
256 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
263 xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
264 d = xbt_dynar_new(sizeof(double), NULL);
265 for (cpt = 0; cpt < 5000; cpt++) {
267 xbt_dynar_push(d, &d1);
269 for (cpt = 2000; cpt < 4000; cpt++) {
271 xbt_dynar_remove_at(d, 2000, &d2);
272 xbt_test_assert2(d1 == d2,
273 "Remove a bad value. Got %f, expected %f", d2, d1);
280 /* doxygen_string_cruft */
282 /*******************************************************************************/
283 /*******************************************************************************/
284 /*******************************************************************************/
285 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
293 xbt_test_add0("==== Traverse the empty dynar");
294 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
295 xbt_dynar_foreach(d, iter, s1) {
296 xbt_test_assert0(FALSE, "Damnit, there is something in the empty dynar");
301 xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",
303 /* Populate_str [doxygen cruft] */
304 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
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 **), &xbt_free_ref);
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 */
367 ("==== Push %d strings, insert %d strings in the middle, shift everything",
368 NB_ELEM, NB_ELEM / 5);
369 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
370 for (cpt = 0; cpt < NB_ELEM; cpt++) {
371 sprintf(buf, "%d", cpt);
373 xbt_dynar_push(d, &s1);
375 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
376 sprintf(buf, "%d", cpt);
378 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
381 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
382 sprintf(buf, "%d", cpt);
383 xbt_dynar_shift(d, &s2);
384 xbt_test_assert2(!strcmp(buf, s2),
385 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
389 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
390 sprintf(buf, "%d", cpt);
391 xbt_dynar_shift(d, &s2);
392 xbt_test_assert2(!strcmp(buf, s2),
393 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
397 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
398 sprintf(buf, "%d", cpt);
399 xbt_dynar_shift(d, &s2);
400 xbt_test_assert2(!strcmp(buf, s2),
401 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
409 xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest", NB_ELEM,
410 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
411 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
412 for (cpt = 0; cpt < NB_ELEM; cpt++) {
413 sprintf(buf, "%d", cpt);
415 xbt_dynar_push(d, &s1);
417 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
418 sprintf(buf, "%d", cpt);
419 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
420 xbt_test_assert2(!strcmp(buf, s2),
421 "Remove a bad value. Got %s, expected %s", s2, buf);
424 xbt_dynar_free(&d); /* end_of_doxygen */
428 /*******************************************************************************/
429 /*******************************************************************************/
430 /*******************************************************************************/
431 #include "xbt/synchro.h"
432 static void pusher_f(void *a)
434 xbt_dynar_t d = (xbt_dynar_t) a;
436 for (i = 0; i < 500; i++) {
437 xbt_dynar_push(d, &i);
441 static void poper_f(void *a)
443 xbt_dynar_t d = (xbt_dynar_t) a;
448 for (i = 0; i < 500; i++) {
450 xbt_dynar_pop(d, &data);
453 if (e.category == bound_error) {
464 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int,"Synchronized dynars of integers")
466 /* Vars_decl [doxygen cruft] */
468 xbt_thread_t pusher, poper;
470 xbt_test_add0("==== Have a pusher and a popper on the dynar");
471 d = xbt_dynar_new_sync(sizeof(int), NULL);
472 pusher = xbt_thread_create("pusher", pusher_f, d,0/*not joinable*/);
473 poper = xbt_thread_create("poper", poper_f, d,0/*not joinable*/);
474 xbt_thread_join(pusher);
475 xbt_thread_join(poper);
479 /*******************************/
480 /* GENERATED FILE, DO NOT EDIT */
481 /*******************************/