Logo AND Algorithmique Numérique Distribuée

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