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 dynar->size = new_size;
101 dynar->data = new_data;
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)
274 unsigned long size_wanted;
278 size_wanted = dynar->used + empty_slots_wanted;
279 if (size_wanted != dynar->size) {
280 dynar->size = size_wanted;
281 dynar->data = xbt_realloc(dynar->data, dynar->elmsize * dynar->size);
283 _dynar_unlock(dynar);
286 /** @brief Destructor
288 * \param dynar poor victim
290 * kilkil a dynar and its content
293 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
295 if (dynar && *dynar) {
296 xbt_dynar_reset(*dynar);
297 xbt_dynar_free_container(dynar);
301 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
302 void xbt_dynar_free_voidp(void *d)
304 xbt_dynar_t dynar = (xbt_dynar_t)d;
305 xbt_dynar_free(&dynar);
308 /** @brief Count of dynar's elements
310 * \param dynar the dynar we want to mesure
312 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
314 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
317 /**@brief check if a dynar is empty
319 *\param dynar the dynat we want to check
322 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
324 return (xbt_dynar_length(dynar) == 0);
327 /** @brief Retrieve a copy of the Nth element of a dynar.
329 * \param dynar information dealer
330 * \param idx index of the slot we want to retrieve
331 * \param[out] dst where to put the result to.
334 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
335 const unsigned long idx, void *const dst)
338 _sanity_check_dynar(dynar);
339 _check_inbound_idx(dynar, idx);
341 _xbt_dynar_get_elm(dst, dynar, idx);
342 _dynar_unlock(dynar);
345 /** @brief Retrieve a pointer to the Nth element of a dynar.
347 * \param dynar information dealer
348 * \param idx index of the slot we want to retrieve
349 * \return the \a idx-th element of \a dynar.
351 * \warning The returned value is the actual content of the dynar.
352 * Make a copy before fooling with it.
354 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
355 const unsigned long idx)
360 _sanity_check_dynar(dynar);
361 _check_inbound_idx(dynar, idx);
363 res = _xbt_dynar_elm(dynar, idx);
364 _dynar_unlock(dynar);
368 /* not synchronized */
369 static XBT_INLINE void *_xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
370 const unsigned long idx)
372 _sanity_check_dynar(dynar);
374 if (idx >= dynar->used) {
375 _xbt_dynar_expand(dynar, idx + 1);
376 _xbt_clear_mem(((char * const)dynar->data) + dynar->used * dynar->elmsize,
377 (idx + 1 - dynar->used)*dynar->elmsize);
378 dynar->used = idx + 1;
380 return _xbt_dynar_elm(dynar, idx);
383 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
384 const unsigned long idx)
388 res = _xbt_dynar_set_at_ptr(dynar, idx);
389 _dynar_unlock(dynar);
393 static void XBT_INLINE /* not synchronized */
394 _xbt_dynar_set(xbt_dynar_t dynar,
395 const unsigned long idx, const void *const src)
397 memcpy(_xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
400 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
402 * \param dynar information dealer
403 * \param idx index of the slot we want to modify
404 * \param src What will be feeded to the dynar
406 * If you want to free the previous content, use xbt_dynar_replace().
408 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
409 const void *const src)
413 _xbt_dynar_set(dynar, idx, src);
414 _dynar_unlock(dynar);
417 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
423 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
424 * free the previous value at this position. If you don't want to free the
425 * previous content, use xbt_dynar_set().
428 xbt_dynar_replace(xbt_dynar_t dynar,
429 const unsigned long idx, const void *const object)
432 _sanity_check_dynar(dynar);
434 if (idx < dynar->used && dynar->free_f) {
435 void *const old_object = _xbt_dynar_elm(dynar, idx);
437 dynar->free_f(old_object);
440 _xbt_dynar_set(dynar, idx, object);
441 _dynar_unlock(dynar);
444 static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
445 const unsigned long idx)
448 unsigned long old_used;
449 unsigned long new_used;
452 _sanity_check_dynar(dynar);
453 _sanity_check_idx(idx);
455 old_used = dynar->used;
456 new_used = old_used + 1;
458 _xbt_dynar_expand(dynar, new_used);
460 nb_shift = old_used - idx;
463 memmove(_xbt_dynar_elm(dynar, idx + 1),
464 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
467 dynar->used = new_used;
468 res = _xbt_dynar_elm(dynar, idx);
472 /** @brief Make room for a new element, and return a pointer to it
474 * You can then use regular affectation to set its value instead of relying
475 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
477 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
482 res = _xbt_dynar_insert_at_ptr(dynar, idx);
483 _dynar_unlock(dynar);
487 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
489 * Set the Nth element of a dynar, expanding the dynar if needed, and
490 * moving the previously existing value and all subsequent ones to one
491 * position right in the dynar.
494 xbt_dynar_insert_at(xbt_dynar_t const dynar,
495 const int idx, const void *const src)
499 /* checks done in xbt_dynar_insert_at_ptr */
500 memcpy(_xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
501 _dynar_unlock(dynar);
504 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
506 * Get the Nth element of a dynar, removing it from the dynar and moving
507 * all subsequent values to one position left in the dynar.
509 * If the object argument of this function is a non-null pointer, the removed
510 * element is copied to this address. If not, the element is freed using the
511 * free_f function passed at dynar creation.
514 xbt_dynar_remove_at(xbt_dynar_t const dynar,
515 const int idx, void *const object)
519 _xbt_dynar_remove_at(dynar, idx, object);
520 _dynar_unlock(dynar);
523 /** @brief Returns the position of the element in the dynar
525 * Raises not_found_error if not found.
527 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
532 for (it = 0; it < dynar->used; it++)
533 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
534 _dynar_unlock(dynar);
538 _dynar_unlock(dynar);
539 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
543 /** @brief Returns a boolean indicating whether the element is part of the dynar */
544 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
550 xbt_dynar_search(dynar, elem);
553 if (e.category == not_found_error) {
562 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
564 * You can then use regular affectation to set its value instead of relying
565 * on the slow memcpy. This is what xbt_dynar_push_as() does.
567 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
571 /* we have to inline xbt_dynar_insert_at_ptr here to make sure that
572 dynar->used don't change between reading it and getting the lock
573 within xbt_dynar_insert_at_ptr */
575 res = _xbt_dynar_insert_at_ptr(dynar, dynar->used);
576 _dynar_unlock(dynar);
580 /** @brief Add an element at the end of the dynar */
581 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
582 const void *const src)
585 /* checks done in xbt_dynar_insert_at_ptr */
586 memcpy(_xbt_dynar_insert_at_ptr(dynar, dynar->used), src,
588 _dynar_unlock(dynar);
591 /** @brief Mark the last dynar's element as unused and return a pointer to it.
593 * You can then use regular affectation to set its value instead of relying
594 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
596 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
601 _check_populated_dynar(dynar);
602 XBT_DEBUG("Pop %p", (void *) dynar);
604 res = _xbt_dynar_elm(dynar, dynar->used);
605 _dynar_unlock(dynar);
609 /** @brief Get and remove the last element of the dynar */
610 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
613 /* sanity checks done by remove_at */
614 XBT_DEBUG("Pop %p", (void *) dynar);
616 _xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
617 _dynar_unlock(dynar);
620 /** @brief Add an element at the begining of the dynar.
622 * This is less efficient than xbt_dynar_push()
624 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
625 const void *const src)
628 /* sanity checks done by insert_at */
629 xbt_dynar_insert_at(dynar, 0, src);
632 /** @brief Get and remove the first element of the dynar.
634 * This is less efficient than xbt_dynar_pop()
636 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
639 /* sanity checks done by remove_at */
640 xbt_dynar_remove_at(dynar, 0, dst);
643 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
645 char *const data = (char *) dynar->data;
646 const unsigned long elmsize = dynar->elmsize;
647 const unsigned long used = dynar->used;
650 for (i = 0; i < used; i++) {
651 char* elm = (char*) data + i * elmsize;
656 /** @brief Apply a function to each member of a dynar
658 * The mapped function may change the value of the element itself,
659 * but should not mess with the structure of the dynar.
661 * If the dynar is synchronized, it is locked during the whole map
662 * operation, so make sure your function don't call any function
663 * from xbt_dynar_* on it, or you'll get a deadlock.
665 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
666 void_f_pvoid_t const op)
669 _sanity_check_dynar(dynar);
672 _dynar_map(dynar, op);
674 _dynar_unlock(dynar);
678 /** @brief Removes and free the entry pointed by the cursor
680 * This function can be used while traversing without problem.
682 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
683 unsigned int *const cursor)
686 _xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
689 /** @brief Unlocks a synchronized dynar when you want to break the traversal
691 * This function must be used if you <tt>break</tt> the
692 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
693 * regular traversal reaching the end of the elements
695 XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
697 _dynar_unlock(dynar);
700 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
702 * \param dynar the dynar to sort
703 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
705 * Remark: if the elements stored in the dynar are structures, the compar_fn
706 * function has to retrieve the field to sort first.
708 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
709 int_f_cpvoid_cpvoid_t compar_fn)
714 #ifdef HAVE_MERGESORT
715 mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
717 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
719 _dynar_unlock(dynar);
722 /** @brief Transform a dynar into a NULL terminated array
724 * \param dynar the dynar to transform
726 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
729 void * last = xbt_new0(char,dynar->elmsize);
730 xbt_dynar_push(dynar, last);
738 * Return 0 if d1 and d2 are equal and 1 if not equal
740 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
741 int(*compar)(const void *, const void *))
745 if((!d1) && (!d2)) return 0;
748 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
752 if((d1->elmsize)!=(d2->elmsize))
754 XBT_DEBUG("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize);
758 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
760 XBT_DEBUG("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2));
765 size = xbt_dynar_length(d1);
768 void *data1 = xbt_dynar_get_as(d1, i, void *);
769 void *data2 = xbt_dynar_get_as(d2, i, void *);
770 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
771 if(compar(data1,data2)){
784 XBT_TEST_SUITE("dynar", "Dynar data container");
785 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
786 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
788 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
790 /* Vars_decl [doxygen cruft] */
796 xbt_test_add("==== Traverse the empty dynar");
797 d = xbt_dynar_new(sizeof(int), NULL);
798 xbt_dynar_foreach(d, cursor, i) {
799 xbt_die( "Damnit, there is something in the empty dynar");
801 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
802 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
803 /* in your code is naturally the way to go outside a regression test */
806 ("==== Push %d int, set them again 3 times, traverse them, shift them",
808 /* Populate_ints [doxygen cruft] */
809 /* 1. Populate the dynar */
810 d = xbt_dynar_new(sizeof(int), NULL);
811 for (cpt = 0; cpt < NB_ELEM; cpt++) {
812 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
813 /* xbt_dynar_push(d,&cpt); This would also work */
814 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
817 /* 2. Traverse manually the dynar */
818 for (cursor = 0; cursor < NB_ELEM; cursor++) {
819 iptr = xbt_dynar_get_ptr(d, cursor);
820 xbt_test_assert(cursor == *iptr,
821 "The retrieved value is not the same than the injected one (%d!=%d)",
825 /* 3. Traverse the dynar using the neat macro to that extend */
826 xbt_dynar_foreach(d, cursor, cpt) {
827 xbt_test_assert(cursor == cpt,
828 "The retrieved value is not the same than the injected one (%d!=%d)",
831 /* end_of_traversal */
833 for (cpt = 0; cpt < NB_ELEM; cpt++)
834 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
836 for (cpt = 0; cpt < NB_ELEM; cpt++)
837 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
838 /* xbt_dynar_set(d,cpt,&cpt); */
840 for (cpt = 0; cpt < NB_ELEM; cpt++)
841 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
844 xbt_dynar_foreach(d, cursor, i) {
845 xbt_test_assert(i == cpt,
846 "The retrieved value is not the same than the injected one (%d!=%d)",
850 xbt_test_assert(cpt == NB_ELEM,
851 "Cannot retrieve my %d values. Last got one is %d",
854 /* shifting [doxygen cruft] */
855 /* 4. Shift all the values */
856 for (cpt = 0; cpt < NB_ELEM; cpt++) {
857 xbt_dynar_shift(d, &i);
858 xbt_test_assert(i == cpt,
859 "The retrieved value is not the same than the injected one (%d!=%d)",
861 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
864 /* 5. Free the resources */
865 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
866 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
867 /* in your code is naturally the way to go outside a regression test */
869 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
870 d = xbt_dynar_new(sizeof(int), NULL);
871 for (cpt = 0; cpt < NB_ELEM; cpt++) {
872 xbt_dynar_unshift(d, &cpt);
873 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
875 for (cpt = 0; cpt < NB_ELEM; cpt++) {
876 i = xbt_dynar_pop_as(d, int);
877 xbt_test_assert(i == cpt,
878 "The retrieved value is not the same than the injected one (%d!=%d)",
880 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
882 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
883 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
884 /* in your code is naturally the way to go outside a regression test */
888 ("==== Push %d int, insert 1000 int in the middle, shift everything",
890 d = xbt_dynar_new(sizeof(int), NULL);
891 for (cpt = 0; cpt < NB_ELEM; cpt++) {
892 xbt_dynar_push_as(d, int, cpt);
893 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
895 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
896 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
897 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
900 for (cpt = 0; cpt < NB_ELEM/2; 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 at the begining (%d!=%d)",
905 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
907 for (cpt = 999; cpt >= 0; cpt--) {
908 xbt_dynar_shift(d, &i);
909 xbt_test_assert(i == cpt,
910 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
913 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
914 xbt_dynar_shift(d, &i);
915 xbt_test_assert(i == cpt,
916 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
919 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
920 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
921 /* in your code is naturally the way to go outside a regression test */
923 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
925 d = xbt_dynar_new(sizeof(int), NULL);
926 for (cpt = 0; cpt < NB_ELEM; cpt++)
927 xbt_dynar_push_as(d, int, cpt);
929 for (cpt = 2000; cpt < 4000; cpt++) {
930 xbt_dynar_remove_at(d, 2000, &i);
931 xbt_test_assert(i == cpt,
932 "Remove a bad value. Got %d, expected %d", i, cpt);
933 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
935 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
936 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
937 /* in your code is naturally the way to go outside a regression test */
940 /*******************************************************************************/
941 /*******************************************************************************/
942 /*******************************************************************************/
943 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
945 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
949 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
950 /* Populate_ints [doxygen cruft] */
951 /* 1. Populate the dynar */
952 for (cpt = 0; cpt < NB_ELEM; cpt++) {
953 xbt_dynar_insert_at(d, cpt, &cpt);
954 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
957 /* 3. Traverse the dynar */
958 xbt_dynar_foreach(d, cursor, cpt) {
959 xbt_test_assert(cursor == cpt,
960 "The retrieved value is not the same than the injected one (%d!=%d)",
963 /* end_of_traversal */
965 /* Re-fill with the same values using set_as (and re-verify) */
966 for (cpt = 0; cpt < NB_ELEM; cpt++)
967 xbt_dynar_set_as(d, cpt, int, cpt);
968 xbt_dynar_foreach(d, cursor, cpt)
969 xbt_test_assert(cursor == cpt,
970 "The retrieved value is not the same than the injected one (%d!=%d)",
973 for (cpt = 0; cpt < NB_ELEM; cpt++) {
975 xbt_dynar_remove_at(d,0,&val);
976 xbt_test_assert(cpt == val,
977 "The retrieved value is not the same than the injected one (%d!=%d)",
980 xbt_test_assert(xbt_dynar_is_empty(d),
981 "There is still %lu elements in the dynar after removing everything",
982 xbt_dynar_length(d));
985 /* ********************* */
986 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
987 d = xbt_dynar_new(sizeof(int), NULL);
988 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
989 xbt_dynar_replace(d, cpt, &cpt);
990 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
993 /* 3. Traverse the dynar */
994 xbt_dynar_foreach(d, cursor, cpt) {
995 xbt_test_assert(cursor == cpt,
996 "The retrieved value is not the same than the injected one (%d!=%d)",
999 /* end_of_traversal */
1001 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
1003 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
1004 xbt_test_assert(cpt == val,
1005 "The retrieved value is not the same than the injected one (%d!=%d)",
1008 xbt_test_assert(xbt_dynar_is_empty(d),
1009 "There is still %lu elements in the dynar after removing everything",
1010 xbt_dynar_length(d));
1014 /*******************************************************************************/
1015 /*******************************************************************************/
1016 /*******************************************************************************/
1017 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
1021 unsigned int cursor;
1024 xbt_test_add("==== Traverse the empty dynar");
1025 d = xbt_dynar_new(sizeof(int), NULL);
1026 xbt_dynar_foreach(d, cursor, cpt) {
1027 xbt_test_assert(FALSE,
1028 "Damnit, there is something in the empty dynar");
1030 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1031 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1032 /* in your code is naturally the way to go outside a regression test */
1034 xbt_test_add("==== Push/shift 5000 doubles");
1035 d = xbt_dynar_new(sizeof(double), NULL);
1036 for (cpt = 0; cpt < 5000; cpt++) {
1038 xbt_dynar_push(d, &d1);
1040 xbt_dynar_foreach(d, cursor, d2) {
1041 d1 = (double) cursor;
1042 xbt_test_assert(d1 == d2,
1043 "The retrieved value is not the same than the injected one (%f!=%f)",
1046 for (cpt = 0; cpt < 5000; cpt++) {
1048 xbt_dynar_shift(d, &d2);
1049 xbt_test_assert(d1 == d2,
1050 "The retrieved value is not the same than the injected one (%f!=%f)",
1053 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1054 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1055 /* in your code is naturally the way to go outside a regression test */
1057 xbt_test_add("==== Unshift/pop 5000 doubles");
1058 d = xbt_dynar_new(sizeof(double), NULL);
1059 for (cpt = 0; cpt < 5000; cpt++) {
1061 xbt_dynar_unshift(d, &d1);
1063 for (cpt = 0; cpt < 5000; cpt++) {
1065 xbt_dynar_pop(d, &d2);
1066 xbt_test_assert(d1 == d2,
1067 "The retrieved value is not the same than the injected one (%f!=%f)",
1070 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1071 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1072 /* in your code is naturally the way to go outside a regression test */
1077 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1078 d = xbt_dynar_new(sizeof(double), NULL);
1079 for (cpt = 0; cpt < 5000; cpt++) {
1081 xbt_dynar_push(d, &d1);
1083 for (cpt = 0; cpt < 1000; cpt++) {
1085 xbt_dynar_insert_at(d, 2500, &d1);
1088 for (cpt = 0; cpt < 2500; cpt++) {
1090 xbt_dynar_shift(d, &d2);
1091 xbt_test_assert(d1 == d2,
1092 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1094 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1096 for (cpt = 999; cpt >= 0; 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 in the middle (%f!=%f)",
1103 for (cpt = 2500; cpt < 5000; cpt++) {
1105 xbt_dynar_shift(d, &d2);
1106 xbt_test_assert(d1 == d2,
1107 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1110 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1111 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1112 /* in your code is naturally the way to go outside a regression test */
1115 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1116 d = xbt_dynar_new(sizeof(double), NULL);
1117 for (cpt = 0; cpt < 5000; cpt++) {
1119 xbt_dynar_push(d, &d1);
1121 for (cpt = 2000; cpt < 4000; cpt++) {
1123 xbt_dynar_remove_at(d, 2000, &d2);
1124 xbt_test_assert(d1 == d2,
1125 "Remove a bad value. Got %f, expected %f", d2, d1);
1127 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1128 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1129 /* in your code is naturally the way to go outside a regression test */
1133 /* doxygen_string_cruft */
1135 /*******************************************************************************/
1136 /*******************************************************************************/
1137 /*******************************************************************************/
1138 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1146 xbt_test_add("==== Traverse the empty dynar");
1147 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1148 xbt_dynar_foreach(d, iter, s1) {
1149 xbt_test_assert(FALSE,
1150 "Damnit, there is something in the empty dynar");
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 */
1156 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1158 /* Populate_str [doxygen cruft] */
1159 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1160 /* 1. Populate the dynar */
1161 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1162 sprintf(buf, "%d", cpt);
1164 xbt_dynar_push(d, &s1);
1166 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1167 sprintf(buf, "%d", cpt);
1169 xbt_dynar_replace(d, cpt, &s1);
1171 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1172 sprintf(buf, "%d", cpt);
1174 xbt_dynar_replace(d, cpt, &s1);
1176 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1177 sprintf(buf, "%d", cpt);
1179 xbt_dynar_replace(d, cpt, &s1);
1181 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1182 sprintf(buf, "%d", cpt);
1183 xbt_dynar_shift(d, &s2);
1184 xbt_test_assert(!strcmp(buf, s2),
1185 "The retrieved value is not the same than the injected one (%s!=%s)",
1189 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1190 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1191 /* in your code is naturally the way to go outside a regression test */
1193 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1194 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1195 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1196 sprintf(buf, "%d", cpt);
1198 xbt_dynar_unshift(d, &s1);
1200 /* 2. Traverse the dynar with the macro */
1201 xbt_dynar_foreach(d, iter, s1) {
1202 sprintf(buf, "%d", NB_ELEM - iter - 1);
1203 xbt_test_assert(!strcmp(buf, s1),
1204 "The retrieved value is not the same than the injected one (%s!=%s)",
1207 /* 3. Traverse the dynar with the macro */
1208 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1209 sprintf(buf, "%d", cpt);
1210 xbt_dynar_pop(d, &s2);
1211 xbt_test_assert(!strcmp(buf, s2),
1212 "The retrieved value is not the same than the injected one (%s!=%s)",
1216 /* 4. Free the resources */
1217 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1218 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1219 /* in your code is naturally the way to go outside a regression test */
1223 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1224 NB_ELEM, NB_ELEM / 5);
1225 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1226 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1227 sprintf(buf, "%d", cpt);
1229 xbt_dynar_push(d, &s1);
1231 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1232 sprintf(buf, "%d", cpt);
1234 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1237 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1238 sprintf(buf, "%d", cpt);
1239 xbt_dynar_shift(d, &s2);
1240 xbt_test_assert(!strcmp(buf, s2),
1241 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1245 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1246 sprintf(buf, "%d", cpt);
1247 xbt_dynar_shift(d, &s2);
1248 xbt_test_assert(!strcmp(buf, s2),
1249 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1253 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1254 sprintf(buf, "%d", cpt);
1255 xbt_dynar_shift(d, &s2);
1256 xbt_test_assert(!strcmp(buf, s2),
1257 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1261 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1262 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1263 /* in your code is naturally the way to go outside a regression test */
1266 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1267 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1268 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1269 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1270 sprintf(buf, "%d", cpt);
1272 xbt_dynar_push(d, &s1);
1274 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1275 sprintf(buf, "%d", cpt);
1276 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1277 xbt_test_assert(!strcmp(buf, s2),
1278 "Remove a bad value. Got %s, expected %s", s2, buf);
1281 xbt_dynar_free(&d); /* end_of_doxygen */
1285 /*******************************************************************************/
1286 /*******************************************************************************/
1287 /*******************************************************************************/
1288 #include "xbt/synchro.h"
1289 static void pusher_f(void *a)
1291 xbt_dynar_t d = (xbt_dynar_t) a;
1293 for (i = 0; i < 500; i++) {
1294 xbt_dynar_push(d, &i);
1298 static void poper_f(void *a)
1300 xbt_dynar_t d = (xbt_dynar_t) a;
1305 for (i = 0; i < 500; i++) {
1307 xbt_dynar_pop(d, &data);
1310 if (e.category == bound_error) {
1321 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int, "Synchronized dynars of integers")
1323 /* Vars_decl [doxygen cruft] */
1325 xbt_thread_t pusher, poper;
1327 xbt_test_add("==== Have a pusher and a popper on the dynar");
1328 d = xbt_dynar_new_sync(sizeof(int), NULL);
1329 pusher = xbt_thread_create("pusher", pusher_f, d, 0 /*not joinable */ );
1330 poper = xbt_thread_create("poper", poper_f, d, 0 /*not joinable */ );
1331 xbt_thread_join(pusher);
1332 xbt_thread_join(poper);
1336 #endif /* SIMGRID_TEST */