Logo AND Algorithmique Numérique Distribuée

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