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_resize(xbt_dynar_t dynar, unsigned long new_size)
86 if (new_size != dynar->size) {
87 dynar->size = new_size;
88 dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize);
93 void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
95 const unsigned long old_size = dynar->size;
98 const unsigned long expand = 2 * (old_size + 1);
99 _xbt_dynar_resize(dynar, (nb > expand ? nb : expand));
100 XBT_DEBUG("expand %p from %lu to %lu elements",
101 dynar, old_size, dynar->size);
106 void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
108 char *const data = (char *) dynar->data;
109 const unsigned long elmsize = dynar->elmsize;
111 return data + idx * elmsize;
116 _xbt_dynar_get_elm(void *const dst,
117 const xbt_dynar_t dynar, const unsigned long idx)
119 void *const elm = _xbt_dynar_elm(dynar, idx);
121 memcpy(dst, elm, dynar->elmsize);
126 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
127 const unsigned long idx, const void *const src)
129 void *const elm = _xbt_dynar_elm(dynar, idx);
130 const unsigned long elmsize = dynar->elmsize;
132 memcpy(elm, src, elmsize);
137 _xbt_dynar_remove_at(xbt_dynar_t const dynar,
138 const unsigned long idx, void *const object)
141 unsigned long nb_shift;
142 unsigned long offset;
144 _sanity_check_dynar(dynar);
145 _check_inbound_idx(dynar, idx);
148 _xbt_dynar_get_elm(object, dynar, idx);
149 } else if (dynar->free_f) {
150 if (dynar->elmsize <= SIZEOF_MAX) {
151 char elm[SIZEOF_MAX];
152 _xbt_dynar_get_elm(elm, dynar, idx);
155 char *elm = malloc(dynar->elmsize);
156 _xbt_dynar_get_elm(elm, dynar, idx);
162 nb_shift = dynar->used - 1 - idx;
165 offset = nb_shift * dynar->elmsize;
166 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
173 void xbt_dynar_dump(xbt_dynar_t dynar)
175 XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
176 dynar->size, dynar->used, dynar->elmsize, dynar->data,
180 /** @brief Constructor
182 * \param elmsize size of each element in the dynar
183 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
185 * Creates a new dynar. If a free_func is provided, the elements have to be
186 * pointer of pointer. That is to say that dynars can contain either base
187 * types (int, char, double, etc) or pointer of pointers (struct **).
190 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
193 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
197 dynar->elmsize = elmsize;
199 dynar->free_f = free_f;
205 /** @brief Creates a synchronized dynar.
207 * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
211 xbt_dynar_new_sync(const unsigned long elmsize,
212 void_f_pvoid_t const free_f)
214 xbt_dynar_t res = xbt_dynar_new(elmsize, free_f);
215 res->mutex = xbt_mutex_init();
219 /** @brief Destructor of the structure not touching to the content
221 * \param dynar poor victim
223 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
224 * is not touched (the \a free_f function is not used)
226 void xbt_dynar_free_container(xbt_dynar_t * dynar)
228 if (dynar && *dynar) {
229 xbt_dynar_t d = *dynar;
232 xbt_mutex_destroy(d->mutex);
238 /** @brief Frees the content and set the size to 0
240 * \param dynar who to squeeze
242 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
246 _sanity_check_dynar(dynar);
248 XBT_DEBUG("Reset the dynar %p", (void *) dynar);
250 _dynar_map(dynar, dynar->free_f);
254 _dynar_unlock(dynar);
258 * \brief Shrink the dynar by removing empty slots at the end of the internal array
259 * \param dynar a dynar
260 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
261 * internal array for further insertions
263 * Reduces the internal array size of the dynar to the number of elements plus
264 * \a empty_slots_wanted.
265 * After removing elements from the dynar, you can call this function to make
266 * the dynar use less memory.
267 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
269 * Note that if \a empty_slots_wanted is greater than the array size, the internal
270 * array is expanded instead of shriked.
272 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
275 _xbt_dynar_resize(dynar, dynar->used + empty_slots_wanted);
276 _dynar_unlock(dynar);
279 /** @brief Destructor
281 * \param dynar poor victim
283 * kilkil a dynar and its content
286 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
288 if (dynar && *dynar) {
289 xbt_dynar_reset(*dynar);
290 xbt_dynar_free_container(dynar);
294 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
295 void xbt_dynar_free_voidp(void *d)
297 xbt_dynar_t dynar = (xbt_dynar_t)d;
298 xbt_dynar_free(&dynar);
301 /** @brief Count of dynar's elements
303 * \param dynar the dynar we want to mesure
305 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
307 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
310 /**@brief check if a dynar is empty
312 *\param dynar the dynat we want to check
315 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
317 return (xbt_dynar_length(dynar) == 0);
320 /** @brief Retrieve a copy of the Nth element of a dynar.
322 * \param dynar information dealer
323 * \param idx index of the slot we want to retrieve
324 * \param[out] dst where to put the result to.
327 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
328 const unsigned long idx, void *const dst)
331 _sanity_check_dynar(dynar);
332 _check_inbound_idx(dynar, idx);
334 _xbt_dynar_get_elm(dst, dynar, idx);
335 _dynar_unlock(dynar);
338 /** @brief Retrieve a pointer to the Nth element of a dynar.
340 * \param dynar information dealer
341 * \param idx index of the slot we want to retrieve
342 * \return the \a idx-th element of \a dynar.
344 * \warning The returned value is the actual content of the dynar.
345 * Make a copy before fooling with it.
347 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
348 const unsigned long idx)
353 _sanity_check_dynar(dynar);
354 _check_inbound_idx(dynar, idx);
356 res = _xbt_dynar_elm(dynar, idx);
357 _dynar_unlock(dynar);
361 /* not synchronized */
362 static XBT_INLINE void *_xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
363 const unsigned long idx)
365 _sanity_check_dynar(dynar);
367 if (idx >= dynar->used) {
368 _xbt_dynar_expand(dynar, idx + 1);
369 _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
370 (idx + 1 - dynar->used)*dynar->elmsize);
371 dynar->used = idx + 1;
373 return _xbt_dynar_elm(dynar, idx);
376 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
377 const unsigned long idx)
381 res = _xbt_dynar_set_at_ptr(dynar, idx);
382 _dynar_unlock(dynar);
386 static void XBT_INLINE /* not synchronized */
387 _xbt_dynar_set(xbt_dynar_t dynar,
388 const unsigned long idx, const void *const src)
390 memcpy(_xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
393 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
395 * \param dynar information dealer
396 * \param idx index of the slot we want to modify
397 * \param src What will be feeded to the dynar
399 * If you want to free the previous content, use xbt_dynar_replace().
401 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
402 const void *const src)
406 _xbt_dynar_set(dynar, idx, src);
407 _dynar_unlock(dynar);
410 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
416 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
417 * free the previous value at this position. If you don't want to free the
418 * previous content, use xbt_dynar_set().
421 xbt_dynar_replace(xbt_dynar_t dynar,
422 const unsigned long idx, const void *const object)
425 _sanity_check_dynar(dynar);
427 if (idx < dynar->used && dynar->free_f) {
428 void *const old_object = _xbt_dynar_elm(dynar, idx);
430 dynar->free_f(old_object);
433 _xbt_dynar_set(dynar, idx, object);
434 _dynar_unlock(dynar);
437 static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
438 const unsigned long idx)
441 unsigned long old_used;
442 unsigned long new_used;
445 _sanity_check_dynar(dynar);
446 _sanity_check_idx(idx);
448 old_used = dynar->used;
449 new_used = old_used + 1;
451 _xbt_dynar_expand(dynar, new_used);
453 nb_shift = old_used - idx;
456 memmove(_xbt_dynar_elm(dynar, idx + 1),
457 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
460 dynar->used = new_used;
461 res = _xbt_dynar_elm(dynar, idx);
465 /** @brief Make room for a new element, and return a pointer to it
467 * You can then use regular affectation to set its value instead of relying
468 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
470 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
475 res = _xbt_dynar_insert_at_ptr(dynar, idx);
476 _dynar_unlock(dynar);
480 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
482 * Set the Nth element of a dynar, expanding the dynar if needed, and
483 * moving the previously existing value and all subsequent ones to one
484 * position right in the dynar.
487 xbt_dynar_insert_at(xbt_dynar_t const dynar,
488 const int idx, const void *const src)
492 /* checks done in xbt_dynar_insert_at_ptr */
493 memcpy(_xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
494 _dynar_unlock(dynar);
497 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
499 * Get the Nth element of a dynar, removing it from the dynar and moving
500 * all subsequent values to one position left in the dynar.
502 * If the object argument of this function is a non-null pointer, the removed
503 * element is copied to this address. If not, the element is freed using the
504 * free_f function passed at dynar creation.
507 xbt_dynar_remove_at(xbt_dynar_t const dynar,
508 const int idx, void *const object)
512 _xbt_dynar_remove_at(dynar, idx, object);
513 _dynar_unlock(dynar);
516 /** @brief Returns the position of the element in the dynar
518 * Raises not_found_error if not found.
520 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
525 for (it = 0; it < dynar->used; it++)
526 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
527 _dynar_unlock(dynar);
531 _dynar_unlock(dynar);
532 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
536 /** @brief Returns a boolean indicating whether the element is part of the dynar */
537 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
543 xbt_dynar_search(dynar, elem);
546 if (e.category == not_found_error) {
555 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
557 * You can then use regular affectation to set its value instead of relying
558 * on the slow memcpy. This is what xbt_dynar_push_as() does.
560 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
564 /* we have to inline xbt_dynar_insert_at_ptr here to make sure that
565 dynar->used don't change between reading it and getting the lock
566 within xbt_dynar_insert_at_ptr */
568 res = _xbt_dynar_insert_at_ptr(dynar, dynar->used);
569 _dynar_unlock(dynar);
573 /** @brief Add an element at the end of the dynar */
574 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
575 const void *const src)
578 /* checks done in xbt_dynar_insert_at_ptr */
579 memcpy(_xbt_dynar_insert_at_ptr(dynar, dynar->used), src,
581 _dynar_unlock(dynar);
584 /** @brief Mark the last dynar's element as unused and return a pointer to it.
586 * You can then use regular affectation to set its value instead of relying
587 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
589 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
594 _check_populated_dynar(dynar);
595 XBT_DEBUG("Pop %p", (void *) dynar);
597 res = _xbt_dynar_elm(dynar, dynar->used);
598 _dynar_unlock(dynar);
602 /** @brief Get and remove the last element of the dynar */
603 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
606 /* sanity checks done by remove_at */
607 XBT_DEBUG("Pop %p", (void *) dynar);
609 _xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
610 _dynar_unlock(dynar);
613 /** @brief Add an element at the begining of the dynar.
615 * This is less efficient than xbt_dynar_push()
617 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
618 const void *const src)
621 /* sanity checks done by insert_at */
622 xbt_dynar_insert_at(dynar, 0, src);
625 /** @brief Get and remove the first element of the dynar.
627 * This is less efficient than xbt_dynar_pop()
629 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
632 /* sanity checks done by remove_at */
633 xbt_dynar_remove_at(dynar, 0, dst);
636 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
638 char *const data = (char *) dynar->data;
639 const unsigned long elmsize = dynar->elmsize;
640 const unsigned long used = dynar->used;
643 for (i = 0; i < used; i++) {
644 char* elm = (char*) data + i * elmsize;
649 /** @brief Apply a function to each member of a dynar
651 * The mapped function may change the value of the element itself,
652 * but should not mess with the structure of the dynar.
654 * If the dynar is synchronized, it is locked during the whole map
655 * operation, so make sure your function don't call any function
656 * from xbt_dynar_* on it, or you'll get a deadlock.
658 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
659 void_f_pvoid_t const op)
662 _sanity_check_dynar(dynar);
665 _dynar_map(dynar, op);
667 _dynar_unlock(dynar);
671 /** @brief Removes and free the entry pointed by the cursor
673 * This function can be used while traversing without problem.
675 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
676 unsigned int *const cursor)
679 _xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
682 /** @brief Unlocks a synchronized dynar when you want to break the traversal
684 * This function must be used if you <tt>break</tt> the
685 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
686 * regular traversal reaching the end of the elements
688 XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
690 _dynar_unlock(dynar);
693 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
695 * \param dynar the dynar to sort
696 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
698 * Remark: if the elements stored in the dynar are structures, the compar_fn
699 * function has to retrieve the field to sort first.
701 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
702 int_f_cpvoid_cpvoid_t compar_fn)
707 #ifdef HAVE_MERGESORT
708 mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
710 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
712 _dynar_unlock(dynar);
715 /** @brief Transform a dynar into a NULL terminated array
717 * \param dynar the dynar to transform
719 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
722 void * last = xbt_new0(char,dynar->elmsize);
723 xbt_dynar_push(dynar, last);
731 * Return 0 if d1 and d2 are equal and 1 if not equal
733 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
734 int(*compar)(const void *, const void *))
738 if((!d1) && (!d2)) return 0;
741 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
745 if((d1->elmsize)!=(d2->elmsize))
747 XBT_DEBUG("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize);
751 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
753 XBT_DEBUG("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2));
758 size = xbt_dynar_length(d1);
761 void *data1 = xbt_dynar_get_as(d1, i, void *);
762 void *data2 = xbt_dynar_get_as(d2, i, void *);
763 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
764 if(compar(data1,data2)){
777 XBT_TEST_SUITE("dynar", "Dynar data container");
778 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
779 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
781 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
783 /* Vars_decl [doxygen cruft] */
789 xbt_test_add("==== Traverse the empty dynar");
790 d = xbt_dynar_new(sizeof(int), NULL);
791 xbt_dynar_foreach(d, cursor, i) {
792 xbt_die( "Damnit, there is something in the empty dynar");
794 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
795 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
796 /* in your code is naturally the way to go outside a regression test */
799 ("==== Push %d int, set them again 3 times, traverse them, shift them",
801 /* Populate_ints [doxygen cruft] */
802 /* 1. Populate the dynar */
803 d = xbt_dynar_new(sizeof(int), NULL);
804 for (cpt = 0; cpt < NB_ELEM; cpt++) {
805 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
806 /* xbt_dynar_push(d,&cpt); This would also work */
807 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
810 /* 2. Traverse manually the dynar */
811 for (cursor = 0; cursor < NB_ELEM; cursor++) {
812 iptr = xbt_dynar_get_ptr(d, cursor);
813 xbt_test_assert(cursor == *iptr,
814 "The retrieved value is not the same than the injected one (%d!=%d)",
818 /* 3. Traverse the dynar using the neat macro to that extend */
819 xbt_dynar_foreach(d, cursor, cpt) {
820 xbt_test_assert(cursor == cpt,
821 "The retrieved value is not the same than the injected one (%d!=%d)",
824 /* end_of_traversal */
826 for (cpt = 0; cpt < NB_ELEM; cpt++)
827 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
829 for (cpt = 0; cpt < NB_ELEM; cpt++)
830 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
831 /* xbt_dynar_set(d,cpt,&cpt); */
833 for (cpt = 0; cpt < NB_ELEM; cpt++)
834 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
837 xbt_dynar_foreach(d, cursor, i) {
838 xbt_test_assert(i == cpt,
839 "The retrieved value is not the same than the injected one (%d!=%d)",
843 xbt_test_assert(cpt == NB_ELEM,
844 "Cannot retrieve my %d values. Last got one is %d",
847 /* shifting [doxygen cruft] */
848 /* 4. Shift all the values */
849 for (cpt = 0; cpt < NB_ELEM; cpt++) {
850 xbt_dynar_shift(d, &i);
851 xbt_test_assert(i == cpt,
852 "The retrieved value is not the same than the injected one (%d!=%d)",
854 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
857 /* 5. Free the resources */
858 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
859 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
860 /* in your code is naturally the way to go outside a regression test */
862 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
863 d = xbt_dynar_new(sizeof(int), NULL);
864 for (cpt = 0; cpt < NB_ELEM; cpt++) {
865 xbt_dynar_unshift(d, &cpt);
866 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
868 for (cpt = 0; cpt < NB_ELEM; cpt++) {
869 i = xbt_dynar_pop_as(d, int);
870 xbt_test_assert(i == cpt,
871 "The retrieved value is not the same than the injected one (%d!=%d)",
873 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
875 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
876 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
877 /* in your code is naturally the way to go outside a regression test */
881 ("==== Push %d int, insert 1000 int in the middle, shift everything",
883 d = xbt_dynar_new(sizeof(int), NULL);
884 for (cpt = 0; cpt < NB_ELEM; cpt++) {
885 xbt_dynar_push_as(d, int, cpt);
886 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
888 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
889 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
890 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
893 for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
894 xbt_dynar_shift(d, &i);
895 xbt_test_assert(i == cpt,
896 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
898 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
900 for (cpt = 999; cpt >= 0; cpt--) {
901 xbt_dynar_shift(d, &i);
902 xbt_test_assert(i == cpt,
903 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
906 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
907 xbt_dynar_shift(d, &i);
908 xbt_test_assert(i == cpt,
909 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
912 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
913 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
914 /* in your code is naturally the way to go outside a regression test */
916 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
918 d = xbt_dynar_new(sizeof(int), NULL);
919 for (cpt = 0; cpt < NB_ELEM; cpt++)
920 xbt_dynar_push_as(d, int, cpt);
922 for (cpt = 2000; cpt < 4000; cpt++) {
923 xbt_dynar_remove_at(d, 2000, &i);
924 xbt_test_assert(i == cpt,
925 "Remove a bad value. Got %d, expected %d", i, cpt);
926 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
928 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
929 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
930 /* in your code is naturally the way to go outside a regression test */
933 /*******************************************************************************/
934 /*******************************************************************************/
935 /*******************************************************************************/
936 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
938 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
942 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
943 /* Populate_ints [doxygen cruft] */
944 /* 1. Populate the dynar */
945 for (cpt = 0; cpt < NB_ELEM; cpt++) {
946 xbt_dynar_insert_at(d, cpt, &cpt);
947 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
950 /* 3. Traverse the dynar */
951 xbt_dynar_foreach(d, cursor, cpt) {
952 xbt_test_assert(cursor == cpt,
953 "The retrieved value is not the same than the injected one (%d!=%d)",
956 /* end_of_traversal */
958 /* Re-fill with the same values using set_as (and re-verify) */
959 for (cpt = 0; cpt < NB_ELEM; cpt++)
960 xbt_dynar_set_as(d, cpt, int, cpt);
961 xbt_dynar_foreach(d, cursor, cpt)
962 xbt_test_assert(cursor == cpt,
963 "The retrieved value is not the same than the injected one (%d!=%d)",
966 for (cpt = 0; cpt < NB_ELEM; cpt++) {
968 xbt_dynar_remove_at(d,0,&val);
969 xbt_test_assert(cpt == val,
970 "The retrieved value is not the same than the injected one (%d!=%d)",
973 xbt_test_assert(xbt_dynar_is_empty(d),
974 "There is still %lu elements in the dynar after removing everything",
975 xbt_dynar_length(d));
978 /* ********************* */
979 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
980 d = xbt_dynar_new(sizeof(int), NULL);
981 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
982 xbt_dynar_replace(d, cpt, &cpt);
983 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
986 /* 3. Traverse the dynar */
987 xbt_dynar_foreach(d, cursor, cpt) {
988 xbt_test_assert(cursor == cpt,
989 "The retrieved value is not the same than the injected one (%d!=%d)",
992 /* end_of_traversal */
994 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
996 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
997 xbt_test_assert(cpt == val,
998 "The retrieved value is not the same than the injected one (%d!=%d)",
1001 xbt_test_assert(xbt_dynar_is_empty(d),
1002 "There is still %lu elements in the dynar after removing everything",
1003 xbt_dynar_length(d));
1007 /*******************************************************************************/
1008 /*******************************************************************************/
1009 /*******************************************************************************/
1010 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
1014 unsigned int cursor;
1017 xbt_test_add("==== Traverse the empty dynar");
1018 d = xbt_dynar_new(sizeof(int), NULL);
1019 xbt_dynar_foreach(d, cursor, cpt) {
1020 xbt_test_assert(FALSE,
1021 "Damnit, there is something in the empty dynar");
1023 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1024 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1025 /* in your code is naturally the way to go outside a regression test */
1027 xbt_test_add("==== Push/shift 5000 doubles");
1028 d = xbt_dynar_new(sizeof(double), NULL);
1029 for (cpt = 0; cpt < 5000; cpt++) {
1031 xbt_dynar_push(d, &d1);
1033 xbt_dynar_foreach(d, cursor, d2) {
1034 d1 = (double) cursor;
1035 xbt_test_assert(d1 == d2,
1036 "The retrieved value is not the same than the injected one (%f!=%f)",
1039 for (cpt = 0; cpt < 5000; cpt++) {
1041 xbt_dynar_shift(d, &d2);
1042 xbt_test_assert(d1 == d2,
1043 "The retrieved value is not the same than the injected one (%f!=%f)",
1046 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1047 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1048 /* in your code is naturally the way to go outside a regression test */
1050 xbt_test_add("==== Unshift/pop 5000 doubles");
1051 d = xbt_dynar_new(sizeof(double), NULL);
1052 for (cpt = 0; cpt < 5000; cpt++) {
1054 xbt_dynar_unshift(d, &d1);
1056 for (cpt = 0; cpt < 5000; cpt++) {
1058 xbt_dynar_pop(d, &d2);
1059 xbt_test_assert(d1 == d2,
1060 "The retrieved value is not the same than the injected one (%f!=%f)",
1063 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1064 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1065 /* in your code is naturally the way to go outside a regression test */
1070 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1071 d = xbt_dynar_new(sizeof(double), NULL);
1072 for (cpt = 0; cpt < 5000; cpt++) {
1074 xbt_dynar_push(d, &d1);
1076 for (cpt = 0; cpt < 1000; cpt++) {
1078 xbt_dynar_insert_at(d, 2500, &d1);
1081 for (cpt = 0; cpt < 2500; cpt++) {
1083 xbt_dynar_shift(d, &d2);
1084 xbt_test_assert(d1 == d2,
1085 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1087 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1089 for (cpt = 999; cpt >= 0; cpt--) {
1091 xbt_dynar_shift(d, &d2);
1092 xbt_test_assert(d1 == d2,
1093 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1096 for (cpt = 2500; cpt < 5000; cpt++) {
1098 xbt_dynar_shift(d, &d2);
1099 xbt_test_assert(d1 == d2,
1100 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1103 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1104 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1105 /* in your code is naturally the way to go outside a regression test */
1108 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1109 d = xbt_dynar_new(sizeof(double), NULL);
1110 for (cpt = 0; cpt < 5000; cpt++) {
1112 xbt_dynar_push(d, &d1);
1114 for (cpt = 2000; cpt < 4000; cpt++) {
1116 xbt_dynar_remove_at(d, 2000, &d2);
1117 xbt_test_assert(d1 == d2,
1118 "Remove a bad value. Got %f, expected %f", d2, d1);
1120 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1121 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1122 /* in your code is naturally the way to go outside a regression test */
1126 /* doxygen_string_cruft */
1128 /*******************************************************************************/
1129 /*******************************************************************************/
1130 /*******************************************************************************/
1131 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1139 xbt_test_add("==== Traverse the empty dynar");
1140 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1141 xbt_dynar_foreach(d, iter, s1) {
1142 xbt_test_assert(FALSE,
1143 "Damnit, there is something in the empty dynar");
1145 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1146 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1147 /* in your code is naturally the way to go outside a regression test */
1149 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1151 /* Populate_str [doxygen cruft] */
1152 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1153 /* 1. Populate the dynar */
1154 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1155 sprintf(buf, "%d", cpt);
1157 xbt_dynar_push(d, &s1);
1159 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1160 sprintf(buf, "%d", cpt);
1162 xbt_dynar_replace(d, cpt, &s1);
1164 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1165 sprintf(buf, "%d", cpt);
1167 xbt_dynar_replace(d, cpt, &s1);
1169 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1170 sprintf(buf, "%d", cpt);
1172 xbt_dynar_replace(d, cpt, &s1);
1174 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1175 sprintf(buf, "%d", cpt);
1176 xbt_dynar_shift(d, &s2);
1177 xbt_test_assert(!strcmp(buf, s2),
1178 "The retrieved value is not the same than the injected one (%s!=%s)",
1182 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1183 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1184 /* in your code is naturally the way to go outside a regression test */
1186 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1187 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1188 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1189 sprintf(buf, "%d", cpt);
1191 xbt_dynar_unshift(d, &s1);
1193 /* 2. Traverse the dynar with the macro */
1194 xbt_dynar_foreach(d, iter, s1) {
1195 sprintf(buf, "%d", NB_ELEM - iter - 1);
1196 xbt_test_assert(!strcmp(buf, s1),
1197 "The retrieved value is not the same than the injected one (%s!=%s)",
1200 /* 3. Traverse the dynar with the macro */
1201 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1202 sprintf(buf, "%d", cpt);
1203 xbt_dynar_pop(d, &s2);
1204 xbt_test_assert(!strcmp(buf, s2),
1205 "The retrieved value is not the same than the injected one (%s!=%s)",
1209 /* 4. Free the resources */
1210 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1211 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1212 /* in your code is naturally the way to go outside a regression test */
1216 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1217 NB_ELEM, NB_ELEM / 5);
1218 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1219 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1220 sprintf(buf, "%d", cpt);
1222 xbt_dynar_push(d, &s1);
1224 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1225 sprintf(buf, "%d", cpt);
1227 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1230 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1231 sprintf(buf, "%d", cpt);
1232 xbt_dynar_shift(d, &s2);
1233 xbt_test_assert(!strcmp(buf, s2),
1234 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1238 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1239 sprintf(buf, "%d", cpt);
1240 xbt_dynar_shift(d, &s2);
1241 xbt_test_assert(!strcmp(buf, s2),
1242 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1246 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1247 sprintf(buf, "%d", cpt);
1248 xbt_dynar_shift(d, &s2);
1249 xbt_test_assert(!strcmp(buf, s2),
1250 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1254 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1255 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1256 /* in your code is naturally the way to go outside a regression test */
1259 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1260 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1261 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1262 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1263 sprintf(buf, "%d", cpt);
1265 xbt_dynar_push(d, &s1);
1267 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1268 sprintf(buf, "%d", cpt);
1269 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1270 xbt_test_assert(!strcmp(buf, s2),
1271 "Remove a bad value. Got %s, expected %s", s2, buf);
1274 xbt_dynar_free(&d); /* end_of_doxygen */
1278 /*******************************************************************************/
1279 /*******************************************************************************/
1280 /*******************************************************************************/
1281 #include "xbt/synchro.h"
1282 static void pusher_f(void *a)
1284 xbt_dynar_t d = (xbt_dynar_t) a;
1286 for (i = 0; i < 500; i++) {
1287 xbt_dynar_push(d, &i);
1291 static void poper_f(void *a)
1293 xbt_dynar_t d = (xbt_dynar_t) a;
1298 for (i = 0; i < 500; i++) {
1300 xbt_dynar_pop(d, &data);
1303 if (e.category == bound_error) {
1314 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int, "Synchronized dynars of integers")
1316 /* Vars_decl [doxygen cruft] */
1318 xbt_thread_t pusher, poper;
1320 xbt_test_add("==== Have a pusher and a popper on the dynar");
1321 d = xbt_dynar_new_sync(sizeof(int), NULL);
1322 pusher = xbt_thread_create("pusher", pusher_f, d, 0 /*not joinable */ );
1323 poper = xbt_thread_create("poper", poper_f, d, 0 /*not joinable */ );
1324 xbt_thread_join(pusher);
1325 xbt_thread_join(poper);
1329 #endif /* SIMGRID_TEST */