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_new(), 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);
45 dict->homogeneous = 1;
52 * \param dict the dictionary to be freed
54 * Frees a dictionary with all the data
56 void xbt_dict_free(xbt_dict_t * dict)
58 if (dict != nullptr && *dict != nullptr) {
59 int table_size = (*dict)->table_size;
60 xbt_dictelm_t* table = (*dict)->table;
61 /* Warning: the size of the table is 'table_size+1'...
62 * This is because table_size is used as a binary mask in xbt_dict_rehash */
63 for (int i = 0; (*dict)->count && i <= table_size; i++) {
64 xbt_dictelm_t current = table[i];
65 xbt_dictelm_t previous;
67 while (current != nullptr) {
69 current = current->next;
70 xbt_dictelm_free(*dict, previous);
80 /** Returns the amount of elements in the dict */
81 unsigned int xbt_dict_size(xbt_dict_t dict)
83 return (dict != nullptr ? static_cast<unsigned int>(dict->count) : static_cast<unsigned int>(0));
86 /* Expend the size of the dict */
87 static void xbt_dict_rehash(xbt_dict_t dict)
89 const unsigned oldsize = dict->table_size + 1;
90 unsigned newsize = oldsize * 2;
92 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
93 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
95 dict->table_size = newsize;
96 dict->table = currcell;
97 XBT_DEBUG("REHASH (%u->%u)", oldsize, newsize);
99 for (unsigned i = 0; i < oldsize; i++, currcell++) {
100 if (*currcell == nullptr) /* empty cell */
103 xbt_dictelm_t *twincell = currcell + oldsize;
104 xbt_dictelm_t *pprev = currcell;
105 xbt_dictelm_t bucklet = *currcell;
106 for (; bucklet != nullptr; bucklet = *pprev) {
107 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
108 - stay in cell i (ie, currcell)
109 - go to the cell i+oldsize (ie, twincell) */
110 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
111 *pprev = bucklet->next;
112 bucklet->next = *twincell;
113 if (*twincell == nullptr)
117 pprev = &bucklet->next;
121 if (*currcell == nullptr) /* everything moved */
127 * \brief Add data to the dict (arbitrary key)
128 * \param dict the container
129 * \param key the key to set the new data
130 * \param key_len the size of the \a key
131 * \param data the data to add in the dict
132 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
133 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
135 * 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
138 void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn)
140 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
142 xbt_dictelm_t current;
143 xbt_dictelm_t previous = nullptr;
145 xbt_assert(not free_ctn, "Cannot set an individual free function in homogeneous dicts.");
146 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
147 dict->table_size, hash_code & dict->table_size);
148 current = dict->table[hash_code & dict->table_size];
149 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
150 || memcmp(key, current->key, key_len))) {
152 current = current->next;
155 if (current == nullptr) {
156 /* this key doesn't exist yet */
157 current = xbt_dictelm_new(key, key_len, hash_code, data);
159 if (previous == nullptr) {
160 dict->table[hash_code & dict->table_size] = current;
162 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
163 xbt_dict_rehash(dict);
165 previous->next = current;
168 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
169 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
170 /* there is already an element with the same key: overwrite it */
171 xbt_dictelm_set_data(dict, current, data, free_ctn);
176 * \brief Add data to the dict (null-terminated key)
178 * \param dict the dict
179 * \param key the key to set the new data
180 * \param data the data to add in the dict
181 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
182 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
184 * set the \a data in the structure under the \a key, which is anull terminated string.
186 void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
188 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
192 * \brief Retrieve data from the dict (arbitrary key)
194 * \param dict the dealer of data
195 * \param key the key to find data
196 * \param key_len the size of the \a key
197 * \return the data that we are looking for
199 * Search the given \a key. Throws not_found_error when not found.
201 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
203 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
204 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
206 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
207 || memcmp(key, current->key, key_len))) {
208 current = current->next;
211 if (current == nullptr)
212 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
214 return current->content;
217 /** @brief like xbt_dict_get_ext(), but returning nullptr when not found */
218 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
220 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
221 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
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;
228 if (current == nullptr)
231 return current->content;
235 * @brief retrieve the key associated to that object. Warning, that's a linear search
237 * Returns nullptr if the object cannot be found
239 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
241 for (int i = 0; i <= dict->table_size; i++) {
242 xbt_dictelm_t current = dict->table[i];
243 while (current != nullptr) {
244 if (current->content == data)
246 current = current->next;
252 /** @brief retrieve the key associated to that xbt_dictelm_t. */
253 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
259 * \brief Retrieve data from the dict (null-terminated key)
261 * \param dict the dealer of data
262 * \param key the key to find data
263 * \return the data that we are looking for
265 * Search the given \a key. Throws not_found_error when not found.
266 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
268 void *xbt_dict_get(xbt_dict_t dict, const char *key)
270 return xbt_dict_get_elm(dict, key)->content;
274 * \brief Retrieve element from the dict (null-terminated key)
276 * \param dict the dealer of data
277 * \param key the key to find data
278 * \return the s_xbt_dictelm_t that we are looking for
280 * Search the given \a key. Throws not_found_error when not found.
281 * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found.
283 xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
285 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
287 if (current == nullptr)
288 THROWF(not_found_error, 0, "key %s not found", key);
294 * \brief like xbt_dict_get(), but returning nullptr when not found
296 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
298 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
300 if (current == nullptr)
303 return current->content;
307 * \brief like xbt_dict_get_elm(), but returning nullptr when not found
309 xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
311 unsigned int hash_code = xbt_str_hash(key);
312 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
314 while (current != nullptr && (hash_code != current->hash_code || strcmp(key, current->key)))
315 current = current->next;
320 * \brief Remove data from the dict (arbitrary key)
322 * \param dict the trash can
323 * \param key the key of the data to be removed
324 * \param key_len the size of the \a key
326 * Remove the entry associated with the given \a key (throws not_found)
328 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
330 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
331 xbt_dictelm_t previous = nullptr;
332 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
334 while (current != nullptr && (hash_code != current->hash_code || key_len != current->key_len
335 || strncmp(key, current->key, key_len))) {
336 previous = current; /* save the previous node */
337 current = current->next;
340 if (current == nullptr)
341 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
343 if (previous != nullptr) {
344 previous->next = current->next;
346 dict->table[hash_code & dict->table_size] = current->next;
350 if (not dict->table[hash_code & dict->table_size])
353 xbt_dictelm_free(dict, current);
358 * \brief Remove data from the dict (null-terminated key)
360 * \param dict the dict
361 * \param key the key of the data to be removed
363 * Remove the entry associated with the given \a key
365 void xbt_dict_remove(xbt_dict_t dict, const char *key)
367 xbt_dict_remove_ext(dict, key, strlen(key));
370 /** @brief Remove all data from the dict */
371 void xbt_dict_reset(xbt_dict_t dict)
373 if (dict->count == 0)
376 for (int i = 0; i <= dict->table_size; i++) {
377 xbt_dictelm_t previous = nullptr;
378 xbt_dictelm_t current = dict->table[i];
379 while (current != nullptr) {
381 current = current->next;
382 xbt_dictelm_free(dict, previous);
384 dict->table[i] = nullptr;
392 * \brief Return the number of elements in the dict.
393 * \param dict a dictionary
395 int xbt_dict_length(xbt_dict_t dict)
400 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
401 void xbt_dict_dump_output_string(void *s)
403 fputs((char*) s, stdout);
407 * \brief test if the dict is empty or not
409 int xbt_dict_is_empty(xbt_dict_t dict)
411 return not dict || (xbt_dict_length(dict) == 0);
415 * \brief Outputs the content of the structure (debugging purpose)
417 * \param dict the exibitionist
418 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
420 * Outputs the content of the structure. (for debugging purpose). \a output is a function to output the data. If nullptr,
421 * data won't be displayed.
423 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
425 xbt_dictelm_t element;
426 printf("Dict %p:\n", dict);
427 if (dict != nullptr) {
428 for (int i = 0; i < dict->table_size; i++) {
429 element = dict->table[i];
432 while (element != nullptr) {
433 printf(" %s -> '", element->key);
434 if (output != nullptr) {
435 output(element->content);
438 element = element->next;
448 xbt_dynar_t all_sizes = nullptr;
449 /** @brief shows some debugging info about the bucklet repartition */
450 void xbt_dict_dump_sizes(xbt_dict_t dict)
455 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
461 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), nullptr);
463 for (int i = 0; i < dict->table_size; i++) {
464 xbt_dictelm_t element = dict->table[i];
467 while (element != nullptr) {
469 element = element->next;
472 if (xbt_dynar_length(sizes) <= size) {
474 xbt_dynar_set(sizes, size, &prevsize);
477 xbt_dynar_get_cpy(sizes, size, &prevsize);
479 xbt_dynar_set(sizes, size, &prevsize);
483 all_sizes = xbt_dynar_new(sizeof(int), nullptr);
485 xbt_dynar_foreach(sizes, count, size) {
486 /* Copy values of this one into all_sizes */
488 if (xbt_dynar_length(all_sizes) <= count) {
490 xbt_dynar_set(all_sizes, count, &prevcount);
492 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
494 xbt_dynar_set(all_sizes, count, &prevcount);
497 /* Report current sizes */
498 if (count != 0 && size != 0)
499 printf("%uelm x %u cells; ", count, size);
502 xbt_dynar_free(&sizes);
506 * Create the dict mallocators.
507 * This is an internal XBT function called during the lib initialization.
508 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
510 void xbt_dict_preinit()
512 if (dict_elm_mallocator == nullptr)
513 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
514 dict_elm_mallocator_reset_f);
518 * Destroy the dict mallocators.
519 * This is an internal XBT function during the lib initialization
521 void xbt_dict_postexit()
523 if (dict_elm_mallocator != nullptr) {
524 xbt_mallocator_free(dict_elm_mallocator);
525 dict_elm_mallocator = nullptr;
532 printf("Overall stats:");
533 xbt_dynar_foreach(all_sizes, count, size) {
534 if (count != 0 && size != 0) {
535 printf("%uelm x %d cells; ", count, size);
541 printf("; %f elm per cell\n", avg / (double)total_count);
543 printf("; 0 elm per cell\n");
548 #include "src/internal_config.h"
552 #include <xbt/ex.hpp>
554 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
556 XBT_TEST_SUITE("dict", "Dict data container");
558 static void debugged_add_ext(xbt_dict_t head, const char* key, const char* data_to_fill)
560 char *data = xbt_strdup(data_to_fill);
562 xbt_test_log("Add %s under %s", data_to_fill, key);
564 xbt_dict_set(head, key, data, nullptr);
565 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
566 xbt_dict_dump(head, (void (*)(void *)) &printf);
571 static void debugged_add(xbt_dict_t head, const char* key)
573 debugged_add_ext(head, key, key);
576 static xbt_dict_t new_fixture()
578 xbt_test_add("Fill in the dictionnary");
580 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
581 debugged_add(head, "12");
582 debugged_add(head, "12a");
583 debugged_add(head, "12b");
584 debugged_add(head, "123");
585 debugged_add(head, "123456");
586 debugged_add(head, "1234");
587 debugged_add(head, "123457");
592 static void search_ext(xbt_dict_t head, const char *key, const char *data)
594 xbt_test_add("Search %s", key);
595 char *found = (char*) xbt_dict_get(head, key);
596 xbt_test_log("Found %s", found);
598 xbt_test_assert(found, "data do not match expectations: found nullptr while searching for %s", data);
600 xbt_test_assert(not strcmp(data, found), "data do not match expectations: found %s while searching for %s", found,
603 xbt_test_assert(not found, "data do not match expectations: found %s while searching for nullptr", found);
607 static void search(xbt_dict_t head, const char *key)
609 search_ext(head, key, key);
612 static void debugged_remove(xbt_dict_t head, const char* key)
614 xbt_test_add("Remove '%s'", key);
615 xbt_dict_remove(head, key);
618 static void traverse(xbt_dict_t head)
620 xbt_dict_cursor_t cursor = nullptr;
625 xbt_dict_foreach(head, cursor, key, data) {
626 if (not key || not data || strcmp(key, data)) {
627 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
629 xbt_test_log("Seen #%d: %s", ++i, key);
631 xbt_test_assert(key && data && strcmp(key, data) == 0, "Key(%s) != value(%s). Aborting", key, data);
635 static void search_not_found(xbt_dict_t head, const char *data)
638 xbt_test_add("Search %s (expected not to be found)", data);
641 data = (const char*) xbt_dict_get(head, data);
642 THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
645 if (e.category != not_found_error)
646 xbt_test_exception(e);
649 xbt_test_assert(ok, "Exception not raised");
652 static void count(xbt_dict_t dict, int length)
654 xbt_test_add("Count elements (expecting %d)", length);
655 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
657 xbt_dict_cursor_t cursor;
661 xbt_dict_foreach(dict, cursor, key, data)
664 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
667 static void count_check_get_key(xbt_dict_t dict, int length)
669 xbt_dict_cursor_t cursor;
674 xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
675 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
677 xbt_dict_foreach(dict, cursor, key, data) {
679 char* key2 = xbt_dict_get_key(dict, data);
680 xbt_assert(not strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
683 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
686 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve and traverse homogeneous dicts")
688 xbt_test_add("Traversal the null dictionary");
691 xbt_test_add("Traversal and search the empty dictionary");
692 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
695 debugged_remove(head, "12346");
698 if (e.category != not_found_error)
699 xbt_test_exception(e);
701 xbt_dict_free(&head);
703 xbt_test_add("Traverse the full dictionary");
704 head = new_fixture();
705 count_check_get_key(head, 7);
707 debugged_add_ext(head, "toto", "tutu");
708 search_ext(head, "toto", "tutu");
709 debugged_remove(head, "toto");
714 xbt_test_add("Free the dictionary (twice)");
715 xbt_dict_free(&head);
716 xbt_dict_free(&head);
719 head = new_fixture();
720 count_check_get_key(head, 7);
721 xbt_test_add("Change 123 to 'Changed 123'");
722 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), nullptr);
723 count_check_get_key(head, 7);
725 xbt_test_add("Change 123 back to '123'");
726 xbt_dict_set(head, "123", xbt_strdup("123"), nullptr);
727 count_check_get_key(head, 7);
729 xbt_test_add("Change 12a to 'Dummy 12a'");
730 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), nullptr);
731 count_check_get_key(head, 7);
733 xbt_test_add("Change 12a to '12a'");
734 xbt_dict_set(head, "12a", xbt_strdup("12a"), nullptr);
735 count_check_get_key(head, 7);
737 xbt_test_add("Traverse the resulting dictionary");
741 xbt_test_add("Search 123");
742 char* data = (char*)xbt_dict_get(head, "123");
743 xbt_test_assert(data && strcmp("123", data) == 0);
745 search_not_found(head, "Can't be found");
746 search_not_found(head, "123 Can't be found");
747 search_not_found(head, "12345678 NOT");
752 search(head, "123456");
753 search(head, "1234");
754 search(head, "123457");
756 xbt_test_add("Traverse the resulting dictionary");
759 xbt_test_add("Free the dictionary twice");
760 xbt_dict_free(&head);
761 xbt_dict_free(&head);
763 xbt_test_add("Traverse the resulting dictionary");
767 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove, "Removing some values from homogeneous dicts")
769 xbt_dict_t head = new_fixture();
771 xbt_test_add("Remove non existing data");
773 debugged_remove(head, "Does not exist");
776 if (e.category != not_found_error)
777 xbt_test_exception(e);
781 xbt_dict_free(&head);
783 xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
784 head = new_fixture();
785 debugged_remove(head, "12a");
788 debugged_remove(head, "12b");
791 debugged_remove(head, "12");
794 debugged_remove(head, "123456");
798 debugged_remove(head, "12346");
801 if (e.category != not_found_error)
802 xbt_test_exception(e);
805 debugged_remove(head, "1234");
807 debugged_remove(head, "123457");
809 debugged_remove(head, "123");
812 debugged_remove(head, "12346");
815 if (e.category != not_found_error)
816 xbt_test_exception(e);
820 xbt_test_add("Free dict, create new fresh one, and then reset the dict");
821 xbt_dict_free(&head);
822 head = new_fixture();
823 xbt_dict_reset(head);
827 xbt_test_add("Free the dictionary twice");
828 xbt_dict_free(&head);
829 xbt_dict_free(&head);
832 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "nullptr data management")
834 xbt_dict_t head = new_fixture();
836 xbt_test_add("Store nullptr under 'null'");
837 xbt_dict_set(head, "null", nullptr, nullptr);
838 search_ext(head, "null", nullptr);
840 xbt_test_add("Check whether I see it while traversing...");
842 xbt_dict_cursor_t cursor = nullptr;
847 xbt_dict_foreach(head, cursor, key, data) {
848 if (not key || not data || strcmp(key, data)) {
849 xbt_test_log("Seen: %s->%s", key, data);
851 xbt_test_log("Seen: %s", key);
854 if (key && strcmp(key, "null") == 0)
857 xbt_test_assert(found, "the key 'null', associated to nullptr is not found");
859 xbt_dict_free(&head);
863 #define SIZEOFKEY 1024
864 static int countelems(xbt_dict_t head)
866 xbt_dict_cursor_t cursor;
871 xbt_dict_foreach(head, cursor, key, data) {
877 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
879 srand((unsigned int) time(nullptr));
881 for (int i = 0; i < 10; i++) {
882 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
883 xbt_test_log("Fill the struct, count its elems and frees the structure");
884 xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
885 xbt_dict_t head = xbt_dict_new_homogeneous(free);
886 for (int j = 0; j < 1000; j++) {
887 char* data = nullptr;
888 char* key = (char*)xbt_malloc(SIZEOFKEY);
891 for (int k = 0; k < SIZEOFKEY - 1; k++)
892 key[k] = rand() % ('z' - 'a') + 'a';
893 key[SIZEOFKEY - 1] = '\0';
894 data = (char*) xbt_dict_get_or_null(head, key);
895 } while (data != nullptr);
897 xbt_dict_set(head, key, key, nullptr);
898 data = (char*) xbt_dict_get(head, key);
899 xbt_test_assert(not strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
904 xbt_dict_free(&head);
905 xbt_dict_free(&head);
908 xbt_dict_t head = xbt_dict_new_homogeneous(&free);
909 xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
910 for (int j = 0; j < NB_ELM; j++) {
911 char* key = (char*)xbt_malloc(10);
913 snprintf(key,10, "%d", j);
914 xbt_dict_set(head, key, key, nullptr);
917 xbt_test_add("Count the elements (retrieving the key and data for each)");
918 xbt_test_log("There is %d elements", countelems(head));
920 xbt_test_add("Search my %d elements 20 times", NB_ELM);
921 char* key = (char*)xbt_malloc(10);
922 for (int i = 0; i < 20; i++) {
923 for (int j = 0; j < NB_ELM; j++) {
924 snprintf(key,10, "%d", j);
925 void* data = xbt_dict_get(head, key);
926 xbt_test_assert(not strcmp(key, (char*)data), "with get, key=%s != data=%s", key, (char*)data);
927 data = xbt_dict_get_ext(head, key, strlen(key));
928 xbt_test_assert(not strcmp(key, (char*)data), "with get_ext, key=%s != data=%s", key, (char*)data);
933 xbt_test_add("Remove my %d elements", NB_ELM);
934 key = (char*) xbt_malloc(10);
935 for (int j = 0; j < NB_ELM; j++) {
936 snprintf(key,10, "%d", j);
937 xbt_dict_remove(head, key);
941 xbt_test_add("Free the object (twice)");
942 xbt_dict_free(&head);
943 xbt_dict_free(&head);
946 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
948 xbt_dict_t dict = xbt_dict_new_homogeneous(nullptr);
951 xbt_test_add("Insert elements");
952 for (int i = 0; i < count; ++i)
953 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, nullptr);
954 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Bad number of elements in the dictionnary");
956 xbt_test_add("Check elements");
957 for (int i = 0; i < count; ++i) {
958 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
959 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
962 xbt_test_add("Free the array");
963 xbt_dict_free(&dict);
965 #endif /* SIMGRID_TEST */