Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
227347b76a6d53918e5af29ee9ac9c1ae0f7589b
[simgrid.git] / src / xbt / dict.c
1 /* $Id$ */
2
3 /* dict - a generic dictionnary, variation over the B-tree concept          */
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 <string.h>
11 #include "xbt/ex.h"
12 #include "xbt/log.h"
13 #include "xbt/mallocator.h"
14 #include "xbt_modinter.h"
15 #include "dict_private.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict,xbt,
18    "Dictionaries provide the same functionnalities than hash tables");
19 /*####[ Private prototypes ]#################################################*/
20
21 static xbt_mallocator_t dict_mallocator = NULL;
22 static void* dict_mallocator_new_f(void);
23 static void dict_mallocator_free_f(void* dict);
24 static void dict_mallocator_reset_f(void* dict);
25
26
27 /*####[ Code ]###############################################################*/
28
29 /**
30  * \brief Constructor
31  * \return pointer to the destination
32  * \see xbt_dict_new_ext(), xbt_dict_free()
33  *
34  * Creates and initialize a new dictionnary with a default hashtable size.
35  */
36 xbt_dict_t xbt_dict_new(void) {
37   return xbt_dict_new_ext(256);
38 }
39
40 /**
41  * \brief Create a new dictionary with the specified hashtable size
42  * \param hashsize the hashtable size
43  * \return a pointer to the created object
44  * \see xbt_dict_new(), xbt_dict_free()
45  */
46 xbt_dict_t xbt_dict_new_ext(int hashsize) {
47   xbt_dict_t dict;
48
49   if (dict_mallocator == NULL) {
50     /* first run */
51     dict_mallocator = xbt_mallocator_new(256,
52                                          dict_mallocator_new_f,
53                                          dict_mallocator_free_f,
54                                          dict_mallocator_reset_f);
55     dict_elm_mallocator = xbt_mallocator_new(256,
56                                              dict_elm_mallocator_new_f,
57                                              dict_elm_mallocator_free_f,
58                                              dict_elm_mallocator_reset_f);
59   }
60
61   dict = xbt_mallocator_get(dict_mallocator);
62   dict->table_size = hashsize;
63   dict->table = xbt_new0(xbt_dictelm_t, dict->table_size);
64   dict->count = 0;
65
66   return dict;
67 }
68
69 /**
70  * \brief Destructor
71  * \param dict the dictionnary to be freed
72  *
73  * Frees a dictionary with all the data
74  */
75 void xbt_dict_free(xbt_dict_t *dict) {
76   int i;
77   xbt_dictelm_t current, previous;
78   int table_size;
79   xbt_dictelm_t *table;
80
81   if (dict != NULL && *dict != NULL) {
82     table_size = (*dict)->table_size;
83     table = (*dict)->table;
84     for (i = 0; i < table_size; i++) {
85       current = table[i];
86       while (current != NULL) {
87         previous = current;
88         current = current->next;
89         xbt_dictelm_free(previous);
90       }
91     }
92     xbt_free(table);
93     xbt_mallocator_release(dict_mallocator, *dict);
94     *dict = NULL;
95   }
96 }
97
98 /**
99  * \brief Change the hashtable size
100  * \param dict a dictionary
101  * \param hashsize the new hashtable size
102  *
103  * Change the hashtable size is a long operation, so it's better
104  * to use xbt_dict_new_ext or to call xbt_dict_hashsize_set when
105  * the dictionary is empty. 
106  */
107 void xbt_dict_hashsize_set(xbt_dict_t dict, int hashsize) {
108   xbt_dict_t new_dict = xbt_dict_new_ext(hashsize);
109   xbt_dictelm_t element, next;
110   int i;
111
112   for (i = 0; i < dict->table_size; i++) {
113     element = dict->table[i];
114     while (element != NULL) {
115       next = element->next; /* save the next because it will be lost */
116       xbt_dict_add_element(new_dict, element); /* no new xbt_dictelm_t is mallocated */
117       element = next;
118     }
119   }
120
121   xbt_free(dict->table);
122   dict->table = new_dict->table;
123   dict->table_size = hashsize;
124   xbt_free(new_dict);
125 }
126
127 /**
128  * Returns the hash code of a string.
129  */
130 static unsigned int xbt_dict_hash_ext(const char *str, int str_len) {
131   /* fast implementation of djb2 algorithm */
132   unsigned int hash = 5381;
133   int c;
134   
135   while (str_len--) {
136     c = *str++;
137     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
138   }
139   
140   return hash;
141 }
142
143 static unsigned int xbt_dict_hash(const char *str) {
144   /* fast implementation of djb2 algorithm */
145   unsigned int hash = 5381;
146   int c;
147   
148   while ( (c = *str++) ) {
149     hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
150   }
151   
152   return hash;
153 }
154
155 /**
156  * \brief Add data to the dict (arbitrary key)
157  * \param dict the container
158  * \param key the key to set the new data
159  * \param key_len the size of the \a key
160  * \param data the data to add in the dict
161  * \param free_ctn function to call with (\a key as argument) when 
162  *        \a key is removed from the dictionnary
163  *
164  * Set the \a data in the structure under the \a key, which can be any kind 
165  * of data, as long as its length is provided in \a key_len.
166  */
167 void xbt_dict_set_ext(xbt_dict_t      dict,
168                       const char      *key,
169                       int              key_len,
170                       void            *data,
171                       void_f_pvoid_t  *free_ctn) {
172   xbt_assert(dict);
173
174   unsigned int hash_code = xbt_dict_hash_ext(key,key_len) % dict->table_size;
175   xbt_dictelm_t current, previous = NULL;
176
177   current = dict->table[hash_code];
178   while (current != NULL &&
179          (key_len != current->key_len || strncmp(key, current->key, key_len))) {
180     previous = current;
181     current = current->next;
182   }
183
184   if (current == NULL) {
185     /* this key doesn't exist yet */
186     current = xbt_dictelm_new(key, key_len, data, free_ctn, NULL);
187     if (previous == NULL) {
188       dict->table[hash_code] = current;
189     }
190     else {
191       previous->next = current;
192     }
193     dict->count++;
194   }
195   else {
196     /* there is already an element with the same key: we overwrite it */
197     if (current->content != NULL && current->free_f != NULL) {
198       current->free_f(current->content);
199     }
200     current->content = data;
201     current->free_f = free_ctn;
202   }
203 }
204
205 /**
206  * \brief Add data to the dict (null-terminated key)
207  *
208  * \param dict the dict
209  * \param key the key to set the new data
210  * \param data the data to add in the dict
211  * \param free_ctn function to call with (\a key as argument) when 
212  *        \a key is removed from the dictionnary
213  *
214  * set the \a data in the structure under the \a key, which is a 
215  * null terminated string.
216  */
217 void xbt_dict_set(xbt_dict_t     dict,
218                   const char     *key,
219                   void           *data,
220                   void_f_pvoid_t *free_ctn) {
221
222   xbt_assert(dict);
223   
224   xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
225 }
226
227 /**
228  * \brief Retrieve data from the dict (arbitrary key)
229  *
230  * \param dict the dealer of data
231  * \param key the key to find data
232  * \param key_len the size of the \a key
233  * \return the data that we are looking for
234  *
235  * Search the given \a key. Throws not_found_error when not found.
236  */
237 void *xbt_dict_get_ext(xbt_dict_t      dict,
238                        const char     *key,
239                        int             key_len) {
240   xbt_assert(dict);
241
242   unsigned int hash_code = xbt_dict_hash_ext(key,key_len) % dict->table_size;
243   xbt_dictelm_t current;
244
245   current = dict->table[hash_code];
246   while (current != NULL &&
247          (key_len != current->key_len || strncmp(key, current->key, key_len))) {
248     current = current->next;
249   }
250
251   if (current == NULL) {
252     THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
253   }
254
255   return current->content;
256 }
257
258 /**
259  * \brief Retrieve data from the dict (null-terminated key) 
260  *
261  * \param dict the dealer of data
262  * \param key the key to find data
263  * \return the data that we are looking for
264  *
265  * Search the given \a key. Throws not_found_error when not found. 
266  * Check xbt_dict_get_or_null() for a version returning NULL without exception when 
267  * not found.
268  */
269 void *xbt_dict_get(xbt_dict_t dict,
270                    const char *key) {
271   xbt_assert(dict);
272
273   unsigned int hash_code = xbt_dict_hash(key) % dict->table_size;
274   xbt_dictelm_t current;
275
276   current = dict->table[hash_code];
277   while (current != NULL && (strcmp(key, current->key))) {
278     current = current->next;
279   }
280
281   if (current == NULL) {
282     THROW1(not_found_error, 0, "key %s not found", key);
283   }
284
285   return current->content;
286 }
287
288 /**
289  * \brief like xbt_dict_get(), but returning NULL when not found
290  */
291 void *xbt_dict_get_or_null(xbt_dict_t     dict,
292                      const char     *key) {
293   xbt_ex_t e;
294   void *result = NULL;
295   TRY {
296     result = xbt_dict_get(dict, key);
297   } CATCH(e) {
298     if (e.category != not_found_error) 
299       RETHROW;
300     xbt_ex_free(e);
301     result = NULL;
302   }
303   return result;
304 }
305
306
307 /**
308  * \brief Remove data from the dict (arbitrary key)
309  *
310  * \param dict the trash can
311  * \param key the key of the data to be removed
312  * \param key_len the size of the \a key
313  *
314  * Remove the entry associated with the given \a key (throws not_found)
315  */
316 void xbt_dict_remove_ext(xbt_dict_t  dict,
317                          const char  *key,
318                          int          key_len) {
319   xbt_assert(dict);
320
321   unsigned int hash_code = xbt_dict_hash_ext(key,key_len) % dict->table_size;
322   xbt_dictelm_t current, previous = NULL;
323
324   current = dict->table[hash_code];
325   while (current != NULL &&
326          (key_len != current->key_len || strncmp(key, current->key, key_len))) {
327     previous = current; /* save the previous node */
328     current = current->next;
329   }
330
331   if (current == NULL) {
332     THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
333   }
334
335   if (previous != NULL) {
336     xbt_assert0(previous->next == current, "previous-next != current");
337     previous->next = current->next;
338   }
339   else {
340     dict->table[hash_code] = current->next;
341   }
342
343   xbt_dictelm_free(current);
344   dict->count--;
345 }
346
347 /**
348  * \brief Remove data from the dict (null-terminated key)
349  *
350  * \param dict the dict
351  * \param key the key of the data to be removed
352  *
353  * Remove the entry associated with the given \a key
354  */
355 void xbt_dict_remove(xbt_dict_t  dict,
356                      const char  *key) {
357   xbt_assert(dict);
358
359   xbt_dict_remove_ext(dict, key, strlen(key));
360 }
361
362 /**
363  * \brief Remove all data from the dict
364  * \param dict the dict
365  */
366 void xbt_dict_reset(xbt_dict_t dict) {
367   xbt_assert(dict);
368
369   int i;
370   xbt_dictelm_t current, previous = NULL;
371    
372   if (dict->count == 0)
373     return;
374    
375   for (i = 0; i < dict->table_size; i++) {
376     current = dict->table[i];
377     while (current != NULL) {
378       previous = current;
379       current = current->next;
380       xbt_dictelm_free(previous);
381     }
382     dict->table[i] = NULL;
383   }
384
385   dict->count = 0;
386 }
387
388 /**
389  * \brief Return the number of elements in the dict.
390  * \param dict a dictionary
391  */
392 int xbt_dict_length(xbt_dict_t dict) {
393   xbt_assert(dict);
394
395   return dict->count;
396 }
397
398 /*
399  * Add an already mallocated element to a dictionary.
400  */
401 void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) {
402   xbt_assert(dict);
403
404   int hashcode = xbt_dict_hash_ext(element->key,element->key_len) % dict->table_size;
405   element->next = dict->table[hashcode];
406   dict->table[hashcode] = element;
407 }
408
409 /**
410  * \brief Outputs the content of the structure (debuging purpose) 
411  *
412  * \param dict the exibitionist
413  * \param output a function to dump each data in the tree
414  *
415  * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
416  * function to output the data. If NULL, data won't be displayed.
417  */
418
419 void xbt_dict_dump(xbt_dict_t     dict,
420                    void_f_pvoid_t *output) {
421   int i;
422   xbt_dictelm_t element;
423   printf("Dict %p:\n", dict);
424   if (dict != NULL) {
425     for (i = 0; i < dict->table_size; i++) {
426       element = dict->table[i];
427       while (element != NULL) {
428         printf("%s -> ", element->key);
429         if (output != NULL) {
430           output(element->content);
431         }
432         printf("\n");
433         element = element->next;
434       }
435     }
436   }
437 }
438
439 /**
440  * Destroy the dict mallocators.
441  * This is an internal XBT function called by xbt_exit().
442  */
443 void xbt_dict_exit(void) {
444   if (dict_mallocator != NULL) {
445     xbt_mallocator_free(dict_mallocator);
446     xbt_mallocator_free(dict_elm_mallocator);
447   }
448 }
449
450 static void* dict_mallocator_new_f(void) {
451   return xbt_new(s_xbt_dict_t, 1);
452 }
453
454 static void dict_mallocator_free_f(void* dict) {
455   xbt_free(dict);
456 }
457
458 static void dict_mallocator_reset_f(void* dict) {
459   /* nothing to do because all fields are
460    * initialized in xbt_dict_new
461    */
462 }
463
464 #ifdef SIMGRID_TEST
465 #include "xbt.h"
466 #include "xbt/ex.h"
467 #include "portable.h"
468
469 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
470 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
471
472 XBT_TEST_SUITE("dict","Dict data container");
473
474 static void print_str(void *str) {
475   printf("%s",(char*)PRINTF_STR(str));
476 }
477
478 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
479   char *data=xbt_strdup(data_to_fill);
480
481   xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
482
483   xbt_dict_set(head,key,data,&free);
484   if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
485     xbt_dict_dump(head,(void (*)(void*))&printf);
486     fflush(stdout);
487   }
488 }
489 static void debuged_add(xbt_dict_t head,const char*key) {
490    debuged_add_ext(head,key,key);
491 }
492
493 static void fill(xbt_dict_t *head) {
494   xbt_test_add0("Fill in the dictionnary");
495
496   *head = xbt_dict_new();
497   debuged_add(*head,"12");
498   debuged_add(*head,"12a");
499   debuged_add(*head,"12b");
500   debuged_add(*head,"123");
501   debuged_add(*head,"123456");
502   /* Child becomes child of what to add */
503   debuged_add(*head,"1234");
504   /* Need of common ancestor */
505   debuged_add(*head,"123457");
506 }
507
508
509 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
510   void *found;
511   
512   xbt_test_add1("Search %s",key);
513   found=xbt_dict_get(head,key);
514   xbt_test_log1("Found %s",(char *)found);
515   if (data)
516     xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
517   if (found)
518     xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
519 }
520
521 static void search(xbt_dict_t head,const char*key) {
522   search_ext(head,key,key);
523 }
524
525 static void debuged_remove(xbt_dict_t head,const char*key) {
526
527   xbt_test_add1("Remove '%s'",PRINTF_STR(key));
528   xbt_dict_remove(head,key);
529   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
530 }
531
532
533 static void traverse(xbt_dict_t head) {
534   xbt_dict_cursor_t cursor=NULL;
535   char *key;
536   char *data;
537
538   xbt_dict_foreach(head,cursor,key,data) {
539     xbt_test_log2("Seen:  %s->%s",PRINTF_STR(key),PRINTF_STR(data));
540     xbt_test_assert2(!data || !strcmp(key,data),
541                      "Key(%s) != value(%s). Abording\n",key,data);
542   }
543 }
544
545 static void search_not_found(xbt_dict_t head, const char *data) {
546   int ok=0;
547   xbt_ex_t e;
548
549   xbt_test_add1("Search %s (expected not to be found)",data);
550
551   TRY {    
552     data = xbt_dict_get(head, data);
553     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
554   } CATCH(e) {
555     if (e.category != not_found_error) 
556       xbt_test_exception(e);
557     xbt_ex_free(e);
558     ok=1;
559   }
560   xbt_test_assert0(ok,"Exception not raised");
561 }
562
563 static void count(xbt_dict_t dict, int length) {
564   xbt_test_add1("Count elements (expecting %d)", length);
565   xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
566 }
567
568 xbt_ex_t e;
569 xbt_dict_t head=NULL;
570 char *data;
571
572
573 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
574   xbt_test_add0("Traversal the null dictionnary");
575   traverse(head);
576
577   xbt_test_add0("Traversal and search the empty dictionnary");
578   head = xbt_dict_new();
579   traverse(head);
580   TRY {
581     debuged_remove(head,"12346");
582   } CATCH(e) {
583     if (e.category != not_found_error) 
584       xbt_test_exception(e);
585     xbt_ex_free(e);
586   }
587   xbt_dict_free(&head);
588
589   xbt_test_add0("Traverse the full dictionnary");
590   fill(&head);
591   count(head, 7);
592    
593   debuged_add_ext(head,"toto","tutu");
594   search_ext(head,"toto","tutu");
595   debuged_remove(head,"toto");
596
597   search(head,"12a");
598   traverse(head);
599
600   xbt_test_add0("Free the dictionnary (twice)");
601   xbt_dict_free(&head);
602   xbt_dict_free(&head);
603
604   /* CHANGING */
605   fill(&head);
606   count(head, 7);
607   xbt_test_add0("Change 123 to 'Changed 123'");
608   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
609   count(head, 7);
610
611   xbt_test_add0("Change 123 back to '123'");
612   xbt_dict_set(head,"123",strdup("123"),&free);
613   count(head, 7);
614
615   xbt_test_add0("Change 12a to 'Dummy 12a'");
616   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
617   count(head, 7);
618
619   xbt_test_add0("Change 12a to '12a'");
620   xbt_dict_set(head,"12a",strdup("12a"),&free);
621   count(head, 7);
622
623   xbt_test_add0("Traverse the resulting dictionnary");
624   traverse(head);
625   
626   /* RETRIEVE */
627   xbt_test_add0("Search 123");
628   data = xbt_dict_get(head,"123");
629   xbt_test_assert(data);
630   xbt_test_assert(!strcmp("123",data));
631
632   search_not_found(head,"Can't be found");
633   search_not_found(head,"123 Can't be found");
634   search_not_found(head,"12345678 NOT");
635
636   search(head,"12a");
637   search(head,"12b");
638   search(head,"12");
639   search(head,"123456");
640   search(head,"1234");
641   search(head,"123457");
642
643   xbt_test_add0("Traverse the resulting dictionnary");
644   traverse(head);
645
646   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
647
648   xbt_test_add0("Free the dictionnary twice");
649   xbt_dict_free(&head);
650   xbt_dict_free(&head);
651
652   xbt_test_add0("Traverse the resulting dictionnary");
653   traverse(head);
654 }
655
656 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
657   fill(&head);
658   count(head, 7);
659   xbt_test_add0("Remove non existing data");
660   TRY {
661     debuged_remove(head,"Does not exist");
662   } CATCH(e) {
663     if (e.category != not_found_error) 
664       xbt_test_exception(e);
665     xbt_ex_free(e);
666   }
667   traverse(head);
668
669   xbt_dict_free(&head);
670
671   xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
672   fill(&head);
673   debuged_remove(head,"12a");    traverse(head);
674   count(head, 6);
675   debuged_remove(head,"12b");    traverse(head);
676   count(head, 5);
677   debuged_remove(head,"12");     traverse(head);
678   count(head, 4);
679   debuged_remove(head,"123456"); traverse(head);
680   count(head, 3);
681   TRY {
682     debuged_remove(head,"12346");
683   } CATCH(e) {
684     if (e.category != not_found_error) 
685       xbt_test_exception(e);
686     xbt_ex_free(e);         
687     traverse(head);
688   } 
689   debuged_remove(head,"1234");   traverse(head);
690   debuged_remove(head,"123457"); traverse(head);
691   debuged_remove(head,"123");    traverse(head);
692   TRY {
693     debuged_remove(head,"12346");
694   } CATCH(e) {
695     if (e.category != not_found_error) 
696       xbt_test_exception(e);
697     xbt_ex_free(e);
698   }                              traverse(head);
699   
700   xbt_test_add0("Remove all values");
701   xbt_dict_free(&head);
702   fill(&head);
703   xbt_dict_reset(head);
704   count(head, 0);
705   traverse(head);
706
707   xbt_test_add0("Free the dictionnary twice");
708   xbt_dict_free(&head);
709   xbt_dict_free(&head);      
710 }
711
712 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
713   fill(&head);
714
715   xbt_test_add0("Store NULL under 'null'");
716   xbt_dict_set(head,"null",NULL,NULL);
717   search_ext(head,"null",NULL);
718
719   xbt_test_add0("Check whether I see it while traversing...");
720   {
721     xbt_dict_cursor_t cursor=NULL;
722     char *key;
723     int found=0;
724
725     xbt_dict_foreach(head,cursor,key,data) {
726       xbt_test_log2("Seen:  %s->%s",PRINTF_STR(key),PRINTF_STR(data));
727       if (!strcmp(key,"null"))
728         found = 1;
729     }
730     xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
731   }
732   xbt_dict_free(&head);
733 }
734
735 #define NB_ELM 20000
736 #define SIZEOFKEY 1024
737 static int countelems(xbt_dict_t head) {
738   xbt_dict_cursor_t cursor;
739   char *key;
740   void *data;
741   int res = 0;
742
743   xbt_dict_foreach(head,cursor,key,data) {
744     res++;
745   }
746   return res;
747 }
748
749 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
750   xbt_dict_t head=NULL;
751   int i,j,k, nb;
752   char *key;
753   void *data;
754
755   srand((unsigned int)time(NULL));
756
757   xbt_test_add0("CRASH test");
758   xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
759   xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
760
761   for (i=0;i<10;i++) {
762     head=xbt_dict_new();
763     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
764     nb=0;
765     for (j=0;j<1000;j++) {
766       key=xbt_malloc(SIZEOFKEY);
767
768       for (k=0;k<SIZEOFKEY-1;k++)
769         key[k]=rand() % ('z' - 'a') + 'a';
770       key[k]='\0';
771       /*      printf("[%d %s]\n",j,key); */
772       xbt_dict_set(head,key,key,&free);
773     }
774     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
775     nb = countelems(head);
776     xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
777     traverse(head);
778     xbt_dict_free(&head);
779     xbt_dict_free(&head);
780   }
781
782
783   head=xbt_dict_new();
784   xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
785   for (j=0;j<NB_ELM;j++) {
786     /* if (!(j%1000)) { printf("."); fflush(stdout); } */
787
788     key = xbt_malloc(10);
789     
790     sprintf(key,"%d",j);
791     xbt_dict_set(head,key,key,&free);
792   }
793
794   xbt_test_add0("Count the elements (retrieving the key and data for each)");
795   i = countelems(head);
796   xbt_test_log1("There is %d elements",i);
797
798   xbt_test_add1("Search my %d elements 20 times",NB_ELM);
799   key=xbt_malloc(10);
800   for (i=0;i<20;i++) {
801     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
802     for (j=0;j<NB_ELM;j++) {
803       
804       sprintf(key,"%d",j);
805       data = xbt_dict_get(head,key);
806       xbt_test_assert2(!strcmp(key,(char*)data),
807                        "key=%s != data=%s\n",key,(char*)data);
808     }
809   }
810   free(key);
811
812   xbt_test_add1("Remove my %d elements",NB_ELM);
813   key=xbt_malloc(10);
814   for (j=0;j<NB_ELM;j++) {
815     /* if (!(j%10000)) printf("."); fflush(stdout); */
816     
817     sprintf(key,"%d",j);
818     xbt_dict_remove(head,key);
819   }
820   free(key);
821
822   
823   xbt_test_add0("Free the structure (twice)");
824   xbt_dict_free(&head);
825   xbt_dict_free(&head);
826 }
827
828 static void str_free(void *s) {
829   char *c=*(char**)s;
830   free(c);
831 }
832
833 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
834
835 #undef NB_ELM
836 #define NB_ELM 100 /*00*/
837 #define DEPTH 5
838 #define KEY_SIZE 512
839 #define NB_TEST 20 /*20*/
840 int verbose=0;
841
842   xbt_dict_t mdict = NULL;
843   int i,j,k,l;
844   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
845   void *data;
846   char *key;
847
848
849   xbt_test_add0("Generic multicache CRASH test");
850   xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
851                 "depth of multicache=%d, key size=%d",
852                 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
853
854   for (l=0 ; l<DEPTH ; l++) {
855     key=xbt_malloc(KEY_SIZE);
856     xbt_dynar_push(keys,&key);
857   }     
858
859   for (i=0;i<NB_TEST;i++) {
860     mdict = xbt_dict_new();
861     VERB1("mdict=%p",mdict);
862     if (verbose>0)
863       printf("Test %d\n",i);
864     /* else if (i%10) printf("."); else printf("%d",i/10);*/
865     
866     for (j=0;j<NB_ELM;j++) {
867       if (verbose>0) printf ("  Add {");
868       
869       for (l=0 ; l<DEPTH ; l++) {
870         key=*(char**)xbt_dynar_get_ptr(keys,l);
871         
872         for (k=0;k<KEY_SIZE-1;k++) 
873           key[k]=rand() % ('z' - 'a') + 'a';
874           
875         key[k]='\0';
876         
877         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
878       }
879       if (verbose>0) printf("in multitree %p.\n",mdict);
880                                                         
881       xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
882
883       data = xbt_multidict_get(mdict,keys);
884
885       xbt_test_assert2(data && !strcmp((char*)data,key),
886                        "Retrieved value (%s) does not match the entrered one (%s)\n",
887                        (char*)data,key);
888     }
889     xbt_dict_free(&mdict);
890   }
891   
892   xbt_dynar_free(&keys);
893
894 /*  if (verbose>0)
895     xbt_dict_dump(mdict,&xbt_dict_print);*/
896     
897   xbt_dict_free(&mdict);
898   xbt_dynar_free(&keys);
899
900 }
901 #endif /* SIMGRID_TEST */