Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a dict_hash_ext function able to deal with non-null-terminated strings...
[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   for (i = 0; i < dict->table_size; i++) {
372     current = dict->table[i];
373     while (current != NULL) {
374       previous = current;
375       current = current->next;
376       xbt_dictelm_free(previous);
377     }
378     dict->table[i] = NULL;
379   }
380
381   dict->count = 0;
382 }
383
384 /**
385  * \brief Return the number of elements in the dict.
386  * \param dict a dictionary
387  */
388 int xbt_dict_length(xbt_dict_t dict) {
389   xbt_assert(dict);
390
391   return dict->count;
392 }
393
394 /*
395  * Add an already mallocated element to a dictionary.
396  */
397 void xbt_dict_add_element(xbt_dict_t dict, xbt_dictelm_t element) {
398   xbt_assert(dict);
399
400   int hashcode = xbt_dict_hash_ext(element->key,element->key_len) % dict->table_size;
401   element->next = dict->table[hashcode];
402   dict->table[hashcode] = element;
403 }
404
405 /**
406  * \brief Outputs the content of the structure (debuging purpose) 
407  *
408  * \param dict the exibitionist
409  * \param output a function to dump each data in the tree
410  *
411  * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
412  * function to output the data. If NULL, data won't be displayed.
413  */
414
415 void xbt_dict_dump(xbt_dict_t     dict,
416                    void_f_pvoid_t *output) {
417   int i;
418   xbt_dictelm_t element;
419   printf("Dict %p:\n", dict);
420   if (dict != NULL) {
421     for (i = 0; i < dict->table_size; i++) {
422       element = dict->table[i];
423       while (element != NULL) {
424         printf("%s -> ", element->key);
425         if (output != NULL) {
426           output(element->content);
427         }
428         printf("\n");
429         element = element->next;
430       }
431     }
432   }
433 }
434
435 /**
436  * Destroy the dict mallocators.
437  * This is an internal XBT function called by xbt_exit().
438  */
439 void xbt_dict_exit(void) {
440   if (dict_mallocator != NULL) {
441     xbt_mallocator_free(dict_mallocator);
442     xbt_mallocator_free(dict_elm_mallocator);
443   }
444 }
445
446 static void* dict_mallocator_new_f(void) {
447   return xbt_new(s_xbt_dict_t, 1);
448 }
449
450 static void dict_mallocator_free_f(void* dict) {
451   xbt_free(dict);
452 }
453
454 static void dict_mallocator_reset_f(void* dict) {
455   /* nothing to do because all fields are
456    * initialized in xbt_dict_new
457    */
458 }
459
460 #ifdef SIMGRID_TEST
461 #include "xbt.h"
462 #include "xbt/ex.h"
463 #include "portable.h"
464
465 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
466 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
467
468 XBT_TEST_SUITE("dict","Dict data container");
469
470 static void print_str(void *str) {
471   printf("%s",(char*)PRINTF_STR(str));
472 }
473
474 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
475   char *data=xbt_strdup(data_to_fill);
476
477   xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
478
479   xbt_dict_set(head,key,data,&free);
480   if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
481     xbt_dict_dump(head,(void (*)(void*))&printf);
482     fflush(stdout);
483   }
484 }
485 static void debuged_add(xbt_dict_t head,const char*key) {
486    debuged_add_ext(head,key,key);
487 }
488
489 static void fill(xbt_dict_t *head) {
490   xbt_test_add0("Fill in the dictionnary");
491
492   *head = xbt_dict_new();
493   debuged_add(*head,"12");
494   debuged_add(*head,"12a");
495   debuged_add(*head,"12b");
496   debuged_add(*head,"123");
497   debuged_add(*head,"123456");
498   /* Child becomes child of what to add */
499   debuged_add(*head,"1234");
500   /* Need of common ancestor */
501   debuged_add(*head,"123457");
502 }
503
504
505 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
506   void *found;
507   
508   xbt_test_add1("Search %s",key);
509   found=xbt_dict_get(head,key);
510   xbt_test_log1("Found %s",(char *)found);
511   if (data)
512     xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
513   if (found)
514     xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
515 }
516
517 static void search(xbt_dict_t head,const char*key) {
518   search_ext(head,key,key);
519 }
520
521 static void debuged_remove(xbt_dict_t head,const char*key) {
522
523   xbt_test_add1("Remove '%s'",PRINTF_STR(key));
524   xbt_dict_remove(head,key);
525   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
526 }
527
528
529 static void traverse(xbt_dict_t head) {
530   xbt_dict_cursor_t cursor=NULL;
531   char *key;
532   char *data;
533
534   xbt_dict_foreach(head,cursor,key,data) {
535     xbt_test_log2("Seen:  %s->%s",PRINTF_STR(key),PRINTF_STR(data));
536     xbt_test_assert2(!data || !strcmp(key,data),
537                      "Key(%s) != value(%s). Abording\n",key,data);
538   }
539 }
540
541 static void search_not_found(xbt_dict_t head, const char *data) {
542   int ok=0;
543   xbt_ex_t e;
544
545   xbt_test_add1("Search %s (expected not to be found)",data);
546
547   TRY {    
548     data = xbt_dict_get(head, data);
549     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
550   } CATCH(e) {
551     if (e.category != not_found_error) 
552       xbt_test_exception(e);
553     xbt_ex_free(e);
554     ok=1;
555   }
556   xbt_test_assert0(ok,"Exception not raised");
557 }
558
559 static void count(xbt_dict_t dict, int length) {
560   xbt_test_add1("Count elements (expecting %d)", length);
561   xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
562 }
563
564 xbt_ex_t e;
565 xbt_dict_t head=NULL;
566 char *data;
567
568
569 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
570   xbt_test_add0("Traversal the empty dictionnary");
571   traverse(head);
572
573   xbt_test_add0("Traverse the full dictionnary");
574   fill(&head);
575   count(head, 7);
576    
577   debuged_add_ext(head,"toto","tutu");
578   search_ext(head,"toto","tutu");
579   debuged_remove(head,"toto");
580
581   search(head,"12a");
582   traverse(head);
583
584   xbt_test_add0("Free the dictionnary (twice)");
585   xbt_dict_free(&head);
586   xbt_dict_free(&head);
587
588   /* CHANGING */
589   fill(&head);
590   count(head, 7);
591   xbt_test_add0("Change 123 to 'Changed 123'");
592   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
593   count(head, 7);
594
595   xbt_test_add0("Change 123 back to '123'");
596   xbt_dict_set(head,"123",strdup("123"),&free);
597   count(head, 7);
598
599   xbt_test_add0("Change 12a to 'Dummy 12a'");
600   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
601   count(head, 7);
602
603   xbt_test_add0("Change 12a to '12a'");
604   xbt_dict_set(head,"12a",strdup("12a"),&free);
605   count(head, 7);
606
607   xbt_test_add0("Traverse the resulting dictionnary");
608   traverse(head);
609   
610   /* RETRIEVE */
611   xbt_test_add0("Search 123");
612   data = xbt_dict_get(head,"123");
613   xbt_test_assert(data);
614   xbt_test_assert(!strcmp("123",data));
615
616   search_not_found(head,"Can't be found");
617   search_not_found(head,"123 Can't be found");
618   search_not_found(head,"12345678 NOT");
619
620   search(head,"12a");
621   search(head,"12b");
622   search(head,"12");
623   search(head,"123456");
624   search(head,"1234");
625   search(head,"123457");
626
627   xbt_test_add0("Traverse the resulting dictionnary");
628   traverse(head);
629
630   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
631
632   xbt_test_add0("Free the dictionnary twice");
633   xbt_dict_free(&head);
634   xbt_dict_free(&head);
635
636   xbt_test_add0("Traverse the resulting dictionnary");
637   traverse(head);
638 }
639
640 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
641   fill(&head);
642   count(head, 7);
643   xbt_test_add0("Remove non existing data");
644   TRY {
645     debuged_remove(head,"Does not exist");
646   } CATCH(e) {
647     if (e.category != not_found_error) 
648       xbt_test_exception(e);
649     xbt_ex_free(e);
650   }
651   traverse(head);
652
653   xbt_dict_free(&head);
654
655   xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
656   fill(&head);
657   debuged_remove(head,"12a");    traverse(head);
658   count(head, 6);
659   debuged_remove(head,"12b");    traverse(head);
660   count(head, 5);
661   debuged_remove(head,"12");     traverse(head);
662   count(head, 4);
663   debuged_remove(head,"123456"); traverse(head);
664   count(head, 3);
665   TRY {
666     debuged_remove(head,"12346");
667   } CATCH(e) {
668     if (e.category != not_found_error) 
669       xbt_test_exception(e);
670     xbt_ex_free(e);         
671     traverse(head);
672   } 
673   debuged_remove(head,"1234");   traverse(head);
674   debuged_remove(head,"123457"); traverse(head);
675   debuged_remove(head,"123");    traverse(head);
676   TRY {
677     debuged_remove(head,"12346");
678   } CATCH(e) {
679     if (e.category != not_found_error) 
680       xbt_test_exception(e);
681     xbt_ex_free(e);
682   }                              traverse(head);
683   
684   xbt_test_add0("Remove all values");
685   xbt_dict_free(&head);
686   fill(&head);
687   xbt_dict_reset(head);
688   count(head, 0);
689   traverse(head);
690
691   xbt_test_add0("Free the dictionnary twice");
692   xbt_dict_free(&head);
693   xbt_dict_free(&head);      
694 }
695
696 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
697   fill(&head);
698
699   xbt_test_add0("Store NULL under 'null'");
700   xbt_dict_set(head,"null",NULL,NULL);
701   search_ext(head,"null",NULL);
702
703   xbt_test_add0("Check whether I see it while traversing...");
704   {
705     xbt_dict_cursor_t cursor=NULL;
706     char *key;
707     int found=0;
708
709     xbt_dict_foreach(head,cursor,key,data) {
710       xbt_test_log2("Seen:  %s->%s",PRINTF_STR(key),PRINTF_STR(data));
711       if (!strcmp(key,"null"))
712         found = 1;
713     }
714     xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
715   }
716   xbt_dict_free(&head);
717 }
718
719 #define NB_ELM 20000
720 #define SIZEOFKEY 1024
721 static int countelems(xbt_dict_t head) {
722   xbt_dict_cursor_t cursor;
723   char *key;
724   void *data;
725   int res = 0;
726
727   xbt_dict_foreach(head,cursor,key,data) {
728     res++;
729   }
730   return res;
731 }
732
733 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
734   xbt_dict_t head=NULL;
735   int i,j,k, nb;
736   char *key;
737   void *data;
738
739   srand((unsigned int)time(NULL));
740
741   xbt_test_add0("CRASH test");
742   xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
743   xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
744
745   for (i=0;i<10;i++) {
746     head=xbt_dict_new();
747     //    if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
748     nb=0;
749     for (j=0;j<1000;j++) {
750       key=xbt_malloc(SIZEOFKEY);
751
752       for (k=0;k<SIZEOFKEY-1;k++)
753         key[k]=rand() % ('z' - 'a') + 'a';
754       key[k]='\0';
755       /*      printf("[%d %s]\n",j,key); */
756       xbt_dict_set(head,key,key,&free);
757     }
758     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
759     nb = countelems(head);
760     xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
761     traverse(head);
762     xbt_dict_free(&head);
763     xbt_dict_free(&head);
764   }
765
766
767   head=xbt_dict_new();
768   xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
769   for (j=0;j<NB_ELM;j++) {
770     //    if (!(j%1000)) { printf("."); fflush(stdout); }
771
772     key = xbt_malloc(10);
773     
774     sprintf(key,"%d",j);
775     xbt_dict_set(head,key,key,&free);
776   }
777
778   xbt_test_add0("Count the elements (retrieving the key and data for each)");
779   i = countelems(head);
780   xbt_test_log1("There is %d elements",i);
781
782   xbt_test_add1("Search my %d elements 20 times",NB_ELM);
783   key=xbt_malloc(10);
784   for (i=0;i<20;i++) {
785     //    if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
786     for (j=0;j<NB_ELM;j++) {
787       
788       sprintf(key,"%d",j);
789       data = xbt_dict_get(head,key);
790       xbt_test_assert2(!strcmp(key,(char*)data),
791                        "key=%s != data=%s\n",key,(char*)data);
792     }
793   }
794   free(key);
795
796   xbt_test_add1("Remove my %d elements",NB_ELM);
797   key=xbt_malloc(10);
798   for (j=0;j<NB_ELM;j++) {
799     //if (!(j%10000)) printf("."); fflush(stdout);
800     
801     sprintf(key,"%d",j);
802     xbt_dict_remove(head,key);
803   }
804   free(key);
805
806   
807   xbt_test_add0("Free the structure (twice)");
808   xbt_dict_free(&head);
809   xbt_dict_free(&head);
810 }
811
812 static void str_free(void *s) {
813   char *c=*(char**)s;
814   free(c);
815 }
816
817 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
818
819 #undef NB_ELM
820 #define NB_ELM 100 /*00*/
821 #define DEPTH 5
822 #define KEY_SIZE 512
823 #define NB_TEST 20 /*20*/
824 int verbose=0;
825
826   xbt_dict_t mdict = NULL;
827   int i,j,k,l;
828   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
829   void *data;
830   char *key;
831
832
833   xbt_test_add0("Generic multicache CRASH test");
834   xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
835                 "depth of multicache=%d, key size=%d",
836                 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
837
838   for (l=0 ; l<DEPTH ; l++) {
839     key=xbt_malloc(KEY_SIZE);
840     xbt_dynar_push(keys,&key);
841   }     
842
843   for (i=0;i<NB_TEST;i++) {
844     mdict = xbt_dict_new();
845     VERB1("mdict=%p",mdict);
846     if (verbose>0)
847       printf("Test %d\n",i);
848     /* else if (i%10) printf("."); else printf("%d",i/10);*/
849     
850     for (j=0;j<NB_ELM;j++) {
851       if (verbose>0) printf ("  Add {");
852       
853       for (l=0 ; l<DEPTH ; l++) {
854         key=*(char**)xbt_dynar_get_ptr(keys,l);
855         
856         for (k=0;k<KEY_SIZE-1;k++) 
857           key[k]=rand() % ('z' - 'a') + 'a';
858           
859         key[k]='\0';
860         
861         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
862       }
863       if (verbose>0) printf("in multitree %p.\n",mdict);
864                                                         
865       xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
866
867       data = xbt_multidict_get(mdict,keys);
868
869       xbt_test_assert2(data && !strcmp((char*)data,key),
870                        "Retrieved value (%s) does not match the entrered one (%s)\n",
871                        (char*)data,key);
872     }
873     xbt_dict_free(&mdict);
874   }
875   
876   xbt_dynar_free(&keys);
877
878 /*  if (verbose>0)
879     xbt_dict_dump(mdict,&xbt_dict_print);*/
880     
881   xbt_dict_free(&mdict);
882   xbt_dynar_free(&keys);
883
884 }
885 #endif /* SIMGRID_TEST */