Logo AND Algorithmique Numérique Distribuée

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