Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2c1e7dc89150ab0d7e8daa9325b892f8bdc3c53f
[simgrid.git] / src / xbt / dynar.c
1 /* $Id$ */
2
3 /* a generic DYNamic ARray implementation.                                  */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "portable.h" /* SIZEOF_MAX */
11 #include "xbt/misc.h"
12 #include "xbt/sysdep.h"
13 #include "xbt/log.h"
14 #include "xbt/ex.h"
15 #include "xbt/dynar.h"
16 #include <sys/types.h>
17
18 #include "xbt/dynar_private.h" /* type definition, which we share with the 
19                                   code in charge of sending this across the net */
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn,xbt,"Dynamic arrays");
22
23
24 #define __sanity_check_dynar(dynar)       \
25            xbt_assert0(dynar,           \
26                         "dynar is NULL")
27 #define __sanity_check_idx(idx)                \
28            xbt_assert1(idx >= 0,             \
29                         "dynar idx(=%d) < 0", \
30                         (int) (idx))
31 #define __check_inbound_idx(dynar, idx)                                                \
32            xbt_assert2(idx < dynar->used,                                             \
33                         "dynar is not that long. You asked %d, but it's only %lu long", \
34                         (int) (idx), (unsigned long) dynar->used)
35 #define __check_sloppy_inbound_idx(dynar, idx)                                         \
36            xbt_assert2(idx <= dynar->used,                                            \
37                         "dynar is not that long. You asked %d, but it's only %lu long (could have been equal to it)", \
38                         (int) (idx), (unsigned long) dynar->used)
39 #define __check_populated_dynar(dynar)            \
40            xbt_assert1(dynar->used,              \
41                         "dynar %p contains nothing",(void*)dynar)
42
43 static XBT_INLINE 
44 void _xbt_clear_mem(void * const ptr,
45                      const unsigned long length) {
46   memset(ptr, 0, length);
47 }
48
49 static XBT_INLINE
50 void
51 _xbt_dynar_expand(xbt_dynar_t const dynar,
52                    const int          nb) {
53   const unsigned long old_size    = dynar->size;
54
55   if (nb > old_size) {
56     char * const old_data    = (char *) dynar->data;
57
58     const unsigned long elmsize     = dynar->elmsize;
59     const unsigned long old_length  = old_size*elmsize;
60
61     const unsigned long used        = dynar->used;
62     const unsigned long used_length = used*elmsize;
63
64     const unsigned long new_size    = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
65     const unsigned long new_length  = new_size*elmsize;
66     char * const new_data    = (char *) xbt_malloc0(elmsize*new_size);
67
68     DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
69
70     if (old_data) {
71       memcpy(new_data, old_data, used_length);
72       _xbt_clear_mem(old_data, old_length);
73       free(old_data);
74     }
75
76     _xbt_clear_mem(new_data + used_length, new_length - used_length);
77
78     dynar->size = new_size;
79     dynar->data = new_data;
80   }
81 }
82
83 static XBT_INLINE
84 void *
85 _xbt_dynar_elm(const xbt_dynar_t  dynar,
86                 const unsigned long idx) {
87   char * const data    = (char*) dynar->data;
88   const unsigned long elmsize = dynar->elmsize;
89
90   return data + idx*elmsize;
91 }
92
93 static XBT_INLINE
94 void
95 _xbt_dynar_get_elm(void  * const       dst,
96                     const xbt_dynar_t  dynar,
97                     const unsigned long idx) {
98   void * const elm     = _xbt_dynar_elm(dynar, idx);
99
100   memcpy(dst, elm, dynar->elmsize);
101 }
102
103 static XBT_INLINE
104 void
105 _xbt_dynar_put_elm(const xbt_dynar_t  dynar,
106                     const unsigned long idx,
107                     const void * const  src) {
108   void * const elm     = _xbt_dynar_elm(dynar, idx);
109   const unsigned long elmsize = dynar->elmsize;
110
111   memcpy(elm, src, elmsize);
112 }
113
114 void
115 xbt_dynar_dump(xbt_dynar_t dynar) {
116   INFO5("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
117         dynar->size, dynar->used, dynar->elmsize, dynar->data, dynar->free_f);
118 }       
119
120 /** @brief Constructor
121  * 
122  * \param elmsize size of each element in the dynar
123  * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
124  *
125  * Creates a new dynar. If a free_func is provided, the elements have to be
126  * pointer of pointer. That is to say that dynars can contain either base
127  * types (int, char, double, etc) or pointer of pointers (struct **).
128  */
129 xbt_dynar_t 
130 xbt_dynar_new(const unsigned long elmsize,
131                void_f_pvoid_t * const free_f) {
132    
133   xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
134
135   dynar->size    = 0;
136   dynar->used    = 0;
137   dynar->elmsize = elmsize;
138   dynar->data    = NULL;
139   dynar->free_f    = free_f;
140
141   return dynar;
142 }
143
144 /** @brief Destructor of the structure not touching to the content
145  * 
146  * \param dynar poor victim
147  *
148  * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
149  * is not touched (the \a free_f function is not used)
150  */
151 void
152 xbt_dynar_free_container(xbt_dynar_t *dynar) {
153   if (dynar && *dynar) {
154
155     if ((*dynar)->data) {
156       _xbt_clear_mem((*dynar)->data, (*dynar)->size);
157       free((*dynar)->data);
158     }
159
160     _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
161
162     free(*dynar);
163     *dynar=NULL;
164   }
165 }
166
167 /** @brief Frees the content and set the size to 0
168  *
169  * \param dynar who to squeeze
170  */
171 void
172 xbt_dynar_reset(xbt_dynar_t const dynar) {
173
174   __sanity_check_dynar(dynar);
175
176   DEBUG1("Reset the dynar %p",(void*)dynar);
177   if (dynar->free_f) {
178     xbt_dynar_map(dynar, dynar->free_f);
179   }
180 /*
181   if (dynar->data)
182     free(dynar->data);
183
184   dynar->size = 0;
185   */
186   dynar->used = 0;
187 /*  dynar->data = NULL;*/
188 }
189
190 /**
191  * \brief Shrink the dynar by removing empty slots at the end of the internal array
192  * \param dynar a dynar
193  * \param empty_slots_wanted number of empty slots you want to keep at the end of the
194  * internal array for further insertions
195  * 
196  * Reduces the internal array size of the dynar to the number of elements plus
197  * \a empty_slots_wanted.
198  * After removing elements from the dynar, you can call this function to make
199  * the dynar use less memory.
200  * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
201  * as possible.
202  * Note that if \a empty_slots_wanted is greater than the array size, the internal
203  * array is not expanded and nothing is done.
204  */
205 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted) {
206   int size_wanted = dynar->used + empty_slots_wanted;
207   if (size_wanted < dynar->size) {
208     dynar->size = size_wanted;
209     dynar->data = xbt_realloc(dynar->data, sizeof(void*) * dynar->size);
210   }
211 }
212
213 /** @brief Destructor
214  * 
215  * \param dynar poor victim
216  *
217  * kilkil a dynar and its content
218  */
219
220 void
221 xbt_dynar_free(xbt_dynar_t * dynar) {
222   if (dynar && *dynar) {
223     xbt_dynar_reset(*dynar);
224     xbt_dynar_free_container(dynar);
225   }
226 }
227 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
228 void xbt_dynar_free_voidp(void *d) {
229    xbt_dynar_free( (xbt_dynar_t*) d);
230 }
231    
232 /** @brief Count of dynar's elements
233  * 
234  * \param dynar the dynar we want to mesure
235  */
236 unsigned long
237 xbt_dynar_length(const xbt_dynar_t dynar) {
238   return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
239 }
240
241 /** @brief Retrieve a copy of the Nth element of a dynar.
242  *
243  * \param dynar information dealer
244  * \param idx index of the slot we want to retrieve
245  * \param[out] dst where to put the result to.
246  */
247 void
248 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
249                    const int          idx,
250                    void       * const dst) {
251
252   __sanity_check_dynar(dynar);
253   __sanity_check_idx(idx);
254   __check_inbound_idx(dynar, idx);
255
256   _xbt_dynar_get_elm(dst, dynar, idx);
257 }
258
259 /** @brief Retrieve a pointer to the Nth element of a dynar.
260  *
261  * \param dynar information dealer
262  * \param idx index of the slot we want to retrieve
263  * \return the \a idx-th element of \a dynar.
264  *
265  * \warning The returned value is the actual content of the dynar. 
266  * Make a copy before fooling with it.
267  */
268 void*
269 xbt_dynar_get_ptr(const xbt_dynar_t dynar, const int idx) {
270
271   __sanity_check_dynar(dynar);
272   __sanity_check_idx(idx);
273   __check_inbound_idx(dynar, idx);
274
275   return _xbt_dynar_elm(dynar, idx);
276 }
277
278 /** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed
279  * 
280  * \param dynar information dealer
281  * \param idx index of the slot we want to modify
282  * \param src What will be feeded to the dynar
283  *
284  * If you want to free the previous content, use xbt_dynar_replace().
285  */
286 void
287 xbt_dynar_set(xbt_dynar_t         dynar,
288                const int            idx,
289                const void   * const src) {
290
291   __sanity_check_dynar(dynar);
292   __sanity_check_idx(idx);
293
294   _xbt_dynar_expand(dynar, idx+1);
295
296   if (idx >= dynar->used) {
297     dynar->used = idx+1;
298   }
299
300   _xbt_dynar_put_elm(dynar, idx, src);
301 }
302
303 /** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed
304  *
305  * \param dynar
306  * \param idx
307  * \param object
308  *
309  * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
310  * free the previous value at this position. If you don't want to free the
311  * previous content, use xbt_dynar_set().
312  */
313 void
314 xbt_dynar_replace(xbt_dynar_t         dynar,
315                    const int            idx,
316                    const void   * const object) {
317
318   __sanity_check_dynar(dynar);
319   __sanity_check_idx(idx);
320
321   if (idx < dynar->used && dynar->free_f) {
322     void * const old_object = _xbt_dynar_elm(dynar, idx);
323
324     dynar->free_f(old_object);
325   }
326
327   xbt_dynar_set(dynar, idx, object);
328 }
329
330 /** @brief Make room for a new element, and return a pointer to it
331  * 
332  * You can then use regular affectation to set its value instead of relying 
333  * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
334  */
335 void *
336 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
337                         const int            idx) {
338    
339   __sanity_check_dynar(dynar);
340   __sanity_check_idx(idx);
341   __check_sloppy_inbound_idx(dynar, idx);
342
343   {
344     const unsigned long old_used = dynar->used;
345     const unsigned long new_used = old_used + 1;
346
347     _xbt_dynar_expand(dynar, new_used);
348
349     {
350       const unsigned long nb_shift =  old_used - idx;
351
352       if (nb_shift)
353          memmove(_xbt_dynar_elm(dynar, idx+1), 
354                  _xbt_dynar_elm(dynar, idx), 
355                  nb_shift * dynar->elmsize);
356     }
357
358     dynar->used = new_used;
359     return _xbt_dynar_elm(dynar,idx);
360   }
361 }
362
363 /** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right
364  * 
365  * Set the Nth element of a dynar, expanding the dynar if needed, and
366  * moving the previously existing value and all subsequent ones to one
367  * position right in the dynar.
368  */
369 void
370 xbt_dynar_insert_at(xbt_dynar_t  const dynar,
371                     const int            idx,
372                     const void   * const src) {
373
374   /* checks done in xbt_dynar_insert_at_ptr */
375   memcpy(xbt_dynar_insert_at_ptr(dynar,idx),
376          src,
377          dynar->elmsize);
378 }
379
380 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
381  *
382  * Get the Nth element of a dynar, removing it from the dynar and moving
383  * all subsequent values to one position left in the dynar.
384  */
385 void
386 xbt_dynar_remove_at(xbt_dynar_t  const dynar,
387                      const int            idx,
388                      void         * const object) {
389
390   unsigned long nb_shift;
391   unsigned long offset;
392
393   __sanity_check_dynar(dynar);
394   __sanity_check_idx(idx);
395   __check_inbound_idx(dynar, idx);
396
397   if (object) {
398     _xbt_dynar_get_elm(object, dynar, idx);
399   } else if (dynar->free_f) {
400     if (dynar->elmsize <= SIZEOF_MAX) {
401        char elm[SIZEOF_MAX];
402        _xbt_dynar_get_elm(elm, dynar, idx);
403        (*dynar->free_f)(elm);
404     } else {
405        char *elm=malloc(dynar->elmsize);
406        _xbt_dynar_get_elm(elm, dynar, idx);
407        (*dynar->free_f)(elm);
408        free(elm);
409     }
410   }
411
412   nb_shift =  dynar->used-1 - idx;
413   offset   =  nb_shift * dynar->elmsize;
414
415   memmove(_xbt_dynar_elm(dynar, idx),
416           _xbt_dynar_elm(dynar, idx+1), 
417           offset);
418
419   dynar->used--;
420 }
421
422 /** @brief Returns the position of the element in the dynar
423  *
424  * Raises not_found_error if not found.
425  */
426 int
427 xbt_dynar_search(xbt_dynar_t  const dynar,
428                  void        *const elem) {
429   int it;
430    
431   for (it=0; it< dynar->size; it++) 
432     if (!memcmp(_xbt_dynar_elm(dynar, it),elem,dynar->elmsize))
433       return it;
434
435   THROW2(not_found_error,0,"Element %p not part of dynar %p",elem,dynar);
436 }
437
438 /** @brief Returns a boolean indicating whether the element is part of the dynar */
439 int
440 xbt_dynar_member(xbt_dynar_t  const dynar,
441                  void        *const elem) {
442
443   xbt_ex_t e;
444    
445   TRY {
446      xbt_dynar_search(dynar,elem);
447   } CATCH(e) {
448      if (e.category == not_found_error) {
449         xbt_ex_free(e);
450         return 0;
451      }
452      RETHROW;
453   }
454   return 1;
455 }
456
457 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
458  *
459  * You can then use regular affectation to set its value instead of relying 
460  * on the slow memcpy. This is what xbt_dynar_push_as() does.
461  */
462 void *
463 xbt_dynar_push_ptr(xbt_dynar_t  const dynar) {
464   return xbt_dynar_insert_at_ptr(dynar, dynar->used);    
465 }
466
467 /** @brief Add an element at the end of the dynar */
468 void
469 xbt_dynar_push(xbt_dynar_t  const dynar,
470                 const void   * const src) {
471   /* sanity checks done by insert_at */
472   xbt_dynar_insert_at(dynar, dynar->used, src); 
473 }
474
475 /** @brief Mark the last dynar's element as unused and return a pointer to it.
476  *
477  * You can then use regular affectation to set its value instead of relying 
478  * on the slow memcpy. This is what xbt_dynar_pop_as() does.
479  */
480 void *
481 xbt_dynar_pop_ptr(xbt_dynar_t  const dynar) {
482
483   __check_populated_dynar(dynar);
484   DEBUG1("Pop %p",(void*)dynar);
485   dynar->used--;
486   return _xbt_dynar_elm(dynar,dynar->used);
487 }
488
489 /** @brief Get and remove the last element of the dynar */
490 void
491 xbt_dynar_pop(xbt_dynar_t  const dynar,
492               void         * const dst) {
493
494   /* sanity checks done by remove_at */
495   DEBUG1("Pop %p",(void*)dynar);
496   xbt_dynar_remove_at(dynar, dynar->used-1, dst);
497 }
498
499 /** @brief Add an element at the begining of the dynar.
500  *
501  * This is less efficient than xbt_dynar_push()
502  */
503 void
504 xbt_dynar_unshift(xbt_dynar_t  const dynar,
505                    const void   * const src) {
506   
507   /* sanity checks done by insert_at */
508   xbt_dynar_insert_at(dynar, 0, src);
509 }
510
511 /** @brief Get and remove the first element of the dynar.
512  *
513  * This is less efficient than xbt_dynar_pop()
514  */
515 void
516 xbt_dynar_shift(xbt_dynar_t  const dynar,
517                  void         * const dst) {
518
519   /* sanity checks done by remove_at */
520   xbt_dynar_remove_at(dynar, 0, dst);
521 }
522
523 /** @brief Apply a function to each member of a dynar
524  *
525  * The mapped function may change the value of the element itself, 
526  * but should not mess with the structure of the dynar.
527  */
528 void
529 xbt_dynar_map(const xbt_dynar_t  dynar,
530                void_f_pvoid_t     * const op) {
531
532   __sanity_check_dynar(dynar);
533
534   {
535     char         elm[SIZEOF_MAX];
536     const unsigned long used = dynar->used;
537     unsigned long       i    = 0;
538
539     for (i = 0; i < used; i++) {
540       _xbt_dynar_get_elm(elm, dynar, i);
541       op(elm);
542     }
543   }
544 }
545
546 /** @brief Put the cursor at the begining of the dynar.
547  *
548  * Actually, the cursor is set one step before the begining, so that you
549  * can iterate over the dynar with a for loop.
550  */
551 void
552 xbt_dynar_cursor_first(const xbt_dynar_t dynar,
553                        int        * const cursor) {
554
555   DEBUG1("Set cursor on %p to the first position",(void*)dynar);
556   *cursor = 0;
557 }
558
559 /** @brief Move the cursor to the next value */
560 void
561 xbt_dynar_cursor_step(const xbt_dynar_t dynar,
562                        int        * const cursor) {
563   
564   (*cursor)++;
565 }
566
567 /** @brief Get the data currently pointed by the cursor */
568 int
569 xbt_dynar_cursor_get(const xbt_dynar_t dynar,
570                       int                * const cursor,
571                       void               * const dst) {
572
573   __sanity_check_dynar(dynar);
574   {
575
576     const int idx = *cursor;
577
578     if (idx >= dynar->used) {
579       DEBUG1("Cursor on %p already on last elem",(void*)dynar);
580       return FALSE;
581     }
582     DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
583
584     _xbt_dynar_get_elm(dst, dynar, idx);
585   }
586   return TRUE;
587
588 }
589
590 /** @brief Removes and free the entry pointed by the cursor 
591  *
592  * This function can be used while traversing without problem.
593  */
594 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
595                           int          * const cursor) {
596   void *dst;
597
598   if (dynar->elmsize > sizeof(void*)) {
599     DEBUG0("Elements too big to fit into a pointer");
600     if (dynar->free_f) {
601       dst=xbt_malloc(dynar->elmsize);
602       xbt_dynar_remove_at(dynar,(*cursor)--,dst);
603       (dynar->free_f)(dst);
604       free(dst);
605     } else {
606       DEBUG0("Ok, we dont care about the element without free function");
607       xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
608     }
609       
610   } else {
611     xbt_dynar_remove_at(dynar,(*cursor)--,&dst);
612     if (dynar->free_f)
613       (dynar->free_f)(dst);
614   }
615 }
616
617 #ifdef SIMGRID_TEST
618
619 #define NB_ELEM 5000
620
621 XBT_TEST_SUITE("dynar","Dynar data container");
622 XBT_LOG_EXTERNAL_CATEGORY(xbt_dyn);
623 XBT_LOG_DEFAULT_CATEGORY(xbt_dyn);
624
625 XBT_TEST_UNIT("int",test_dynar_int,"Dyars of integers") {
626    /* Vars_decl [doxygen cruft] */
627    xbt_dynar_t d;
628    int i,cpt,cursor;
629    int *iptr;
630    
631    xbt_test_add0("==== Traverse the empty dynar");
632    d=xbt_dynar_new(sizeof(int),NULL);
633    xbt_dynar_foreach(d,cursor,i){
634      xbt_assert0(0,"Damnit, there is something in the empty dynar");
635    }
636    xbt_dynar_free(&d);
637    xbt_dynar_free(&d);
638
639    xbt_test_add1("==== Push %d int, set them again 3 times, traverse them, shift them",
640         NB_ELEM);
641    /* Populate_ints [doxygen cruft] */
642    /* 1. Populate the dynar */
643    d=xbt_dynar_new(sizeof(int),NULL);
644    for (cpt=0; cpt< NB_ELEM; cpt++) {
645      xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
646      /* xbt_dynar_push(d,&cpt);       This would also work */
647      xbt_test_log2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
648    }
649    
650    /* 2. Traverse manually the dynar */
651    for (cursor=0; cursor< NB_ELEM; cursor++) {
652      iptr=xbt_dynar_get_ptr(d,cursor);
653      xbt_test_assert2(cursor == *iptr,
654                       "The retrieved value is not the same than the injected one (%d!=%d)",
655                       cursor,cpt);
656    }
657    
658    /* 3. Traverse the dynar using the neat macro to that extend */
659    xbt_dynar_foreach(d,cursor,cpt){
660      xbt_test_assert2(cursor == cpt,
661                       "The retrieved value is not the same than the injected one (%d!=%d)",
662                       cursor,cpt);
663    }
664    /* end_of_traversal */
665    
666    for (cpt=0; cpt< NB_ELEM; cpt++)
667      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
668
669    for (cpt=0; cpt< NB_ELEM; cpt++) 
670      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
671 /*     xbt_dynar_set(d,cpt,&cpt);*/
672    
673    for (cpt=0; cpt< NB_ELEM; cpt++) 
674      *(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
675    
676    cpt=0;
677    xbt_dynar_foreach(d,cursor,i){
678      xbt_test_assert2(i == cpt,
679                       "The retrieved value is not the same than the injected one (%d!=%d)",
680                       i,cpt);
681      cpt++;
682    }
683    xbt_test_assert2(cpt == NB_ELEM,
684                     "Cannot retrieve my %d values. Last got one is %d",
685                     NB_ELEM, cpt);
686
687    /* shifting [doxygen cruft] */
688    /* 4. Shift all the values */
689    for (cpt=0; cpt< NB_ELEM; cpt++) {
690      xbt_dynar_shift(d,&i);
691      xbt_test_assert2(i == cpt,
692                       "The retrieved value is not the same than the injected one (%d!=%d)",
693                       i,cpt);
694      xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
695    }
696    
697    /* 5. Free the resources */
698    xbt_dynar_free(&d);
699    xbt_dynar_free(&d);
700
701    
702    xbt_test_add1("==== Unshift/pop %d int",NB_ELEM);
703    d=xbt_dynar_new(sizeof(int),NULL);
704    for (cpt=0; cpt< NB_ELEM; cpt++) {
705      xbt_dynar_unshift(d,&cpt);
706      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
707    }
708    for (cpt=0; cpt< NB_ELEM; cpt++) {
709      i=xbt_dynar_pop_as(d,int);
710      xbt_test_assert2(i == cpt,
711                       "The retrieved value is not the same than the injected one (%d!=%d)",
712                       i,cpt);
713      xbt_test_log2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
714    }
715    xbt_dynar_free(&d);
716    xbt_dynar_free(&d);
717
718    
719    xbt_test_add1("==== Push %d int, insert 1000 int in the middle, shift everything",NB_ELEM);
720    d=xbt_dynar_new(sizeof(int),NULL);
721    for (cpt=0; cpt< NB_ELEM; cpt++) {
722      xbt_dynar_push_as(d,int,cpt);
723      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
724    }
725    for (cpt=0; cpt< 1000; cpt++) {
726      xbt_dynar_insert_at_as(d,2500,int,cpt);
727      DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
728    }
729
730    for (cpt=0; cpt< 2500; cpt++) {
731      xbt_dynar_shift(d,&i);
732      xbt_test_assert2(i == cpt,
733              "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
734                i,cpt);
735      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
736    }
737    for (cpt=999; cpt>=0; cpt--) {
738      xbt_dynar_shift(d,&i);
739      xbt_test_assert2(i == cpt,
740            "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
741                       i,cpt);
742    }
743    for (cpt=2500; cpt< NB_ELEM; cpt++) {
744      xbt_dynar_shift(d,&i);
745       xbt_test_assert2(i == cpt,
746            "The retrieved value is not the same than the injected one at the end (%d!=%d)",
747                        i,cpt);
748    }
749    xbt_dynar_free(&d);
750    xbt_dynar_free(&d);
751
752
753    xbt_test_add1("==== Push %d int, remove 2000-4000. free the rest",NB_ELEM);
754    d=xbt_dynar_new(sizeof(int),NULL);
755    for (cpt=0; cpt< NB_ELEM; cpt++) 
756      xbt_dynar_push_as(d,int,cpt);
757    
758    for (cpt=2000; cpt< 4000; cpt++) {
759      xbt_dynar_remove_at(d,2000,&i);
760      xbt_test_assert2(i == cpt,
761                       "Remove a bad value. Got %d, expected %d",
762                       i,cpt);
763      DEBUG2("remove %d, length=%lu",cpt, xbt_dynar_length(d));
764    }
765    xbt_dynar_free(&d);
766    xbt_dynar_free(&d);
767 }
768
769 XBT_TEST_UNIT("double",test_dynar_double,"Dyars of doubles") {
770    xbt_dynar_t d;
771    int cpt,cursor;
772    double d1,d2;
773    
774    xbt_test_add0("==== Traverse the empty dynar");
775    d=xbt_dynar_new(sizeof(int),NULL);
776    xbt_dynar_foreach(d,cursor,cpt){
777      xbt_test_assert0(FALSE,
778              "Damnit, there is something in the empty dynar");
779    }
780    xbt_dynar_free(&d);
781    xbt_dynar_free(&d);
782
783    xbt_test_add0("==== Push/shift 5000 doubles");
784    d=xbt_dynar_new(sizeof(double),NULL);
785    for (cpt=0; cpt< 5000; cpt++) {
786      d1=(double)cpt;
787      xbt_dynar_push(d,&d1);
788    }
789    xbt_dynar_foreach(d,cursor,d2){
790      d1=(double)cursor;
791      xbt_test_assert2(d1 == d2,
792            "The retrieved value is not the same than the injected one (%f!=%f)",
793                   d1,d2);
794    }
795    for (cpt=0; cpt< 5000; cpt++) {
796      d1=(double)cpt;
797      xbt_dynar_shift(d,&d2);
798      xbt_test_assert2(d1 == d2,
799            "The retrieved value is not the same than the injected one (%f!=%f)",
800                   d1,d2);
801    }
802    xbt_dynar_free(&d);
803    xbt_dynar_free(&d);
804
805
806    xbt_test_add0("==== Unshift/pop 5000 doubles");
807    d=xbt_dynar_new(sizeof(double),NULL);
808    for (cpt=0; cpt< 5000; cpt++) {
809      d1=(double)cpt;
810      xbt_dynar_unshift(d,&d1);
811    }
812    for (cpt=0; cpt< 5000; cpt++) {
813      d1=(double)cpt;
814      xbt_dynar_pop(d,&d2);
815      xbt_test_assert2 (d1 == d2,
816            "The retrieved value is not the same than the injected one (%f!=%f)",
817                    d1,d2);
818    }
819    xbt_dynar_free(&d);
820    xbt_dynar_free(&d);
821
822
823
824    xbt_test_add0("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
825    d=xbt_dynar_new(sizeof(double),NULL);
826    for (cpt=0; cpt< 5000; cpt++) {
827      d1=(double)cpt;
828      xbt_dynar_push(d,&d1);
829    }
830    for (cpt=0; cpt< 1000; cpt++) {
831      d1=(double)cpt;
832      xbt_dynar_insert_at(d,2500,&d1);
833    }
834
835    for (cpt=0; cpt< 2500; cpt++) {
836      d1=(double)cpt;
837      xbt_dynar_shift(d,&d2);
838      xbt_test_assert2(d1 == d2,
839            "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
840                   d1,d2);
841      DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
842    }
843    for (cpt=999; cpt>=0; cpt--) {
844      d1=(double)cpt;
845      xbt_dynar_shift(d,&d2);
846      xbt_test_assert2 (d1 == d2,
847            "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
848                    d1,d2);
849    }
850    for (cpt=2500; cpt< 5000; cpt++) {
851      d1=(double)cpt;
852      xbt_dynar_shift(d,&d2);
853      xbt_test_assert2 (d1 == d2,
854            "The retrieved value is not the same than the injected one at the end (%f!=%f)",
855                    d1,d2);
856    }
857    xbt_dynar_free(&d);
858    xbt_dynar_free(&d);
859
860
861    xbt_test_add0("==== Push 5000 double, remove 2000-4000. free the rest");
862    d=xbt_dynar_new(sizeof(double),NULL);
863    for (cpt=0; cpt< 5000; cpt++) {
864      d1=(double)cpt;
865      xbt_dynar_push(d,&d1);
866    }
867    for (cpt=2000; cpt< 4000; cpt++) {
868      d1=(double)cpt;
869      xbt_dynar_remove_at(d,2000,&d2);
870      xbt_test_assert2 (d1 == d2,
871            "Remove a bad value. Got %f, expected %f",
872                d2,d1);
873    }
874    xbt_dynar_free(&d);
875    xbt_dynar_free(&d);
876 }
877
878
879 /* doxygen_string_cruft */
880
881 /* The function we will use to free the data */
882 static void free_string(void *d){
883   free(*(void**)d);
884 }
885
886 XBT_TEST_UNIT("string",test_dynar_string,"Dyars of strings") {
887    xbt_dynar_t d;
888    int cpt;
889    char buf[1024];
890    char *s1,*s2;
891    
892    xbt_test_add0("==== Traverse the empty dynar");
893    d=xbt_dynar_new(sizeof(char *),&free_string);
894    xbt_dynar_foreach(d,cpt,s1){
895      xbt_test_assert0(FALSE,
896                   "Damnit, there is something in the empty dynar");
897    }
898    xbt_dynar_free(&d);
899    xbt_dynar_free(&d);
900
901    xbt_test_add1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
902    /* Populate_str [doxygen cruft] */
903    d=xbt_dynar_new(sizeof(char*),&free_string);
904    /* 1. Populate the dynar */
905    for (cpt=0; cpt< NB_ELEM; cpt++) {
906      sprintf(buf,"%d",cpt);
907      s1=strdup(buf);
908      xbt_dynar_push(d,&s1);
909    }
910    for (cpt=0; cpt< NB_ELEM; cpt++) {
911      sprintf(buf,"%d",cpt);
912      s1=strdup(buf);
913      xbt_dynar_replace(d,cpt,&s1);
914    }
915    for (cpt=0; cpt< NB_ELEM; cpt++) {
916      sprintf(buf,"%d",cpt);
917      s1=strdup(buf);
918      xbt_dynar_replace(d,cpt,&s1);
919    }
920    for (cpt=0; cpt< NB_ELEM; cpt++) {
921      sprintf(buf,"%d",cpt);
922      s1=strdup(buf);
923      xbt_dynar_replace(d,cpt,&s1);
924    }
925    for (cpt=0; cpt< NB_ELEM; cpt++) {
926      sprintf(buf,"%d",cpt);
927      xbt_dynar_shift(d,&s2);
928      xbt_test_assert2 (!strcmp(buf,s2),
929             "The retrieved value is not the same than the injected one (%s!=%s)",
930                    buf,s2);
931      free(s2);
932    }
933    xbt_dynar_free(&d);
934    xbt_dynar_free(&d);
935
936
937    xbt_test_add1("==== Unshift, traverse and pop %d strings",NB_ELEM);
938    d=xbt_dynar_new(sizeof(char**),&free_string);
939    for (cpt=0; cpt< NB_ELEM; cpt++) {
940      sprintf(buf,"%d",cpt);
941      s1=strdup(buf);
942      xbt_dynar_unshift(d,&s1);
943    }
944    /* 2. Traverse the dynar with the macro */
945    xbt_dynar_foreach(d,cpt,s1) {
946      sprintf(buf,"%d",NB_ELEM - cpt -1);
947      xbt_test_assert2 (!strcmp(buf,s1),
948            "The retrieved value is not the same than the injected one (%s!=%s)",
949                buf,s1);
950    }
951    /* 3. Traverse the dynar with the macro */
952    for (cpt=0; cpt< NB_ELEM; cpt++) {
953      sprintf(buf,"%d",cpt);
954      xbt_dynar_pop(d,&s2);
955      xbt_test_assert2 (!strcmp(buf,s2),
956            "The retrieved value is not the same than the injected one (%s!=%s)",
957                buf,s2);
958      free(s2);
959    }
960    /* 4. Free the resources */
961    xbt_dynar_free(&d);
962    xbt_dynar_free(&d);
963
964
965    xbt_test_add2("==== Push %d strings, insert %d strings in the middle, shift everything",NB_ELEM,NB_ELEM/5);
966    d=xbt_dynar_new(sizeof(char*),&free_string);
967    for (cpt=0; cpt< NB_ELEM; cpt++) {
968      sprintf(buf,"%d",cpt);
969      s1=strdup(buf);
970      xbt_dynar_push(d,&s1);
971    }
972    for (cpt=0; cpt< NB_ELEM/5; cpt++) {
973      sprintf(buf,"%d",cpt);
974      s1=strdup(buf);
975      xbt_dynar_insert_at(d,NB_ELEM/2,&s1);
976    }
977
978    for (cpt=0; cpt< NB_ELEM/2; cpt++) {
979      sprintf(buf,"%d",cpt);
980      xbt_dynar_shift(d,&s2);
981      xbt_test_assert2(!strcmp(buf,s2),
982            "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
983                buf,s2);
984       free(s2);
985    }
986    for (cpt=(NB_ELEM/5)-1; cpt>=0; cpt--) {
987      sprintf(buf,"%d",cpt);
988      xbt_dynar_shift(d,&s2);
989      xbt_test_assert2 (!strcmp(buf,s2),
990            "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
991                buf,s2);
992      free(s2);
993    }
994    for (cpt=NB_ELEM/2; cpt< NB_ELEM; cpt++) {
995      sprintf(buf,"%d",cpt);
996      xbt_dynar_shift(d,&s2);
997      xbt_test_assert2 (!strcmp(buf,s2),
998            "The retrieved value is not the same than the injected one at the end (%s!=%s)",
999                buf,s2);
1000      free(s2);
1001    }
1002    xbt_dynar_free(&d);
1003    xbt_dynar_free(&d);
1004
1005
1006    xbt_test_add3("==== Push %d strings, remove %d-%d. free the rest",NB_ELEM,2*(NB_ELEM/5),4*(NB_ELEM/5));
1007    d=xbt_dynar_new(sizeof(char*),&free_string);
1008    for (cpt=0; cpt< NB_ELEM; cpt++) {
1009      sprintf(buf,"%d",cpt);
1010      s1=strdup(buf);
1011      xbt_dynar_push(d,&s1);
1012    }
1013    for (cpt=2*(NB_ELEM/5); cpt< 4*(NB_ELEM/5); cpt++) {
1014      sprintf(buf,"%d",cpt);
1015      xbt_dynar_remove_at(d,2*(NB_ELEM/5),&s2);
1016      xbt_test_assert2(!strcmp(buf,s2),
1017                   "Remove a bad value. Got %s, expected %s",
1018                   s2,buf);
1019       free(s2);
1020    }
1021    xbt_dynar_free(&d); /* end_of_doxygen */
1022 }
1023 #endif /* SIMGRID_TEST */