Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix doxygen/javadoc warnings.
[simgrid.git] / src / xbt / dynar.c
1 /* a generic DYNamic ARray implementation.                                  */
2
3 /* Copyright (c) 2004-2013. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "xbt/misc.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/ex.h"
13 #include "xbt/dynar.h"
14 #include <sys/types.h>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
17
18 static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
19 {
20   xbt_assert(dynar, "dynar is NULL");
21 }
22
23 static XBT_INLINE void _sanity_check_idx(int idx)
24 {
25   xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
26 }
27
28 static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
29 {
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);
34   }
35 }
36
37 static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
38 {
39   if (dynar->used == 0) {
40     THROWF(bound_error, 0, "dynar %p is empty", dynar);
41   }
42 }
43
44 static XBT_INLINE
45 void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size)
46 {
47   if (new_size != dynar->size) {
48     dynar->size = new_size;
49     dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize);
50   }
51 }
52
53 static XBT_INLINE
54     void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
55 {
56   const unsigned long old_size = dynar->size;
57
58   if (nb > old_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);
63   }
64 }
65
66 static XBT_INLINE
67     void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
68 {
69   char *const data = (char *) dynar->data;
70   const unsigned long elmsize = dynar->elmsize;
71
72   return data + idx * elmsize;
73 }
74
75 static XBT_INLINE
76     void
77 _xbt_dynar_get_elm(void *const dst,
78                    const xbt_dynar_t dynar, const unsigned long idx)
79 {
80   void *const elm = _xbt_dynar_elm(dynar, idx);
81
82   memcpy(dst, elm, dynar->elmsize);
83 }
84
85 static XBT_INLINE
86     void
87 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
88                    const unsigned long idx, const void *const src)
89 {
90   void *const elm = _xbt_dynar_elm(dynar, idx);
91   const unsigned long elmsize = dynar->elmsize;
92
93   memcpy(elm, src, elmsize);
94 }
95
96 void xbt_dynar_dump(xbt_dynar_t dynar)
97 {
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,
100         dynar->free_f);
101 }
102
103 /** @brief Constructor
104  *
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).
107  *
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 **).
111  */
112 xbt_dynar_t
113 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
114 {
115
116   xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
117
118   dynar->size = 0;
119   dynar->used = 0;
120   dynar->elmsize = elmsize;
121   dynar->data = NULL;
122   dynar->free_f = free_f;
123
124   return dynar;
125 }
126
127 /** @brief Destructor of the structure not touching to the content
128  *
129  * \param dynar poor victim
130  *
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)
133  */
134 void xbt_dynar_free_container(xbt_dynar_t * dynar)
135 {
136   if (dynar && *dynar) {
137     xbt_dynar_t d = *dynar;
138     free(d->data);
139     free(d);
140     *dynar = NULL;
141   }
142 }
143
144 /** @brief Frees the content and set the size to 0
145  *
146  * \param dynar who to squeeze
147  */
148 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
149 {
150   _sanity_check_dynar(dynar);
151
152   XBT_CDEBUG(xbt_dyn, "Reset the dynar %p", (void *) dynar);
153   if (dynar->free_f) {
154     xbt_dynar_map(dynar, dynar->free_f);
155   }
156   dynar->used = 0;
157 }
158
159 /** @brief Merge dynar d2 into d1
160  *
161  * \param d1 dynar to keep
162  * \param d2 dynar to merge into d1. This dynar is free at end.
163  */
164 void xbt_dynar_merge(xbt_dynar_t *d1, xbt_dynar_t *d2)
165 {
166   if((*d1)->elmsize != (*d2)->elmsize)
167     xbt_die("Element size must are not equal");
168
169   const unsigned long elmsize = (*d1)->elmsize;
170
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);
174
175   memcpy(elm, ptr, ((*d2)->size)*elmsize);
176   (*d1)->used += (*d2)->used;
177   (*d2)->used = 0;
178   xbt_dynar_free(d2);
179 }
180
181 /**
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
186  *
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
192  * as possible.
193  * Note that if \a empty_slots_wanted is greater than the array size, the internal
194  * array is expanded instead of shriked.
195  */
196 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
197 {
198   _xbt_dynar_resize(dynar, dynar->used + empty_slots_wanted);
199 }
200
201 /** @brief Destructor
202  *
203  * \param dynar poor victim
204  *
205  * kilkil a dynar and its content
206  */
207
208 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
209 {
210   if (dynar && *dynar) {
211     xbt_dynar_reset(*dynar);
212     xbt_dynar_free_container(dynar);
213   }
214 }
215
216 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
217 void xbt_dynar_free_voidp(void *d)
218 {
219   xbt_dynar_t dynar = (xbt_dynar_t)d;
220   xbt_dynar_free(&dynar);
221 }
222
223 /** @brief Count of dynar's elements
224  *
225  * \param dynar the dynar we want to mesure
226  */
227 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
228 {
229   return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
230 }
231
232  /**@brief check if a dynar is empty
233  *
234  *\param dynar the dynat we want to check
235  */
236
237 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
238 {
239   return (xbt_dynar_length(dynar) == 0);
240 }
241
242 /** @brief Retrieve a copy of the Nth element of a dynar.
243  *
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.
247  */
248 XBT_INLINE void
249 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
250                   const unsigned long idx, void *const dst)
251 {
252   _sanity_check_dynar(dynar);
253   _check_inbound_idx(dynar, idx);
254
255   _xbt_dynar_get_elm(dst, dynar, idx);
256 }
257
258 /** @brief Retrieve a pointer to the Nth element of a dynar.
259  *
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.
263  *
264  * \warning The returned value is the actual content of the dynar.
265  * Make a copy before fooling with it.
266  */
267 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
268                                    const unsigned long idx)
269 {
270
271   void *res;
272   _sanity_check_dynar(dynar);
273   _check_inbound_idx(dynar, idx);
274
275   res = _xbt_dynar_elm(dynar, idx);
276   return res;
277 }
278
279 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
280                                       const unsigned long idx)
281 {
282   _sanity_check_dynar(dynar);
283
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);
289     }
290     dynar->used = idx + 1;
291   }
292   return _xbt_dynar_elm(dynar, idx);
293 }
294
295 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
296  *
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
300  *
301  * If you want to free the previous content, use xbt_dynar_replace().
302  */
303 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
304                               const void *const src)
305 {
306
307   memcpy(xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
308 }
309
310 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
311  *
312  * \param dynar
313  * \param idx
314  * \param object
315  *
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().
319  */
320 void
321 xbt_dynar_replace(xbt_dynar_t dynar,
322                   const unsigned long idx, const void *const object)
323 {
324   _sanity_check_dynar(dynar);
325
326   if (idx < dynar->used && dynar->free_f) {
327     void *const old_object = _xbt_dynar_elm(dynar, idx);
328
329     dynar->free_f(old_object);
330   }
331
332   xbt_dynar_set(dynar, idx, object);
333 }
334
335 /** @brief Make room for a new element, and return a pointer to it
336  *
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.
339  */
340 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
341 {
342   void *res;
343   unsigned long old_used;
344   unsigned long new_used;
345   long nb_shift;
346
347   _sanity_check_dynar(dynar);
348   _sanity_check_idx(idx);
349
350   old_used = dynar->used;
351   new_used = old_used + 1;
352
353   _xbt_dynar_expand(dynar, new_used);
354
355   nb_shift = old_used - idx;
356
357   if (nb_shift>0) {
358     memmove(_xbt_dynar_elm(dynar, idx + 1),
359             _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
360   }
361
362   dynar->used = new_used;
363   res = _xbt_dynar_elm(dynar, idx);
364   return res;
365 }
366
367 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
368  *
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.
372  */
373 XBT_INLINE void
374 xbt_dynar_insert_at(xbt_dynar_t const dynar,
375                     const int idx, const void *const src)
376 {
377
378   /* checks done in xbt_dynar_insert_at_ptr */
379   memcpy(xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
380 }
381
382 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
383  *
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.
386  *
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.
390  */
391 void
392 xbt_dynar_remove_at(xbt_dynar_t const dynar,
393                     const int idx, void *const object)
394 {
395   unsigned long nb_shift;
396   unsigned long offset;
397
398   _sanity_check_dynar(dynar);
399   _check_inbound_idx(dynar, idx);
400
401   if (object) {
402     _xbt_dynar_get_elm(object, dynar, idx);
403   } else if (dynar->free_f) {
404     dynar->free_f(_xbt_dynar_elm(dynar, idx));
405   }
406
407   nb_shift = dynar->used - 1 - idx;
408
409   if (nb_shift) {
410     offset = nb_shift * dynar->elmsize;
411     memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
412             offset);
413   }
414
415   dynar->used--;
416 }
417
418 /** @brief Remove a slice of the dynar, sliding the rest of the values to the left
419  *
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.
422  *
423  * Each of the removed elements is freed using the free_f function passed at dynar
424  * creation.
425  */
426 void
427 xbt_dynar_remove_n_at(xbt_dynar_t const dynar,
428                     const unsigned int n, const int idx)
429 {
430   unsigned long nb_shift;
431   unsigned long offset;
432   unsigned long cur;
433
434   if (!n) return;
435
436   _sanity_check_dynar(dynar);
437   _check_inbound_idx(dynar, idx);
438   _check_inbound_idx(dynar, idx + n - 1);
439
440   if (dynar->free_f) {
441     for (cur = idx; cur < idx + n; cur++) {
442       dynar->free_f(_xbt_dynar_elm(dynar, cur));
443     }
444   }
445
446   nb_shift = dynar->used - n - idx;
447
448   if (nb_shift) {
449     offset = nb_shift * dynar->elmsize;
450     memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + n),
451             offset);
452   }
453
454   dynar->used -= n;
455 }
456
457 /** @brief Returns the position of the element in the dynar
458  *
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:
462  * \code
463  * signed int position = -1;
464  * xbt_dynar_foreach(dynar, iter, elem) {
465  *    if (!memcmp(elem, searched_element, sizeof(*elem))) {
466  *        position = iter;
467  *        break;
468  *    }
469  * }
470  * \endcode
471  * 
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.
475  */
476 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
477 {
478   unsigned long it;
479
480   for (it = 0; it < dynar->used; it++)
481     if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
482       return it;
483     }
484
485   THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
486          dynar);
487 }
488
489 /** @brief Returns the position of the element in the dynar (or -1 if not found)
490  *
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.
494  * 
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).
497  */
498 signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem)
499 {
500   unsigned long it;
501
502   for (it = 0; it < dynar->used; it++)
503     if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
504       return it;
505     }
506
507   return -1;
508 }
509
510 /** @brief Returns a boolean indicating whether the element is part of the dynar 
511  *
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.
515  */
516 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
517 {
518
519   xbt_ex_t e;
520
521   TRY {
522     xbt_dynar_search(dynar, elem);
523   }
524   CATCH(e) {
525     if (e.category == not_found_error) {
526       xbt_ex_free(e);
527       return 0;
528     }
529     RETHROW;
530   }
531   return 1;
532 }
533
534 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
535  *
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.
538  */
539 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
540 {
541   return xbt_dynar_insert_at_ptr(dynar, dynar->used);
542 }
543
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)
547 {
548   /* checks done in xbt_dynar_insert_at_ptr */
549   memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, dynar->elmsize);
550 }
551
552 /** @brief Mark the last dynar's element as unused and return a pointer to it.
553  *
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.
556  */
557 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
558 {
559   _check_populated_dynar(dynar);
560   XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
561   dynar->used--;
562   return _xbt_dynar_elm(dynar, dynar->used);
563 }
564
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)
567 {
568
569   /* sanity checks done by remove_at */
570   XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
571   xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
572 }
573
574 /** @brief Add an element at the begining of the dynar.
575  *
576  * This is less efficient than xbt_dynar_push()
577  */
578 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
579                                   const void *const src)
580 {
581
582   /* sanity checks done by insert_at */
583   xbt_dynar_insert_at(dynar, 0, src);
584 }
585
586 /** @brief Get and remove the first element of the dynar.
587  *
588  * This is less efficient than xbt_dynar_pop()
589  */
590 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
591 {
592
593   /* sanity checks done by remove_at */
594   xbt_dynar_remove_at(dynar, 0, dst);
595 }
596
597 /** @brief Apply a function to each member of a dynar
598  *
599  * The mapped function may change the value of the element itself,
600  * but should not mess with the structure of the dynar.
601  */
602 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
603                               void_f_pvoid_t const op)
604 {
605   char *const data = (char *) dynar->data;
606   const unsigned long elmsize = dynar->elmsize;
607   const unsigned long used = dynar->used;
608   unsigned long i;
609
610   _sanity_check_dynar(dynar);
611
612   for (i = 0; i < used; i++) {
613     char* elm = (char*) data + i * elmsize;
614     op(elm);
615   }
616 }
617
618
619 /** @brief Removes and free the entry pointed by the cursor
620  *
621  * This function can be used while traversing without problem.
622  */
623 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
624                                     unsigned int *const cursor)
625 {
626
627   xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
628 }
629
630 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
631  *
632  * \param dynar the dynar to sort
633  * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
634  *
635  * Remark: if the elements stored in the dynar are structures, the compar_fn
636  * function has to retrieve the field to sort first.
637  */
638 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
639                                int_f_cpvoid_cpvoid_t compar_fn)
640 {
641   qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
642 }
643
644 /** @brief Sorts a dynar according to their color assuming elements can have only three colors.
645  * Since there are only three colors, it is linear and much faster than a classical sort.
646  * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem
647  *
648  * \param dynar the dynar to sort
649  * \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.
650  *
651  * 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.
652  *
653  * Remark: if the elements stored in the dynar are structures, the color
654  * function has to retrieve the field to sort first.
655  */
656 XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
657                                                int_f_pvoid_t color)
658 {
659   unsigned long int i;
660   unsigned long int p = -1;
661   unsigned long int q = dynar->used;
662   const unsigned long elmsize = dynar->elmsize;
663   void *tmp = xbt_malloc(elmsize);
664   void *elm;
665
666   for (i = 0; i < q;) {
667     void *elmi = _xbt_dynar_elm(dynar, i);
668     int colori = color(elmi);
669
670     if (colori == 1) {
671       ++i;
672     } else {
673       if (colori == 0) {
674         elm = _xbt_dynar_elm(dynar, ++p);
675         ++i;
676       } else {                  /* colori == 2 */
677         elm = _xbt_dynar_elm(dynar, --q);
678       }
679       if (elm != elmi) {
680         memcpy(tmp,  elm,  elmsize);
681         memcpy(elm,  elmi, elmsize);
682         memcpy(elmi, tmp,  elmsize);
683       }
684     }
685   }
686   xbt_free(tmp);
687 }
688
689 /** @brief Transform a dynar into a NULL terminated array. 
690  *  The dynar won't be usable afterwards.
691  * \param dynar the dynar to transform
692  */
693 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
694 {
695   void *res;
696   xbt_dynar_shrink(dynar, 1);
697   memset(xbt_dynar_push_ptr(dynar), 0, dynar->elmsize);
698   res = dynar->data;
699   free(dynar);
700   return res;
701 }
702
703 /*
704  * Return 0 if d1 and d2 are equal and 1 if not equal
705  */
706 int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
707           int(*compar)(const void *, const void *))
708 {
709   int i ;
710   int size;
711   if((!d1) && (!d2)) return 0;
712   if((!d1) || (!d2))
713   {
714     XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
715     xbt_dynar_free(&d2);
716     return 1;
717   }
718   if((d1->elmsize)!=(d2->elmsize))
719   {
720     XBT_DEBUG("Size of elmsize d1=%lu d2=%lu",d1->elmsize,d2->elmsize);
721     xbt_dynar_free(&d2);
722     return 1; // xbt_die
723   }
724   if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
725   {
726     XBT_DEBUG("Size of dynar d1=%lu d2=%lu",xbt_dynar_length(d1),xbt_dynar_length(d2));
727     xbt_dynar_free(&d2);
728     return 1;
729   }
730
731   size = xbt_dynar_length(d1);
732   for(i=0;i<size;i++)
733   {
734     void *data1 = xbt_dynar_get_as(d1, i, void *);
735     void *data2 = xbt_dynar_get_as(d2, i, void *);
736     XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
737     if(compar(data1,data2)){
738       xbt_dynar_free(&d2);
739       return 1;
740     }
741   }
742   xbt_dynar_free(&d2);
743   return 0;
744 }
745
746 #ifdef SIMGRID_TEST
747
748 #define NB_ELEM 5000
749
750 XBT_TEST_SUITE("dynar", "Dynar data container");
751 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dyn);
752
753 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
754 {
755   /* Vars_decl [doxygen cruft] */
756   xbt_dynar_t d;
757   int i, cpt;
758   unsigned int cursor;
759   int *iptr;
760
761   xbt_test_add("==== Traverse the empty dynar");
762   d = xbt_dynar_new(sizeof(int), NULL);
763   xbt_dynar_foreach(d, cursor, i) {
764     xbt_die( "Damnit, there is something in the empty dynar");
765   }
766   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
767   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
768   /* in your code is naturally the way to go outside a regression test */
769
770   xbt_test_add
771       ("==== Push %d int, set them again 3 times, traverse them, shift them",
772        NB_ELEM);
773   /* Populate_ints [doxygen cruft] */
774   /* 1. Populate the dynar */
775   d = xbt_dynar_new(sizeof(int), NULL);
776   for (cpt = 0; cpt < NB_ELEM; cpt++) {
777     xbt_dynar_push_as(d, int, cpt);     /* This is faster (and possible only with scalars) */
778     /* xbt_dynar_push(d,&cpt);       This would also work */
779     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
780   }
781
782   /* 2. Traverse manually the dynar */
783   for (cursor = 0; cursor < NB_ELEM; cursor++) {
784     iptr = xbt_dynar_get_ptr(d, cursor);
785     xbt_test_assert(cursor == *iptr,
786                      "The retrieved value is not the same than the injected one (%u!=%d)",
787                      cursor, cpt);
788   }
789
790   /* 3. Traverse the dynar using the neat macro to that extend */
791   xbt_dynar_foreach(d, cursor, cpt) {
792     xbt_test_assert(cursor == cpt,
793                      "The retrieved value is not the same than the injected one (%u!=%d)",
794                      cursor, cpt);
795   }
796   /* end_of_traversal */
797
798   for (cpt = 0; cpt < NB_ELEM; cpt++)
799     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
800
801   for (cpt = 0; cpt < NB_ELEM; cpt++)
802     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
803   /*     xbt_dynar_set(d,cpt,&cpt); */
804
805   for (cpt = 0; cpt < NB_ELEM; cpt++)
806     *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
807
808   cpt = 0;
809   xbt_dynar_foreach(d, cursor, i) {
810     xbt_test_assert(i == cpt,
811                      "The retrieved value is not the same than the injected one (%d!=%d)",
812                      i, cpt);
813     cpt++;
814   }
815   xbt_test_assert(cpt == NB_ELEM,
816                    "Cannot retrieve my %d values. Last got one is %d",
817                    NB_ELEM, cpt);
818
819   /* shifting [doxygen cruft] */
820   /* 4. Shift all the values */
821   for (cpt = 0; cpt < NB_ELEM; cpt++) {
822     xbt_dynar_shift(d, &i);
823     xbt_test_assert(i == cpt,
824                      "The retrieved value is not the same than the injected one (%d!=%d)",
825                      i, cpt);
826     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
827   }
828
829   /* 5. Free the resources */
830   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
831   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
832   /* in your code is naturally the way to go outside a regression test */
833
834   xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
835   d = xbt_dynar_new(sizeof(int), NULL);
836   for (cpt = 0; cpt < NB_ELEM; cpt++) {
837     xbt_dynar_unshift(d, &cpt);
838     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
839   }
840   for (cpt = 0; cpt < NB_ELEM; cpt++) {
841     i = xbt_dynar_pop_as(d, int);
842     xbt_test_assert(i == cpt,
843                      "The retrieved value is not the same than the injected one (%d!=%d)",
844                      i, cpt);
845     xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
846   }
847   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
848   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
849   /* in your code is naturally the way to go outside a regression test */
850
851
852   xbt_test_add
853       ("==== Push %d int, insert 1000 int in the middle, shift everything",
854        NB_ELEM);
855   d = xbt_dynar_new(sizeof(int), NULL);
856   for (cpt = 0; cpt < NB_ELEM; cpt++) {
857     xbt_dynar_push_as(d, int, cpt);
858     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
859   }
860   for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
861     xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
862     XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
863   }
864
865   for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
866     xbt_dynar_shift(d, &i);
867     xbt_test_assert(i == cpt,
868                      "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
869                      i, cpt);
870     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
871   }
872   for (cpt = 999; cpt >= 0; cpt--) {
873     xbt_dynar_shift(d, &i);
874     xbt_test_assert(i == cpt,
875                      "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
876                      i, cpt);
877   }
878   for (cpt = 2500; cpt < NB_ELEM; cpt++) {
879     xbt_dynar_shift(d, &i);
880     xbt_test_assert(i == cpt,
881                      "The retrieved value is not the same than the injected one at the end (%d!=%d)",
882                      i, cpt);
883   }
884   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
885   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
886   /* in your code is naturally the way to go outside a regression test */
887
888   xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
889                 NB_ELEM);
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
894   for (cpt = 2000; cpt < 4000; cpt++) {
895     xbt_dynar_remove_at(d, 2000, &i);
896     xbt_test_assert(i == cpt,
897                      "Remove a bad value. Got %d, expected %d", i, cpt);
898     XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
899   }
900   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
901   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
902   /* in your code is naturally the way to go outside a regression test */
903 }
904
905 /*******************************************************************************/
906 /*******************************************************************************/
907 /*******************************************************************************/
908 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
909 {
910   xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
911   unsigned int cursor;
912   int cpt;
913
914   xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
915   /* Populate_ints [doxygen cruft] */
916   /* 1. Populate the dynar */
917   for (cpt = 0; cpt < NB_ELEM; cpt++) {
918     xbt_dynar_insert_at(d, cpt, &cpt);
919     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
920   }
921
922   /* 3. Traverse the dynar */
923   xbt_dynar_foreach(d, cursor, cpt) {
924     xbt_test_assert(cursor == cpt,
925                      "The retrieved value is not the same than the injected one (%u!=%d)",
926                      cursor, cpt);
927   }
928   /* end_of_traversal */
929
930   /* Re-fill with the same values using set_as (and re-verify) */
931   for (cpt = 0; cpt < NB_ELEM; cpt++)
932     xbt_dynar_set_as(d, cpt, int, cpt);
933   xbt_dynar_foreach(d, cursor, cpt)
934     xbt_test_assert(cursor == cpt,
935                      "The retrieved value is not the same than the injected one (%u!=%d)",
936                      cursor, cpt);
937
938   for (cpt = 0; cpt < NB_ELEM; cpt++) {
939     int val;
940     xbt_dynar_remove_at(d,0,&val);
941     xbt_test_assert(cpt == val,
942                      "The retrieved value is not the same than the injected one (%u!=%d)",
943                      cursor, cpt);
944   }
945   xbt_test_assert(xbt_dynar_is_empty(d),
946                    "There is still %lu elements in the dynar after removing everything",
947                    xbt_dynar_length(d));
948   xbt_dynar_free(&d);
949
950   /* ********************* */
951   xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
952   d = xbt_dynar_new(sizeof(int), NULL);
953   for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
954     xbt_dynar_replace(d, cpt, &cpt);
955     xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
956   }
957
958   /* 3. Traverse the dynar */
959   xbt_dynar_foreach(d, cursor, cpt) {
960     xbt_test_assert(cursor == cpt,
961                      "The retrieved value is not the same than the injected one (%u!=%d)",
962                      cursor, cpt);
963   }
964   /* end_of_traversal */
965
966   for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
967     int val;
968     xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
969     xbt_test_assert(cpt == val,
970                      "The retrieved value is not the same than the injected one (%u!=%d)",
971                      cursor, cpt);
972   }
973   xbt_test_assert(xbt_dynar_is_empty(d),
974                    "There is still %lu elements in the dynar after removing everything",
975                    xbt_dynar_length(d));
976   xbt_dynar_free(&d);
977 }
978
979 /*******************************************************************************/
980 /*******************************************************************************/
981 /*******************************************************************************/
982 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
983 {
984   xbt_dynar_t d;
985   int cpt;
986   unsigned int cursor;
987   double d1, d2;
988
989   xbt_test_add("==== Traverse the empty dynar");
990   d = xbt_dynar_new(sizeof(int), NULL);
991   xbt_dynar_foreach(d, cursor, cpt) {
992     xbt_test_assert(FALSE,
993                      "Damnit, there is something in the empty dynar");
994   }
995   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
996   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
997   /* in your code is naturally the way to go outside a regression test */
998
999   xbt_test_add("==== Push/shift 5000 doubles");
1000   d = xbt_dynar_new(sizeof(double), NULL);
1001   for (cpt = 0; cpt < 5000; cpt++) {
1002     d1 = (double) cpt;
1003     xbt_dynar_push(d, &d1);
1004   }
1005   xbt_dynar_foreach(d, cursor, d2) {
1006     d1 = (double) cursor;
1007     xbt_test_assert(d1 == d2,
1008                      "The retrieved value is not the same than the injected one (%f!=%f)",
1009                      d1, d2);
1010   }
1011   for (cpt = 0; cpt < 5000; cpt++) {
1012     d1 = (double) cpt;
1013     xbt_dynar_shift(d, &d2);
1014     xbt_test_assert(d1 == d2,
1015                      "The retrieved value is not the same than the injected one (%f!=%f)",
1016                      d1, d2);
1017   }
1018   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1019   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1020   /* in your code is naturally the way to go outside a regression test */
1021
1022   xbt_test_add("==== Unshift/pop 5000 doubles");
1023   d = xbt_dynar_new(sizeof(double), NULL);
1024   for (cpt = 0; cpt < 5000; cpt++) {
1025     d1 = (double) cpt;
1026     xbt_dynar_unshift(d, &d1);
1027   }
1028   for (cpt = 0; cpt < 5000; cpt++) {
1029     d1 = (double) cpt;
1030     xbt_dynar_pop(d, &d2);
1031     xbt_test_assert(d1 == d2,
1032                      "The retrieved value is not the same than the injected one (%f!=%f)",
1033                      d1, d2);
1034   }
1035   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1036   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1037   /* in your code is naturally the way to go outside a regression test */
1038
1039
1040
1041   xbt_test_add
1042       ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1043   d = xbt_dynar_new(sizeof(double), NULL);
1044   for (cpt = 0; cpt < 5000; cpt++) {
1045     d1 = (double) cpt;
1046     xbt_dynar_push(d, &d1);
1047   }
1048   for (cpt = 0; cpt < 1000; cpt++) {
1049     d1 = (double) cpt;
1050     xbt_dynar_insert_at(d, 2500, &d1);
1051   }
1052
1053   for (cpt = 0; cpt < 2500; cpt++) {
1054     d1 = (double) cpt;
1055     xbt_dynar_shift(d, &d2);
1056     xbt_test_assert(d1 == d2,
1057                      "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1058                      d1, d2);
1059     XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1060   }
1061   for (cpt = 999; cpt >= 0; cpt--) {
1062     d1 = (double) cpt;
1063     xbt_dynar_shift(d, &d2);
1064     xbt_test_assert(d1 == d2,
1065                      "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1066                      d1, d2);
1067   }
1068   for (cpt = 2500; cpt < 5000; cpt++) {
1069     d1 = (double) cpt;
1070     xbt_dynar_shift(d, &d2);
1071     xbt_test_assert(d1 == d2,
1072                      "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1073                      d1, d2);
1074   }
1075   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1076   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1077   /* in your code is naturally the way to go outside a regression test */
1078
1079
1080   xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1081   d = xbt_dynar_new(sizeof(double), NULL);
1082   for (cpt = 0; cpt < 5000; cpt++) {
1083     d1 = (double) cpt;
1084     xbt_dynar_push(d, &d1);
1085   }
1086   for (cpt = 2000; cpt < 4000; cpt++) {
1087     d1 = (double) cpt;
1088     xbt_dynar_remove_at(d, 2000, &d2);
1089     xbt_test_assert(d1 == d2,
1090                      "Remove a bad value. Got %f, expected %f", d2, d1);
1091   }
1092   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1093   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1094   /* in your code is naturally the way to go outside a regression test */
1095 }
1096
1097
1098 /* doxygen_string_cruft */
1099
1100 /*******************************************************************************/
1101 /*******************************************************************************/
1102 /*******************************************************************************/
1103 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1104 {
1105   xbt_dynar_t d;
1106   int cpt;
1107   unsigned int iter;
1108   char buf[1024];
1109   char *s1, *s2;
1110
1111   xbt_test_add("==== Traverse the empty dynar");
1112   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1113   xbt_dynar_foreach(d, iter, s1) {
1114     xbt_test_assert(FALSE,
1115                      "Damnit, there is something in the empty dynar");
1116   }
1117   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1118   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1119   /* in your code is naturally the way to go outside a regression test */
1120
1121   xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1122                 NB_ELEM);
1123   /* Populate_str [doxygen cruft] */
1124   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1125   /* 1. Populate the dynar */
1126   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1127     sprintf(buf, "%d", cpt);
1128     s1 = strdup(buf);
1129     xbt_dynar_push(d, &s1);
1130   }
1131   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1132     sprintf(buf, "%d", cpt);
1133     s1 = strdup(buf);
1134     xbt_dynar_replace(d, cpt, &s1);
1135   }
1136   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1137     sprintf(buf, "%d", cpt);
1138     s1 = strdup(buf);
1139     xbt_dynar_replace(d, cpt, &s1);
1140   }
1141   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1142     sprintf(buf, "%d", cpt);
1143     s1 = strdup(buf);
1144     xbt_dynar_replace(d, cpt, &s1);
1145   }
1146   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1147     sprintf(buf, "%d", cpt);
1148     xbt_dynar_shift(d, &s2);
1149     xbt_test_assert(!strcmp(buf, s2),
1150                      "The retrieved value is not the same than the injected one (%s!=%s)",
1151                      buf, s2);
1152     free(s2);
1153   }
1154   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1155   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1156   /* in your code is naturally the way to go outside a regression test */
1157
1158   xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1159   d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1160   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1161     sprintf(buf, "%d", cpt);
1162     s1 = strdup(buf);
1163     xbt_dynar_unshift(d, &s1);
1164   }
1165   /* 2. Traverse the dynar with the macro */
1166   xbt_dynar_foreach(d, iter, s1) {
1167     sprintf(buf, "%u", NB_ELEM - iter - 1);
1168     xbt_test_assert(!strcmp(buf, s1),
1169                      "The retrieved value is not the same than the injected one (%s!=%s)",
1170                      buf, s1);
1171   }
1172   /* 3. Traverse the dynar with the macro */
1173   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1174     sprintf(buf, "%d", cpt);
1175     xbt_dynar_pop(d, &s2);
1176     xbt_test_assert(!strcmp(buf, s2),
1177                      "The retrieved value is not the same than the injected one (%s!=%s)",
1178                      buf, s2);
1179     free(s2);
1180   }
1181   /* 4. Free the resources */
1182   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1183   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1184   /* in your code is naturally the way to go outside a regression test */
1185
1186
1187   xbt_test_add
1188       ("==== Push %d strings, insert %d strings in the middle, shift everything",
1189        NB_ELEM, NB_ELEM / 5);
1190   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1191   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1192     sprintf(buf, "%d", cpt);
1193     s1 = strdup(buf);
1194     xbt_dynar_push(d, &s1);
1195   }
1196   for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1197     sprintf(buf, "%d", cpt);
1198     s1 = strdup(buf);
1199     xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1200   }
1201
1202   for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1203     sprintf(buf, "%d", cpt);
1204     xbt_dynar_shift(d, &s2);
1205     xbt_test_assert(!strcmp(buf, s2),
1206                      "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1207                      buf, s2);
1208     free(s2);
1209   }
1210   for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1211     sprintf(buf, "%d", cpt);
1212     xbt_dynar_shift(d, &s2);
1213     xbt_test_assert(!strcmp(buf, s2),
1214                      "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1215                      buf, s2);
1216     free(s2);
1217   }
1218   for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1219     sprintf(buf, "%d", cpt);
1220     xbt_dynar_shift(d, &s2);
1221     xbt_test_assert(!strcmp(buf, s2),
1222                      "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1223                      buf, s2);
1224     free(s2);
1225   }
1226   xbt_dynar_free(&d);           /* This code is used both as example and as regression test, so we try to */
1227   xbt_dynar_free(&d);           /* free the struct twice here to check that it's ok, but freeing  it only once */
1228   /* in your code is naturally the way to go outside a regression test */
1229
1230
1231   xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1232                 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1233   d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1234   for (cpt = 0; cpt < NB_ELEM; cpt++) {
1235     sprintf(buf, "%d", cpt);
1236     s1 = strdup(buf);
1237     xbt_dynar_push(d, &s1);
1238   }
1239   for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1240     sprintf(buf, "%d", cpt);
1241     xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1242     xbt_test_assert(!strcmp(buf, s2),
1243                      "Remove a bad value. Got %s, expected %s", s2, buf);
1244     free(s2);
1245   }
1246   xbt_dynar_free(&d);           /* end_of_doxygen */
1247 }
1248 #endif                          /* SIMGRID_TEST */