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)
57 void _xbt_clear_mem(void * const ptr,
58 const unsigned long length) {
59 memset(ptr, 0, length);
64 _xbt_dynar_expand(xbt_dynar_t const dynar,
66 const unsigned long old_size = dynar->size;
69 char * const old_data = (char *) dynar->data;
71 const unsigned long elmsize = dynar->elmsize;
72 const unsigned long old_length = old_size*elmsize;
74 const unsigned long used = dynar->used;
75 const unsigned long used_length = used*elmsize;
77 const unsigned long new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
78 const unsigned long new_length = new_size*elmsize;
79 char * const new_data = (char *) xbt_malloc0(elmsize*new_size);
81 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
84 memcpy(new_data, old_data, used_length);
85 _xbt_clear_mem(old_data, old_length);
89 _xbt_clear_mem(new_data + used_length, new_length - used_length);
91 dynar->size = new_size;
92 dynar->data = new_data;
98 _xbt_dynar_elm(const xbt_dynar_t dynar,
99 const unsigned long idx) {
100 char * const data = (char*) dynar->data;
101 const unsigned long elmsize = dynar->elmsize;
103 return data + idx*elmsize;
108 _xbt_dynar_get_elm(void * const dst,
109 const xbt_dynar_t dynar,
110 const unsigned long idx) {
111 void * const elm = _xbt_dynar_elm(dynar, idx);
113 memcpy(dst, elm, dynar->elmsize);
118 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
119 const unsigned long idx,
120 const void * const src) {
121 void * const elm = _xbt_dynar_elm(dynar, idx);
122 const unsigned long elmsize = dynar->elmsize;
124 memcpy(elm, src, elmsize);
128 xbt_dynar_dump(xbt_dynar_t dynar) {
129 INFO5("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
130 dynar->size, dynar->used, dynar->elmsize, dynar->data, dynar->free_f);
133 /** @brief Constructor
135 * \param elmsize size of each element in the dynar
136 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
138 * Creates a new dynar. If a free_func is provided, the elements have to be
139 * pointer of pointer. That is to say that dynars can contain either base
140 * types (int, char, double, etc) or pointer of pointers (struct **).
143 xbt_dynar_new(const unsigned long elmsize,
144 void_f_pvoid_t * const free_f) {
146 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
150 dynar->elmsize = elmsize;
152 dynar->free_f = free_f;
158 /** @brief Creates a synchronized dynar.
160 * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
164 xbt_dynar_new_sync(const unsigned long elmsize,
165 void_f_pvoid_t * const free_f) {
166 xbt_dynar_t res = xbt_dynar_new(elmsize,free_f);
167 res->mutex = xbt_mutex_init();
171 /** @brief Destructor of the structure not touching to the content
173 * \param dynar poor victim
175 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
176 * is not touched (the \a free_f function is not used)
179 xbt_dynar_free_container(xbt_dynar_t *dynar) {
180 if (dynar && *dynar) {
182 if ((*dynar)->data) {
183 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
184 free((*dynar)->data);
187 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
190 xbt_mutex_destroy((*dynar)->mutex);
197 /** @brief Frees the content and set the size to 0
199 * \param dynar who to squeeze
202 xbt_dynar_reset(xbt_dynar_t const dynar) {
205 _sanity_check_dynar(dynar);
207 DEBUG1("Reset the dynar %p",(void*)dynar);
209 xbt_dynar_map(dynar, dynar->free_f);
219 _dynar_unlock(dynar);
221 /* dynar->data = NULL;*/
225 * \brief Shrink the dynar by removing empty slots at the end of the internal array
226 * \param dynar a dynar
227 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
228 * internal array for further insertions
230 * Reduces the internal array size of the dynar to the number of elements plus
231 * \a empty_slots_wanted.
232 * After removing elements from the dynar, you can call this function to make
233 * the dynar use less memory.
234 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
236 * Note that if \a empty_slots_wanted is greater than the array size, the internal
237 * array is not expanded and nothing is done.
239 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) {
244 size_wanted = dynar->used + empty_slots_wanted;
245 if (size_wanted < dynar->size) {
246 dynar->size = size_wanted;
247 dynar->data = xbt_realloc(dynar->data, sizeof(void*) * dynar->size);
249 _dynar_unlock(dynar);
252 /** @brief Destructor
254 * \param dynar poor victim
256 * kilkil a dynar and its content
260 xbt_dynar_free(xbt_dynar_t * dynar) {
261 if (dynar && *dynar) {
262 xbt_dynar_reset(*dynar);
263 xbt_dynar_free_container(dynar);
266 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
267 void xbt_dynar_free_voidp(void *d) {
268 xbt_dynar_free( (xbt_dynar_t*) d);
271 /** @brief Count of dynar's elements
273 * \param dynar the dynar we want to mesure
276 xbt_dynar_length(const xbt_dynar_t dynar) {
277 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
280 /** @brief Retrieve a copy of the Nth element of a dynar.
282 * \param dynar information dealer
283 * \param idx index of the slot we want to retrieve
284 * \param[out] dst where to put the result to.
287 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
291 _sanity_check_dynar(dynar);
292 _sanity_check_idx(idx);
293 _check_inbound_idx(dynar, idx);
295 _xbt_dynar_get_elm(dst, dynar, idx);
296 _dynar_unlock(dynar);
299 /** @brief Retrieve a pointer to the Nth element of a dynar.
301 * \param dynar information dealer
302 * \param idx index of the slot we want to retrieve
303 * \return the \a idx-th element of \a dynar.
305 * \warning The returned value is the actual content of the dynar.
306 * Make a copy before fooling with it.
309 xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) {
313 _sanity_check_dynar(dynar);
314 _sanity_check_idx(idx);
315 _check_inbound_idx(dynar, idx);
317 res = _xbt_dynar_elm(dynar, idx);
318 _dynar_unlock(dynar);
323 static void XBT_INLINE /* not synchronized */
324 _xbt_dynar_set(xbt_dynar_t dynar,
326 const void * const src) {
328 _sanity_check_dynar(dynar);
329 _sanity_check_idx(idx);
331 _xbt_dynar_expand(dynar, idx+1);
333 if (idx >= dynar->used) {
337 _xbt_dynar_put_elm(dynar, idx, src);
340 /** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed
342 * \param dynar information dealer
343 * \param idx index of the slot we want to modify
344 * \param src What will be feeded to the dynar
346 * If you want to free the previous content, use xbt_dynar_replace().
349 xbt_dynar_set(xbt_dynar_t dynar,
351 const void * const src) {
354 _xbt_dynar_set(dynar,idx,src);
355 _dynar_unlock(dynar);
358 /** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed
364 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
365 * free the previous value at this position. If you don't want to free the
366 * previous content, use xbt_dynar_set().
369 xbt_dynar_replace(xbt_dynar_t dynar,
371 const void * const object) {
373 _sanity_check_dynar(dynar);
374 _sanity_check_idx(idx);
376 if (idx < dynar->used && dynar->free_f) {
377 void * const old_object = _xbt_dynar_elm(dynar, idx);
379 dynar->free_f(old_object);
382 _xbt_dynar_set(dynar, idx, object);
383 _dynar_unlock(dynar);
386 static XBT_INLINE void *
387 _xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
391 _sanity_check_dynar(dynar);
392 _sanity_check_idx(idx);
393 _check_sloppy_inbound_idx(dynar, idx);
395 const unsigned long old_used = dynar->used;
396 const unsigned long new_used = old_used + 1;
398 _xbt_dynar_expand(dynar, new_used);
400 const unsigned long nb_shift = old_used - idx;
403 memmove(_xbt_dynar_elm(dynar, idx+1),
404 _xbt_dynar_elm(dynar, idx),
405 nb_shift * dynar->elmsize);
407 dynar->used = new_used;
408 res = _xbt_dynar_elm(dynar,idx);
412 /** @brief Make room for a new element, and return a pointer to it
414 * You can then use regular affectation to set its value instead of relying
415 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
418 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
423 res = _xbt_dynar_insert_at_ptr(dynar,idx);
424 _dynar_unlock(dynar);
428 /** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right
430 * Set the Nth element of a dynar, expanding the dynar if needed, and
431 * moving the previously existing value and all subsequent ones to one
432 * position right in the dynar.
435 xbt_dynar_insert_at(xbt_dynar_t const dynar,
437 const void * const src) {
440 /* checks done in xbt_dynar_insert_at_ptr */
441 memcpy(_xbt_dynar_insert_at_ptr(dynar,idx),
444 _dynar_unlock(dynar);
447 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
449 * Get the Nth element of a dynar, removing it from the dynar and moving
450 * all subsequent values to one position left in the dynar.
452 * If the object argument of this function is a non-null pointer, the removed
453 * element is copied to this address. If not, the element is freed using the
454 * free_f function passed at dynar creation.
457 xbt_dynar_remove_at(xbt_dynar_t const dynar,
459 void * const object) {
461 unsigned long nb_shift;
462 unsigned long offset;
465 _sanity_check_dynar(dynar);
466 _sanity_check_idx(idx);
467 _check_inbound_idx(dynar, idx);
470 _xbt_dynar_get_elm(object, dynar, idx);
471 } else if (dynar->free_f) {
472 if (dynar->elmsize <= SIZEOF_MAX) {
473 char elm[SIZEOF_MAX];
474 _xbt_dynar_get_elm(elm, dynar, idx);
475 (*dynar->free_f)(elm);
477 char *elm=malloc(dynar->elmsize);
478 _xbt_dynar_get_elm(elm, dynar, idx);
479 (*dynar->free_f)(elm);
484 nb_shift = dynar->used-1 - idx;
485 offset = nb_shift * dynar->elmsize;
487 memmove(_xbt_dynar_elm(dynar, idx),
488 _xbt_dynar_elm(dynar, idx+1),
492 _dynar_unlock(dynar);
495 /** @brief Returns the position of the element in the dynar
497 * Raises not_found_error if not found.
500 xbt_dynar_search(xbt_dynar_t const dynar,
505 for (it=0; it< dynar->used; it++)
506 if (!memcmp(_xbt_dynar_elm(dynar, it),elem,dynar->elmsize)) {
507 _dynar_unlock(dynar);
511 _dynar_unlock(dynar);
512 THROW2(not_found_error,0,"Element %p not part of dynar %p",elem,dynar);
515 /** @brief Returns a boolean indicating whether the element is part of the dynar */
517 xbt_dynar_member(xbt_dynar_t const dynar,
523 xbt_dynar_search(dynar,elem);
525 if (e.category == not_found_error) {
534 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
536 * You can then use regular affectation to set its value instead of relying
537 * on the slow memcpy. This is what xbt_dynar_push_as() does.
540 xbt_dynar_push_ptr(xbt_dynar_t const dynar) {
541 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
544 /** @brief Add an element at the end of the dynar */
546 xbt_dynar_push(xbt_dynar_t const dynar,
547 const void * const src) {
548 /* sanity checks done by insert_at */
549 xbt_dynar_insert_at(dynar, dynar->used, src);
552 /** @brief Mark the last dynar's element as unused and return a pointer to it.
554 * You can then use regular affectation to set its value instead of relying
555 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
558 xbt_dynar_pop_ptr(xbt_dynar_t const dynar) {
562 _check_populated_dynar(dynar);
563 DEBUG1("Pop %p",(void*)dynar);
565 res = _xbt_dynar_elm(dynar,dynar->used);
566 _dynar_unlock(dynar);
570 /** @brief Get and remove the last element of the dynar */
572 xbt_dynar_pop(xbt_dynar_t const dynar,
575 /* sanity checks done by remove_at */
576 DEBUG1("Pop %p",(void*)dynar);
577 xbt_dynar_remove_at(dynar, dynar->used-1, dst);
580 /** @brief Add an element at the begining of the dynar.
582 * This is less efficient than xbt_dynar_push()
585 xbt_dynar_unshift(xbt_dynar_t const dynar,
586 const void * const src) {
588 /* sanity checks done by insert_at */
589 xbt_dynar_insert_at(dynar, 0, src);
592 /** @brief Get and remove the first element of the dynar.
594 * This is less efficient than xbt_dynar_pop()
597 xbt_dynar_shift(xbt_dynar_t const dynar,
600 /* sanity checks done by remove_at */
601 xbt_dynar_remove_at(dynar, 0, dst);
604 /** @brief Apply a function to each member of a dynar
606 * The mapped function may change the value of the element itself,
607 * but should not mess with the structure of the dynar.
609 * If the dynar is synchronized, it is locked during the whole map
610 * operation, so make sure your function don't call any function
611 * from xbt_dynar_* on it, or you'll get a deadlock.
614 xbt_dynar_map(const xbt_dynar_t dynar,
615 void_f_pvoid_t * const op) {
618 _sanity_check_dynar(dynar);
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);
630 _dynar_unlock(dynar);
633 /** @brief Put the cursor at the begining of the dynar.
635 * Actually, the cursor is set one step before the begining, so that you
636 * can iterate over the dynar with a for loop.
638 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
641 _xbt_dynar_cursor_first(const xbt_dynar_t dynar,
642 int * const cursor) {
645 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
649 /** @brief Move the cursor to the next value
651 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
654 _xbt_dynar_cursor_step(const xbt_dynar_t dynar,
655 int * const cursor) {
660 /** @brief Get the data currently pointed by the cursor
662 * @warning Do not call this function directly, but only through xbt_dynar_foreach.
665 _xbt_dynar_cursor_get(const xbt_dynar_t dynar,
669 _sanity_check_dynar(dynar);
672 const int idx = *cursor;
674 if (idx >= dynar->used) {
675 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
676 _dynar_unlock(dynar);
679 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
681 _xbt_dynar_get_elm(dst, dynar, idx);
687 /** @brief Removes and free the entry pointed by the cursor
689 * This function can be used while traversing without problem.
691 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
692 int * const cursor) {
694 xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
697 /** @brief Unlocks a synchronized dynar when you want to break the traversal
699 * This function must be used if you <tt>break</tt> the
700 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
701 * regular traversal reaching the end of the elements
703 void xbt_dynar_cursor_unlock(xbt_dynar_t dynar) {
704 _dynar_unlock(dynar);
711 XBT_TEST_SUITE("dynar","Dynar data container");
712 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
713 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
715 XBT_TEST_UNIT("int",test_dynar_int,"Dynars of integers") {
716 /* Vars_decl [doxygen cruft] */
721 xbt_test_add0("==== Traverse the empty dynar");
722 d=xbt_dynar_new(sizeof(int),NULL);
723 xbt_dynar_foreach(d,cursor,i){
724 xbt_assert0(0,"Damnit, there is something in the empty dynar");
729 xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
731 /* Populate_ints [doxygen cruft] */
732 /* 1. Populate the dynar */
733 d=xbt_dynar_new(sizeof(int),NULL);
734 for (cpt=0; cpt< NB_ELEM; cpt++) {
735 xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
736 /* xbt_dynar_push(d,&cpt); This would also work */
737 xbt_test_log2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
740 /* 2. Traverse manually the dynar */
741 for (cursor=0; cursor< NB_ELEM; cursor++) {
742 iptr=xbt_dynar_get_ptr(d,cursor);
743 xbt_test_assert2(cursor == *iptr,
744 "The retrieved value is not the same than the injected one (%d!=%d)",
748 /* 3. Traverse the dynar using the neat macro to that extend */
749 xbt_dynar_foreach(d,cursor,cpt){
750 xbt_test_assert2(cursor == cpt,
751 "The retrieved value is not the same than the injected one (%d!=%d)",
754 /* end_of_traversal */
756 for (cpt=0; cpt< NB_ELEM; cpt++)
757 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
759 for (cpt=0; cpt< NB_ELEM; cpt++)
760 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
761 /* xbt_dynar_set(d,cpt,&cpt);*/
763 for (cpt=0; cpt< NB_ELEM; cpt++)
764 *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
767 xbt_dynar_foreach(d,cursor,i){
768 xbt_test_assert2(i == cpt,
769 "The retrieved value is not the same than the injected one (%d!=%d)",
773 xbt_test_assert2(cpt == NB_ELEM,
774 "Cannot retrieve my %d values. Last got one is %d",
777 /* shifting [doxygen cruft] */
778 /* 4. Shift all the values */
779 for (cpt=0; cpt< NB_ELEM; cpt++) {
780 xbt_dynar_shift(d,&i);
781 xbt_test_assert2(i == cpt,
782 "The retrieved value is not the same than the injected one (%d!=%d)",
784 xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
787 /* 5. Free the resources */
792 xbt_test_add1("==== Unshift/pop %d int",NB_ELEM);
793 d=xbt_dynar_new(sizeof(int),NULL);
794 for (cpt=0; cpt< NB_ELEM; cpt++) {
795 xbt_dynar_unshift(d,&cpt);
796 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
798 for (cpt=0; cpt< NB_ELEM; cpt++) {
799 i=xbt_dynar_pop_as(d,int);
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));
809 xbt_test_add1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
810 d=xbt_dynar_new(sizeof(int),NULL);
811 for (cpt=0; cpt< NB_ELEM; cpt++) {
812 xbt_dynar_push_as(d,int,cpt);
813 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
815 for (cpt=0; cpt< 1000; cpt++) {
816 xbt_dynar_insert_at_as(d,2500,int,cpt);
817 DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
820 for (cpt=0; cpt< 2500; cpt++) {
821 xbt_dynar_shift(d,&i);
822 xbt_test_assert2(i == cpt,
823 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
825 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
827 for (cpt=999; cpt>=0; cpt--) {
828 xbt_dynar_shift(d,&i);
829 xbt_test_assert2(i == cpt,
830 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
833 for (cpt=2500; cpt< NB_ELEM; cpt++) {
834 xbt_dynar_shift(d,&i);
835 xbt_test_assert2(i == cpt,
836 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
843 xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
844 d=xbt_dynar_new(sizeof(int),NULL);
845 for (cpt=0; cpt< NB_ELEM; cpt++)
846 xbt_dynar_push_as(d,int,cpt);
848 for (cpt=2000; cpt< 4000; cpt++) {
849 xbt_dynar_remove_at(d,2000,&i);
850 xbt_test_assert2(i == cpt,
851 "Remove a bad value. Got %d, expected %d",
853 DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
858 /*******************************************************************************/
859 /*******************************************************************************/
860 /*******************************************************************************/
861 XBT_TEST_UNIT("double",test_dynar_double,"Dynars of doubles") {
866 xbt_test_add0("==== Traverse the empty dynar");
867 d=xbt_dynar_new(sizeof(int),NULL);
868 xbt_dynar_foreach(d,cursor,cpt){
869 xbt_test_assert0(FALSE,
870 "Damnit, there is something in the empty dynar");
875 xbt_test_add0("==== Push/shift 5000 doubles");
876 d=xbt_dynar_new(sizeof(double),NULL);
877 for (cpt=0; cpt< 5000; cpt++) {
879 xbt_dynar_push(d,&d1);
881 xbt_dynar_foreach(d,cursor,d2){
883 xbt_test_assert2(d1 == d2,
884 "The retrieved value is not the same than the injected one (%f!=%f)",
887 for (cpt=0; cpt< 5000; cpt++) {
889 xbt_dynar_shift(d,&d2);
890 xbt_test_assert2(d1 == d2,
891 "The retrieved value is not the same than the injected one (%f!=%f)",
898 xbt_test_add0("==== Unshift/pop 5000 doubles");
899 d=xbt_dynar_new(sizeof(double),NULL);
900 for (cpt=0; cpt< 5000; cpt++) {
902 xbt_dynar_unshift(d,&d1);
904 for (cpt=0; cpt< 5000; cpt++) {
906 xbt_dynar_pop(d,&d2);
907 xbt_test_assert2 (d1 == d2,
908 "The retrieved value is not the same than the injected one (%f!=%f)",
916 xbt_test_add0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
917 d=xbt_dynar_new(sizeof(double),NULL);
918 for (cpt=0; cpt< 5000; cpt++) {
920 xbt_dynar_push(d,&d1);
922 for (cpt=0; cpt< 1000; cpt++) {
924 xbt_dynar_insert_at(d,2500,&d1);
927 for (cpt=0; cpt< 2500; cpt++) {
929 xbt_dynar_shift(d,&d2);
930 xbt_test_assert2(d1 == d2,
931 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
933 DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
935 for (cpt=999; cpt>=0; cpt--) {
937 xbt_dynar_shift(d,&d2);
938 xbt_test_assert2 (d1 == d2,
939 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
942 for (cpt=2500; cpt< 5000; cpt++) {
944 xbt_dynar_shift(d,&d2);
945 xbt_test_assert2 (d1 == d2,
946 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
953 xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
954 d=xbt_dynar_new(sizeof(double),NULL);
955 for (cpt=0; cpt< 5000; cpt++) {
957 xbt_dynar_push(d,&d1);
959 for (cpt=2000; cpt< 4000; cpt++) {
961 xbt_dynar_remove_at(d,2000,&d2);
962 xbt_test_assert2 (d1 == d2,
963 "Remove a bad value. Got %f, expected %f",
971 /* doxygen_string_cruft */
973 /* The function we will use to free the data */
974 static void free_string(void *d){
978 /*******************************************************************************/
979 /*******************************************************************************/
980 /*******************************************************************************/
981 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
987 xbt_test_add0("==== Traverse the empty dynar");
988 d=xbt_dynar_new(sizeof(char *),&free_string);
989 xbt_dynar_foreach(d,cpt,s1){
990 xbt_test_assert0(FALSE,
991 "Damnit, there is something in the empty dynar");
996 xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
997 /* Populate_str [doxygen cruft] */
998 d=xbt_dynar_new(sizeof(char*),&free_string);
999 /* 1. Populate the dynar */
1000 for (cpt=0; cpt< NB_ELEM; cpt++) {
1001 sprintf(buf,"%d",cpt);
1003 xbt_dynar_push(d,&s1);
1005 for (cpt=0; cpt< NB_ELEM; cpt++) {
1006 sprintf(buf,"%d",cpt);
1008 xbt_dynar_replace(d,cpt,&s1);
1010 for (cpt=0; cpt< NB_ELEM; cpt++) {
1011 sprintf(buf,"%d",cpt);
1013 xbt_dynar_replace(d,cpt,&s1);
1015 for (cpt=0; cpt< NB_ELEM; cpt++) {
1016 sprintf(buf,"%d",cpt);
1018 xbt_dynar_replace(d,cpt,&s1);
1020 for (cpt=0; cpt< NB_ELEM; cpt++) {
1021 sprintf(buf,"%d",cpt);
1022 xbt_dynar_shift(d,&s2);
1023 xbt_test_assert2 (!strcmp(buf,s2),
1024 "The retrieved value is not the same than the injected one (%s!=%s)",
1032 xbt_test_add1("==== Unshift, traverse and pop %d strings",NB_ELEM);
1033 d=xbt_dynar_new(sizeof(char**),&free_string);
1034 for (cpt=0; cpt< NB_ELEM; cpt++) {
1035 sprintf(buf,"%d",cpt);
1037 xbt_dynar_unshift(d,&s1);
1039 /* 2. Traverse the dynar with the macro */
1040 xbt_dynar_foreach(d,cpt,s1) {
1041 sprintf(buf,"%d",NB_ELEM - cpt -1);
1042 xbt_test_assert2 (!strcmp(buf,s1),
1043 "The retrieved value is not the same than the injected one (%s!=%s)",
1046 /* 3. Traverse the dynar with the macro */
1047 for (cpt=0; cpt< NB_ELEM; cpt++) {
1048 sprintf(buf,"%d",cpt);
1049 xbt_dynar_pop(d,&s2);
1050 xbt_test_assert2 (!strcmp(buf,s2),
1051 "The retrieved value is not the same than the injected one (%s!=%s)",
1055 /* 4. Free the resources */
1060 xbt_test_add2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
1061 d=xbt_dynar_new(sizeof(char*),&free_string);
1062 for (cpt=0; cpt< NB_ELEM; cpt++) {
1063 sprintf(buf,"%d",cpt);
1065 xbt_dynar_push(d,&s1);
1067 for (cpt=0; cpt< NB_ELEM/5; cpt++) {
1068 sprintf(buf,"%d",cpt);
1070 xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
1073 for (cpt=0; cpt< NB_ELEM/2; cpt++) {
1074 sprintf(buf,"%d",cpt);
1075 xbt_dynar_shift(d,&s2);
1076 xbt_test_assert2(!strcmp(buf,s2),
1077 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1081 for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
1082 sprintf(buf,"%d",cpt);
1083 xbt_dynar_shift(d,&s2);
1084 xbt_test_assert2 (!strcmp(buf,s2),
1085 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1089 for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
1090 sprintf(buf,"%d",cpt);
1091 xbt_dynar_shift(d,&s2);
1092 xbt_test_assert2 (!strcmp(buf,s2),
1093 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1101 xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
1102 d=xbt_dynar_new(sizeof(char*),&free_string);
1103 for (cpt=0; cpt< NB_ELEM; cpt++) {
1104 sprintf(buf,"%d",cpt);
1106 xbt_dynar_push(d,&s1);
1108 for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
1109 sprintf(buf,"%d",cpt);
1110 xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
1111 xbt_test_assert2(!strcmp(buf,s2),
1112 "Remove a bad value. Got %s, expected %s",
1116 xbt_dynar_free(&d); /* end_of_doxygen */
1120 /*******************************************************************************/
1121 /*******************************************************************************/
1122 /*******************************************************************************/
1123 #include "xbt/synchro.h"
1124 static void pusher_f(void *a) {
1125 xbt_dynar_t d=(xbt_dynar_t)a;
1127 for (i=0; i<500; i++) {
1128 xbt_dynar_push(d,&i);
1131 static void poper_f(void *a) {
1132 xbt_dynar_t d=(xbt_dynar_t)a;
1137 for (i=0; i<500; i++) {
1139 xbt_dynar_pop(d,&data);
1141 if (e.category == bound_error) {
1152 XBT_TEST_UNIT("synchronized int",test_dynar_sync_int,"Synchronized dynars of integers") {
1153 /* Vars_decl [doxygen cruft] */
1155 xbt_thread_t pusher,poper;
1157 xbt_test_add0("==== Have a pusher and a popper on the dynar");
1158 d=xbt_dynar_new_sync(sizeof(int),NULL);
1159 pusher = xbt_thread_create(pusher_f,d);
1160 poper = xbt_thread_create(poper_f,d);
1161 xbt_thread_join(pusher);
1162 xbt_thread_join(poper);
1166 #endif /* SIMGRID_TEST */