Logo AND Algorithmique Numérique Distribuée

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