3 /* a generic DYNamic ARray implementation. */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "portable.h" /* SIZEOF_MAX */
12 #include "xbt/sysdep.h"
15 #include "xbt/dynar.h"
16 #include <sys/types.h>
18 #include "xbt/dynar_private.h" /* type definition, which we share with the
19 code in charge of sending this across the net */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays");
24 #define __sanity_check_dynar(dynar) \
27 #define __sanity_check_idx(idx) \
28 xbt_assert1(idx >= 0, \
29 "dynar idx(=%d) < 0", \
31 #define __check_inbound_idx(dynar, idx) \
32 xbt_assert2(idx < dynar->used, \
33 "dynar is not that long. You asked %d, but it's only %lu long", \
34 (int) (idx), (unsigned long) dynar->used)
35 #define __check_sloppy_inbound_idx(dynar, idx) \
36 xbt_assert2(idx <= dynar->used, \
37 "dynar is not that long. You asked %d, but it's only %lu long", \
38 (int) (idx), (unsigned long) dynar->used)
39 #define __check_populated_dynar(dynar) \
40 xbt_assert1(dynar->used, \
41 "dynar %p contains nothing",(void*)dynar)
44 void _xbt_clear_mem(void * const ptr,
45 const unsigned long length) {
46 memset(ptr, 0, length);
51 _xbt_dynar_expand(xbt_dynar_t const dynar,
53 const unsigned long old_size = dynar->size;
56 char * const old_data = (char *) dynar->data;
58 const unsigned long elmsize = dynar->elmsize;
59 const unsigned long old_length = old_size*elmsize;
61 const unsigned long used = dynar->used;
62 const unsigned long used_length = used*elmsize;
64 const unsigned long new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
65 const unsigned long new_length = new_size*elmsize;
66 char * const new_data = (char *) xbt_malloc0(elmsize*new_size);
68 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
71 memcpy(new_data, old_data, used_length);
72 _xbt_clear_mem(old_data, old_length);
76 _xbt_clear_mem(new_data + used_length, new_length - used_length);
78 dynar->size = new_size;
79 dynar->data = new_data;
85 _xbt_dynar_elm(const xbt_dynar_t dynar,
86 const unsigned long idx) {
87 char * const data = (char*) dynar->data;
88 const unsigned long elmsize = dynar->elmsize;
90 return data + idx*elmsize;
95 _xbt_dynar_get_elm(void * const dst,
96 const xbt_dynar_t dynar,
97 const unsigned long idx) {
98 void * const elm = _xbt_dynar_elm(dynar, idx);
100 memcpy(dst, elm, dynar->elmsize);
105 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
106 const unsigned long idx,
107 const void * const src) {
108 void * const elm = _xbt_dynar_elm(dynar, idx);
109 const unsigned long elmsize = dynar->elmsize;
111 memcpy(elm, src, elmsize);
115 xbt_dynar_dump(xbt_dynar_t dynar) {
116 INFO5("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
117 dynar->size, dynar->used, dynar->elmsize, dynar->data, dynar->free_f);
120 /** @brief Constructor
122 * \param elmsize size of each element in the dynar
123 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
125 * Creates a new dynar. If a free_func is provided, the elements have to be
126 * pointer of pointer. That is to say that dynars can contain either base
127 * types (int, char, double, etc) or pointer of pointers (struct **).
130 xbt_dynar_new(const unsigned long elmsize,
131 void_f_pvoid_t * const free_f) {
133 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
137 dynar->elmsize = elmsize;
139 dynar->free_f = free_f;
144 /** @brief Destructor of the structure not touching to the content
146 * \param dynar poor victim
148 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
149 * is not touched (the \a free_f function is not used)
152 xbt_dynar_free_container(xbt_dynar_t *dynar) {
153 if (dynar && *dynar) {
155 if ((*dynar)->data) {
156 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
157 free((*dynar)->data);
160 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
167 /** @brief Frees the content and set the size to 0
169 * \param dynar who to squeeze
172 xbt_dynar_reset(xbt_dynar_t const dynar) {
174 __sanity_check_dynar(dynar);
176 DEBUG1("Reset the dynar %p",(void*)dynar);
178 xbt_dynar_map(dynar, dynar->free_f);
187 /* dynar->data = NULL;*/
190 /** @brief Destructor
192 * \param dynar poor victim
194 * kilkil a dynar and its content
198 xbt_dynar_free(xbt_dynar_t * dynar) {
199 if (dynar && *dynar) {
200 xbt_dynar_reset(*dynar);
201 xbt_dynar_free_container(dynar);
204 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
205 void xbt_dynar_free_voidp(void *d) {
206 xbt_dynar_free( (xbt_dynar_t*) d);
209 /** @brief Count of dynar's elements
211 * \param dynar the dynar we want to mesure
214 xbt_dynar_length(const xbt_dynar_t dynar) {
215 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
218 /** @brief Retrieve a copy of the Nth element of a dynar.
220 * \param dynar information dealer
221 * \param idx index of the slot we want to retrieve
222 * \param[out] dst where to put the result to.
225 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
229 __sanity_check_dynar(dynar);
230 __sanity_check_idx(idx);
231 __check_inbound_idx(dynar, idx);
233 _xbt_dynar_get_elm(dst, dynar, idx);
236 /** @brief Retrieve a pointer to the Nth element of a dynar.
238 * \param dynar information dealer
239 * \param idx index of the slot we want to retrieve
240 * \return the \a idx-th element of \a dynar.
242 * \warning The returned value is the actual content of the dynar.
243 * Make a copy before fooling with it.
246 xbt_dynar_get_ptr(const xbt_dynar_t dynar,
249 __sanity_check_dynar(dynar);
250 __sanity_check_idx(idx);
251 __check_inbound_idx(dynar, idx);
253 return _xbt_dynar_elm(dynar, idx);
256 /** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed
258 * \param dynar information dealer
259 * \param idx index of the slot we want to modify
260 * \param src What will be feeded to the dynar
262 * If you want to free the previous content, use xbt_dynar_replace().
265 xbt_dynar_set(xbt_dynar_t dynar,
267 const void * const src) {
269 __sanity_check_dynar(dynar);
270 __sanity_check_idx(idx);
272 _xbt_dynar_expand(dynar, idx+1);
274 if (idx >= dynar->used) {
278 _xbt_dynar_put_elm(dynar, idx, src);
281 /** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed
287 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
288 * free the previous value at this position. If you don't want to free the
289 * previous content, use xbt_dynar_set().
292 xbt_dynar_replace(xbt_dynar_t dynar,
294 const void * const object) {
296 __sanity_check_dynar(dynar);
297 __sanity_check_idx(idx);
299 if (idx < dynar->used && dynar->free_f) {
300 void * const old_object = _xbt_dynar_elm(dynar, idx);
302 dynar->free_f(old_object);
305 xbt_dynar_set(dynar, idx, object);
308 /** @brief Make room for a new element, and return a pointer to it
310 * You can then use regular affectation to set its value instead of relying
311 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
314 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
317 __sanity_check_dynar(dynar);
318 __sanity_check_idx(idx);
319 __check_sloppy_inbound_idx(dynar, idx);
322 const unsigned long old_used = dynar->used;
323 const unsigned long new_used = old_used + 1;
325 _xbt_dynar_expand(dynar, new_used);
328 const unsigned long nb_shift = old_used - idx;
331 memmove(_xbt_dynar_elm(dynar, idx+1),
332 _xbt_dynar_elm(dynar, idx),
333 nb_shift * dynar->elmsize);
336 dynar->used = new_used;
337 return _xbt_dynar_elm(dynar,idx);
341 /** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right
343 * Set the Nth element of a dynar, expanding the dynar if needed, and
344 * moving the previously existing value and all subsequent ones to one
345 * position right in the dynar.
348 xbt_dynar_insert_at(xbt_dynar_t const dynar,
350 const void * const src) {
352 /* checks done in xbt_dynar_insert_at_ptr */
353 memcpy(xbt_dynar_insert_at_ptr(dynar,idx),
358 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
360 * Get the Nth element of a dynar, removing it from the dynar and moving
361 * all subsequent values to one position left in the dynar.
364 xbt_dynar_remove_at(xbt_dynar_t const dynar,
366 void * const object) {
368 unsigned long nb_shift;
369 unsigned long offset;
371 __sanity_check_dynar(dynar);
372 __sanity_check_idx(idx);
373 __check_inbound_idx(dynar, idx);
376 _xbt_dynar_get_elm(object, dynar, idx);
377 } else if (dynar->free_f) {
378 char elm[SIZEOF_MAX];
379 _xbt_dynar_get_elm(elm, dynar, idx);
380 (*dynar->free_f)(elm);
383 nb_shift = dynar->used-1 - idx;
384 offset = nb_shift * dynar->elmsize;
386 memmove(_xbt_dynar_elm(dynar, idx),
387 _xbt_dynar_elm(dynar, idx+1),
393 /** @brief Returns the position of the element in the dynar
395 * Raises not_found_error if not found.
398 xbt_dynar_search(xbt_dynar_t const dynar,
402 for (it=0; it< dynar->size; it++)
403 if (!memcmp(_xbt_dynar_elm(dynar, it),elem,dynar->elmsize))
406 THROW2(not_found_error,0,"Element %p not part of dynar %p",elem,dynar);
409 /** @brief Returns a boolean indicating whether the element is part of the dynar */
411 xbt_dynar_member(xbt_dynar_t const dynar,
417 xbt_dynar_search(dynar,elem);
419 if (e.category == not_found_error) {
428 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
430 * You can then use regular affectation to set its value instead of relying
431 * on the slow memcpy. This is what xbt_dynar_push_as() does.
434 xbt_dynar_push_ptr(xbt_dynar_t const dynar) {
435 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
438 /** @brief Add an element at the end of the dynar */
440 xbt_dynar_push(xbt_dynar_t const dynar,
441 const void * const src) {
442 /* sanity checks done by insert_at */
443 xbt_dynar_insert_at(dynar, dynar->used, src);
446 /** @brief Mark the last dynar's element as unused and return a pointer to it.
448 * You can then use regular affectation to set its value instead of relying
449 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
452 xbt_dynar_pop_ptr(xbt_dynar_t const dynar) {
454 __check_populated_dynar(dynar);
455 DEBUG1("Pop %p",(void*)dynar);
457 return _xbt_dynar_elm(dynar,dynar->used);
460 /** @brief Get and remove the last element of the dynar */
462 xbt_dynar_pop(xbt_dynar_t const dynar,
465 /* sanity checks done by remove_at */
466 DEBUG1("Pop %p",(void*)dynar);
467 xbt_dynar_remove_at(dynar, dynar->used-1, dst);
470 /** @brief Add an element at the begining of the dynar.
472 * This is less efficient than xbt_dynar_push()
475 xbt_dynar_unshift(xbt_dynar_t const dynar,
476 const void * const src) {
478 /* sanity checks done by insert_at */
479 xbt_dynar_insert_at(dynar, 0, src);
482 /** @brief Get and remove the first element of the dynar.
484 * This is less efficient than xbt_dynar_pop()
487 xbt_dynar_shift(xbt_dynar_t const dynar,
490 /* sanity checks done by remove_at */
491 xbt_dynar_remove_at(dynar, 0, dst);
494 /** @brief Apply a function to each member of a dynar
496 * The mapped function may change the value of the element itself,
497 * but should not mess with the structure of the dynar.
500 xbt_dynar_map(const xbt_dynar_t dynar,
501 void_f_pvoid_t * const op) {
503 __sanity_check_dynar(dynar);
506 char elm[SIZEOF_MAX];
507 const unsigned long used = dynar->used;
510 for (i = 0; i < used; i++) {
511 _xbt_dynar_get_elm(elm, dynar, i);
517 /** @brief Put the cursor at the begining of the dynar.
519 * Actually, the cursor is set one step before the begining, so that you
520 * can iterate over the dynar with a for loop.
523 xbt_dynar_cursor_first(const xbt_dynar_t dynar,
524 int * const cursor) {
526 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
530 /** @brief Move the cursor to the next value */
532 xbt_dynar_cursor_step(const xbt_dynar_t dynar,
533 int * const cursor) {
538 /** @brief Get the data currently pointed by the cursor */
540 xbt_dynar_cursor_get(const xbt_dynar_t dynar,
544 __sanity_check_dynar(dynar);
547 const int idx = *cursor;
549 if (idx >= dynar->used) {
550 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
553 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
555 _xbt_dynar_get_elm(dst, dynar, idx);
561 /** @brief Removes and free the entry pointed by the cursor
563 * This function can be used while traversing without problem.
565 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
566 int * const cursor) {
569 if (dynar->elmsize > sizeof(void*)) {
570 DEBUG0("Elements too big to fit into a pointer");
572 dst=xbt_malloc(dynar->elmsize);
573 xbt_dynar_remove_at(dynar,(*cursor)--,dst);
574 (dynar->free_f)(dst);
577 DEBUG0("Ok, we dont care about the element without free function");
578 xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
582 xbt_dynar_remove_at(dynar,(*cursor)--,&dst);
584 (dynar->free_f)(dst);
592 XBT_TEST_SUITE("dynar","Dynar data container");
593 XBT_LOG_EXTERNAL_CATEGORY(dynar);
594 XBT_LOG_DEFAULT_CATEGORY(dynar);
596 XBT_TEST_UNIT("int",test_dynar_int,"Dyars of integers") {
597 /* Vars_decl [doxygen cruft] */
602 xbt_test_add0("==== Traverse the empty dynar");
603 d=xbt_dynar_new(sizeof(int),NULL);
604 xbt_dynar_foreach(d,cursor,i){
605 xbt_assert0(0,"Damnit, there is something in the empty dynar");
610 xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
612 /* Populate_ints [doxygen cruft] */
613 /* 1. Populate the dynar */
614 d=xbt_dynar_new(sizeof(int),NULL);
615 for (cpt=0; cpt< NB_ELEM; cpt++) {
616 xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
617 /* xbt_dynar_push(d,&cpt); This would also work */
618 xbt_test_log2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
621 /* 2. Traverse manually the dynar */
622 for (cursor=0; cursor< NB_ELEM; cursor++) {
623 iptr=xbt_dynar_get_ptr(d,cursor);
624 xbt_test_assert2(cursor == *iptr,
625 "The retrieved value is not the same than the injected one (%d!=%d)",
629 /* 3. Traverse the dynar using the neat macro to that extend */
630 xbt_dynar_foreach(d,cursor,cpt){
631 xbt_test_assert2(cursor == cpt,
632 "The retrieved value is not the same than the injected one (%d!=%d)",
635 /* end_of_traversal */
637 for (cpt=0; cpt< NB_ELEM; cpt++)
638 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
640 for (cpt=0; cpt< NB_ELEM; cpt++)
641 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
642 /* xbt_dynar_set(d,cpt,&cpt);*/
644 for (cpt=0; cpt< NB_ELEM; cpt++)
645 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
648 xbt_dynar_foreach(d,cursor,i){
649 xbt_test_assert2(i == cpt,
650 "The retrieved value is not the same than the injected one (%d!=%d)",
654 xbt_test_assert2(cpt == NB_ELEM,
655 "Cannot retrieve my %d values. Last got one is %d",
658 /* shifting [doxygen cruft] */
659 /* 4. Shift all the values */
660 for (cpt=0; cpt< NB_ELEM; cpt++) {
661 xbt_dynar_shift(d,&i);
662 xbt_test_assert2(i == cpt,
663 "The retrieved value is not the same than the injected one (%d!=%d)",
665 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
668 /* 5. Free the resources */
673 xbt_test_add1("==== Unshift/pop %d int",NB_ELEM);
674 d=xbt_dynar_new(sizeof(int),NULL);
675 for (cpt=0; cpt< NB_ELEM; cpt++) {
676 xbt_dynar_unshift(d,&cpt);
677 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
679 for (cpt=0; cpt< NB_ELEM; cpt++) {
680 i=xbt_dynar_pop_as(d,int);
681 xbt_test_assert2(i == cpt,
682 "The retrieved value is not the same than the injected one (%d!=%d)",
684 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
690 xbt_test_add1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
691 d=xbt_dynar_new(sizeof(int),NULL);
692 for (cpt=0; cpt< NB_ELEM; cpt++) {
693 xbt_dynar_push_as(d,int,cpt);
694 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
696 for (cpt=0; cpt< 1000; cpt++) {
697 xbt_dynar_insert_at_as(d,2500,int,cpt);
698 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
701 for (cpt=0; cpt< 2500; cpt++) {
702 xbt_dynar_shift(d,&i);
703 xbt_test_assert2(i == cpt,
704 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
706 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
708 for (cpt=999; cpt>=0; cpt--) {
709 xbt_dynar_shift(d,&i);
710 xbt_test_assert2(i == cpt,
711 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
714 for (cpt=2500; cpt< NB_ELEM; cpt++) {
715 xbt_dynar_shift(d,&i);
716 xbt_test_assert2(i == cpt,
717 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
724 xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
725 d=xbt_dynar_new(sizeof(int),NULL);
726 for (cpt=0; cpt< NB_ELEM; cpt++)
727 xbt_dynar_push_as(d,int,cpt);
729 for (cpt=2000; cpt< 4000; cpt++) {
730 xbt_dynar_remove_at(d,2000,&i);
731 xbt_test_assert2(i == cpt,
732 "Remove a bad value. Got %d, expected %d",
734 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
740 XBT_TEST_UNIT("double",test_dynar_double,"Dyars of doubles") {
745 xbt_test_add0("==== Traverse the empty dynar");
746 d=xbt_dynar_new(sizeof(int),NULL);
747 xbt_dynar_foreach(d,cursor,cpt){
748 xbt_test_assert0(FALSE,
749 "Damnit, there is something in the empty dynar");
754 xbt_test_add0("==== Push/shift 5000 doubles");
755 d=xbt_dynar_new(sizeof(double),NULL);
756 for (cpt=0; cpt< 5000; cpt++) {
758 xbt_dynar_push(d,&d1);
760 xbt_dynar_foreach(d,cursor,d2){
762 xbt_test_assert2(d1 == d2,
763 "The retrieved value is not the same than the injected one (%f!=%f)",
766 for (cpt=0; cpt< 5000; cpt++) {
768 xbt_dynar_shift(d,&d2);
769 xbt_test_assert2(d1 == d2,
770 "The retrieved value is not the same than the injected one (%f!=%f)",
777 xbt_test_add0("==== Unshift/pop 5000 doubles");
778 d=xbt_dynar_new(sizeof(double),NULL);
779 for (cpt=0; cpt< 5000; cpt++) {
781 xbt_dynar_unshift(d,&d1);
783 for (cpt=0; cpt< 5000; cpt++) {
785 xbt_dynar_pop(d,&d2);
786 xbt_test_assert2 (d1 == d2,
787 "The retrieved value is not the same than the injected one (%f!=%f)",
795 xbt_test_add0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
796 d=xbt_dynar_new(sizeof(double),NULL);
797 for (cpt=0; cpt< 5000; cpt++) {
799 xbt_dynar_push(d,&d1);
801 for (cpt=0; cpt< 1000; cpt++) {
803 xbt_dynar_insert_at(d,2500,&d1);
806 for (cpt=0; cpt< 2500; cpt++) {
808 xbt_dynar_shift(d,&d2);
809 xbt_test_assert2(d1 == d2,
810 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
812 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
814 for (cpt=999; cpt>=0; cpt--) {
816 xbt_dynar_shift(d,&d2);
817 xbt_test_assert2 (d1 == d2,
818 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
821 for (cpt=2500; cpt< 5000; cpt++) {
823 xbt_dynar_shift(d,&d2);
824 xbt_test_assert2 (d1 == d2,
825 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
832 xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
833 d=xbt_dynar_new(sizeof(double),NULL);
834 for (cpt=0; cpt< 5000; cpt++) {
836 xbt_dynar_push(d,&d1);
838 for (cpt=2000; cpt< 4000; cpt++) {
840 xbt_dynar_remove_at(d,2000,&d2);
841 xbt_test_assert2 (d1 == d2,
842 "Remove a bad value. Got %f, expected %f",
850 /* doxygen_string_cruft */
852 /* The function we will use to free the data */
853 static void free_string(void *d){
857 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
863 xbt_test_add0("==== Traverse the empty dynar");
864 d=xbt_dynar_new(sizeof(char *),&free_string);
865 xbt_dynar_foreach(d,cpt,s1){
866 xbt_test_assert0(FALSE,
867 "Damnit, there is something in the empty dynar");
872 xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
873 /* Populate_str [doxygen cruft] */
874 d=xbt_dynar_new(sizeof(char*),&free_string);
875 /* 1. Populate the dynar */
876 for (cpt=0; cpt< NB_ELEM; cpt++) {
877 sprintf(buf,"%d",cpt);
879 xbt_dynar_push(d,&s1);
881 for (cpt=0; cpt< NB_ELEM; cpt++) {
882 sprintf(buf,"%d",cpt);
884 xbt_dynar_replace(d,cpt,&s1);
886 for (cpt=0; cpt< NB_ELEM; cpt++) {
887 sprintf(buf,"%d",cpt);
889 xbt_dynar_replace(d,cpt,&s1);
891 for (cpt=0; cpt< NB_ELEM; cpt++) {
892 sprintf(buf,"%d",cpt);
894 xbt_dynar_replace(d,cpt,&s1);
896 for (cpt=0; cpt< NB_ELEM; cpt++) {
897 sprintf(buf,"%d",cpt);
898 xbt_dynar_shift(d,&s2);
899 xbt_test_assert2 (!strcmp(buf,s2),
900 "The retrieved value is not the same than the injected one (%s!=%s)",
908 xbt_test_add1("==== Unshift, traverse and pop %d strings",NB_ELEM);
909 d=xbt_dynar_new(sizeof(char**),&free_string);
910 for (cpt=0; cpt< NB_ELEM; cpt++) {
911 sprintf(buf,"%d",cpt);
913 xbt_dynar_unshift(d,&s1);
915 /* 2. Traverse the dynar with the macro */
916 xbt_dynar_foreach(d,cpt,s1) {
917 sprintf(buf,"%d",NB_ELEM - cpt -1);
918 xbt_test_assert2 (!strcmp(buf,s1),
919 "The retrieved value is not the same than the injected one (%s!=%s)",
922 /* 3. Traverse the dynar with the macro */
923 for (cpt=0; cpt< NB_ELEM; cpt++) {
924 sprintf(buf,"%d",cpt);
925 xbt_dynar_pop(d,&s2);
926 xbt_test_assert2 (!strcmp(buf,s2),
927 "The retrieved value is not the same than the injected one (%s!=%s)",
931 /* 4. Free the resources */
936 xbt_test_add2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
937 d=xbt_dynar_new(sizeof(char*),&free_string);
938 for (cpt=0; cpt< NB_ELEM; cpt++) {
939 sprintf(buf,"%d",cpt);
941 xbt_dynar_push(d,&s1);
943 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
944 sprintf(buf,"%d",cpt);
946 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
949 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
950 sprintf(buf,"%d",cpt);
951 xbt_dynar_shift(d,&s2);
952 xbt_test_assert2(!strcmp(buf,s2),
953 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
957 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
958 sprintf(buf,"%d",cpt);
959 xbt_dynar_shift(d,&s2);
960 xbt_test_assert2 (!strcmp(buf,s2),
961 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
965 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
966 sprintf(buf,"%d",cpt);
967 xbt_dynar_shift(d,&s2);
968 xbt_test_assert2 (!strcmp(buf,s2),
969 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
977 xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
978 d=xbt_dynar_new(sizeof(char*),&free_string);
979 for (cpt=0; cpt< NB_ELEM; cpt++) {
980 sprintf(buf,"%d",cpt);
982 xbt_dynar_push(d,&s1);
984 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
985 sprintf(buf,"%d",cpt);
986 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
987 xbt_test_assert2(!strcmp(buf,s2),
988 "Remove a bad value. Got %s, expected %s",
992 xbt_dynar_free(&d); /* end_of_doxygen */
994 #endif /* SIMGRID_TEST */