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. */
10 #include "xbt/sysdep.h"
13 #include "xbt/dynar.h"
14 #include <sys/types.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
18 static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
20 xbt_assert(dynar, "dynar is NULL");
23 static XBT_INLINE void _sanity_check_idx(int idx)
25 xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
28 static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
30 if (idx < 0 || idx >= dynar->used) {
31 THROWF(bound_error, idx,
32 "dynar is not that long. You asked %d, but it's only %lu long",
33 (int) (idx), (unsigned long) dynar->used);
37 static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
39 if (dynar->used == 0) {
40 THROWF(bound_error, 0, "dynar %p is empty", dynar);
45 void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size)
47 if (new_size != dynar->size) {
48 dynar->size = new_size;
49 dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize);
54 void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
56 const unsigned long old_size = dynar->size;
59 const unsigned long expand = 2 * (old_size + 1);
60 _xbt_dynar_resize(dynar, (nb > expand ? nb : expand));
61 XBT_DEBUG("expand %p from %lu to %lu elements",
62 dynar, old_size, dynar->size);
67 void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
69 char *const data = (char *) dynar->data;
70 const unsigned long elmsize = dynar->elmsize;
72 return data + idx * elmsize;
77 _xbt_dynar_get_elm(void *const dst,
78 const xbt_dynar_t dynar, const unsigned long idx)
80 void *const elm = _xbt_dynar_elm(dynar, idx);
82 memcpy(dst, elm, dynar->elmsize);
87 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
88 const unsigned long idx, const void *const src)
90 void *const elm = _xbt_dynar_elm(dynar, idx);
91 const unsigned long elmsize = dynar->elmsize;
93 memcpy(elm, src, elmsize);
96 void xbt_dynar_dump(xbt_dynar_t dynar)
98 XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
99 dynar->size, dynar->used, dynar->elmsize, dynar->data,
103 /** @brief Constructor
105 * \param elmsize size of each element in the dynar
106 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
108 * Creates a new dynar. If a free_func is provided, the elements have to be
109 * pointer of pointer. That is to say that dynars can contain either base
110 * types (int, char, double, etc) or pointer of pointers (struct **).
113 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
116 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
120 dynar->elmsize = elmsize;
122 dynar->free_f = free_f;
127 /** @brief Destructor of the structure not touching to the content
129 * \param dynar poor victim
131 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
132 * is not touched (the \a free_f function is not used)
134 void xbt_dynar_free_container(xbt_dynar_t * dynar)
136 if (dynar && *dynar) {
137 xbt_dynar_t d = *dynar;
144 /** @brief Frees the content and set the size to 0
146 * \param dynar who to squeeze
148 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
150 _sanity_check_dynar(dynar);
152 XBT_DEBUG("Reset the dynar %p", (void *) dynar);
154 xbt_dynar_map(dynar, dynar->free_f);
159 /** @brief Merge dynar d2 into d1
161 * \param d1 dynar to keep
162 * \param d2 dynar to merge into d1. This dynar is free at end.
164 void xbt_dynar_merge(xbt_dynar_t *d1, xbt_dynar_t *d2)
166 if((*d1)->elmsize != (*d2)->elmsize)
167 xbt_die("Element size must are not equal");
169 const unsigned long elmsize = (*d1)->elmsize;
171 void *ptr = _xbt_dynar_elm((*d2), 0);
172 _xbt_dynar_resize(*d1, (*d1)->size + (*d2)->size);
173 void *elm = _xbt_dynar_elm((*d1), (*d1)->used);
175 memcpy(elm, ptr, ((*d2)->size)*elmsize);
176 (*d1)->used += (*d2)->used;
182 * \brief Shrink the dynar by removing empty slots at the end of the internal array
183 * \param dynar a dynar
184 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
185 * internal array for further insertions
187 * Reduces the internal array size of the dynar to the number of elements plus
188 * \a empty_slots_wanted.
189 * After removing elements from the dynar, you can call this function to make
190 * the dynar use less memory.
191 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
193 * Note that if \a empty_slots_wanted is greater than the array size, the internal
194 * array is expanded instead of shriked.
196 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
198 _xbt_dynar_resize(dynar, dynar->used + empty_slots_wanted);
201 /** @brief Destructor
203 * \param dynar poor victim
205 * kilkil a dynar and its content
208 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
210 if (dynar && *dynar) {
211 xbt_dynar_reset(*dynar);
212 xbt_dynar_free_container(dynar);
216 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
217 void xbt_dynar_free_voidp(void *d)
219 xbt_dynar_t dynar = (xbt_dynar_t)d;
220 xbt_dynar_free(&dynar);
223 /** @brief Count of dynar's elements
225 * \param dynar the dynar we want to mesure
227 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
229 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
232 /**@brief check if a dynar is empty
234 *\param dynar the dynat we want to check
237 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
239 return (xbt_dynar_length(dynar) == 0);
242 /** @brief Retrieve a copy of the Nth element of a dynar.
244 * \param dynar information dealer
245 * \param idx index of the slot we want to retrieve
246 * \param[out] dst where to put the result to.
249 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
250 const unsigned long idx, void *const dst)
252 _sanity_check_dynar(dynar);
253 _check_inbound_idx(dynar, idx);
255 _xbt_dynar_get_elm(dst, dynar, idx);
258 /** @brief Retrieve a pointer to the Nth element of a dynar.
260 * \param dynar information dealer
261 * \param idx index of the slot we want to retrieve
262 * \return the \a idx-th element of \a dynar.
264 * \warning The returned value is the actual content of the dynar.
265 * Make a copy before fooling with it.
267 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
268 const unsigned long idx)
272 _sanity_check_dynar(dynar);
273 _check_inbound_idx(dynar, idx);
275 res = _xbt_dynar_elm(dynar, idx);
279 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
280 const unsigned long idx)
282 _sanity_check_dynar(dynar);
284 if (idx >= dynar->used) {
285 _xbt_dynar_expand(dynar, idx + 1);
286 if (idx > dynar->used) {
287 memset(_xbt_dynar_elm(dynar, dynar->used), 0,
288 (idx - dynar->used) * dynar->elmsize);
290 dynar->used = idx + 1;
292 return _xbt_dynar_elm(dynar, idx);
295 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
297 * \param dynar information dealer
298 * \param idx index of the slot we want to modify
299 * \param src What will be feeded to the dynar
301 * If you want to free the previous content, use xbt_dynar_replace().
303 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
304 const void *const src)
307 memcpy(xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
310 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
316 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
317 * free the previous value at this position. If you don't want to free the
318 * previous content, use xbt_dynar_set().
321 xbt_dynar_replace(xbt_dynar_t dynar,
322 const unsigned long idx, const void *const object)
324 _sanity_check_dynar(dynar);
326 if (idx < dynar->used && dynar->free_f) {
327 void *const old_object = _xbt_dynar_elm(dynar, idx);
329 dynar->free_f(old_object);
332 xbt_dynar_set(dynar, idx, object);
335 /** @brief Make room for a new element, and return a pointer to it
337 * You can then use regular affectation to set its value instead of relying
338 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
340 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
343 unsigned long old_used;
344 unsigned long new_used;
347 _sanity_check_dynar(dynar);
348 _sanity_check_idx(idx);
350 old_used = dynar->used;
351 new_used = old_used + 1;
353 _xbt_dynar_expand(dynar, new_used);
355 nb_shift = old_used - idx;
358 memmove(_xbt_dynar_elm(dynar, idx + 1),
359 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
362 dynar->used = new_used;
363 res = _xbt_dynar_elm(dynar, idx);
367 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
369 * Set the Nth element of a dynar, expanding the dynar if needed, and
370 * moving the previously existing value and all subsequent ones to one
371 * position right in the dynar.
374 xbt_dynar_insert_at(xbt_dynar_t const dynar,
375 const int idx, const void *const src)
378 /* checks done in xbt_dynar_insert_at_ptr */
379 memcpy(xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
382 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
384 * Get the Nth element of a dynar, removing it from the dynar and moving
385 * all subsequent values to one position left in the dynar.
387 * If the object argument of this function is a non-null pointer, the removed
388 * element is copied to this address. If not, the element is freed using the
389 * free_f function passed at dynar creation.
392 xbt_dynar_remove_at(xbt_dynar_t const dynar,
393 const int idx, void *const object)
395 unsigned long nb_shift;
396 unsigned long offset;
398 _sanity_check_dynar(dynar);
399 _check_inbound_idx(dynar, idx);
402 _xbt_dynar_get_elm(object, dynar, idx);
403 } else if (dynar->free_f) {
404 dynar->free_f(_xbt_dynar_elm(dynar, idx));
407 nb_shift = dynar->used - 1 - idx;
410 offset = nb_shift * dynar->elmsize;
411 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
418 /** @brief Remove a slice of the dynar, sliding the rest of the values to the left
420 * This function removes an n-sized slice that starts at element idx. It is equivalent
421 * to xbt_dynar_remove_at with a NULL object argument if n equals to 1.
423 * Each of the removed elements is freed using the free_f function passed at dynar
427 xbt_dynar_remove_n_at(xbt_dynar_t const dynar,
428 const unsigned int n, const int idx)
430 unsigned long nb_shift;
431 unsigned long offset;
436 _sanity_check_dynar(dynar);
437 _check_inbound_idx(dynar, idx);
438 _check_inbound_idx(dynar, idx + n - 1);
441 for (cur = idx; cur < idx + n; cur++) {
442 dynar->free_f(_xbt_dynar_elm(dynar, cur));
446 nb_shift = dynar->used - n - idx;
449 offset = nb_shift * dynar->elmsize;
450 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + n),
457 /** @brief Returns the position of the element in the dynar
459 * Beware that if your dynar contains pointed values (such as strings) instead
460 * of scalar, this function compares the pointer value, not what's pointed. The only
461 * solution to search for a pointed value is then to write the foreach loop yourself:
463 * signed int position = -1;
464 * xbt_dynar_foreach(dynar, iter, elem) {
465 * if (!memcmp(elem, searched_element, sizeof(*elem))) {
472 * Raises not_found_error if not found. If you have less than 2 millions elements,
473 * you probably want to use #xbt_dynar_search_or_negative() instead, so that you
474 * don't have to TRY/CATCH on element not found.
476 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
480 for (it = 0; it < dynar->used; it++)
481 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
485 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
489 /** @brief Returns the position of the element in the dynar (or -1 if not found)
491 * Beware that if your dynar contains pointed values (such as
492 * strings) instead of scalar, this function is probably not what you
493 * want. Check the documentation of xbt_dynar_search() for more info.
495 * Note that usually, the dynar indices are unsigned integers. If you have more
496 * than 2 million elements in your dynar, this very function will not work (but the other will).
498 signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem)
502 for (it = 0; it < dynar->used; it++)
503 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
510 /** @brief Returns a boolean indicating whether the element is part of the dynar
512 * Beware that if your dynar contains pointed values (such as
513 * strings) instead of scalar, this function is probably not what you
514 * want. Check the documentation of xbt_dynar_search() for more info.
516 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
522 xbt_dynar_search(dynar, elem);
525 if (e.category == not_found_error) {
534 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
536 * You can then use regular affectation to set its value instead of relying
537 * on the slow memcpy. This is what xbt_dynar_push_as() does.
539 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
541 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
544 /** @brief Add an element at the end of the dynar */
545 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
546 const void *const src)
548 /* checks done in xbt_dynar_insert_at_ptr */
549 memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, dynar->elmsize);
552 /** @brief Mark the last dynar's element as unused and return a pointer to it.
554 * You can then use regular affectation to set its value instead of relying
555 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
557 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
559 _check_populated_dynar(dynar);
560 XBT_DEBUG("Pop %p", (void *) dynar);
562 return _xbt_dynar_elm(dynar, dynar->used);
565 /** @brief Get and remove the last element of the dynar */
566 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
569 /* sanity checks done by remove_at */
570 XBT_DEBUG("Pop %p", (void *) dynar);
571 xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
574 /** @brief Add an element at the begining of the dynar.
576 * This is less efficient than xbt_dynar_push()
578 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
579 const void *const src)
582 /* sanity checks done by insert_at */
583 xbt_dynar_insert_at(dynar, 0, src);
586 /** @brief Get and remove the first element of the dynar.
588 * This is less efficient than xbt_dynar_pop()
590 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
593 /* sanity checks done by remove_at */
594 xbt_dynar_remove_at(dynar, 0, dst);
597 /** @brief Apply a function to each member of a dynar
599 * The mapped function may change the value of the element itself,
600 * but should not mess with the structure of the dynar.
602 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
603 void_f_pvoid_t const op)
605 char *const data = (char *) dynar->data;
606 const unsigned long elmsize = dynar->elmsize;
607 const unsigned long used = dynar->used;
610 _sanity_check_dynar(dynar);
612 for (i = 0; i < used; i++) {
613 char* elm = (char*) data + i * elmsize;
619 /** @brief Removes and free the entry pointed by the cursor
621 * This function can be used while traversing without problem.
623 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
624 unsigned int *const cursor)
627 xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
630 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
632 * \param dynar the dynar to sort
633 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
635 * Remark: if the elements stored in the dynar are structures, the compar_fn
636 * function has to retrieve the field to sort first.
638 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
639 int_f_cpvoid_cpvoid_t compar_fn)
641 #ifdef HAVE_MERGESORT
642 mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
644 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
648 /** @brief Sorts a dynar according to their color assuming elements can have only three colors.
649 * Since there are only three colors, it is linear and much faster than a classical sort.
650 * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem
652 * \param dynar the dynar to sort
653 * \param color the color function of type (int (compar_fn*) (void*) (void*)). The return value of color is assumed to be 0, 1, or 2.
655 * At the end of the call, elements with color 0 are at the beginning of the dynar, elements with color 2 are at the end and elements with color 1 are in the middle.
657 * Remark: if the elements stored in the dynar are structures, the color
658 * function has to retrieve the field to sort first.
660 XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
664 unsigned long int p = -1;
665 unsigned long int q = dynar->used;
666 const unsigned long elmsize = dynar->elmsize;
667 void *tmp = xbt_malloc(elmsize);
670 for (i = 0; i < q;) {
671 void *elmi = _xbt_dynar_elm(dynar, i);
672 int colori = color(elmi);
678 elm = _xbt_dynar_elm(dynar, ++p);
680 } else { /* colori == 2 */
681 elm = _xbt_dynar_elm(dynar, --q);
684 memcpy(tmp, elm, elmsize);
685 memcpy(elm, elmi, elmsize);
686 memcpy(elmi, tmp, elmsize);
693 /** @brief Transform a dynar into a NULL terminated array.
694 * The dynar won't be usable afterwards.
695 * \param dynar the dynar to transform
697 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
700 xbt_dynar_shrink(dynar, 1);
701 memset(xbt_dynar_push_ptr(dynar), 0, dynar->elmsize);
708 * Return 0 if d1 and d2 are equal and 1 if not equal
710 XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
711 int(*compar)(const void *, const void *))
715 if((!d1) && (!d2)) return 0;
718 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
722 if((d1->elmsize)!=(d2->elmsize))
724 XBT_DEBUG("Size of elmsize d1=%lu d2=%lu",d1->elmsize,d2->elmsize);
728 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
730 XBT_DEBUG("Size of dynar d1=%lu d2=%lu",xbt_dynar_length(d1),xbt_dynar_length(d2));
735 size = xbt_dynar_length(d1);
738 void *data1 = xbt_dynar_get_as(d1, i, void *);
739 void *data2 = xbt_dynar_get_as(d2, i, void *);
740 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
741 if(compar(data1,data2)){
754 XBT_TEST_SUITE("dynar", "Dynar data container");
755 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dyn);
757 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
759 /* Vars_decl [doxygen cruft] */
765 xbt_test_add("==== Traverse the empty dynar");
766 d = xbt_dynar_new(sizeof(int), NULL);
767 xbt_dynar_foreach(d, cursor, i) {
768 xbt_die( "Damnit, there is something in the empty dynar");
770 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
771 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
772 /* in your code is naturally the way to go outside a regression test */
775 ("==== Push %d int, set them again 3 times, traverse them, shift them",
777 /* Populate_ints [doxygen cruft] */
778 /* 1. Populate the dynar */
779 d = xbt_dynar_new(sizeof(int), NULL);
780 for (cpt = 0; cpt < NB_ELEM; cpt++) {
781 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
782 /* xbt_dynar_push(d,&cpt); This would also work */
783 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
786 /* 2. Traverse manually the dynar */
787 for (cursor = 0; cursor < NB_ELEM; cursor++) {
788 iptr = xbt_dynar_get_ptr(d, cursor);
789 xbt_test_assert(cursor == *iptr,
790 "The retrieved value is not the same than the injected one (%u!=%d)",
794 /* 3. Traverse the dynar using the neat macro to that extend */
795 xbt_dynar_foreach(d, cursor, cpt) {
796 xbt_test_assert(cursor == cpt,
797 "The retrieved value is not the same than the injected one (%u!=%d)",
800 /* end_of_traversal */
802 for (cpt = 0; cpt < NB_ELEM; cpt++)
803 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
805 for (cpt = 0; cpt < NB_ELEM; cpt++)
806 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
807 /* xbt_dynar_set(d,cpt,&cpt); */
809 for (cpt = 0; cpt < NB_ELEM; cpt++)
810 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
813 xbt_dynar_foreach(d, cursor, i) {
814 xbt_test_assert(i == cpt,
815 "The retrieved value is not the same than the injected one (%d!=%d)",
819 xbt_test_assert(cpt == NB_ELEM,
820 "Cannot retrieve my %d values. Last got one is %d",
823 /* shifting [doxygen cruft] */
824 /* 4. Shift all the values */
825 for (cpt = 0; cpt < NB_ELEM; cpt++) {
826 xbt_dynar_shift(d, &i);
827 xbt_test_assert(i == cpt,
828 "The retrieved value is not the same than the injected one (%d!=%d)",
830 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
833 /* 5. Free the resources */
834 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
835 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
836 /* in your code is naturally the way to go outside a regression test */
838 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
839 d = xbt_dynar_new(sizeof(int), NULL);
840 for (cpt = 0; cpt < NB_ELEM; cpt++) {
841 xbt_dynar_unshift(d, &cpt);
842 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
844 for (cpt = 0; cpt < NB_ELEM; cpt++) {
845 i = xbt_dynar_pop_as(d, int);
846 xbt_test_assert(i == cpt,
847 "The retrieved value is not the same than the injected one (%d!=%d)",
849 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
851 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
852 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
853 /* in your code is naturally the way to go outside a regression test */
857 ("==== Push %d int, insert 1000 int in the middle, shift everything",
859 d = xbt_dynar_new(sizeof(int), NULL);
860 for (cpt = 0; cpt < NB_ELEM; cpt++) {
861 xbt_dynar_push_as(d, int, cpt);
862 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
864 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
865 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
866 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
869 for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
870 xbt_dynar_shift(d, &i);
871 xbt_test_assert(i == cpt,
872 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
874 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
876 for (cpt = 999; cpt >= 0; cpt--) {
877 xbt_dynar_shift(d, &i);
878 xbt_test_assert(i == cpt,
879 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
882 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
883 xbt_dynar_shift(d, &i);
884 xbt_test_assert(i == cpt,
885 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
888 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
889 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
890 /* in your code is naturally the way to go outside a regression test */
892 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
894 d = xbt_dynar_new(sizeof(int), NULL);
895 for (cpt = 0; cpt < NB_ELEM; cpt++)
896 xbt_dynar_push_as(d, int, cpt);
898 for (cpt = 2000; cpt < 4000; cpt++) {
899 xbt_dynar_remove_at(d, 2000, &i);
900 xbt_test_assert(i == cpt,
901 "Remove a bad value. Got %d, expected %d", i, cpt);
902 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
904 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
905 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
906 /* in your code is naturally the way to go outside a regression test */
909 /*******************************************************************************/
910 /*******************************************************************************/
911 /*******************************************************************************/
912 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
914 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
918 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
919 /* Populate_ints [doxygen cruft] */
920 /* 1. Populate the dynar */
921 for (cpt = 0; cpt < NB_ELEM; cpt++) {
922 xbt_dynar_insert_at(d, cpt, &cpt);
923 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
926 /* 3. Traverse the dynar */
927 xbt_dynar_foreach(d, cursor, cpt) {
928 xbt_test_assert(cursor == cpt,
929 "The retrieved value is not the same than the injected one (%u!=%d)",
932 /* end_of_traversal */
934 /* Re-fill with the same values using set_as (and re-verify) */
935 for (cpt = 0; cpt < NB_ELEM; cpt++)
936 xbt_dynar_set_as(d, cpt, int, cpt);
937 xbt_dynar_foreach(d, cursor, cpt)
938 xbt_test_assert(cursor == cpt,
939 "The retrieved value is not the same than the injected one (%u!=%d)",
942 for (cpt = 0; cpt < NB_ELEM; cpt++) {
944 xbt_dynar_remove_at(d,0,&val);
945 xbt_test_assert(cpt == val,
946 "The retrieved value is not the same than the injected one (%u!=%d)",
949 xbt_test_assert(xbt_dynar_is_empty(d),
950 "There is still %lu elements in the dynar after removing everything",
951 xbt_dynar_length(d));
954 /* ********************* */
955 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
956 d = xbt_dynar_new(sizeof(int), NULL);
957 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
958 xbt_dynar_replace(d, cpt, &cpt);
959 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
962 /* 3. Traverse the dynar */
963 xbt_dynar_foreach(d, cursor, cpt) {
964 xbt_test_assert(cursor == cpt,
965 "The retrieved value is not the same than the injected one (%u!=%d)",
968 /* end_of_traversal */
970 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
972 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
973 xbt_test_assert(cpt == val,
974 "The retrieved value is not the same than the injected one (%u!=%d)",
977 xbt_test_assert(xbt_dynar_is_empty(d),
978 "There is still %lu elements in the dynar after removing everything",
979 xbt_dynar_length(d));
983 /*******************************************************************************/
984 /*******************************************************************************/
985 /*******************************************************************************/
986 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
993 xbt_test_add("==== Traverse the empty dynar");
994 d = xbt_dynar_new(sizeof(int), NULL);
995 xbt_dynar_foreach(d, cursor, cpt) {
996 xbt_test_assert(FALSE,
997 "Damnit, there is something in the empty dynar");
999 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1000 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1001 /* in your code is naturally the way to go outside a regression test */
1003 xbt_test_add("==== Push/shift 5000 doubles");
1004 d = xbt_dynar_new(sizeof(double), NULL);
1005 for (cpt = 0; cpt < 5000; cpt++) {
1007 xbt_dynar_push(d, &d1);
1009 xbt_dynar_foreach(d, cursor, d2) {
1010 d1 = (double) cursor;
1011 xbt_test_assert(d1 == d2,
1012 "The retrieved value is not the same than the injected one (%f!=%f)",
1015 for (cpt = 0; cpt < 5000; cpt++) {
1017 xbt_dynar_shift(d, &d2);
1018 xbt_test_assert(d1 == d2,
1019 "The retrieved value is not the same than the injected one (%f!=%f)",
1022 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1023 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1024 /* in your code is naturally the way to go outside a regression test */
1026 xbt_test_add("==== Unshift/pop 5000 doubles");
1027 d = xbt_dynar_new(sizeof(double), NULL);
1028 for (cpt = 0; cpt < 5000; cpt++) {
1030 xbt_dynar_unshift(d, &d1);
1032 for (cpt = 0; cpt < 5000; cpt++) {
1034 xbt_dynar_pop(d, &d2);
1035 xbt_test_assert(d1 == d2,
1036 "The retrieved value is not the same than the injected one (%f!=%f)",
1039 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1040 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1041 /* in your code is naturally the way to go outside a regression test */
1046 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1047 d = xbt_dynar_new(sizeof(double), NULL);
1048 for (cpt = 0; cpt < 5000; cpt++) {
1050 xbt_dynar_push(d, &d1);
1052 for (cpt = 0; cpt < 1000; cpt++) {
1054 xbt_dynar_insert_at(d, 2500, &d1);
1057 for (cpt = 0; cpt < 2500; cpt++) {
1059 xbt_dynar_shift(d, &d2);
1060 xbt_test_assert(d1 == d2,
1061 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1063 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1065 for (cpt = 999; cpt >= 0; cpt--) {
1067 xbt_dynar_shift(d, &d2);
1068 xbt_test_assert(d1 == d2,
1069 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1072 for (cpt = 2500; cpt < 5000; cpt++) {
1074 xbt_dynar_shift(d, &d2);
1075 xbt_test_assert(d1 == d2,
1076 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1079 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1080 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1081 /* in your code is naturally the way to go outside a regression test */
1084 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1085 d = xbt_dynar_new(sizeof(double), NULL);
1086 for (cpt = 0; cpt < 5000; cpt++) {
1088 xbt_dynar_push(d, &d1);
1090 for (cpt = 2000; cpt < 4000; cpt++) {
1092 xbt_dynar_remove_at(d, 2000, &d2);
1093 xbt_test_assert(d1 == d2,
1094 "Remove a bad value. Got %f, expected %f", d2, d1);
1096 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1097 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1098 /* in your code is naturally the way to go outside a regression test */
1102 /* doxygen_string_cruft */
1104 /*******************************************************************************/
1105 /*******************************************************************************/
1106 /*******************************************************************************/
1107 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1115 xbt_test_add("==== Traverse the empty dynar");
1116 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1117 xbt_dynar_foreach(d, iter, s1) {
1118 xbt_test_assert(FALSE,
1119 "Damnit, there is something in the empty dynar");
1121 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1122 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1123 /* in your code is naturally the way to go outside a regression test */
1125 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1127 /* Populate_str [doxygen cruft] */
1128 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1129 /* 1. Populate the dynar */
1130 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1131 sprintf(buf, "%d", cpt);
1133 xbt_dynar_push(d, &s1);
1135 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1136 sprintf(buf, "%d", cpt);
1138 xbt_dynar_replace(d, cpt, &s1);
1140 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1141 sprintf(buf, "%d", cpt);
1143 xbt_dynar_replace(d, cpt, &s1);
1145 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1146 sprintf(buf, "%d", cpt);
1148 xbt_dynar_replace(d, cpt, &s1);
1150 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1151 sprintf(buf, "%d", cpt);
1152 xbt_dynar_shift(d, &s2);
1153 xbt_test_assert(!strcmp(buf, s2),
1154 "The retrieved value is not the same than the injected one (%s!=%s)",
1158 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1159 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1160 /* in your code is naturally the way to go outside a regression test */
1162 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1163 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1164 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1165 sprintf(buf, "%d", cpt);
1167 xbt_dynar_unshift(d, &s1);
1169 /* 2. Traverse the dynar with the macro */
1170 xbt_dynar_foreach(d, iter, s1) {
1171 sprintf(buf, "%u", NB_ELEM - iter - 1);
1172 xbt_test_assert(!strcmp(buf, s1),
1173 "The retrieved value is not the same than the injected one (%s!=%s)",
1176 /* 3. Traverse the dynar with the macro */
1177 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1178 sprintf(buf, "%d", cpt);
1179 xbt_dynar_pop(d, &s2);
1180 xbt_test_assert(!strcmp(buf, s2),
1181 "The retrieved value is not the same than the injected one (%s!=%s)",
1185 /* 4. Free the resources */
1186 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1187 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1188 /* in your code is naturally the way to go outside a regression test */
1192 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1193 NB_ELEM, NB_ELEM / 5);
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_push(d, &s1);
1200 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1201 sprintf(buf, "%d", cpt);
1203 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1206 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1207 sprintf(buf, "%d", cpt);
1208 xbt_dynar_shift(d, &s2);
1209 xbt_test_assert(!strcmp(buf, s2),
1210 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1214 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1215 sprintf(buf, "%d", cpt);
1216 xbt_dynar_shift(d, &s2);
1217 xbt_test_assert(!strcmp(buf, s2),
1218 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1222 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1223 sprintf(buf, "%d", cpt);
1224 xbt_dynar_shift(d, &s2);
1225 xbt_test_assert(!strcmp(buf, s2),
1226 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1230 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1231 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1232 /* in your code is naturally the way to go outside a regression test */
1235 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1236 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1237 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1238 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1239 sprintf(buf, "%d", cpt);
1241 xbt_dynar_push(d, &s1);
1243 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1244 sprintf(buf, "%d", cpt);
1245 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1246 xbt_test_assert(!strcmp(buf, s2),
1247 "Remove a bad value. Got %s, expected %s", s2, buf);
1250 xbt_dynar_free(&d); /* end_of_doxygen */
1252 #endif /* SIMGRID_TEST */