Logo AND Algorithmique Numérique Distribuée

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