1 /* a generic DYNamic ARray implementation. */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 #include "portable.h" /* SIZEOF_MAX */
11 #include "xbt/sysdep.h"
14 #include "xbt/dynar.h"
15 #include <sys/types.h>
17 /* IMPLEMENTATION NOTE ON SYNCHRONIZATION: every functions which name is prefixed by _
18 * assumes that the dynar is already locked if we have to.
19 * Other functions (public ones) check for this.
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
24 static XBT_INLINE void _dynar_lock(xbt_dynar_t dynar)
27 xbt_mutex_acquire(dynar->mutex);
30 static XBT_INLINE void _dynar_unlock(xbt_dynar_t dynar)
33 xbt_mutex_release(dynar->mutex);
36 static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
38 xbt_assert(dynar, "dynar is NULL");
41 static XBT_INLINE void _sanity_check_idx(int idx)
43 xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
46 static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
48 if (idx < 0 || idx >= dynar->used) {
50 THROWF(bound_error, idx,
51 "dynar is not that long. You asked %d, but it's only %lu long",
52 (int) (idx), (unsigned long) dynar->used);
56 static XBT_INLINE void _check_sloppy_inbound_idx(xbt_dynar_t dynar,
59 if (idx > dynar->used) {
61 THROWF(bound_error, idx,
62 "dynar is not that long. You asked %d, but it's only %lu long (could have been equal to it)",
63 (int) (idx), (unsigned long) dynar->used);
67 static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
69 if (dynar->used == 0) {
71 THROWF(bound_error, 0, "dynar %p is empty", dynar);
75 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op);
78 void _xbt_clear_mem(void *const ptr, const unsigned long length)
80 memset(ptr, 0, length);
84 void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
86 const unsigned long old_size = dynar->size;
89 void *const old_data = dynar->data;
90 const unsigned long elmsize = dynar->elmsize;
91 const unsigned long old_length = old_size * elmsize;
93 const unsigned long expand = 2 * (old_size + 1);
94 const unsigned long new_size = (nb > expand ? nb : expand);
95 const unsigned long new_length = new_size * elmsize;
96 void *const new_data = xbt_realloc(old_data, new_length);
98 XBT_DEBUG("expand %p from %lu to %lu elements", dynar, old_size, new_size);
100 _xbt_clear_mem((char *)new_data + old_length, new_length - old_length);
102 dynar->size = new_size;
103 dynar->data = new_data;
108 void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
110 char *const data = (char *) dynar->data;
111 const unsigned long elmsize = dynar->elmsize;
113 return data + idx * elmsize;
118 _xbt_dynar_get_elm(void *const dst,
119 const xbt_dynar_t dynar, const unsigned long idx)
121 void *const elm = _xbt_dynar_elm(dynar, idx);
123 memcpy(dst, elm, dynar->elmsize);
128 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
129 const unsigned long idx, const void *const src)
131 void *const elm = _xbt_dynar_elm(dynar, idx);
132 const unsigned long elmsize = dynar->elmsize;
134 memcpy(elm, src, elmsize);
139 _xbt_dynar_remove_at(xbt_dynar_t const dynar,
140 const unsigned long idx, void *const object)
143 unsigned long nb_shift;
144 unsigned long offset;
146 _sanity_check_dynar(dynar);
147 _check_inbound_idx(dynar, idx);
150 _xbt_dynar_get_elm(object, dynar, idx);
151 } else if (dynar->free_f) {
152 if (dynar->elmsize <= SIZEOF_MAX) {
153 char elm[SIZEOF_MAX];
154 _xbt_dynar_get_elm(elm, dynar, idx);
155 (*dynar->free_f) (elm);
157 char *elm = malloc(dynar->elmsize);
158 _xbt_dynar_get_elm(elm, dynar, idx);
159 (*dynar->free_f) (elm);
164 nb_shift = dynar->used - 1 - idx;
167 offset = nb_shift * dynar->elmsize;
168 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
175 void xbt_dynar_dump(xbt_dynar_t dynar)
177 XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
178 dynar->size, dynar->used, dynar->elmsize, dynar->data,
182 /** @brief Constructor
184 * \param elmsize size of each element in the dynar
185 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
187 * Creates a new dynar. If a free_func is provided, the elements have to be
188 * pointer of pointer. That is to say that dynars can contain either base
189 * types (int, char, double, etc) or pointer of pointers (struct **).
192 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
195 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
199 dynar->elmsize = elmsize;
201 dynar->free_f = free_f;
207 /** @brief Creates a synchronized dynar.
209 * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
213 xbt_dynar_new_sync(const unsigned long elmsize,
214 void_f_pvoid_t const free_f)
216 xbt_dynar_t res = xbt_dynar_new(elmsize, free_f);
217 res->mutex = xbt_mutex_init();
221 /** @brief Destructor of the structure not touching to the content
223 * \param dynar poor victim
225 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
226 * is not touched (the \a free_f function is not used)
228 void xbt_dynar_free_container(xbt_dynar_t * dynar)
230 if (dynar && *dynar) {
232 if ((*dynar)->data) {
233 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
234 free((*dynar)->data);
238 xbt_mutex_destroy((*dynar)->mutex);
240 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
247 /** @brief Frees the content and set the size to 0
249 * \param dynar who to squeeze
251 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
255 _sanity_check_dynar(dynar);
257 XBT_DEBUG("Reset the dynar %p", (void *) dynar);
259 _dynar_map(dynar, dynar->free_f);
269 _dynar_unlock(dynar);
271 /* dynar->data = NULL; */
275 * \brief Shrink the dynar by removing empty slots at the end of the internal array
276 * \param dynar a dynar
277 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
278 * internal array for further insertions
280 * Reduces the internal array size of the dynar to the number of elements plus
281 * \a empty_slots_wanted.
282 * After removing elements from the dynar, you can call this function to make
283 * the dynar use less memory.
284 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
286 * Note that if \a empty_slots_wanted is greater than the array size, the internal
287 * array is expanded instead of shriked.
289 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
291 unsigned long size_wanted;
295 size_wanted = dynar->used + empty_slots_wanted;
296 if (size_wanted != dynar->size) {
297 dynar->size = size_wanted;
298 dynar->data = xbt_realloc(dynar->data, dynar->elmsize * dynar->size);
300 _dynar_unlock(dynar);
303 /** @brief Destructor
305 * \param dynar poor victim
307 * kilkil a dynar and its content
310 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
312 if (dynar && *dynar) {
313 xbt_dynar_reset(*dynar);
314 xbt_dynar_free_container(dynar);
318 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
319 void xbt_dynar_free_voidp(void *d)
321 xbt_dynar_t dynar = (xbt_dynar_t)d;
322 xbt_dynar_free(&dynar);
325 /** @brief Count of dynar's elements
327 * \param dynar the dynar we want to mesure
329 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
331 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
334 /**@brief check if a dynar is empty
336 *\param dynar the dynat we want to check
339 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
341 return (xbt_dynar_length(dynar) == 0);
344 /** @brief Retrieve a copy of the Nth element of a dynar.
346 * \param dynar information dealer
347 * \param idx index of the slot we want to retrieve
348 * \param[out] dst where to put the result to.
351 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
352 const unsigned long idx, void *const dst)
355 _sanity_check_dynar(dynar);
356 _check_inbound_idx(dynar, idx);
358 _xbt_dynar_get_elm(dst, dynar, idx);
359 _dynar_unlock(dynar);
362 /** @brief Retrieve a pointer to the Nth element of a dynar.
364 * \param dynar information dealer
365 * \param idx index of the slot we want to retrieve
366 * \return the \a idx-th element of \a dynar.
368 * \warning The returned value is the actual content of the dynar.
369 * Make a copy before fooling with it.
371 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
372 const unsigned long idx)
377 _sanity_check_dynar(dynar);
378 _check_inbound_idx(dynar, idx);
380 res = _xbt_dynar_elm(dynar, idx);
381 _dynar_unlock(dynar);
385 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
386 const unsigned long idx)
391 _sanity_check_dynar(dynar);
393 _xbt_dynar_expand(dynar, idx + 1);
395 if (idx >= dynar->used) {
396 _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
397 (idx + 1 - dynar->used)*dynar->elmsize);
398 dynar->used = idx + 1;
401 _dynar_unlock(dynar);
403 res = _xbt_dynar_elm(dynar, idx);
408 static void XBT_INLINE /* not synchronized */
409 _xbt_dynar_set(xbt_dynar_t dynar,
410 const unsigned long idx, const void *const src)
413 _sanity_check_dynar(dynar);
415 _xbt_dynar_expand(dynar, idx + 1);
417 if (idx >= dynar->used) {
418 _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
419 (idx + 1 - dynar->used)*dynar->elmsize);
420 dynar->used = idx + 1;
423 _xbt_dynar_put_elm(dynar, idx, src);
426 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
428 * \param dynar information dealer
429 * \param idx index of the slot we want to modify
430 * \param src What will be feeded to the dynar
432 * If you want to free the previous content, use xbt_dynar_replace().
434 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
435 const void *const src)
439 _xbt_dynar_set(dynar, idx, src);
440 _dynar_unlock(dynar);
443 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
449 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
450 * free the previous value at this position. If you don't want to free the
451 * previous content, use xbt_dynar_set().
454 xbt_dynar_replace(xbt_dynar_t dynar,
455 const unsigned long idx, const void *const object)
458 _sanity_check_dynar(dynar);
460 if (idx < dynar->used && dynar->free_f) {
461 void *const old_object = _xbt_dynar_elm(dynar, idx);
463 (*(dynar->free_f)) (old_object);
466 _xbt_dynar_set(dynar, idx, object);
467 _dynar_unlock(dynar);
470 static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
471 const unsigned long idx)
474 unsigned long old_used;
475 unsigned long new_used;
478 _sanity_check_dynar(dynar);
479 _sanity_check_idx(idx);
481 old_used = dynar->used;
482 new_used = old_used + 1;
484 _xbt_dynar_expand(dynar, new_used);
486 nb_shift = old_used - idx;
489 memmove(_xbt_dynar_elm(dynar, idx + 1),
490 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
493 dynar->used = new_used;
494 res = _xbt_dynar_elm(dynar, idx);
498 /** @brief Make room for a new element, and return a pointer to it
500 * You can then use regular affectation to set its value instead of relying
501 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
503 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
508 res = _xbt_dynar_insert_at_ptr(dynar, idx);
509 _dynar_unlock(dynar);
513 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
515 * Set the Nth element of a dynar, expanding the dynar if needed, and
516 * moving the previously existing value and all subsequent ones to one
517 * position right in the dynar.
520 xbt_dynar_insert_at(xbt_dynar_t const dynar,
521 const int idx, const void *const src)
525 /* checks done in xbt_dynar_insert_at_ptr */
526 memcpy(_xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
527 _dynar_unlock(dynar);
530 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
532 * Get the Nth element of a dynar, removing it from the dynar and moving
533 * all subsequent values to one position left in the dynar.
535 * If the object argument of this function is a non-null pointer, the removed
536 * element is copied to this address. If not, the element is freed using the
537 * free_f function passed at dynar creation.
540 xbt_dynar_remove_at(xbt_dynar_t const dynar,
541 const int idx, void *const object)
545 _xbt_dynar_remove_at(dynar, idx, object);
546 _dynar_unlock(dynar);
549 /** @brief Returns the position of the element in the dynar
551 * Raises not_found_error if not found.
553 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
558 for (it = 0; it < dynar->used; it++)
559 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
560 _dynar_unlock(dynar);
564 _dynar_unlock(dynar);
565 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
569 /** @brief Returns a boolean indicating whether the element is part of the dynar */
570 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
576 xbt_dynar_search(dynar, elem);
578 if (e.category == not_found_error) {
587 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
589 * You can then use regular affectation to set its value instead of relying
590 * on the slow memcpy. This is what xbt_dynar_push_as() does.
592 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
596 /* we have to inline xbt_dynar_insert_at_ptr here to make sure that
597 dynar->used don't change between reading it and getting the lock
598 within xbt_dynar_insert_at_ptr */
600 res = _xbt_dynar_insert_at_ptr(dynar, dynar->used);
601 _dynar_unlock(dynar);
605 /** @brief Add an element at the end of the dynar */
606 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
607 const void *const src)
610 /* checks done in xbt_dynar_insert_at_ptr */
611 memcpy(_xbt_dynar_insert_at_ptr(dynar, dynar->used), src,
613 _dynar_unlock(dynar);
616 /** @brief Mark the last dynar's element as unused and return a pointer to it.
618 * You can then use regular affectation to set its value instead of relying
619 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
621 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
626 _check_populated_dynar(dynar);
627 XBT_DEBUG("Pop %p", (void *) dynar);
629 res = _xbt_dynar_elm(dynar, dynar->used);
630 _dynar_unlock(dynar);
634 /** @brief Get and remove the last element of the dynar */
635 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
638 /* sanity checks done by remove_at */
639 XBT_DEBUG("Pop %p", (void *) dynar);
641 _xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
642 _dynar_unlock(dynar);
645 /** @brief Add an element at the begining of the dynar.
647 * This is less efficient than xbt_dynar_push()
649 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
650 const void *const src)
653 /* sanity checks done by insert_at */
654 xbt_dynar_insert_at(dynar, 0, src);
657 /** @brief Get and remove the first element of the dynar.
659 * This is less efficient than xbt_dynar_pop()
661 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
664 /* sanity checks done by remove_at */
665 xbt_dynar_remove_at(dynar, 0, dst);
668 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
670 char *const data = (char *) dynar->data;
671 const unsigned long elmsize = dynar->elmsize;
672 const unsigned long used = dynar->used;
675 for (i = 0; i < used; i++) {
676 char* elm = (char*) data + i * elmsize;
681 /** @brief Apply a function to each member of a dynar
683 * The mapped function may change the value of the element itself,
684 * but should not mess with the structure of the dynar.
686 * If the dynar is synchronized, it is locked during the whole map
687 * operation, so make sure your function don't call any function
688 * from xbt_dynar_* on it, or you'll get a deadlock.
690 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
691 void_f_pvoid_t const op)
694 _sanity_check_dynar(dynar);
697 _dynar_map(dynar, op);
699 _dynar_unlock(dynar);
703 /** @brief Removes and free the entry pointed by the cursor
705 * This function can be used while traversing without problem.
707 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
708 unsigned int *const cursor)
711 _xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
714 /** @brief Unlocks a synchronized dynar when you want to break the traversal
716 * This function must be used if you <tt>break</tt> the
717 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
718 * regular traversal reaching the end of the elements
720 XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
722 _dynar_unlock(dynar);
725 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
727 * \param dynar the dynar to sort
728 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
730 * Remark: if the elements stored in the dynar are structures, the compar_fn
731 * function has to retrieve the field to sort first.
733 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
734 int_f_cpvoid_cpvoid_t compar_fn)
739 #ifdef HAVE_MERGESORT
740 mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
742 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
744 _dynar_unlock(dynar);
747 /** @brief Transform a dynar into a NULL terminated array
749 * \param dynar the dynar to transform
751 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
754 void * last = xbt_new0(char,dynar->elmsize);
755 xbt_dynar_push(dynar, last);
763 * Return 0 if d1 and d2 are equal and 1 if not equal
765 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
766 int(*compar)(const void *, const void *))
770 if((!d1) && (!d2)) return 0;
773 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
777 if((d1->elmsize)!=(d2->elmsize))
779 XBT_DEBUG("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize);
783 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
785 XBT_DEBUG("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2));
790 size = xbt_dynar_length(d1);
793 void *data1 = xbt_dynar_get_as(d1, i, void *);
794 void *data2 = xbt_dynar_get_as(d2, i, void *);
795 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
796 if(compar(data1,data2)){
809 XBT_TEST_SUITE("dynar", "Dynar data container");
810 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
811 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
813 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
815 /* Vars_decl [doxygen cruft] */
821 xbt_test_add("==== Traverse the empty dynar");
822 d = xbt_dynar_new(sizeof(int), NULL);
823 xbt_dynar_foreach(d, cursor, i) {
824 xbt_die( "Damnit, there is something in the empty dynar");
826 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
827 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
828 /* in your code is naturally the way to go outside a regression test */
831 ("==== Push %d int, set them again 3 times, traverse them, shift them",
833 /* Populate_ints [doxygen cruft] */
834 /* 1. Populate the dynar */
835 d = xbt_dynar_new(sizeof(int), NULL);
836 for (cpt = 0; cpt < NB_ELEM; cpt++) {
837 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
838 /* xbt_dynar_push(d,&cpt); This would also work */
839 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
842 /* 2. Traverse manually the dynar */
843 for (cursor = 0; cursor < NB_ELEM; cursor++) {
844 iptr = xbt_dynar_get_ptr(d, cursor);
845 xbt_test_assert(cursor == *iptr,
846 "The retrieved value is not the same than the injected one (%d!=%d)",
850 /* 3. Traverse the dynar using the neat macro to that extend */
851 xbt_dynar_foreach(d, cursor, cpt) {
852 xbt_test_assert(cursor == cpt,
853 "The retrieved value is not the same than the injected one (%d!=%d)",
856 /* end_of_traversal */
858 for (cpt = 0; cpt < NB_ELEM; cpt++)
859 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
861 for (cpt = 0; cpt < NB_ELEM; cpt++)
862 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
863 /* xbt_dynar_set(d,cpt,&cpt); */
865 for (cpt = 0; cpt < NB_ELEM; cpt++)
866 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
869 xbt_dynar_foreach(d, cursor, i) {
870 xbt_test_assert(i == cpt,
871 "The retrieved value is not the same than the injected one (%d!=%d)",
875 xbt_test_assert(cpt == NB_ELEM,
876 "Cannot retrieve my %d values. Last got one is %d",
879 /* shifting [doxygen cruft] */
880 /* 4. Shift all the values */
881 for (cpt = 0; cpt < NB_ELEM; cpt++) {
882 xbt_dynar_shift(d, &i);
883 xbt_test_assert(i == cpt,
884 "The retrieved value is not the same than the injected one (%d!=%d)",
886 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
889 /* 5. Free the resources */
890 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
891 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
892 /* in your code is naturally the way to go outside a regression test */
894 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
895 d = xbt_dynar_new(sizeof(int), NULL);
896 for (cpt = 0; cpt < NB_ELEM; cpt++) {
897 xbt_dynar_unshift(d, &cpt);
898 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
900 for (cpt = 0; cpt < NB_ELEM; cpt++) {
901 i = xbt_dynar_pop_as(d, int);
902 xbt_test_assert(i == cpt,
903 "The retrieved value is not the same than the injected one (%d!=%d)",
905 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
907 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
908 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
909 /* in your code is naturally the way to go outside a regression test */
913 ("==== Push %d int, insert 1000 int in the middle, shift everything",
915 d = xbt_dynar_new(sizeof(int), NULL);
916 for (cpt = 0; cpt < NB_ELEM; cpt++) {
917 xbt_dynar_push_as(d, int, cpt);
918 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
920 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
921 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
922 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
925 for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
926 xbt_dynar_shift(d, &i);
927 xbt_test_assert(i == cpt,
928 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
930 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
932 for (cpt = 999; cpt >= 0; cpt--) {
933 xbt_dynar_shift(d, &i);
934 xbt_test_assert(i == cpt,
935 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
938 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
939 xbt_dynar_shift(d, &i);
940 xbt_test_assert(i == cpt,
941 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
944 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
945 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
946 /* in your code is naturally the way to go outside a regression test */
948 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
950 d = xbt_dynar_new(sizeof(int), NULL);
951 for (cpt = 0; cpt < NB_ELEM; cpt++)
952 xbt_dynar_push_as(d, int, cpt);
954 for (cpt = 2000; cpt < 4000; cpt++) {
955 xbt_dynar_remove_at(d, 2000, &i);
956 xbt_test_assert(i == cpt,
957 "Remove a bad value. Got %d, expected %d", i, cpt);
958 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
960 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
961 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
962 /* in your code is naturally the way to go outside a regression test */
965 /*******************************************************************************/
966 /*******************************************************************************/
967 /*******************************************************************************/
968 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
970 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
974 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
975 /* Populate_ints [doxygen cruft] */
976 /* 1. Populate the dynar */
977 for (cpt = 0; cpt < NB_ELEM; cpt++) {
978 xbt_dynar_insert_at(d, cpt, &cpt);
979 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
982 /* 3. Traverse the dynar */
983 xbt_dynar_foreach(d, cursor, cpt) {
984 xbt_test_assert(cursor == cpt,
985 "The retrieved value is not the same than the injected one (%d!=%d)",
988 /* end_of_traversal */
990 /* Re-fill with the same values using set_as (and re-verify) */
991 for (cpt = 0; cpt < NB_ELEM; cpt++)
992 xbt_dynar_set_as(d, cpt, int, cpt);
993 xbt_dynar_foreach(d, cursor, cpt)
994 xbt_test_assert(cursor == cpt,
995 "The retrieved value is not the same than the injected one (%d!=%d)",
998 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1000 xbt_dynar_remove_at(d,0,&val);
1001 xbt_test_assert(cpt == val,
1002 "The retrieved value is not the same than the injected one (%d!=%d)",
1005 xbt_test_assert(xbt_dynar_length(d) == 0,
1006 "There is still %lu elements in the dynar after removing everything",
1007 xbt_dynar_length(d));
1010 /* ********************* */
1011 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
1012 d = xbt_dynar_new(sizeof(int), NULL);
1013 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
1014 xbt_dynar_replace(d, cpt, &cpt);
1015 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
1018 /* 3. Traverse the dynar */
1019 xbt_dynar_foreach(d, cursor, cpt) {
1020 xbt_test_assert(cursor == cpt,
1021 "The retrieved value is not the same than the injected one (%d!=%d)",
1024 /* end_of_traversal */
1026 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
1028 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
1029 xbt_test_assert(cpt == val,
1030 "The retrieved value is not the same than the injected one (%d!=%d)",
1033 xbt_test_assert(xbt_dynar_length(d) == 0,
1034 "There is still %lu elements in the dynar after removing everything",
1035 xbt_dynar_length(d));
1039 /*******************************************************************************/
1040 /*******************************************************************************/
1041 /*******************************************************************************/
1042 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
1046 unsigned int cursor;
1049 xbt_test_add("==== Traverse the empty dynar");
1050 d = xbt_dynar_new(sizeof(int), NULL);
1051 xbt_dynar_foreach(d, cursor, cpt) {
1052 xbt_test_assert(FALSE,
1053 "Damnit, there is something in the empty dynar");
1055 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1056 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1057 /* in your code is naturally the way to go outside a regression test */
1059 xbt_test_add("==== Push/shift 5000 doubles");
1060 d = xbt_dynar_new(sizeof(double), NULL);
1061 for (cpt = 0; cpt < 5000; cpt++) {
1063 xbt_dynar_push(d, &d1);
1065 xbt_dynar_foreach(d, cursor, d2) {
1066 d1 = (double) cursor;
1067 xbt_test_assert(d1 == d2,
1068 "The retrieved value is not the same than the injected one (%f!=%f)",
1071 for (cpt = 0; cpt < 5000; cpt++) {
1073 xbt_dynar_shift(d, &d2);
1074 xbt_test_assert(d1 == d2,
1075 "The retrieved value is not the same than the injected one (%f!=%f)",
1078 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1079 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1080 /* in your code is naturally the way to go outside a regression test */
1082 xbt_test_add("==== Unshift/pop 5000 doubles");
1083 d = xbt_dynar_new(sizeof(double), NULL);
1084 for (cpt = 0; cpt < 5000; cpt++) {
1086 xbt_dynar_unshift(d, &d1);
1088 for (cpt = 0; cpt < 5000; cpt++) {
1090 xbt_dynar_pop(d, &d2);
1091 xbt_test_assert(d1 == d2,
1092 "The retrieved value is not the same than the injected one (%f!=%f)",
1095 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1096 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1097 /* in your code is naturally the way to go outside a regression test */
1102 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1103 d = xbt_dynar_new(sizeof(double), NULL);
1104 for (cpt = 0; cpt < 5000; cpt++) {
1106 xbt_dynar_push(d, &d1);
1108 for (cpt = 0; cpt < 1000; cpt++) {
1110 xbt_dynar_insert_at(d, 2500, &d1);
1113 for (cpt = 0; cpt < 2500; cpt++) {
1115 xbt_dynar_shift(d, &d2);
1116 xbt_test_assert(d1 == d2,
1117 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1119 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1121 for (cpt = 999; cpt >= 0; cpt--) {
1123 xbt_dynar_shift(d, &d2);
1124 xbt_test_assert(d1 == d2,
1125 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1128 for (cpt = 2500; cpt < 5000; cpt++) {
1130 xbt_dynar_shift(d, &d2);
1131 xbt_test_assert(d1 == d2,
1132 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1135 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1136 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1137 /* in your code is naturally the way to go outside a regression test */
1140 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1141 d = xbt_dynar_new(sizeof(double), NULL);
1142 for (cpt = 0; cpt < 5000; cpt++) {
1144 xbt_dynar_push(d, &d1);
1146 for (cpt = 2000; cpt < 4000; cpt++) {
1148 xbt_dynar_remove_at(d, 2000, &d2);
1149 xbt_test_assert(d1 == d2,
1150 "Remove a bad value. Got %f, expected %f", d2, d1);
1152 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1153 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1154 /* in your code is naturally the way to go outside a regression test */
1158 /* doxygen_string_cruft */
1160 /*******************************************************************************/
1161 /*******************************************************************************/
1162 /*******************************************************************************/
1163 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1171 xbt_test_add("==== Traverse the empty dynar");
1172 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1173 xbt_dynar_foreach(d, iter, s1) {
1174 xbt_test_assert(FALSE,
1175 "Damnit, there is something in the empty dynar");
1177 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1178 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1179 /* in your code is naturally the way to go outside a regression test */
1181 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1183 /* Populate_str [doxygen cruft] */
1184 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1185 /* 1. Populate the dynar */
1186 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1187 sprintf(buf, "%d", cpt);
1189 xbt_dynar_push(d, &s1);
1191 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1192 sprintf(buf, "%d", cpt);
1194 xbt_dynar_replace(d, cpt, &s1);
1196 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1197 sprintf(buf, "%d", cpt);
1199 xbt_dynar_replace(d, cpt, &s1);
1201 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1202 sprintf(buf, "%d", cpt);
1204 xbt_dynar_replace(d, cpt, &s1);
1206 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1207 sprintf(buf, "%d", cpt);
1208 xbt_dynar_shift(d, &s2);
1209 xbt_test_assert(!strcmp(buf, s2),
1210 "The retrieved value is not the same than the injected one (%s!=%s)",
1214 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1215 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1216 /* in your code is naturally the way to go outside a regression test */
1218 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1219 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1220 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1221 sprintf(buf, "%d", cpt);
1223 xbt_dynar_unshift(d, &s1);
1225 /* 2. Traverse the dynar with the macro */
1226 xbt_dynar_foreach(d, iter, s1) {
1227 sprintf(buf, "%d", NB_ELEM - iter - 1);
1228 xbt_test_assert(!strcmp(buf, s1),
1229 "The retrieved value is not the same than the injected one (%s!=%s)",
1232 /* 3. Traverse the dynar with the macro */
1233 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1234 sprintf(buf, "%d", cpt);
1235 xbt_dynar_pop(d, &s2);
1236 xbt_test_assert(!strcmp(buf, s2),
1237 "The retrieved value is not the same than the injected one (%s!=%s)",
1241 /* 4. Free the resources */
1242 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1243 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1244 /* in your code is naturally the way to go outside a regression test */
1248 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1249 NB_ELEM, NB_ELEM / 5);
1250 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1251 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1252 sprintf(buf, "%d", cpt);
1254 xbt_dynar_push(d, &s1);
1256 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1257 sprintf(buf, "%d", cpt);
1259 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1262 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1263 sprintf(buf, "%d", cpt);
1264 xbt_dynar_shift(d, &s2);
1265 xbt_test_assert(!strcmp(buf, s2),
1266 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1270 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1271 sprintf(buf, "%d", cpt);
1272 xbt_dynar_shift(d, &s2);
1273 xbt_test_assert(!strcmp(buf, s2),
1274 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1278 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1279 sprintf(buf, "%d", cpt);
1280 xbt_dynar_shift(d, &s2);
1281 xbt_test_assert(!strcmp(buf, s2),
1282 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1286 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1287 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1288 /* in your code is naturally the way to go outside a regression test */
1291 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1292 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1293 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1294 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1295 sprintf(buf, "%d", cpt);
1297 xbt_dynar_push(d, &s1);
1299 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1300 sprintf(buf, "%d", cpt);
1301 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1302 xbt_test_assert(!strcmp(buf, s2),
1303 "Remove a bad value. Got %s, expected %s", s2, buf);
1306 xbt_dynar_free(&d); /* end_of_doxygen */
1310 /*******************************************************************************/
1311 /*******************************************************************************/
1312 /*******************************************************************************/
1313 #include "xbt/synchro.h"
1314 static void pusher_f(void *a)
1316 xbt_dynar_t d = (xbt_dynar_t) a;
1318 for (i = 0; i < 500; i++) {
1319 xbt_dynar_push(d, &i);
1323 static void poper_f(void *a)
1325 xbt_dynar_t d = (xbt_dynar_t) a;
1330 for (i = 0; i < 500; i++) {
1332 xbt_dynar_pop(d, &data);
1335 if (e.category == bound_error) {
1346 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int, "Synchronized dynars of integers")
1348 /* Vars_decl [doxygen cruft] */
1350 xbt_thread_t pusher, poper;
1352 xbt_test_add("==== Have a pusher and a popper on the dynar");
1353 d = xbt_dynar_new_sync(sizeof(int), NULL);
1354 pusher = xbt_thread_create("pusher", pusher_f, d, 0 /*not joinable */ );
1355 poper = xbt_thread_create("poper", poper_f, d, 0 /*not joinable */ );
1356 xbt_thread_join(pusher);
1357 xbt_thread_join(poper);
1361 #endif /* SIMGRID_TEST */