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 /* IMPLEMENTATION NOTE ON SYNCHRONIZATION: every functions which name is prefixed by _
22 * assumes that the dynar is already locked if we have to.
23 * Other functions (public ones) check for this.
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn,xbt,"Dynamic arrays");
28 #define _dynar_lock(dynar) \
30 xbt_mutex_lock(dynar->mutex)
31 #define _dynar_unlock(dynar) \
33 xbt_mutex_unlock(dynar->mutex)
34 #define _sanity_check_dynar(dynar) \
37 #define _sanity_check_idx(idx) \
38 xbt_assert1(idx >= 0, \
39 "dynar idx(=%d) < 0", \
41 #define _check_inbound_idx(dynar, idx) \
42 if (idx>=dynar->used) \
43 THROW2(bound_error,idx, \
44 "dynar is not that long. You asked %d, but it's only %lu long", \
45 (int) (idx), (unsigned long) dynar->used)
46 #define _check_sloppy_inbound_idx(dynar, idx) \
47 if (idx>dynar->used) \
48 THROW2(bound_error,idx, \
49 "dynar is not that long. You asked %d, but it's only %lu long (could have been equal to it)", \
50 (int) (idx), (unsigned long) dynar->used)
51 #define _check_populated_dynar(dynar) \
52 if (dynar->used == 0) \
53 THROW1(bound_error,0, \
54 "dynar %p is empty", dynar)
56 static void _dynar_map(const xbt_dynar_t dynar,
57 void_f_pvoid_t * const op);
60 void _xbt_clear_mem(void * const ptr,
61 const unsigned long length) {
62 memset(ptr, 0, length);
67 _xbt_dynar_expand(xbt_dynar_t const dynar,
69 const unsigned long old_size = dynar->size;
72 char * const old_data = (char *) dynar->data;
74 const unsigned long elmsize = dynar->elmsize;
75 const unsigned long old_length = old_size*elmsize;
77 const unsigned long used = dynar->used;
78 const unsigned long used_length = used*elmsize;
80 const unsigned long new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
81 const unsigned long new_length = new_size*elmsize;
82 char * const new_data = (char *) xbt_malloc0(elmsize*new_size);
84 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
87 memcpy(new_data, old_data, used_length);
88 _xbt_clear_mem(old_data, old_length);
92 _xbt_clear_mem(new_data + used_length, new_length - used_length);
94 dynar->size = new_size;
95 dynar->data = new_data;
101 _xbt_dynar_elm(const xbt_dynar_t dynar,
102 const unsigned long idx) {
103 char * const data = (char*) dynar->data;
104 const unsigned long elmsize = dynar->elmsize;
106 return data + idx*elmsize;
111 _xbt_dynar_get_elm(void * const dst,
112 const xbt_dynar_t dynar,
113 const unsigned long idx) {
114 void * const elm = _xbt_dynar_elm(dynar, idx);
116 memcpy(dst, elm, dynar->elmsize);
121 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
122 const unsigned long idx,
123 const void * const src) {
124 void * const elm = _xbt_dynar_elm(dynar, idx);
125 const unsigned long elmsize = dynar->elmsize;
127 memcpy(elm, src, elmsize);
132 _xbt_dynar_remove_at(xbt_dynar_t const dynar,
134 void * const object) {
136 unsigned long nb_shift;
137 unsigned long offset;
139 _sanity_check_dynar(dynar);
140 _sanity_check_idx(idx);
141 _check_inbound_idx(dynar, idx);
144 _xbt_dynar_get_elm(object, dynar, idx);
145 } else if (dynar->free_f) {
146 if (dynar->elmsize <= SIZEOF_MAX) {
147 char elm[SIZEOF_MAX];
148 _xbt_dynar_get_elm(elm, dynar, idx);
149 (*dynar->free_f)(elm);
151 char *elm=malloc(dynar->elmsize);
152 _xbt_dynar_get_elm(elm, dynar, idx);
153 (*dynar->free_f)(elm);
158 nb_shift = dynar->used-1 - idx;
159 offset = nb_shift * dynar->elmsize;
161 memmove(_xbt_dynar_elm(dynar, idx),
162 _xbt_dynar_elm(dynar, idx+1),
169 xbt_dynar_dump(xbt_dynar_t dynar) {
170 INFO5("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
171 dynar->size, dynar->used, dynar->elmsize, dynar->data, dynar->free_f);
174 /** @brief Constructor
176 * \param elmsize size of each element in the dynar
177 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
179 * Creates a new dynar. If a free_func is provided, the elements have to be
180 * pointer of pointer. That is to say that dynars can contain either base
181 * types (int, char, double, etc) or pointer of pointers (struct **).
184 xbt_dynar_new(const unsigned long elmsize,
185 void_f_pvoid_t * const free_f) {
187 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
191 dynar->elmsize = elmsize;
193 dynar->free_f = free_f;
199 /** @brief Creates a synchronized dynar.
201 * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
205 xbt_dynar_new_sync(const unsigned long elmsize,
206 void_f_pvoid_t * const free_f) {
207 xbt_dynar_t res = xbt_dynar_new(elmsize,free_f);
208 res->mutex = xbt_mutex_init();
212 /** @brief Destructor of the structure not touching to the content
214 * \param dynar poor victim
216 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
217 * is not touched (the \a free_f function is not used)
220 xbt_dynar_free_container(xbt_dynar_t *dynar) {
221 if (dynar && *dynar) {
223 if ((*dynar)->data) {
224 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
225 free((*dynar)->data);
229 xbt_mutex_destroy((*dynar)->mutex);
231 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
238 /** @brief Frees the content and set the size to 0
240 * \param dynar who to squeeze
243 xbt_dynar_reset(xbt_dynar_t const dynar) {
246 _sanity_check_dynar(dynar);
248 DEBUG1("Reset the dynar %p",(void*)dynar);
250 _dynar_map(dynar, dynar->free_f);
260 _dynar_unlock(dynar);
262 /* dynar->data = NULL;*/
266 * \brief Shrink the dynar by removing empty slots at the end of the internal array
267 * \param dynar a dynar
268 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
269 * internal array for further insertions
271 * Reduces the internal array size of the dynar to the number of elements plus
272 * \a empty_slots_wanted.
273 * After removing elements from the dynar, you can call this function to make
274 * the dynar use less memory.
275 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
277 * Note that if \a empty_slots_wanted is greater than the array size, the internal
278 * array is not expanded and nothing is done.
280 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) {
285 size_wanted = dynar->used + empty_slots_wanted;
286 if (size_wanted < dynar->size) {
287 dynar->size = size_wanted;
288 dynar->data = xbt_realloc(dynar->data, sizeof(void*) * dynar->size);
290 _dynar_unlock(dynar);
293 /** @brief Destructor
295 * \param dynar poor victim
297 * kilkil a dynar and its content
301 xbt_dynar_free(xbt_dynar_t * dynar) {
302 if (dynar && *dynar) {
303 xbt_dynar_reset(*dynar);
304 xbt_dynar_free_container(dynar);
307 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
308 void xbt_dynar_free_voidp(void *d) {
309 xbt_dynar_free( (xbt_dynar_t*) d);
312 /** @brief Count of dynar's elements
314 * \param dynar the dynar we want to mesure
317 xbt_dynar_length(const xbt_dynar_t dynar) {
318 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
321 /** @brief Retrieve a copy of the Nth element of a dynar.
323 * \param dynar information dealer
324 * \param idx index of the slot we want to retrieve
325 * \param[out] dst where to put the result to.
328 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
332 _sanity_check_dynar(dynar);
333 _sanity_check_idx(idx);
334 _check_inbound_idx(dynar, idx);
336 _xbt_dynar_get_elm(dst, dynar, idx);
337 _dynar_unlock(dynar);
340 /** @brief Retrieve a pointer to the Nth element of a dynar.
342 * \param dynar information dealer
343 * \param idx index of the slot we want to retrieve
344 * \return the \a idx-th element of \a dynar.
346 * \warning The returned value is the actual content of the dynar.
347 * Make a copy before fooling with it.
350 xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) {
354 _sanity_check_dynar(dynar);
355 _sanity_check_idx(idx);
356 _check_inbound_idx(dynar, idx);
358 res = _xbt_dynar_elm(dynar, idx);
359 _dynar_unlock(dynar);
364 static void XBT_INLINE /* not synchronized */
365 _xbt_dynar_set(xbt_dynar_t dynar,
367 const void * const src) {
369 _sanity_check_dynar(dynar);
370 _sanity_check_idx(idx);
372 _xbt_dynar_expand(dynar, idx+1);
374 if (idx >= dynar->used) {
378 _xbt_dynar_put_elm(dynar, idx, src);
381 /** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed
383 * \param dynar information dealer
384 * \param idx index of the slot we want to modify
385 * \param src What will be feeded to the dynar
387 * If you want to free the previous content, use xbt_dynar_replace().
390 xbt_dynar_set(xbt_dynar_t dynar,
392 const void * const src) {
395 _xbt_dynar_set(dynar,idx,src);
396 _dynar_unlock(dynar);
399 /** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed
405 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
406 * free the previous value at this position. If you don't want to free the
407 * previous content, use xbt_dynar_set().
410 xbt_dynar_replace(xbt_dynar_t dynar,
412 const void * const object) {
414 _sanity_check_dynar(dynar);
415 _sanity_check_idx(idx);
417 if (idx < dynar->used && dynar->free_f) {
418 void * const old_object = _xbt_dynar_elm(dynar, idx);
420 dynar->free_f(old_object);
423 _xbt_dynar_set(dynar, idx, object);
424 _dynar_unlock(dynar);
427 static XBT_INLINE void *
428 _xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
431 unsigned long old_used;
432 unsigned long new_used;
433 unsigned long nb_shift;
435 _sanity_check_dynar(dynar);
436 _sanity_check_idx(idx);
437 _check_sloppy_inbound_idx(dynar, idx);
439 old_used = dynar->used;
440 new_used = old_used + 1;
442 _xbt_dynar_expand(dynar, new_used);
444 nb_shift = old_used - idx;
447 memmove(_xbt_dynar_elm(dynar, idx+1),
448 _xbt_dynar_elm(dynar, idx),
449 nb_shift * dynar->elmsize);
451 dynar->used = new_used;
452 res = _xbt_dynar_elm(dynar,idx);
456 /** @brief Make room for a new element, and return a pointer to it
458 * You can then use regular affectation to set its value instead of relying
459 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
462 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
467 res = _xbt_dynar_insert_at_ptr(dynar,idx);
468 _dynar_unlock(dynar);
472 /** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right
474 * Set the Nth element of a dynar, expanding the dynar if needed, and
475 * moving the previously existing value and all subsequent ones to one
476 * position right in the dynar.
479 xbt_dynar_insert_at(xbt_dynar_t const dynar,
481 const void * const src) {
484 /* checks done in xbt_dynar_insert_at_ptr */
485 memcpy(_xbt_dynar_insert_at_ptr(dynar,idx),
488 _dynar_unlock(dynar);
491 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
493 * Get the Nth element of a dynar, removing it from the dynar and moving
494 * all subsequent values to one position left in the dynar.
496 * If the object argument of this function is a non-null pointer, the removed
497 * element is copied to this address. If not, the element is freed using the
498 * free_f function passed at dynar creation.
501 xbt_dynar_remove_at(xbt_dynar_t const dynar,
503 void * const object) {
506 _xbt_dynar_remove_at(dynar, idx, object);
507 _dynar_unlock(dynar);
510 /** @brief Returns the position of the element in the dynar
512 * Raises not_found_error if not found.
515 xbt_dynar_search(xbt_dynar_t const dynar,
520 for (it=0; it< dynar->used; it++)
521 if (!memcmp(_xbt_dynar_elm(dynar, it),elem,dynar->elmsize)) {
522 _dynar_unlock(dynar);
526 _dynar_unlock(dynar);
527 THROW2(not_found_error,0,"Element %p not part of dynar %p",elem,dynar);
530 /** @brief Returns a boolean indicating whether the element is part of the dynar */
532 xbt_dynar_member(xbt_dynar_t const dynar,
538 xbt_dynar_search(dynar,elem);
540 if (e.category == not_found_error) {
549 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
551 * You can then use regular affectation to set its value instead of relying
552 * on the slow memcpy. This is what xbt_dynar_push_as() does.
555 xbt_dynar_push_ptr(xbt_dynar_t const dynar) {
556 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
559 /** @brief Add an element at the end of the dynar */
561 xbt_dynar_push(xbt_dynar_t const dynar,
562 const void * const src) {
563 /* sanity checks done by insert_at */
564 xbt_dynar_insert_at(dynar, dynar->used, src);
567 /** @brief Mark the last dynar's element as unused and return a pointer to it.
569 * You can then use regular affectation to set its value instead of relying
570 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
573 xbt_dynar_pop_ptr(xbt_dynar_t const dynar) {
577 _check_populated_dynar(dynar);
578 DEBUG1("Pop %p",(void*)dynar);
580 res = _xbt_dynar_elm(dynar,dynar->used);
581 _dynar_unlock(dynar);
585 /** @brief Get and remove the last element of the dynar */
587 xbt_dynar_pop(xbt_dynar_t const dynar,
590 /* sanity checks done by remove_at */
591 DEBUG1("Pop %p",(void*)dynar);
592 xbt_dynar_remove_at(dynar, dynar->used-1, dst);
595 /** @brief Add an element at the begining of the dynar.
597 * This is less efficient than xbt_dynar_push()
600 xbt_dynar_unshift(xbt_dynar_t const dynar,
601 const void * const src) {
603 /* sanity checks done by insert_at */
604 xbt_dynar_insert_at(dynar, 0, src);
607 /** @brief Get and remove the first element of the dynar.
609 * This is less efficient than xbt_dynar_pop()
612 xbt_dynar_shift(xbt_dynar_t const dynar,
615 /* sanity checks done by remove_at */
616 xbt_dynar_remove_at(dynar, 0, dst);
619 static void _dynar_map(const xbt_dynar_t dynar,
620 void_f_pvoid_t * const op) {
621 char elm[SIZEOF_MAX];
622 const unsigned long used = dynar->used;
625 for (i = 0; i < used; i++) {
626 _xbt_dynar_get_elm(elm, dynar, i);
631 /** @brief Apply a function to each member of a dynar
633 * The mapped function may change the value of the element itself,
634 * but should not mess with the structure of the dynar.
636 * If the dynar is synchronized, it is locked during the whole map
637 * operation, so make sure your function don't call any function
638 * from xbt_dynar_* on it, or you'll get a deadlock.
641 xbt_dynar_map(const xbt_dynar_t dynar,
642 void_f_pvoid_t * const op) {
645 _sanity_check_dynar(dynar);
647 _dynar_map(dynar,op);
649 _dynar_unlock(dynar);
652 /** @brief Put the cursor at the begining of the dynar.
654 * Actually, the cursor is set one step before the begining, so that you
655 * can iterate over the dynar with a for loop.
657 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
660 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
661 int * const cursor) {
664 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
668 /** @brief Move the cursor to the next value
670 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
673 _xbt_dynar_cursor_step(const xbt_dynar_t dynar,
674 int * const cursor) {
679 /** @brief Get the data currently pointed by the cursor
681 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
684 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
688 _sanity_check_dynar(dynar);
691 const int idx = *cursor;
693 if (idx >= dynar->used) {
694 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
695 _dynar_unlock(dynar);
698 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
700 _xbt_dynar_get_elm(dst, dynar, idx);
706 /** @brief Removes and free the entry pointed by the cursor
708 * This function can be used while traversing without problem.
710 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
711 int * const cursor) {
713 _xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
716 /** @brief Unlocks a synchronized dynar when you want to break the traversal
718 * This function must be used if you <tt>break</tt> the
719 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
720 * regular traversal reaching the end of the elements
722 void xbt_dynar_cursor_unlock(xbt_dynar_t dynar) {
723 _dynar_unlock(dynar);
730 XBT_TEST_SUITE("dynar","Dynar data container");
731 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
732 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
734 XBT_TEST_UNIT("int",test_dynar_int,"Dynars of integers") {
735 /* Vars_decl [doxygen cruft] */
740 xbt_test_add0("==== Traverse the empty dynar");
741 d=xbt_dynar_new(sizeof(int),NULL);
742 xbt_dynar_foreach(d,cursor,i){
743 xbt_assert0(0,"Damnit, there is something in the empty dynar");
748 xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
750 /* Populate_ints [doxygen cruft] */
751 /* 1. Populate the dynar */
752 d=xbt_dynar_new(sizeof(int),NULL);
753 for (cpt=0; cpt< NB_ELEM; cpt++) {
754 xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
755 /* xbt_dynar_push(d,&cpt); This would also work */
756 xbt_test_log2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
759 /* 2. Traverse manually the dynar */
760 for (cursor=0; cursor< NB_ELEM; cursor++) {
761 iptr=xbt_dynar_get_ptr(d,cursor);
762 xbt_test_assert2(cursor == *iptr,
763 "The retrieved value is not the same than the injected one (%d!=%d)",
767 /* 3. Traverse the dynar using the neat macro to that extend */
768 xbt_dynar_foreach(d,cursor,cpt){
769 xbt_test_assert2(cursor == cpt,
770 "The retrieved value is not the same than the injected one (%d!=%d)",
773 /* end_of_traversal */
775 for (cpt=0; cpt< NB_ELEM; cpt++)
776 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
778 for (cpt=0; cpt< NB_ELEM; cpt++)
779 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
780 /* xbt_dynar_set(d,cpt,&cpt);*/
782 for (cpt=0; cpt< NB_ELEM; cpt++)
783 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
786 xbt_dynar_foreach(d,cursor,i){
787 xbt_test_assert2(i == cpt,
788 "The retrieved value is not the same than the injected one (%d!=%d)",
792 xbt_test_assert2(cpt == NB_ELEM,
793 "Cannot retrieve my %d values. Last got one is %d",
796 /* shifting [doxygen cruft] */
797 /* 4. Shift all the values */
798 for (cpt=0; cpt< NB_ELEM; cpt++) {
799 xbt_dynar_shift(d,&i);
800 xbt_test_assert2(i == cpt,
801 "The retrieved value is not the same than the injected one (%d!=%d)",
803 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
806 /* 5. Free the resources */
811 xbt_test_add1("==== Unshift/pop %d int",NB_ELEM);
812 d=xbt_dynar_new(sizeof(int),NULL);
813 for (cpt=0; cpt< NB_ELEM; cpt++) {
814 xbt_dynar_unshift(d,&cpt);
815 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
817 for (cpt=0; cpt< NB_ELEM; cpt++) {
818 i=xbt_dynar_pop_as(d,int);
819 xbt_test_assert2(i == cpt,
820 "The retrieved value is not the same than the injected one (%d!=%d)",
822 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
828 xbt_test_add1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
829 d=xbt_dynar_new(sizeof(int),NULL);
830 for (cpt=0; cpt< NB_ELEM; cpt++) {
831 xbt_dynar_push_as(d,int,cpt);
832 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
834 for (cpt=0; cpt< 1000; cpt++) {
835 xbt_dynar_insert_at_as(d,2500,int,cpt);
836 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
839 for (cpt=0; cpt< 2500; cpt++) {
840 xbt_dynar_shift(d,&i);
841 xbt_test_assert2(i == cpt,
842 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
844 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
846 for (cpt=999; cpt>=0; cpt--) {
847 xbt_dynar_shift(d,&i);
848 xbt_test_assert2(i == cpt,
849 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
852 for (cpt=2500; cpt< NB_ELEM; cpt++) {
853 xbt_dynar_shift(d,&i);
854 xbt_test_assert2(i == cpt,
855 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
862 xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
863 d=xbt_dynar_new(sizeof(int),NULL);
864 for (cpt=0; cpt< NB_ELEM; cpt++)
865 xbt_dynar_push_as(d,int,cpt);
867 for (cpt=2000; cpt< 4000; cpt++) {
868 xbt_dynar_remove_at(d,2000,&i);
869 xbt_test_assert2(i == cpt,
870 "Remove a bad value. Got %d, expected %d",
872 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
877 /*******************************************************************************/
878 /*******************************************************************************/
879 /*******************************************************************************/
880 XBT_TEST_UNIT("double",test_dynar_double,"Dynars of doubles") {
885 xbt_test_add0("==== Traverse the empty dynar");
886 d=xbt_dynar_new(sizeof(int),NULL);
887 xbt_dynar_foreach(d,cursor,cpt){
888 xbt_test_assert0(FALSE,
889 "Damnit, there is something in the empty dynar");
894 xbt_test_add0("==== Push/shift 5000 doubles");
895 d=xbt_dynar_new(sizeof(double),NULL);
896 for (cpt=0; cpt< 5000; cpt++) {
898 xbt_dynar_push(d,&d1);
900 xbt_dynar_foreach(d,cursor,d2){
902 xbt_test_assert2(d1 == d2,
903 "The retrieved value is not the same than the injected one (%f!=%f)",
906 for (cpt=0; cpt< 5000; cpt++) {
908 xbt_dynar_shift(d,&d2);
909 xbt_test_assert2(d1 == d2,
910 "The retrieved value is not the same than the injected one (%f!=%f)",
917 xbt_test_add0("==== Unshift/pop 5000 doubles");
918 d=xbt_dynar_new(sizeof(double),NULL);
919 for (cpt=0; cpt< 5000; cpt++) {
921 xbt_dynar_unshift(d,&d1);
923 for (cpt=0; cpt< 5000; cpt++) {
925 xbt_dynar_pop(d,&d2);
926 xbt_test_assert2 (d1 == d2,
927 "The retrieved value is not the same than the injected one (%f!=%f)",
935 xbt_test_add0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
936 d=xbt_dynar_new(sizeof(double),NULL);
937 for (cpt=0; cpt< 5000; cpt++) {
939 xbt_dynar_push(d,&d1);
941 for (cpt=0; cpt< 1000; cpt++) {
943 xbt_dynar_insert_at(d,2500,&d1);
946 for (cpt=0; cpt< 2500; cpt++) {
948 xbt_dynar_shift(d,&d2);
949 xbt_test_assert2(d1 == d2,
950 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
952 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
954 for (cpt=999; cpt>=0; cpt--) {
956 xbt_dynar_shift(d,&d2);
957 xbt_test_assert2 (d1 == d2,
958 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
961 for (cpt=2500; cpt< 5000; cpt++) {
963 xbt_dynar_shift(d,&d2);
964 xbt_test_assert2 (d1 == d2,
965 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
972 xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
973 d=xbt_dynar_new(sizeof(double),NULL);
974 for (cpt=0; cpt< 5000; cpt++) {
976 xbt_dynar_push(d,&d1);
978 for (cpt=2000; cpt< 4000; cpt++) {
980 xbt_dynar_remove_at(d,2000,&d2);
981 xbt_test_assert2 (d1 == d2,
982 "Remove a bad value. Got %f, expected %f",
990 /* doxygen_string_cruft */
992 /* The function we will use to free the data */
993 static void free_string(void *d){
997 /*******************************************************************************/
998 /*******************************************************************************/
999 /*******************************************************************************/
1000 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
1006 xbt_test_add0("==== Traverse the empty dynar");
1007 d=xbt_dynar_new(sizeof(char *),&free_string);
1008 xbt_dynar_foreach(d,cpt,s1){
1009 xbt_test_assert0(FALSE,
1010 "Damnit, there is something in the empty dynar");
1015 xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
1016 /* Populate_str [doxygen cruft] */
1017 d=xbt_dynar_new(sizeof(char*),&free_string);
1018 /* 1. Populate the dynar */
1019 for (cpt=0; cpt< NB_ELEM; cpt++) {
1020 sprintf(buf,"%d",cpt);
1022 xbt_dynar_push(d,&s1);
1024 for (cpt=0; cpt< NB_ELEM; cpt++) {
1025 sprintf(buf,"%d",cpt);
1027 xbt_dynar_replace(d,cpt,&s1);
1029 for (cpt=0; cpt< NB_ELEM; cpt++) {
1030 sprintf(buf,"%d",cpt);
1032 xbt_dynar_replace(d,cpt,&s1);
1034 for (cpt=0; cpt< NB_ELEM; cpt++) {
1035 sprintf(buf,"%d",cpt);
1037 xbt_dynar_replace(d,cpt,&s1);
1039 for (cpt=0; cpt< NB_ELEM; cpt++) {
1040 sprintf(buf,"%d",cpt);
1041 xbt_dynar_shift(d,&s2);
1042 xbt_test_assert2 (!strcmp(buf,s2),
1043 "The retrieved value is not the same than the injected one (%s!=%s)",
1051 xbt_test_add1("==== Unshift, traverse and pop %d strings",NB_ELEM);
1052 d=xbt_dynar_new(sizeof(char**),&free_string);
1053 for (cpt=0; cpt< NB_ELEM; cpt++) {
1054 sprintf(buf,"%d",cpt);
1056 xbt_dynar_unshift(d,&s1);
1058 /* 2. Traverse the dynar with the macro */
1059 xbt_dynar_foreach(d,cpt,s1) {
1060 sprintf(buf,"%d",NB_ELEM - cpt -1);
1061 xbt_test_assert2 (!strcmp(buf,s1),
1062 "The retrieved value is not the same than the injected one (%s!=%s)",
1065 /* 3. Traverse the dynar with the macro */
1066 for (cpt=0; cpt< NB_ELEM; cpt++) {
1067 sprintf(buf,"%d",cpt);
1068 xbt_dynar_pop(d,&s2);
1069 xbt_test_assert2 (!strcmp(buf,s2),
1070 "The retrieved value is not the same than the injected one (%s!=%s)",
1074 /* 4. Free the resources */
1079 xbt_test_add2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
1080 d=xbt_dynar_new(sizeof(char*),&free_string);
1081 for (cpt=0; cpt< NB_ELEM; cpt++) {
1082 sprintf(buf,"%d",cpt);
1084 xbt_dynar_push(d,&s1);
1086 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
1087 sprintf(buf,"%d",cpt);
1089 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
1092 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
1093 sprintf(buf,"%d",cpt);
1094 xbt_dynar_shift(d,&s2);
1095 xbt_test_assert2(!strcmp(buf,s2),
1096 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1100 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
1101 sprintf(buf,"%d",cpt);
1102 xbt_dynar_shift(d,&s2);
1103 xbt_test_assert2 (!strcmp(buf,s2),
1104 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1108 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
1109 sprintf(buf,"%d",cpt);
1110 xbt_dynar_shift(d,&s2);
1111 xbt_test_assert2 (!strcmp(buf,s2),
1112 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1120 xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
1121 d=xbt_dynar_new(sizeof(char*),&free_string);
1122 for (cpt=0; cpt< NB_ELEM; cpt++) {
1123 sprintf(buf,"%d",cpt);
1125 xbt_dynar_push(d,&s1);
1127 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
1128 sprintf(buf,"%d",cpt);
1129 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
1130 xbt_test_assert2(!strcmp(buf,s2),
1131 "Remove a bad value. Got %s, expected %s",
1135 xbt_dynar_free(&d); /* end_of_doxygen */
1139 /*******************************************************************************/
1140 /*******************************************************************************/
1141 /*******************************************************************************/
1142 #include "xbt/synchro.h"
1143 static void pusher_f(void *a) {
1144 xbt_dynar_t d=(xbt_dynar_t)a;
1146 for (i=0; i<500; i++) {
1147 xbt_dynar_push(d,&i);
1150 static void poper_f(void *a) {
1151 xbt_dynar_t d=(xbt_dynar_t)a;
1156 for (i=0; i<500; i++) {
1158 xbt_dynar_pop(d,&data);
1160 if (e.category == bound_error) {
1171 XBT_TEST_UNIT("synchronized int",test_dynar_sync_int,"Synchronized dynars of integers") {
1172 /* Vars_decl [doxygen cruft] */
1174 xbt_thread_t pusher,poper;
1176 xbt_test_add0("==== Have a pusher and a popper on the dynar");
1177 d=xbt_dynar_new_sync(sizeof(int),NULL);
1178 pusher = xbt_thread_create("pusher",pusher_f,d);
1179 poper = xbt_thread_create("poper",poper_f,d);
1180 xbt_thread_join(pusher);
1181 xbt_thread_join(poper);
1185 #endif /* SIMGRID_TEST */