Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill xbt_os_thread_atfork: tesh is not native but python now
[simgrid.git] / src / xbt / dict.cpp
1 /* dict - a generic dictionary, variation over hash table                   */
2
3 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/dict.h"
9 #include "dict_private.h"
10 #include "simgrid/Exception.hpp"
11 #include "src/xbt_modinter.h"
12 #include "xbt/ex.h"
13 #include "xbt/log.h"
14 #include "xbt/mallocator.h"
15 #include "xbt/str.h"
16
17 #include <cstdio>
18 #include <cstring>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
21
22 /**
23  * @brief Constructor
24  * @param free_ctn function to call with (@a data as argument) when @a data is removed from the dictionary
25  * @return pointer to the destination
26  * @see xbt_dict_free()
27  *
28  * Creates and initialize a new dictionary with a default hashtable size.
29  * The dictionary is homogeneous: each element share the same free function.
30  */
31 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
32 {
33   if (dict_elm_mallocator == nullptr)
34     xbt_dict_preinit();
35
36   xbt_dict_t dict;
37
38   dict = xbt_new(s_xbt_dict_t, 1);
39   dict->free_f = free_ctn;
40   dict->table_size = 127;
41   dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
42   dict->count = 0;
43   dict->fill = 0;
44
45   return dict;
46 }
47
48 /**
49  * @brief Destructor
50  * @param dict the dictionary to be freed
51  *
52  * Frees a dictionary with all the data
53  */
54 void xbt_dict_free(xbt_dict_t * dict)
55 {
56   if (dict != nullptr && *dict != nullptr) {
57     int table_size       = (*dict)->table_size;
58     xbt_dictelm_t* table = (*dict)->table;
59     /* Warning: the size of the table is 'table_size+1'...
60      * This is because table_size is used as a binary mask in xbt_dict_rehash */
61     for (int i = 0; (*dict)->count && i <= table_size; i++) {
62       xbt_dictelm_t current = table[i];
63       xbt_dictelm_t previous;
64
65       while (current != nullptr) {
66         previous = current;
67         current = current->next;
68         xbt_dictelm_free(*dict, previous);
69         (*dict)->count--;
70       }
71     }
72     xbt_free(table);
73     xbt_free(*dict);
74     *dict = nullptr;
75   }
76 }
77
78 /** Returns the amount of elements in the dict */
79 unsigned int xbt_dict_size(xbt_dict_t dict)
80 {
81   return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
82 }
83
84 /* Expend the size of the dict */
85 static void xbt_dict_rehash(xbt_dict_t dict)
86 {
87   const unsigned oldsize = dict->table_size + 1;
88   unsigned newsize = oldsize * 2;
89
90   xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
91   memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t));       /* zero second half */
92   newsize--;
93   dict->table_size = newsize;
94   dict->table = currcell;
95   XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
96
97   for (unsigned i = 0; i < oldsize; i++, currcell++) {
98     if (*currcell == nullptr) /* empty cell */
99       continue;
100
101     xbt_dictelm_t *twincell = currcell + oldsize;
102     xbt_dictelm_t *pprev = currcell;
103     xbt_dictelm_t bucklet = *currcell;
104     for (; bucklet != nullptr; bucklet = *pprev) {
105       /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
106          - stay  in  cell i (ie, currcell)
107          - go to the cell i+oldsize (ie, twincell) */
108       if ((bucklet->hash_code & newsize) != i) {        /* Move to b */
109         *pprev = bucklet->next;
110         bucklet->next = *twincell;
111         if (*twincell == nullptr)
112           dict->fill++;
113         *twincell = bucklet;
114       } else {
115         pprev = &bucklet->next;
116       }
117     }
118
119     if (*currcell == nullptr) /* everything moved */
120       dict->fill--;
121   }
122 }
123
124 /**
125  * @brief Add data to the dict (arbitrary key)
126  * @param dict the container
127  * @param key the key to set the new data
128  * @param key_len the size of the @a key
129  * @param data the data to add in the dict
130  * @param free_ctn unused parameter (kept for compatibility)
131  *
132  * Set the @a data in the structure under the @a key, which can be any kind of data, as long as its length is provided
133  * in @a key_len.
134  */
135 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data,
136                       XBT_ATTRIB_UNUSED void_f_pvoid_t free_ctn)
137 {
138   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
139
140   xbt_dictelm_t current;
141   xbt_dictelm_t previous = nullptr;
142
143   XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
144              dict->table_size, hash_code & dict->table_size);
145   current = dict->table[hash_code & dict->table_size];
146   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
147           || memcmp(key, current->key, key_len))) {
148     previous = current;
149     current = current->next;
150   }
151
152   if (current == nullptr) {
153     /* this key doesn't exist yet */
154     current = xbt_dictelm_new(key, key_len, hash_code, data);
155     dict->count++;
156     if (previous == nullptr) {
157       dict->table[hash_code & dict->table_size] = current;
158       dict->fill++;
159       if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
160         xbt_dict_rehash(dict);
161     } else {
162       previous->next = current;
163     }
164   } else {
165     XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
166                key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
167     /* there is already an element with the same key: overwrite it */
168     xbt_dictelm_set_data(dict, current, data);
169   }
170 }
171
172 /**
173  * @brief Add data to the dict (null-terminated key)
174  *
175  * @param dict the dict
176  * @param key the key to set the new data
177  * @param data the data to add in the dict
178  * @param free_ctn unused parameter (kept for compatibility)
179  *
180  * set the @a data in the structure under the @a key, which is anull terminated string.
181  */
182 void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
183 {
184   xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
185 }
186
187 /**
188  * @brief Retrieve data from the dict (arbitrary key)
189  *
190  * @param dict the dealer of data
191  * @param key the key to find data
192  * @param key_len the size of the @a key
193  * @return the data that we are looking for
194  *
195  * Search the given @a key. Throws not_found_error when not found.
196  */
197 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
198 {
199   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
200   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
201
202   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
203           || memcmp(key, current->key, key_len))) {
204     current = current->next;
205   }
206
207   if (current == nullptr)
208     THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
209
210   return current->content;
211 }
212
213 /** @brief like xbt_dict_get_ext(), but returning nullptr when not found */
214 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
215 {
216   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
217   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
218
219   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
220           || memcmp(key, current->key, key_len))) {
221     current = current->next;
222   }
223
224   if (current == nullptr)
225     return nullptr;
226
227   return current->content;
228 }
229
230 /**
231  * @brief retrieve the key associated to that object. Warning, that's a linear search
232  *
233  * Returns nullptr if the object cannot be found
234  */
235 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
236 {
237   for (int i = 0; i <= dict->table_size; i++) {
238     xbt_dictelm_t current = dict->table[i];
239     while (current != nullptr) {
240       if (current->content == data)
241         return current->key;
242       current = current->next;
243     }
244   }
245   return nullptr;
246 }
247
248 /**
249  * @brief Retrieve data from the dict (null-terminated key)
250  *
251  * @param dict the dealer of data
252  * @param key the key to find data
253  * @return the data that we are looking for
254  *
255  * Search the given @a key. Throws not_found_error when not found.
256  * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
257  */
258 void *xbt_dict_get(xbt_dict_t dict, const char *key)
259 {
260   return xbt_dict_get_elm(dict, key)->content;
261 }
262
263 /**
264  * @brief Retrieve element from the dict (null-terminated key)
265  *
266  * @param dict the dealer of data
267  * @param key the key to find data
268  * @return the s_xbt_dictelm_t that we are looking for
269  *
270  * Search the given @a key. Throws not_found_error when not found.
271  * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
272  */
273 xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
274 {
275   xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
276
277   if (current == nullptr)
278     THROWF(not_found_error, 0, "key %s not found", key);
279
280   return current;
281 }
282
283 /**
284  * @brief like xbt_dict_get(), but returning nullptr when not found
285  */
286 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
287 {
288   xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
289
290   if (current == nullptr)
291     return nullptr;
292
293   return current->content;
294 }
295
296 /**
297  * @brief like xbt_dict_get_elm(), but returning nullptr when not found
298  */
299 xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
300 {
301   unsigned int hash_code = xbt_str_hash(key);
302   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
303
304   while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
305     current = current->next;
306   return current;
307 }
308
309 /**
310  * @brief Remove data from the dict (arbitrary key)
311  *
312  * @param dict the trash can
313  * @param key the key of the data to be removed
314  * @param key_len the size of the @a key
315  *
316  * Remove the entry associated with the given @a key (throws not_found)
317  */
318 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
319 {
320   unsigned int hash_code = xbt_str_hash_ext(key, key_len);
321   xbt_dictelm_t previous = nullptr;
322   xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
323
324   while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
325           || strncmp(key, current->key, key_len))) {
326     previous = current;         /* save the previous node */
327     current = current->next;
328   }
329
330   if (current == nullptr)
331     THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
332   else {
333     if (previous != nullptr) {
334       previous->next = current->next;
335     } else {
336       dict->table[hash_code & dict->table_size] = current->next;
337     }
338   }
339
340   if (not dict->table[hash_code & dict->table_size])
341     dict->fill--;
342
343   xbt_dictelm_free(dict, 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, const char *key)
356 {
357   xbt_dict_remove_ext(dict, key, strlen(key));
358 }
359
360 /** @brief Remove all data from the dict */
361 void xbt_dict_reset(xbt_dict_t dict)
362 {
363   if (dict->count == 0)
364     return;
365
366   for (int i = 0; i <= dict->table_size; i++) {
367     xbt_dictelm_t previous = nullptr;
368     xbt_dictelm_t current = dict->table[i];
369     while (current != nullptr) {
370       previous = current;
371       current = current->next;
372       xbt_dictelm_free(dict, previous);
373     }
374     dict->table[i] = nullptr;
375   }
376
377   dict->count = 0;
378   dict->fill = 0;
379 }
380
381 /**
382  * @brief Return the number of elements in the dict.
383  * @param dict a dictionary
384  */
385 int xbt_dict_length(xbt_dict_t dict)
386 {
387   return dict->count;
388 }
389
390 /**
391  * @brief test if the dict is empty or not
392  */
393 int xbt_dict_is_empty(xbt_dict_t dict)
394 {
395   return not dict || (xbt_dict_length(dict) == 0);
396 }
397
398 /**
399  * @brief Outputs the content of the structure (debugging purpose)
400  *
401  * @param dict the exibitionist
402  * @param output a function to dump each data in the tree
403  *
404  * Outputs the content of the structure. (for debugging purpose).
405  * @a output is a function to output the data. If nullptr, data won't be displayed.
406  */
407 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
408 {
409   xbt_dictelm_t element;
410   printf("Dict %p:\n", dict);
411   if (dict != nullptr) {
412     for (int i = 0; i < dict->table_size; i++) {
413       element = dict->table[i];
414       if (element) {
415         printf("[\n");
416         while (element != nullptr) {
417           printf(" %s -> '", element->key);
418           if (output != nullptr) {
419             output(element->content);
420           }
421           printf("'\n");
422           element = element->next;
423         }
424         printf("]\n");
425       } else {
426         printf("[]\n");
427       }
428     }
429   }
430 }
431
432 /**
433  * Create the dict mallocators.
434  * This is an internal XBT function called during the lib initialization.
435  * It can be used several times to recreate the mallocator, for example when you switch to MC mode
436  */
437 void xbt_dict_preinit()
438 {
439   if (dict_elm_mallocator == nullptr)
440     dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
441       dict_elm_mallocator_reset_f);
442 }
443
444 /**
445  * Destroy the dict mallocators.
446  * This is an internal XBT function during the lib initialization
447  */
448 void xbt_dict_postexit()
449 {
450   if (dict_elm_mallocator != nullptr) {
451     xbt_mallocator_free(dict_elm_mallocator);
452     dict_elm_mallocator = nullptr;
453   }
454 }
455
456 #ifdef SIMGRID_TEST
457 #include "simgrid/Exception.hpp"
458 #include "src/internal_config.h"
459 #include "xbt.h"
460 #include "xbt/ex.h"
461 #include <ctime>
462
463 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
464
465 XBT_TEST_SUITE("dict", "Dict data container");
466
467 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
468 {
469   char *data = xbt_strdup(data_to_fill);
470
471   xbt_test_log("Add %s under %s", data_to_fill, key);
472
473   xbt_dict_set(head, key, data, nullptr);
474   if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
475     xbt_dict_dump(head, [](void* s) { fputs((char*)s, stdout); });
476     fflush(stdout);
477   }
478 }
479
480 static void debugged_add(xbt_dict_t head, const char* key)
481 {
482   debugged_add_ext(head, key, key);
483 }
484
485 static xbt_dict_t new_fixture()
486 {
487   xbt_test_add("Fill in the dictionnary");
488
489   xbt_dict_t head = xbt_dict_new_homogeneous(&free);
490   debugged_add(head, "12");
491   debugged_add(head, "12a");
492   debugged_add(head, "12b");
493   debugged_add(head, "123");
494   debugged_add(head, "123456");
495   debugged_add(head, "1234");
496   debugged_add(head, "123457");
497
498   return head;
499 }
500
501 static void search_ext(xbt_dict_t head, const char *key, const char *data)
502 {
503   xbt_test_add("Search %s", key);
504   char *found = (char*) xbt_dict_get(head, key);
505   xbt_test_log("Found %s", found);
506   if (data) {
507     xbt_test_assert(found, "data do not match expectations: found nullptr while searching for %s", data);
508     if (found)
509       xbt_test_assert(not strcmp(data, found), "data do not match expectations: found %s while searching for %s", found,
510                       data);
511   } else {
512     xbt_test_assert(not found, "data do not match expectations: found %s while searching for nullptr", found);
513   }
514 }
515
516 static void search(xbt_dict_t head, const char *key)
517 {
518   search_ext(head, key, key);
519 }
520
521 static void debugged_remove(xbt_dict_t head, const char* key)
522 {
523   xbt_test_add("Remove '%s'", key);
524   xbt_dict_remove(head, key);
525 }
526
527 static void traverse(xbt_dict_t head)
528 {
529   xbt_dict_cursor_t cursor = nullptr;
530   char *key;
531   char *data;
532   int i = 0;
533
534   xbt_dict_foreach(head, cursor, key, data) {
535     if (not key || not data || strcmp(key, data)) {
536       xbt_test_log("Seen #%d:  %s->%s", ++i, key, data);
537     } else {
538       xbt_test_log("Seen #%d:  %s", ++i, key);
539     }
540     xbt_test_assert(key && data && strcmp(key, data) == 0, "Key(%s) != value(%s). Aborting", key, data);
541   }
542 }
543
544 static void search_not_found(xbt_dict_t head, const char *data)
545 {
546   int ok = 0;
547   xbt_test_add("Search %s (expected not to be found)", data);
548
549   try {
550     data = (const char*) xbt_dict_get(head, data);
551     THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
552   }
553   catch(xbt_ex& e) {
554     if (e.category != not_found_error)
555       xbt_test_exception(e);
556     ok = 1;
557   }
558   xbt_test_assert(ok, "Exception not raised");
559 }
560
561 static void count(xbt_dict_t dict, int length)
562 {
563   xbt_test_add("Count elements (expecting %d)", length);
564   xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
565
566   xbt_dict_cursor_t cursor;
567   char *key;
568   void *data;
569   int effective = 0;
570   xbt_dict_foreach(dict, cursor, key, data)
571       effective++;
572
573   xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
574 }
575
576 static void count_check_get_key(xbt_dict_t dict, int length)
577 {
578   xbt_dict_cursor_t cursor;
579   char *key;
580   void *data;
581   int effective = 0;
582
583   xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
584   xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
585
586   xbt_dict_foreach(dict, cursor, key, data) {
587     effective++;
588     char* key2 = xbt_dict_get_key(dict, data);
589     xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
590   }
591
592   xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
593 }
594
595 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve and traverse homogeneous dicts")
596 {
597   xbt_test_add("Traversal the null dictionary");
598   traverse(nullptr);
599
600   xbt_test_add("Traversal and search the empty dictionary");
601   xbt_dict_t head = xbt_dict_new_homogeneous(&free);
602   traverse(head);
603   try {
604     debugged_remove(head, "12346");
605   }
606   catch(xbt_ex& e) {
607     if (e.category != not_found_error)
608       xbt_test_exception(e);
609   }
610   xbt_dict_free(&head);
611
612   xbt_test_add("Traverse the full dictionary");
613   head = new_fixture();
614   count_check_get_key(head, 7);
615
616   debugged_add_ext(head, "toto", "tutu");
617   search_ext(head, "toto", "tutu");
618   debugged_remove(head, "toto");
619
620   search(head, "12a");
621   traverse(head);
622
623   xbt_test_add("Free the dictionary (twice)");
624   xbt_dict_free(&head);
625   xbt_dict_free(&head);
626
627   /* CHANGING */
628   head = new_fixture();
629   count_check_get_key(head, 7);
630   xbt_test_add("Change 123 to 'Changed 123'");
631   xbt_dict_set(head, "123", xbt_strdup("Changed 123"), nullptr);
632   count_check_get_key(head, 7);
633
634   xbt_test_add("Change 123 back to '123'");
635   xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
636   count_check_get_key(head, 7);
637
638   xbt_test_add("Change 12a to 'Dummy 12a'");
639   xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), nullptr);
640   count_check_get_key(head, 7);
641
642   xbt_test_add("Change 12a to '12a'");
643   xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
644   count_check_get_key(head, 7);
645
646   xbt_test_add("Traverse the resulting dictionary");
647   traverse(head);
648
649   /* RETRIEVE */
650   xbt_test_add("Search 123");
651   char* data = (char*)xbt_dict_get(head, "123");
652   xbt_test_assert(data && strcmp("123", data) == 0);
653
654   search_not_found(head, "Can't be found");
655   search_not_found(head, "123 Can't be found");
656   search_not_found(head, "12345678 NOT");
657
658   search(head, "12a");
659   search(head, "12b");
660   search(head, "12");
661   search(head, "123456");
662   search(head, "1234");
663   search(head, "123457");
664
665   xbt_test_add("Traverse the resulting dictionary");
666   traverse(head);
667
668   xbt_test_add("Free the dictionary twice");
669   xbt_dict_free(&head);
670   xbt_dict_free(&head);
671
672   xbt_test_add("Traverse the resulting dictionary");
673   traverse(head);
674 }
675
676 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove, "Removing some values from homogeneous dicts")
677 {
678   xbt_dict_t head = new_fixture();
679   count(head, 7);
680   xbt_test_add("Remove non existing data");
681   try {
682     debugged_remove(head, "Does not exist");
683   }
684   catch(xbt_ex& e) {
685     if (e.category != not_found_error)
686       xbt_test_exception(e);
687   }
688   traverse(head);
689
690   xbt_dict_free(&head);
691
692   xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
693   head = new_fixture();
694   debugged_remove(head, "12a");
695   traverse(head);
696   count(head, 6);
697   debugged_remove(head, "12b");
698   traverse(head);
699   count(head, 5);
700   debugged_remove(head, "12");
701   traverse(head);
702   count(head, 4);
703   debugged_remove(head, "123456");
704   traverse(head);
705   count(head, 3);
706   try {
707     debugged_remove(head, "12346");
708   }
709   catch(xbt_ex& e) {
710     if (e.category != not_found_error)
711       xbt_test_exception(e);
712     traverse(head);
713   }
714   debugged_remove(head, "1234");
715   traverse(head);
716   debugged_remove(head, "123457");
717   traverse(head);
718   debugged_remove(head, "123");
719   traverse(head);
720   try {
721     debugged_remove(head, "12346");
722   }
723   catch(xbt_ex& e) {
724     if (e.category != not_found_error)
725       xbt_test_exception(e);
726   }
727   traverse(head);
728
729   xbt_test_add("Free dict, create new fresh one, and then reset the dict");
730   xbt_dict_free(&head);
731   head = new_fixture();
732   xbt_dict_reset(head);
733   count(head, 0);
734   traverse(head);
735
736   xbt_test_add("Free the dictionary twice");
737   xbt_dict_free(&head);
738   xbt_dict_free(&head);
739 }
740
741 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "nullptr data management")
742 {
743   xbt_dict_t head = new_fixture();
744
745   xbt_test_add("Store nullptr under 'null'");
746   xbt_dict_set(head, "null", nullptr, nullptr);
747   search_ext(head, "null", nullptr);
748
749   xbt_test_add("Check whether I see it while traversing...");
750   {
751     xbt_dict_cursor_t cursor = nullptr;
752     char *key;
753     int found = 0;
754     char* data;
755
756     xbt_dict_foreach(head, cursor, key, data) {
757       if (not key || not data || strcmp(key, data)) {
758         xbt_test_log("Seen:  %s->%s", key, data);
759       } else {
760         xbt_test_log("Seen:  %s", key);
761       }
762
763       if (key && strcmp(key, "null") == 0)
764         found = 1;
765     }
766     xbt_test_assert(found, "the key 'null', associated to nullptr is not found");
767   }
768   xbt_dict_free(&head);
769 }
770
771 #define NB_ELM 20000
772 #define SIZEOFKEY 1024
773 static int countelems(xbt_dict_t head)
774 {
775   xbt_dict_cursor_t cursor;
776   char *key;
777   void *data;
778   int res = 0;
779
780   xbt_dict_foreach(head, cursor, key, data) {
781     res++;
782   }
783   return res;
784 }
785
786 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
787 {
788   srand((unsigned int) time(nullptr));
789
790   for (int i = 0; i < 10; i++) {
791     xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
792     xbt_test_log("Fill the struct, count its elems and frees the structure");
793     xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
794     xbt_dict_t head = xbt_dict_new_homogeneous(free);
795     for (int j = 0; j < 1000; j++) {
796       char* data = nullptr;
797       char* key  = (char*)xbt_malloc(SIZEOFKEY);
798
799       do {
800         for (int k         = 0; k < SIZEOFKEY - 1; k++)
801           key[k] = rand() % ('z' - 'a') + 'a';
802         key[SIZEOFKEY - 1] = '\0';
803         data = (char*) xbt_dict_get_or_null(head, key);
804       } while (data != nullptr);
805
806       xbt_dict_set(head, key, key, nullptr);
807       data = (char*) xbt_dict_get(head, key);
808       xbt_test_assert(not strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
809
810       count(head, j + 1);
811     }
812     traverse(head);
813     xbt_dict_free(&head);
814     xbt_dict_free(&head);
815   }
816
817   xbt_dict_t head = xbt_dict_new_homogeneous(&free);
818   xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
819   for (int j = 0; j < NB_ELM; j++) {
820     char* key = (char*)xbt_malloc(10);
821
822     snprintf(key,10, "%d", j);
823     xbt_dict_set(head, key, key, nullptr);
824   }
825
826   xbt_test_add("Count the elements (retrieving the key and data for each)");
827   xbt_test_log("There is %d elements", countelems(head));
828
829   xbt_test_add("Search my %d elements 20 times", NB_ELM);
830   char* key = (char*)xbt_malloc(10);
831   for (int i = 0; i < 20; i++) {
832     for (int j = 0; j < NB_ELM; j++) {
833       snprintf(key,10, "%d", j);
834       void* data = xbt_dict_get(head, key);
835       xbt_test_assert(not strcmp(key, (char*)data), "with get, key=%s != data=%s", key, (char*)data);
836       data = xbt_dict_get_ext(head, key, strlen(key));
837       xbt_test_assert(not strcmp(key, (char*)data), "with get_ext, key=%s != data=%s", key, (char*)data);
838     }
839   }
840   free(key);
841
842   xbt_test_add("Remove my %d elements", NB_ELM);
843   key = (char*) xbt_malloc(10);
844   for (int j = 0; j < NB_ELM; j++) {
845     snprintf(key,10, "%d", j);
846     xbt_dict_remove(head, key);
847   }
848   free(key);
849
850   xbt_test_add("Free the object (twice)");
851   xbt_dict_free(&head);
852   xbt_dict_free(&head);
853 }
854
855 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
856 {
857   xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
858   int count = 500;
859
860   xbt_test_add("Insert elements");
861   for (int i = 0; i < count; ++i)
862     xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, nullptr);
863   xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Bad number of elements in the dictionnary");
864
865   xbt_test_add("Check elements");
866   for (int i = 0; i < count; ++i) {
867     int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
868     xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
869   }
870
871   xbt_test_add("Free the array");
872   xbt_dict_free(&dict);
873 }
874 #endif                          /* SIMGRID_TEST */