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_dynar_resize(xbt_dynar_t dynar, unsigned long new_size)
80 if (new_size != dynar->size) {
81 dynar->size = new_size;
82 dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize);
87 void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
89 const unsigned long old_size = dynar->size;
92 const unsigned long expand = 2 * (old_size + 1);
93 _xbt_dynar_resize(dynar, (nb > expand ? nb : expand));
94 XBT_DEBUG("expand %p from %lu to %lu elements",
95 dynar, old_size, dynar->size);
100 void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
102 char *const data = (char *) dynar->data;
103 const unsigned long elmsize = dynar->elmsize;
105 return data + idx * elmsize;
110 _xbt_dynar_get_elm(void *const dst,
111 const xbt_dynar_t dynar, const unsigned long idx)
113 void *const elm = _xbt_dynar_elm(dynar, idx);
115 memcpy(dst, elm, dynar->elmsize);
120 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
121 const unsigned long idx, const void *const src)
123 void *const elm = _xbt_dynar_elm(dynar, idx);
124 const unsigned long elmsize = dynar->elmsize;
126 memcpy(elm, src, elmsize);
131 _xbt_dynar_remove_at(xbt_dynar_t const dynar,
132 const unsigned long idx, void *const object)
135 unsigned long nb_shift;
136 unsigned long offset;
138 _sanity_check_dynar(dynar);
139 _check_inbound_idx(dynar, idx);
142 _xbt_dynar_get_elm(object, dynar, idx);
143 } else if (dynar->free_f) {
144 if (dynar->elmsize <= SIZEOF_MAX) {
145 char elm[SIZEOF_MAX];
146 _xbt_dynar_get_elm(elm, dynar, idx);
149 char *elm = malloc(dynar->elmsize);
150 _xbt_dynar_get_elm(elm, dynar, idx);
156 nb_shift = dynar->used - 1 - idx;
159 offset = nb_shift * dynar->elmsize;
160 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
167 void xbt_dynar_dump(xbt_dynar_t dynar)
169 XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
170 dynar->size, dynar->used, dynar->elmsize, dynar->data,
174 /** @brief Constructor
176 * \param elmsize size of each element in the dynar
177 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
179 * Creates a new dynar. If a free_func is provided, the elements have to be
180 * pointer of pointer. That is to say that dynars can contain either base
181 * types (int, char, double, etc) or pointer of pointers (struct **).
184 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
187 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
191 dynar->elmsize = elmsize;
193 dynar->free_f = free_f;
199 /** @brief Creates a synchronized dynar.
201 * Just like #xbt_dynar_new, but each access to the structure will be protected by a mutex
205 xbt_dynar_new_sync(const unsigned long elmsize,
206 void_f_pvoid_t const free_f)
208 xbt_dynar_t res = xbt_dynar_new(elmsize, free_f);
209 res->mutex = xbt_mutex_init();
213 /** @brief Destructor of the structure not touching to the content
215 * \param dynar poor victim
217 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
218 * is not touched (the \a free_f function is not used)
220 void xbt_dynar_free_container(xbt_dynar_t * dynar)
222 if (dynar && *dynar) {
223 xbt_dynar_t d = *dynar;
226 xbt_mutex_destroy(d->mutex);
232 /** @brief Frees the content and set the size to 0
234 * \param dynar who to squeeze
236 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
240 _sanity_check_dynar(dynar);
242 XBT_DEBUG("Reset the dynar %p", (void *) dynar);
244 _dynar_map(dynar, dynar->free_f);
248 _dynar_unlock(dynar);
252 * \brief Shrink the dynar by removing empty slots at the end of the internal array
253 * \param dynar a dynar
254 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
255 * internal array for further insertions
257 * Reduces the internal array size of the dynar to the number of elements plus
258 * \a empty_slots_wanted.
259 * After removing elements from the dynar, you can call this function to make
260 * the dynar use less memory.
261 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
263 * Note that if \a empty_slots_wanted is greater than the array size, the internal
264 * array is expanded instead of shriked.
266 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
269 _xbt_dynar_resize(dynar, dynar->used + empty_slots_wanted);
270 _dynar_unlock(dynar);
273 /** @brief Destructor
275 * \param dynar poor victim
277 * kilkil a dynar and its content
280 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
282 if (dynar && *dynar) {
283 xbt_dynar_reset(*dynar);
284 xbt_dynar_free_container(dynar);
288 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
289 void xbt_dynar_free_voidp(void *d)
291 xbt_dynar_t dynar = (xbt_dynar_t)d;
292 xbt_dynar_free(&dynar);
295 /** @brief Count of dynar's elements
297 * \param dynar the dynar we want to mesure
299 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
301 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
304 /**@brief check if a dynar is empty
306 *\param dynar the dynat we want to check
309 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
311 return (xbt_dynar_length(dynar) == 0);
314 /** @brief Retrieve a copy of the Nth element of a dynar.
316 * \param dynar information dealer
317 * \param idx index of the slot we want to retrieve
318 * \param[out] dst where to put the result to.
321 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
322 const unsigned long idx, void *const dst)
325 _sanity_check_dynar(dynar);
326 _check_inbound_idx(dynar, idx);
328 _xbt_dynar_get_elm(dst, dynar, idx);
329 _dynar_unlock(dynar);
332 /** @brief Retrieve a pointer to the Nth element of a dynar.
334 * \param dynar information dealer
335 * \param idx index of the slot we want to retrieve
336 * \return the \a idx-th element of \a dynar.
338 * \warning The returned value is the actual content of the dynar.
339 * Make a copy before fooling with it.
341 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
342 const unsigned long idx)
347 _sanity_check_dynar(dynar);
348 _check_inbound_idx(dynar, idx);
350 res = _xbt_dynar_elm(dynar, idx);
351 _dynar_unlock(dynar);
355 /* not synchronized */
356 static XBT_INLINE void *_xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
357 const unsigned long idx)
359 _sanity_check_dynar(dynar);
361 if (idx >= dynar->used) {
362 _xbt_dynar_expand(dynar, idx + 1);
363 memset(((char * const)dynar->data) + dynar->used * dynar->elmsize, 0,
364 (idx + 1 - dynar->used)*dynar->elmsize);
365 dynar->used = idx + 1;
367 return _xbt_dynar_elm(dynar, idx);
370 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
371 const unsigned long idx)
375 res = _xbt_dynar_set_at_ptr(dynar, idx);
376 _dynar_unlock(dynar);
380 static void XBT_INLINE /* not synchronized */
381 _xbt_dynar_set(xbt_dynar_t dynar,
382 const unsigned long idx, const void *const src)
384 memcpy(_xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
387 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
389 * \param dynar information dealer
390 * \param idx index of the slot we want to modify
391 * \param src What will be feeded to the dynar
393 * If you want to free the previous content, use xbt_dynar_replace().
395 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
396 const void *const src)
400 _xbt_dynar_set(dynar, idx, src);
401 _dynar_unlock(dynar);
404 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
410 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
411 * free the previous value at this position. If you don't want to free the
412 * previous content, use xbt_dynar_set().
415 xbt_dynar_replace(xbt_dynar_t dynar,
416 const unsigned long idx, const void *const object)
419 _sanity_check_dynar(dynar);
421 if (idx < dynar->used && dynar->free_f) {
422 void *const old_object = _xbt_dynar_elm(dynar, idx);
424 dynar->free_f(old_object);
427 _xbt_dynar_set(dynar, idx, object);
428 _dynar_unlock(dynar);
431 static XBT_INLINE void *_xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
432 const unsigned long idx)
435 unsigned long old_used;
436 unsigned long new_used;
439 _sanity_check_dynar(dynar);
440 _sanity_check_idx(idx);
442 old_used = dynar->used;
443 new_used = old_used + 1;
445 _xbt_dynar_expand(dynar, new_used);
447 nb_shift = old_used - idx;
450 memmove(_xbt_dynar_elm(dynar, idx + 1),
451 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
454 dynar->used = new_used;
455 res = _xbt_dynar_elm(dynar, idx);
459 /** @brief Make room for a new element, and return a pointer to it
461 * You can then use regular affectation to set its value instead of relying
462 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
464 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
469 res = _xbt_dynar_insert_at_ptr(dynar, idx);
470 _dynar_unlock(dynar);
474 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
476 * Set the Nth element of a dynar, expanding the dynar if needed, and
477 * moving the previously existing value and all subsequent ones to one
478 * position right in the dynar.
481 xbt_dynar_insert_at(xbt_dynar_t const dynar,
482 const int idx, const void *const src)
486 /* checks done in xbt_dynar_insert_at_ptr */
487 memcpy(_xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
488 _dynar_unlock(dynar);
491 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
493 * Get the Nth element of a dynar, removing it from the dynar and moving
494 * all subsequent values to one position left in the dynar.
496 * If the object argument of this function is a non-null pointer, the removed
497 * element is copied to this address. If not, the element is freed using the
498 * free_f function passed at dynar creation.
501 xbt_dynar_remove_at(xbt_dynar_t const dynar,
502 const int idx, void *const object)
506 _xbt_dynar_remove_at(dynar, idx, object);
507 _dynar_unlock(dynar);
510 /** @brief Returns the position of the element in the dynar
512 * Raises not_found_error if not found.
514 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
519 for (it = 0; it < dynar->used; it++)
520 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
521 _dynar_unlock(dynar);
525 _dynar_unlock(dynar);
526 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
530 /** @brief Returns a boolean indicating whether the element is part of the dynar */
531 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
537 xbt_dynar_search(dynar, elem);
540 if (e.category == not_found_error) {
549 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
551 * You can then use regular affectation to set its value instead of relying
552 * on the slow memcpy. This is what xbt_dynar_push_as() does.
554 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
558 /* we have to inline xbt_dynar_insert_at_ptr here to make sure that
559 dynar->used don't change between reading it and getting the lock
560 within xbt_dynar_insert_at_ptr */
562 res = _xbt_dynar_insert_at_ptr(dynar, dynar->used);
563 _dynar_unlock(dynar);
567 /** @brief Add an element at the end of the dynar */
568 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
569 const void *const src)
572 /* checks done in xbt_dynar_insert_at_ptr */
573 memcpy(_xbt_dynar_insert_at_ptr(dynar, dynar->used), src,
575 _dynar_unlock(dynar);
578 /** @brief Mark the last dynar's element as unused and return a pointer to it.
580 * You can then use regular affectation to set its value instead of relying
581 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
583 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
588 _check_populated_dynar(dynar);
589 XBT_DEBUG("Pop %p", (void *) dynar);
591 res = _xbt_dynar_elm(dynar, dynar->used);
592 _dynar_unlock(dynar);
596 /** @brief Get and remove the last element of the dynar */
597 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
600 /* sanity checks done by remove_at */
601 XBT_DEBUG("Pop %p", (void *) dynar);
603 _xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
604 _dynar_unlock(dynar);
607 /** @brief Add an element at the begining of the dynar.
609 * This is less efficient than xbt_dynar_push()
611 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
612 const void *const src)
615 /* sanity checks done by insert_at */
616 xbt_dynar_insert_at(dynar, 0, src);
619 /** @brief Get and remove the first element of the dynar.
621 * This is less efficient than xbt_dynar_pop()
623 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
626 /* sanity checks done by remove_at */
627 xbt_dynar_remove_at(dynar, 0, dst);
630 static void _dynar_map(const xbt_dynar_t dynar, void_f_pvoid_t const op)
632 char *const data = (char *) dynar->data;
633 const unsigned long elmsize = dynar->elmsize;
634 const unsigned long used = dynar->used;
637 for (i = 0; i < used; i++) {
638 char* elm = (char*) data + i * elmsize;
643 /** @brief Apply a function to each member of a dynar
645 * The mapped function may change the value of the element itself,
646 * but should not mess with the structure of the dynar.
648 * If the dynar is synchronized, it is locked during the whole map
649 * operation, so make sure your function don't call any function
650 * from xbt_dynar_* on it, or you'll get a deadlock.
652 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
653 void_f_pvoid_t const op)
656 _sanity_check_dynar(dynar);
659 _dynar_map(dynar, op);
661 _dynar_unlock(dynar);
665 /** @brief Removes and free the entry pointed by the cursor
667 * This function can be used while traversing without problem.
669 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
670 unsigned int *const cursor)
673 _xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
676 /** @brief Unlocks a synchronized dynar when you want to break the traversal
678 * This function must be used if you <tt>break</tt> the
679 * xbt_dynar_foreach loop, but shouldn't be called at the end of a
680 * regular traversal reaching the end of the elements
682 XBT_INLINE void xbt_dynar_cursor_unlock(xbt_dynar_t dynar)
684 _dynar_unlock(dynar);
687 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
689 * \param dynar the dynar to sort
690 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
692 * Remark: if the elements stored in the dynar are structures, the compar_fn
693 * function has to retrieve the field to sort first.
695 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
696 int_f_cpvoid_cpvoid_t compar_fn)
701 #ifdef HAVE_MERGESORT
702 mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
704 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
706 _dynar_unlock(dynar);
709 /** @brief Transform a dynar into a NULL terminated array
711 * \param dynar the dynar to transform
713 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
716 void * last = xbt_new0(char,dynar->elmsize);
717 xbt_dynar_push(dynar, last);
725 * Return 0 if d1 and d2 are equal and 1 if not equal
727 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
728 int(*compar)(const void *, const void *))
732 if((!d1) && (!d2)) return 0;
735 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
739 if((d1->elmsize)!=(d2->elmsize))
741 XBT_DEBUG("Size of elmsize d1=%ld d2=%ld",d1->elmsize,d2->elmsize);
745 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
747 XBT_DEBUG("Size of dynar d1=%ld d2=%ld",xbt_dynar_length(d1),xbt_dynar_length(d2));
752 size = xbt_dynar_length(d1);
755 void *data1 = xbt_dynar_get_as(d1, i, void *);
756 void *data2 = xbt_dynar_get_as(d2, i, void *);
757 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
758 if(compar(data1,data2)){
771 XBT_TEST_SUITE("dynar", "Dynar data container");
772 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
773 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
775 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
777 /* Vars_decl [doxygen cruft] */
783 xbt_test_add("==== Traverse the empty dynar");
784 d = xbt_dynar_new(sizeof(int), NULL);
785 xbt_dynar_foreach(d, cursor, i) {
786 xbt_die( "Damnit, there is something in the empty dynar");
788 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
789 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
790 /* in your code is naturally the way to go outside a regression test */
793 ("==== Push %d int, set them again 3 times, traverse them, shift them",
795 /* Populate_ints [doxygen cruft] */
796 /* 1. Populate the dynar */
797 d = xbt_dynar_new(sizeof(int), NULL);
798 for (cpt = 0; cpt < NB_ELEM; cpt++) {
799 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
800 /* xbt_dynar_push(d,&cpt); This would also work */
801 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
804 /* 2. Traverse manually the dynar */
805 for (cursor = 0; cursor < NB_ELEM; cursor++) {
806 iptr = xbt_dynar_get_ptr(d, cursor);
807 xbt_test_assert(cursor == *iptr,
808 "The retrieved value is not the same than the injected one (%d!=%d)",
812 /* 3. Traverse the dynar using the neat macro to that extend */
813 xbt_dynar_foreach(d, cursor, cpt) {
814 xbt_test_assert(cursor == cpt,
815 "The retrieved value is not the same than the injected one (%d!=%d)",
818 /* end_of_traversal */
820 for (cpt = 0; cpt < NB_ELEM; cpt++)
821 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
823 for (cpt = 0; cpt < NB_ELEM; cpt++)
824 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
825 /* xbt_dynar_set(d,cpt,&cpt); */
827 for (cpt = 0; cpt < NB_ELEM; cpt++)
828 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
831 xbt_dynar_foreach(d, cursor, i) {
832 xbt_test_assert(i == cpt,
833 "The retrieved value is not the same than the injected one (%d!=%d)",
837 xbt_test_assert(cpt == NB_ELEM,
838 "Cannot retrieve my %d values. Last got one is %d",
841 /* shifting [doxygen cruft] */
842 /* 4. Shift all the values */
843 for (cpt = 0; cpt < NB_ELEM; cpt++) {
844 xbt_dynar_shift(d, &i);
845 xbt_test_assert(i == cpt,
846 "The retrieved value is not the same than the injected one (%d!=%d)",
848 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
851 /* 5. Free the resources */
852 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
853 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
854 /* in your code is naturally the way to go outside a regression test */
856 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
857 d = xbt_dynar_new(sizeof(int), NULL);
858 for (cpt = 0; cpt < NB_ELEM; cpt++) {
859 xbt_dynar_unshift(d, &cpt);
860 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
862 for (cpt = 0; cpt < NB_ELEM; cpt++) {
863 i = xbt_dynar_pop_as(d, int);
864 xbt_test_assert(i == cpt,
865 "The retrieved value is not the same than the injected one (%d!=%d)",
867 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
869 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
870 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
871 /* in your code is naturally the way to go outside a regression test */
875 ("==== Push %d int, insert 1000 int in the middle, shift everything",
877 d = xbt_dynar_new(sizeof(int), NULL);
878 for (cpt = 0; cpt < NB_ELEM; cpt++) {
879 xbt_dynar_push_as(d, int, cpt);
880 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
882 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
883 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
884 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
887 for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
888 xbt_dynar_shift(d, &i);
889 xbt_test_assert(i == cpt,
890 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
892 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
894 for (cpt = 999; cpt >= 0; cpt--) {
895 xbt_dynar_shift(d, &i);
896 xbt_test_assert(i == cpt,
897 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
900 for (cpt = 2500; cpt < NB_ELEM; 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 end (%d!=%d)",
906 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
907 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
908 /* in your code is naturally the way to go outside a regression test */
910 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
912 d = xbt_dynar_new(sizeof(int), NULL);
913 for (cpt = 0; cpt < NB_ELEM; cpt++)
914 xbt_dynar_push_as(d, int, cpt);
916 for (cpt = 2000; cpt < 4000; cpt++) {
917 xbt_dynar_remove_at(d, 2000, &i);
918 xbt_test_assert(i == cpt,
919 "Remove a bad value. Got %d, expected %d", i, cpt);
920 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
922 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
923 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
924 /* in your code is naturally the way to go outside a regression test */
927 /*******************************************************************************/
928 /*******************************************************************************/
929 /*******************************************************************************/
930 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
932 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
936 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
937 /* Populate_ints [doxygen cruft] */
938 /* 1. Populate the dynar */
939 for (cpt = 0; cpt < NB_ELEM; cpt++) {
940 xbt_dynar_insert_at(d, cpt, &cpt);
941 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
944 /* 3. Traverse the dynar */
945 xbt_dynar_foreach(d, cursor, cpt) {
946 xbt_test_assert(cursor == cpt,
947 "The retrieved value is not the same than the injected one (%d!=%d)",
950 /* end_of_traversal */
952 /* Re-fill with the same values using set_as (and re-verify) */
953 for (cpt = 0; cpt < NB_ELEM; cpt++)
954 xbt_dynar_set_as(d, cpt, int, cpt);
955 xbt_dynar_foreach(d, cursor, cpt)
956 xbt_test_assert(cursor == cpt,
957 "The retrieved value is not the same than the injected one (%d!=%d)",
960 for (cpt = 0; cpt < NB_ELEM; cpt++) {
962 xbt_dynar_remove_at(d,0,&val);
963 xbt_test_assert(cpt == val,
964 "The retrieved value is not the same than the injected one (%d!=%d)",
967 xbt_test_assert(xbt_dynar_is_empty(d),
968 "There is still %lu elements in the dynar after removing everything",
969 xbt_dynar_length(d));
972 /* ********************* */
973 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
974 d = xbt_dynar_new(sizeof(int), NULL);
975 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
976 xbt_dynar_replace(d, cpt, &cpt);
977 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
980 /* 3. Traverse the dynar */
981 xbt_dynar_foreach(d, cursor, cpt) {
982 xbt_test_assert(cursor == cpt,
983 "The retrieved value is not the same than the injected one (%d!=%d)",
986 /* end_of_traversal */
988 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
990 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
991 xbt_test_assert(cpt == val,
992 "The retrieved value is not the same than the injected one (%d!=%d)",
995 xbt_test_assert(xbt_dynar_is_empty(d),
996 "There is still %lu elements in the dynar after removing everything",
997 xbt_dynar_length(d));
1001 /*******************************************************************************/
1002 /*******************************************************************************/
1003 /*******************************************************************************/
1004 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
1008 unsigned int cursor;
1011 xbt_test_add("==== Traverse the empty dynar");
1012 d = xbt_dynar_new(sizeof(int), NULL);
1013 xbt_dynar_foreach(d, cursor, cpt) {
1014 xbt_test_assert(FALSE,
1015 "Damnit, there is something in the empty dynar");
1017 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1018 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1019 /* in your code is naturally the way to go outside a regression test */
1021 xbt_test_add("==== Push/shift 5000 doubles");
1022 d = xbt_dynar_new(sizeof(double), NULL);
1023 for (cpt = 0; cpt < 5000; cpt++) {
1025 xbt_dynar_push(d, &d1);
1027 xbt_dynar_foreach(d, cursor, d2) {
1028 d1 = (double) cursor;
1029 xbt_test_assert(d1 == d2,
1030 "The retrieved value is not the same than the injected one (%f!=%f)",
1033 for (cpt = 0; cpt < 5000; cpt++) {
1035 xbt_dynar_shift(d, &d2);
1036 xbt_test_assert(d1 == d2,
1037 "The retrieved value is not the same than the injected one (%f!=%f)",
1040 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1041 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1042 /* in your code is naturally the way to go outside a regression test */
1044 xbt_test_add("==== Unshift/pop 5000 doubles");
1045 d = xbt_dynar_new(sizeof(double), NULL);
1046 for (cpt = 0; cpt < 5000; cpt++) {
1048 xbt_dynar_unshift(d, &d1);
1050 for (cpt = 0; cpt < 5000; cpt++) {
1052 xbt_dynar_pop(d, &d2);
1053 xbt_test_assert(d1 == d2,
1054 "The retrieved value is not the same than the injected one (%f!=%f)",
1057 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1058 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1059 /* in your code is naturally the way to go outside a regression test */
1064 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1065 d = xbt_dynar_new(sizeof(double), NULL);
1066 for (cpt = 0; cpt < 5000; cpt++) {
1068 xbt_dynar_push(d, &d1);
1070 for (cpt = 0; cpt < 1000; cpt++) {
1072 xbt_dynar_insert_at(d, 2500, &d1);
1075 for (cpt = 0; cpt < 2500; cpt++) {
1077 xbt_dynar_shift(d, &d2);
1078 xbt_test_assert(d1 == d2,
1079 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1081 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1083 for (cpt = 999; cpt >= 0; cpt--) {
1085 xbt_dynar_shift(d, &d2);
1086 xbt_test_assert(d1 == d2,
1087 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1090 for (cpt = 2500; cpt < 5000; cpt++) {
1092 xbt_dynar_shift(d, &d2);
1093 xbt_test_assert(d1 == d2,
1094 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1097 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1098 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1099 /* in your code is naturally the way to go outside a regression test */
1102 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1103 d = xbt_dynar_new(sizeof(double), NULL);
1104 for (cpt = 0; cpt < 5000; cpt++) {
1106 xbt_dynar_push(d, &d1);
1108 for (cpt = 2000; cpt < 4000; cpt++) {
1110 xbt_dynar_remove_at(d, 2000, &d2);
1111 xbt_test_assert(d1 == d2,
1112 "Remove a bad value. Got %f, expected %f", d2, d1);
1114 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1115 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1116 /* in your code is naturally the way to go outside a regression test */
1120 /* doxygen_string_cruft */
1122 /*******************************************************************************/
1123 /*******************************************************************************/
1124 /*******************************************************************************/
1125 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1133 xbt_test_add("==== Traverse the empty dynar");
1134 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1135 xbt_dynar_foreach(d, iter, s1) {
1136 xbt_test_assert(FALSE,
1137 "Damnit, there is something in the empty dynar");
1139 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1140 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1141 /* in your code is naturally the way to go outside a regression test */
1143 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1145 /* Populate_str [doxygen cruft] */
1146 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1147 /* 1. Populate the dynar */
1148 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1149 sprintf(buf, "%d", cpt);
1151 xbt_dynar_push(d, &s1);
1153 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1154 sprintf(buf, "%d", cpt);
1156 xbt_dynar_replace(d, cpt, &s1);
1158 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1159 sprintf(buf, "%d", cpt);
1161 xbt_dynar_replace(d, cpt, &s1);
1163 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1164 sprintf(buf, "%d", cpt);
1166 xbt_dynar_replace(d, cpt, &s1);
1168 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1169 sprintf(buf, "%d", cpt);
1170 xbt_dynar_shift(d, &s2);
1171 xbt_test_assert(!strcmp(buf, s2),
1172 "The retrieved value is not the same than the injected one (%s!=%s)",
1176 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1177 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1178 /* in your code is naturally the way to go outside a regression test */
1180 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1181 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1182 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1183 sprintf(buf, "%d", cpt);
1185 xbt_dynar_unshift(d, &s1);
1187 /* 2. Traverse the dynar with the macro */
1188 xbt_dynar_foreach(d, iter, s1) {
1189 sprintf(buf, "%d", NB_ELEM - iter - 1);
1190 xbt_test_assert(!strcmp(buf, s1),
1191 "The retrieved value is not the same than the injected one (%s!=%s)",
1194 /* 3. Traverse the dynar with the macro */
1195 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1196 sprintf(buf, "%d", cpt);
1197 xbt_dynar_pop(d, &s2);
1198 xbt_test_assert(!strcmp(buf, s2),
1199 "The retrieved value is not the same than the injected one (%s!=%s)",
1203 /* 4. Free the resources */
1204 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1205 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1206 /* in your code is naturally the way to go outside a regression test */
1210 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1211 NB_ELEM, NB_ELEM / 5);
1212 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1213 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1214 sprintf(buf, "%d", cpt);
1216 xbt_dynar_push(d, &s1);
1218 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1219 sprintf(buf, "%d", cpt);
1221 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1224 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1225 sprintf(buf, "%d", cpt);
1226 xbt_dynar_shift(d, &s2);
1227 xbt_test_assert(!strcmp(buf, s2),
1228 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1232 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1233 sprintf(buf, "%d", cpt);
1234 xbt_dynar_shift(d, &s2);
1235 xbt_test_assert(!strcmp(buf, s2),
1236 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1240 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1241 sprintf(buf, "%d", cpt);
1242 xbt_dynar_shift(d, &s2);
1243 xbt_test_assert(!strcmp(buf, s2),
1244 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1248 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1249 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1250 /* in your code is naturally the way to go outside a regression test */
1253 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1254 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1255 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1256 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1257 sprintf(buf, "%d", cpt);
1259 xbt_dynar_push(d, &s1);
1261 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1262 sprintf(buf, "%d", cpt);
1263 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1264 xbt_test_assert(!strcmp(buf, s2),
1265 "Remove a bad value. Got %s, expected %s", s2, buf);
1268 xbt_dynar_free(&d); /* end_of_doxygen */
1272 /*******************************************************************************/
1273 /*******************************************************************************/
1274 /*******************************************************************************/
1275 #include "xbt/synchro.h"
1276 static void pusher_f(void *a)
1278 xbt_dynar_t d = (xbt_dynar_t) a;
1280 for (i = 0; i < 500; i++) {
1281 xbt_dynar_push(d, &i);
1285 static void poper_f(void *a)
1287 xbt_dynar_t d = (xbt_dynar_t) a;
1292 for (i = 0; i < 500; i++) {
1294 xbt_dynar_pop(d, &data);
1297 if (e.category == bound_error) {
1308 XBT_TEST_UNIT("synchronized int", test_dynar_sync_int, "Synchronized dynars of integers")
1310 /* Vars_decl [doxygen cruft] */
1312 xbt_thread_t pusher, poper;
1314 xbt_test_add("==== Have a pusher and a popper on the dynar");
1315 d = xbt_dynar_new_sync(sizeof(int), NULL);
1316 pusher = xbt_thread_create("pusher", pusher_f, d, 0 /*not joinable */ );
1317 poper = xbt_thread_create("poper", poper_f, d, 0 /*not joinable */ );
1318 xbt_thread_join(pusher);
1319 xbt_thread_join(poper);
1323 #endif /* SIMGRID_TEST */