Logo AND Algorithmique Numérique Distribuée

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