1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2018. The SimGrid Team.
4 * All rights reserved. */
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. */
16 #include "xbt/mallocator.h"
17 #include "src/xbt_modinter.h"
19 #include "dict_private.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "Dictionaries provide the same functionalities as hash tables");
25 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary
26 * \return pointer to the destination
27 * \see xbt_dict_free()
29 * Creates and initialize a new dictionary with a default hashtable size.
30 * The dictionary is homogeneous: each element share the same free function.
32 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
34 if (dict_elm_mallocator == nullptr)
39 dict = xbt_new(s_xbt_dict_t, 1);
40 dict->free_f = free_ctn;
41 dict->table_size = 127;
42 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
51 * \param dict the dictionary to be freed
53 * Frees a dictionary with all the data
55 void xbt_dict_free(xbt_dict_t * dict)
57 if (dict != nullptr && *dict != nullptr) {
58 int table_size = (*dict)->table_size;
59 xbt_dictelm_t* table = (*dict)->table;
60 /* Warning: the size of the table is 'table_size+1'...
61 * This is because table_size is used as a binary mask in xbt_dict_rehash */
62 for (int i = 0; (*dict)->count && i <= table_size; i++) {
63 xbt_dictelm_t current = table[i];
64 xbt_dictelm_t previous;
66 while (current != nullptr) {
68 current = current->next;
69 xbt_dictelm_free(*dict, previous);
79 /** Returns the amount of elements in the dict */
80 unsigned int xbt_dict_size(xbt_dict_t dict)
82 return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
85 /* Expend the size of the dict */
86 static void xbt_dict_rehash(xbt_dict_t dict)
88 const unsigned oldsize = dict->table_size + 1;
89 unsigned newsize = oldsize * 2;
91 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
92 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
94 dict->table_size = newsize;
95 dict->table = currcell;
96 XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
98 for (unsigned i = 0; i < oldsize; i++, currcell++) {
99 if (*currcell == nullptr) /* empty cell */
102 xbt_dictelm_t *twincell = currcell + oldsize;
103 xbt_dictelm_t *pprev = currcell;
104 xbt_dictelm_t bucklet = *currcell;
105 for (; bucklet != nullptr; bucklet = *pprev) {
106 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
107 - stay in cell i (ie, currcell)
108 - go to the cell i+oldsize (ie, twincell) */
109 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
110 *pprev = bucklet->next;
111 bucklet->next = *twincell;
112 if (*twincell == nullptr)
116 pprev = &bucklet->next;
120 if (*currcell == nullptr) /* everything moved */
126 * \brief Add data to the dict (arbitrary key)
127 * \param dict the container
128 * \param key the key to set the new data
129 * \param key_len the size of the \a key
130 * \param data the data to add in the dict
131 * \param free_ctn unused parameter (kept for compatibility)
133 * 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
136 void xbt_dict_set_ext(xbt_dict_t dict, const char* key, int key_len, void* data,
137 XBT_ATTRIB_UNUSED void_f_pvoid_t free_ctn)
139 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
141 xbt_dictelm_t current;
142 xbt_dictelm_t previous = nullptr;
144 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
145 dict->table_size, hash_code & dict->table_size);
146 current = dict->table[hash_code & dict->table_size];
147 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
148 || memcmp(key, current->key, key_len))) {
150 current = current->next;
153 if (current == nullptr) {
154 /* this key doesn't exist yet */
155 current = xbt_dictelm_new(key, key_len, hash_code, data);
157 if (previous == nullptr) {
158 dict->table[hash_code & dict->table_size] = current;
160 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
161 xbt_dict_rehash(dict);
163 previous->next = current;
166 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
167 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
168 /* there is already an element with the same key: overwrite it */
169 xbt_dictelm_set_data(dict, current, data);
174 * \brief Add data to the dict (null-terminated key)
176 * \param dict the dict
177 * \param key the key to set the new data
178 * \param data the data to add in the dict
179 * \param free_ctn unused parameter (kept for compatibility)
181 * set the \a data in the structure under the \a key, which is anull terminated string.
183 void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
185 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
189 * \brief Retrieve data from the dict (arbitrary key)
191 * \param dict the dealer of data
192 * \param key the key to find data
193 * \param key_len the size of the \a key
194 * \return the data that we are looking for
196 * Search the given \a key. Throws not_found_error when not found.
198 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
200 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
201 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
203 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
204 || memcmp(key, current->key, key_len))) {
205 current = current->next;
208 if (current == nullptr)
209 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
211 return current->content;
214 /** @brief like xbt_dict_get_ext(), but returning nullptr when not found */
215 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
217 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
218 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
220 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
221 || memcmp(key, current->key, key_len))) {
222 current = current->next;
225 if (current == nullptr)
228 return current->content;
232 * @brief retrieve the key associated to that object. Warning, that's a linear search
234 * Returns nullptr if the object cannot be found
236 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
238 for (int i = 0; i <= dict->table_size; i++) {
239 xbt_dictelm_t current = dict->table[i];
240 while (current != nullptr) {
241 if (current->content == data)
243 current = current->next;
249 /** @brief retrieve the key associated to that xbt_dictelm_t. */
250 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
256 * \brief Retrieve data from the dict (null-terminated key)
258 * \param dict the dealer of data
259 * \param key the key to find data
260 * \return the data that we are looking for
262 * Search the given \a key. Throws not_found_error when not found.
263 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
265 void *xbt_dict_get(xbt_dict_t dict, const char *key)
267 return xbt_dict_get_elm(dict, key)->content;
271 * \brief Retrieve element from the dict (null-terminated key)
273 * \param dict the dealer of data
274 * \param key the key to find data
275 * \return the s_xbt_dictelm_t that we are looking for
277 * Search the given \a key. Throws not_found_error when not found.
278 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
280 xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
282 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
284 if (current == nullptr)
285 THROWF(not_found_error, 0, "key %s not found", key);
291 * \brief like xbt_dict_get(), but returning nullptr when not found
293 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
295 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
297 if (current == nullptr)
300 return current->content;
304 * \brief like xbt_dict_get_elm(), but returning nullptr when not found
306 xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
308 unsigned int hash_code = xbt_str_hash(key);
309 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
311 while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
312 current = current->next;
317 * \brief Remove data from the dict (arbitrary key)
319 * \param dict the trash can
320 * \param key the key of the data to be removed
321 * \param key_len the size of the \a key
323 * Remove the entry associated with the given \a key (throws not_found)
325 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
327 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
328 xbt_dictelm_t previous = nullptr;
329 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
331 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
332 || strncmp(key, current->key, key_len))) {
333 previous = current; /* save the previous node */
334 current = current->next;
337 if (current == nullptr)
338 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
340 if (previous != nullptr) {
341 previous->next = current->next;
343 dict->table[hash_code & dict->table_size] = current->next;
347 if (not dict->table[hash_code & dict->table_size])
350 xbt_dictelm_free(dict, current);
355 * \brief Remove data from the dict (null-terminated key)
357 * \param dict the dict
358 * \param key the key of the data to be removed
360 * Remove the entry associated with the given \a key
362 void xbt_dict_remove(xbt_dict_t dict, const char *key)
364 xbt_dict_remove_ext(dict, key, strlen(key));
367 /** @brief Remove all data from the dict */
368 void xbt_dict_reset(xbt_dict_t dict)
370 if (dict->count == 0)
373 for (int i = 0; i <= dict->table_size; i++) {
374 xbt_dictelm_t previous = nullptr;
375 xbt_dictelm_t current = dict->table[i];
376 while (current != nullptr) {
378 current = current->next;
379 xbt_dictelm_free(dict, previous);
381 dict->table[i] = nullptr;
389 * \brief Return the number of elements in the dict.
390 * \param dict a dictionary
392 int xbt_dict_length(xbt_dict_t dict)
397 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
398 void xbt_dict_dump_output_string(void *s)
400 fputs((char*) s, stdout);
404 * \brief test if the dict is empty or not
406 int xbt_dict_is_empty(xbt_dict_t dict)
408 return not dict || (xbt_dict_length(dict) == 0);
412 * \brief Outputs the content of the structure (debugging purpose)
414 * \param dict the exibitionist
415 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
417 * Outputs the content of the structure. (for debugging purpose). \a output is a function to output the data. If nullptr,
418 * data won't be displayed.
420 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
422 xbt_dictelm_t element;
423 printf("Dict %p:\n", dict);
424 if (dict != nullptr) {
425 for (int i = 0; i < dict->table_size; i++) {
426 element = dict->table[i];
429 while (element != nullptr) {
430 printf(" %s -> '", element->key);
431 if (output != nullptr) {
432 output(element->content);
435 element = element->next;
445 xbt_dynar_t all_sizes = nullptr;
446 /** @brief shows some debugging info about the bucklet repartition */
447 void xbt_dict_dump_sizes(xbt_dict_t dict)
452 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
458 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), nullptr);
460 for (int i = 0; i < dict->table_size; i++) {
461 xbt_dictelm_t element = dict->table[i];
464 while (element != nullptr) {
466 element = element->next;
469 if (xbt_dynar_length(sizes) <= size) {
471 xbt_dynar_set(sizes, size, &prevsize);
474 xbt_dynar_get_cpy(sizes, size, &prevsize);
476 xbt_dynar_set(sizes, size, &prevsize);
480 all_sizes = xbt_dynar_new(sizeof(int), nullptr);
482 xbt_dynar_foreach(sizes, count, size) {
483 /* Copy values of this one into all_sizes */
485 if (xbt_dynar_length(all_sizes) <= count) {
487 xbt_dynar_set(all_sizes, count, &prevcount);
489 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
491 xbt_dynar_set(all_sizes, count, &prevcount);
494 /* Report current sizes */
495 if (count != 0 && size != 0)
496 printf("%uelm x %u cells; ", count, size);
499 xbt_dynar_free(&sizes);
503 * Create the dict mallocators.
504 * This is an internal XBT function called during the lib initialization.
505 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
507 void xbt_dict_preinit()
509 if (dict_elm_mallocator == nullptr)
510 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
511 dict_elm_mallocator_reset_f);
515 * Destroy the dict mallocators.
516 * This is an internal XBT function during the lib initialization
518 void xbt_dict_postexit()
520 if (dict_elm_mallocator != nullptr) {
521 xbt_mallocator_free(dict_elm_mallocator);
522 dict_elm_mallocator = nullptr;
529 printf("Overall stats:");
530 xbt_dynar_foreach(all_sizes, count, size) {
531 if (count != 0 && size != 0) {
532 printf("%uelm x %d cells; ", count, size);
538 printf("; %f elm per cell\n", avg / (double)total_count);
540 printf("; 0 elm per cell\n");
545 #include "src/internal_config.h"
549 #include <xbt/ex.hpp>
551 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
553 XBT_TEST_SUITE("dict", "Dict data container");
555 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
557 char *data = xbt_strdup(data_to_fill);
559 xbt_test_log("Add %s under %s", data_to_fill, key);
561 xbt_dict_set(head, key, data, nullptr);
562 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
563 xbt_dict_dump(head, [](void* s) { fputs((char*)s, stdout); });
568 static void debugged_add(xbt_dict_t head, const char* key)
570 debugged_add_ext(head, key, key);
573 static xbt_dict_t new_fixture()
575 xbt_test_add("Fill in the dictionnary");
577 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
578 debugged_add(head, "12");
579 debugged_add(head, "12a");
580 debugged_add(head, "12b");
581 debugged_add(head, "123");
582 debugged_add(head, "123456");
583 debugged_add(head, "1234");
584 debugged_add(head, "123457");
589 static void search_ext(xbt_dict_t head, const char *key, const char *data)
591 xbt_test_add("Search %s", key);
592 char *found = (char*) xbt_dict_get(head, key);
593 xbt_test_log("Found %s", found);
595 xbt_test_assert(found, "data do not match expectations: found nullptr while searching for %s", data);
597 xbt_test_assert(not strcmp(data, found), "data do not match expectations: found %s while searching for %s", found,
600 xbt_test_assert(not found, "data do not match expectations: found %s while searching for nullptr", found);
604 static void search(xbt_dict_t head, const char *key)
606 search_ext(head, key, key);
609 static void debugged_remove(xbt_dict_t head, const char* key)
611 xbt_test_add("Remove '%s'", key);
612 xbt_dict_remove(head, key);
615 static void traverse(xbt_dict_t head)
617 xbt_dict_cursor_t cursor = nullptr;
622 xbt_dict_foreach(head, cursor, key, data) {
623 if (not key || not data || strcmp(key, data)) {
624 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
626 xbt_test_log("Seen #%d: %s", ++i, key);
628 xbt_test_assert(key && data && strcmp(key, data) == 0, "Key(%s) != value(%s). Aborting", key, data);
632 static void search_not_found(xbt_dict_t head, const char *data)
635 xbt_test_add("Search %s (expected not to be found)", data);
638 data = (const char*) xbt_dict_get(head, data);
639 THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
642 if (e.category != not_found_error)
643 xbt_test_exception(e);
646 xbt_test_assert(ok, "Exception not raised");
649 static void count(xbt_dict_t dict, int length)
651 xbt_test_add("Count elements (expecting %d)", length);
652 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
654 xbt_dict_cursor_t cursor;
658 xbt_dict_foreach(dict, cursor, key, data)
661 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
664 static void count_check_get_key(xbt_dict_t dict, int length)
666 xbt_dict_cursor_t cursor;
671 xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
672 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
674 xbt_dict_foreach(dict, cursor, key, data) {
676 char* key2 = xbt_dict_get_key(dict, data);
677 xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
680 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
683 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve and traverse homogeneous dicts")
685 xbt_test_add("Traversal the null dictionary");
688 xbt_test_add("Traversal and search the empty dictionary");
689 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
692 debugged_remove(head, "12346");
695 if (e.category != not_found_error)
696 xbt_test_exception(e);
698 xbt_dict_free(&head);
700 xbt_test_add("Traverse the full dictionary");
701 head = new_fixture();
702 count_check_get_key(head, 7);
704 debugged_add_ext(head, "toto", "tutu");
705 search_ext(head, "toto", "tutu");
706 debugged_remove(head, "toto");
711 xbt_test_add("Free the dictionary (twice)");
712 xbt_dict_free(&head);
713 xbt_dict_free(&head);
716 head = new_fixture();
717 count_check_get_key(head, 7);
718 xbt_test_add("Change 123 to 'Changed 123'");
719 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), nullptr);
720 count_check_get_key(head, 7);
722 xbt_test_add("Change 123 back to '123'");
723 xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
724 count_check_get_key(head, 7);
726 xbt_test_add("Change 12a to 'Dummy 12a'");
727 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), nullptr);
728 count_check_get_key(head, 7);
730 xbt_test_add("Change 12a to '12a'");
731 xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
732 count_check_get_key(head, 7);
734 xbt_test_add("Traverse the resulting dictionary");
738 xbt_test_add("Search 123");
739 char* data = (char*)xbt_dict_get(head, "123");
740 xbt_test_assert(data && strcmp("123", data) == 0);
742 search_not_found(head, "Can't be found");
743 search_not_found(head, "123 Can't be found");
744 search_not_found(head, "12345678 NOT");
749 search(head, "123456");
750 search(head, "1234");
751 search(head, "123457");
753 xbt_test_add("Traverse the resulting dictionary");
756 xbt_test_add("Free the dictionary twice");
757 xbt_dict_free(&head);
758 xbt_dict_free(&head);
760 xbt_test_add("Traverse the resulting dictionary");
764 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove, "Removing some values from homogeneous dicts")
766 xbt_dict_t head = new_fixture();
768 xbt_test_add("Remove non existing data");
770 debugged_remove(head, "Does not exist");
773 if (e.category != not_found_error)
774 xbt_test_exception(e);
778 xbt_dict_free(&head);
780 xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
781 head = new_fixture();
782 debugged_remove(head, "12a");
785 debugged_remove(head, "12b");
788 debugged_remove(head, "12");
791 debugged_remove(head, "123456");
795 debugged_remove(head, "12346");
798 if (e.category != not_found_error)
799 xbt_test_exception(e);
802 debugged_remove(head, "1234");
804 debugged_remove(head, "123457");
806 debugged_remove(head, "123");
809 debugged_remove(head, "12346");
812 if (e.category != not_found_error)
813 xbt_test_exception(e);
817 xbt_test_add("Free dict, create new fresh one, and then reset the dict");
818 xbt_dict_free(&head);
819 head = new_fixture();
820 xbt_dict_reset(head);
824 xbt_test_add("Free the dictionary twice");
825 xbt_dict_free(&head);
826 xbt_dict_free(&head);
829 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "nullptr data management")
831 xbt_dict_t head = new_fixture();
833 xbt_test_add("Store nullptr under 'null'");
834 xbt_dict_set(head, "null", nullptr, nullptr);
835 search_ext(head, "null", nullptr);
837 xbt_test_add("Check whether I see it while traversing...");
839 xbt_dict_cursor_t cursor = nullptr;
844 xbt_dict_foreach(head, cursor, key, data) {
845 if (not key || not data || strcmp(key, data)) {
846 xbt_test_log("Seen: %s->%s", key, data);
848 xbt_test_log("Seen: %s", key);
851 if (key && strcmp(key, "null") == 0)
854 xbt_test_assert(found, "the key 'null', associated to nullptr is not found");
856 xbt_dict_free(&head);
860 #define SIZEOFKEY 1024
861 static int countelems(xbt_dict_t head)
863 xbt_dict_cursor_t cursor;
868 xbt_dict_foreach(head, cursor, key, data) {
874 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
876 srand((unsigned int) time(nullptr));
878 for (int i = 0; i < 10; i++) {
879 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
880 xbt_test_log("Fill the struct, count its elems and frees the structure");
881 xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
882 xbt_dict_t head = xbt_dict_new_homogeneous(free);
883 for (int j = 0; j < 1000; j++) {
884 char* data = nullptr;
885 char* key = (char*)xbt_malloc(SIZEOFKEY);
888 for (int k = 0; k < SIZEOFKEY - 1; k++)
889 key[k] = rand() % ('z' - 'a') + 'a';
890 key[SIZEOFKEY - 1] = '\0';
891 data = (char*) xbt_dict_get_or_null(head, key);
892 } while (data != nullptr);
894 xbt_dict_set(head, key, key, nullptr);
895 data = (char*) xbt_dict_get(head, key);
896 xbt_test_assert(not strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
901 xbt_dict_free(&head);
902 xbt_dict_free(&head);
905 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
906 xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
907 for (int j = 0; j < NB_ELM; j++) {
908 char* key = (char*)xbt_malloc(10);
910 snprintf(key,10, "%d", j);
911 xbt_dict_set(head, key, key, nullptr);
914 xbt_test_add("Count the elements (retrieving the key and data for each)");
915 xbt_test_log("There is %d elements", countelems(head));
917 xbt_test_add("Search my %d elements 20 times", NB_ELM);
918 char* key = (char*)xbt_malloc(10);
919 for (int i = 0; i < 20; i++) {
920 for (int j = 0; j < NB_ELM; j++) {
921 snprintf(key,10, "%d", j);
922 void* data = xbt_dict_get(head, key);
923 xbt_test_assert(not strcmp(key, (char*)data), "with get, key=%s != data=%s", key, (char*)data);
924 data = xbt_dict_get_ext(head, key, strlen(key));
925 xbt_test_assert(not strcmp(key, (char*)data), "with get_ext, key=%s != data=%s", key, (char*)data);
930 xbt_test_add("Remove my %d elements", NB_ELM);
931 key = (char*) xbt_malloc(10);
932 for (int j = 0; j < NB_ELM; j++) {
933 snprintf(key,10, "%d", j);
934 xbt_dict_remove(head, key);
938 xbt_test_add("Free the object (twice)");
939 xbt_dict_free(&head);
940 xbt_dict_free(&head);
943 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
945 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
948 xbt_test_add("Insert elements");
949 for (int i = 0; i < count; ++i)
950 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, nullptr);
951 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Bad number of elements in the dictionnary");
953 xbt_test_add("Check elements");
954 for (int i = 0; i < count; ++i) {
955 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
956 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
959 xbt_test_add("Free the array");
960 xbt_dict_free(&dict);
962 #endif /* SIMGRID_TEST */