1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2015. 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. */
13 #include "xbt/mallocator.h"
14 #include "src/xbt_modinter.h"
16 #include "dict_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
19 "Dictionaries provide the same functionalities as hash tables");
23 * \return pointer to the destination
24 * \see xbt_dict_new_homogenous(), xbt_dict_free()
26 * Creates and initialize a new dictionary with a default hashtable size.
27 * The dictionary is heterogeneous: each element can have a different free
30 xbt_dict_t xbt_dict_new(void)
32 xbt_dict_t dict = xbt_dict_new_homogeneous(NULL);
33 dict->homogeneous = 0;
40 * \param free_ctn function to call with (\a data as argument) when
41 * \a data is removed from the dictionary
42 * \return pointer to the destination
43 * \see xbt_dict_new(), xbt_dict_free()
45 * Creates and initialize a new dictionary with a default hashtable size.
46 * The dictionary is homogeneous: each element share the same free function.
48 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
52 dict = xbt_new(s_xbt_dict_t, 1);
53 dict->free_f = free_ctn;
54 dict->table_size = 127;
55 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
58 dict->homogeneous = 1;
65 * \param dict the dictionary to be freed
67 * Frees a dictionary with all the data
69 void xbt_dict_free(xbt_dict_t * dict)
72 xbt_dictelm_t current, previous;
76 // if ( *dict ) xbt_dict_dump_sizes(*dict);
78 if (dict != NULL && *dict != NULL) {
79 table_size = (*dict)->table_size;
80 table = (*dict)->table;
81 /* Warning: the size of the table is 'table_size+1'...
82 * This is because table_size is used as a binary mask in xbt_dict_rehash */
83 for (i = 0; (*dict)->count && i <= table_size; i++) {
85 while (current != NULL) {
87 current = current->next;
88 xbt_dictelm_free(*dict, previous);
99 * Returns the amount of elements in the dict
101 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
103 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
106 /* Expend the size of the dict */
107 static void xbt_dict_rehash(xbt_dict_t dict)
109 const int oldsize = dict->table_size + 1;
110 int newsize = oldsize * 2;
112 xbt_dictelm_t *currcell;
113 xbt_dictelm_t *twincell;
114 xbt_dictelm_t bucklet;
115 xbt_dictelm_t *pprev;
118 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
119 newsize * sizeof(xbt_dictelm_t));
120 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
121 dict->table_size = --newsize;
122 dict->table = currcell;
123 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
125 for (i = 0; i < oldsize; i++, currcell++) {
126 if (!*currcell) /* empty cell */
128 twincell = currcell + oldsize;
129 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
130 /* Since we use "& size" instead of "%size" and since the size was doubled,
131 each bucklet of this cell must either :
132 - stay in cell i (ie, currcell)
133 - go to the cell i+oldsize (ie, twincell) */
134 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
135 *pprev = bucklet->next;
136 bucklet->next = *twincell;
142 pprev = &bucklet->next;
147 if (!*currcell) /* everything moved */
153 * \brief Add data to the dict (arbitrary key)
154 * \param dict the container
155 * \param key the key to set the new data
156 * \param key_len the size of the \a key
157 * \param data the data to add in the dict
158 * \param free_ctn function to call with (\a data as argument) when
159 * \a data is removed from the dictionary. This param
160 * will only be considered when the dict was instantiated with
161 * xbt_dict_new() and not xbt_dict_new_homogeneous()
163 * Set the \a data in the structure under the \a key, which can be any kind
164 * of data, as long as its length is provided in \a key_len.
166 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
167 const char *key, int key_len,
168 void *data, void_f_pvoid_t free_ctn)
171 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
173 xbt_dictelm_t current, previous = NULL;
177 "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
178 dict->table_size, hash_code & dict->table_size);
179 current = dict->table[hash_code & dict->table_size];
180 while (current != NULL &&
181 (hash_code != current->hash_code || key_len != current->key_len
182 || memcmp(key, current->key, key_len))) {
184 current = current->next;
187 if (current == NULL) {
188 /* this key doesn't exist yet */
189 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
191 if (previous == NULL) {
192 dict->table[hash_code & dict->table_size] = current;
194 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
195 xbt_dict_rehash(dict);
197 previous->next = current;
200 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
201 key_len, (char *) current->content,
202 key_len, (char *) data, key_len, (char *) key);
203 /* there is already an element with the same key: overwrite it */
204 xbt_dictelm_set_data(dict, current, data, free_ctn);
209 * \brief Add data to the dict (null-terminated key)
211 * \param dict the dict
212 * \param key the key to set the new data
213 * \param data the data to add in the dict
214 * \param free_ctn function to call with (\a data as argument) when
215 * \a data is removed from the dictionary. This param
216 * will only be considered when the dict was instantiated with
217 * xbt_dict_new() and not xbt_dict_new_homogeneous()
219 * set the \a data in the structure under the \a key, which is a
220 * null terminated string.
222 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
223 const char *key, void *data,
224 void_f_pvoid_t free_ctn)
227 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
231 * \brief Retrieve data from the dict (arbitrary key)
233 * \param dict the dealer of data
234 * \param key the key to find data
235 * \param key_len the size of the \a key
236 * \return the data that we are looking for
238 * Search the given \a key. Throws not_found_error when not found.
240 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
245 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
246 xbt_dictelm_t current;
250 current = dict->table[hash_code & dict->table_size];
251 while (current != NULL &&
252 (hash_code != current->hash_code || key_len != current->key_len
253 || memcmp(key, current->key, key_len))) {
254 current = current->next;
258 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
260 return current->content;
264 * \brief like xbt_dict_get_ext(), but returning NULL when not found
266 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
270 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
271 xbt_dictelm_t current;
275 current = dict->table[hash_code & dict->table_size];
276 while (current != NULL &&
277 (hash_code != current->hash_code || key_len != current->key_len
278 || memcmp(key, current->key, key_len))) {
279 current = current->next;
285 return current->content;
289 * @brief retrieve the key associated to that object. Warning, that's a linear search
291 * Returns NULL if the object cannot be found
293 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
296 xbt_dictelm_t current;
299 for (i = 0; i <= dict->table_size; i++) {
300 current = dict->table[i];
301 while (current != NULL) {
302 if (current->content == data)
304 current = current->next;
312 * @brief retrieve the key associated to that xbt_dictelm_t.
315 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
321 * \brief Retrieve data from the dict (null-terminated key)
323 * \param dict the dealer of data
324 * \param key the key to find data
325 * \return the data that we are looking for
327 * Search the given \a key. Throws not_found_error when not found.
328 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
331 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
333 return xbt_dict_get_elm(dict, key)->content;
336 * \brief Retrieve element from the dict (null-terminated key)
338 * \param dict the dealer of data
339 * \param key the key to find data
340 * \return the s_xbt_dictelm_t that we are looking for
342 * Search the given \a key. Throws not_found_error when not found.
343 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
346 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
348 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
351 THROWF(not_found_error, 0, "key %s not found", key);
357 * \brief like xbt_dict_get(), but returning NULL when not found
359 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
361 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
366 return current->content;
369 * \brief like xbt_dict_get_elm(), but returning NULL when not found
371 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
373 unsigned int hash_code = xbt_str_hash(key);
374 xbt_dictelm_t current;
378 current = dict->table[hash_code & dict->table_size];
379 while (current != NULL &&
380 (hash_code != current->hash_code || strcmp(key, current->key)))
381 current = current->next;
387 * \brief Remove data from the dict (arbitrary key)
389 * \param dict the trash can
390 * \param key the key of the data to be removed
391 * \param key_len the size of the \a key
393 * Remove the entry associated with the given \a key (throws not_found)
395 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
400 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
401 xbt_dictelm_t current, previous = NULL;
405 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
406 current = dict->table[hash_code & dict->table_size];
407 while (current != NULL &&
408 (hash_code != current->hash_code || key_len != current->key_len
409 || strncmp(key, current->key, key_len))) {
410 previous = current; /* save the previous node */
411 current = current->next;
415 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
417 if (previous != NULL) {
418 previous->next = current->next;
420 dict->table[hash_code & dict->table_size] = current->next;
423 if (!dict->table[hash_code & dict->table_size])
426 xbt_dictelm_free(dict, current);
433 * \brief Remove data from the dict (null-terminated key)
435 * \param dict the dict
436 * \param key the key of the data to be removed
438 * Remove the entry associated with the given \a key
440 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
442 xbt_dict_remove_ext(dict, key, strlen(key));
445 #ifdef XBT_USE_DEPRECATED
447 * \brief Add data to the dict (arbitrary key)
448 * \param dict the container
449 * \param key the key to set the new data
450 * \param data the data to add in the dict
452 * Set the \a data in the structure under the \a key.
453 * Both \a data and \a key are considered as uintptr_t.
455 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
456 uintptr_t key, uintptr_t data)
458 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
462 * \brief Retrieve data from the dict (key considered as a uintptr_t)
464 * \param dict the dealer of data
465 * \param key the key to find data
466 * \return the data that we are looking for (or 0 if not found)
468 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
470 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
472 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
475 /** Remove a uintptr_t key from the dict */
476 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
478 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
483 * \brief Remove all data from the dict
484 * \param dict the dict
486 void xbt_dict_reset(xbt_dict_t dict)
490 xbt_dictelm_t current, previous = NULL;
494 if (dict->count == 0)
497 for (i = 0; i <= dict->table_size; i++) {
498 current = dict->table[i];
499 while (current != NULL) {
501 current = current->next;
502 xbt_dictelm_free(dict, previous);
504 dict->table[i] = NULL;
512 * \brief Return the number of elements in the dict.
513 * \param dict a dictionary
515 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
522 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
523 void xbt_dict_dump_output_string(void *s)
529 * \brief test if the dict is empty or not
531 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
533 return !dict || (xbt_dict_length(dict) == 0);
537 * \brief Outputs the content of the structure (debugging purpose)
539 * \param dict the exibitionist
540 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
542 * Outputs the content of the structure. (for debugging purpose). \a output is a
543 * function to output the data. If NULL, data won't be displayed.
546 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
549 xbt_dictelm_t element;
550 printf("Dict %p:\n", dict);
552 for (i = 0; i < dict->table_size; i++) {
553 element = dict->table[i];
556 while (element != NULL) {
557 printf(" %s -> '", element->key);
558 if (output != NULL) {
559 output(element->content);
562 element = element->next;
572 xbt_dynar_t all_sizes = NULL;
573 /** @brief shows some debugging info about the bucklet repartition */
574 void xbt_dict_dump_sizes(xbt_dict_t dict)
580 xbt_dictelm_t element;
581 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
583 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
584 dict->fill, dict->table_size);
586 for (i = 0; i < dict->table_size; i++) {
587 element = dict->table[i];
590 while (element != NULL) {
592 element = element->next;
595 if (xbt_dynar_length(sizes) <= size) {
597 xbt_dynar_set(sizes, size, &prevsize);
600 xbt_dynar_get_cpy(sizes, size, &prevsize);
602 xbt_dynar_set(sizes, size, &prevsize);
606 all_sizes = xbt_dynar_new(sizeof(int), NULL);
608 xbt_dynar_foreach(sizes, count, size) {
609 /* Copy values of this one into all_sizes */
611 if (xbt_dynar_length(all_sizes) <= count) {
613 xbt_dynar_set(all_sizes, count, &prevcount);
615 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
617 xbt_dynar_set(all_sizes, count, &prevcount);
620 /* Report current sizes */
625 printf("%uelm x %u cells; ", count, size);
629 xbt_dynar_free(&sizes);
633 * Create the dict mallocators.
634 * This is an internal XBT function called during the lib initialization.
635 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
637 void xbt_dict_preinit(void)
639 dict_elm_mallocator = xbt_mallocator_new(256,
640 dict_elm_mallocator_new_f,
641 dict_elm_mallocator_free_f,
642 dict_elm_mallocator_reset_f);
643 dict_het_elm_mallocator = xbt_mallocator_new(256,
644 dict_het_elm_mallocator_new_f,
645 dict_het_elm_mallocator_free_f,
646 dict_het_elm_mallocator_reset_f);
650 * Destroy the dict mallocators.
651 * This is an internal XBT function during the lib initialization
653 void xbt_dict_postexit(void)
655 if (dict_elm_mallocator != NULL) {
656 xbt_mallocator_free(dict_elm_mallocator);
657 dict_elm_mallocator = NULL;
658 xbt_mallocator_free(dict_het_elm_mallocator);
659 dict_het_elm_mallocator = NULL;
666 printf("Overall stats:");
667 xbt_dynar_foreach(all_sizes, count, size) {
672 printf("%uelm x %d cells; ", count, size);
676 printf("; %f elm per cell\n", avg / (double) total_count);
683 #include "src/portable.h"
685 #define PRINTF_STR(a) (a)?:"(null)"
687 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
689 XBT_TEST_SUITE("dict", "Dict data container");
691 static void debuged_add_ext(xbt_dict_t head, const char *key,
692 const char *data_to_fill, void_f_pvoid_t free_f)
694 char *data = xbt_strdup(data_to_fill);
696 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
699 xbt_dict_set(head, key, data, free_f);
700 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
701 xbt_dict_dump(head, (void (*)(void *)) &printf);
706 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
708 debuged_add_ext(head, key, key, free_f);
711 static void fill(xbt_dict_t * head, int homogeneous)
713 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
715 xbt_test_add("Fill in the dictionnary");
717 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
718 debuged_add(*head, "12", free_f);
719 debuged_add(*head, "12a", free_f);
720 debuged_add(*head, "12b", free_f);
721 debuged_add(*head, "123", free_f);
722 debuged_add(*head, "123456", free_f);
723 /* Child becomes child of what to add */
724 debuged_add(*head, "1234", free_f);
725 /* Need of common ancestor */
726 debuged_add(*head, "123457", free_f);
730 static void search_ext(xbt_dict_t head, const char *key, const char *data)
734 xbt_test_add("Search %s", key);
735 found = xbt_dict_get(head, key);
736 xbt_test_log("Found %s", found);
738 xbt_test_assert(found,
739 "data do not match expectations: found NULL while searching for %s",
742 xbt_test_assert(!strcmp(data, found),
743 "data do not match expectations: found %s while searching for %s",
746 xbt_test_assert(!found,
747 "data do not match expectations: found %s while searching for NULL",
752 static void search(xbt_dict_t head, const char *key)
754 search_ext(head, key, key);
757 static void debuged_remove(xbt_dict_t head, const char *key)
760 xbt_test_add("Remove '%s'", PRINTF_STR(key));
761 xbt_dict_remove(head, key);
762 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
766 static void traverse(xbt_dict_t head)
768 xbt_dict_cursor_t cursor = NULL;
773 xbt_dict_foreach(head, cursor, key, data) {
774 if (!key || !data || strcmp(key, data)) {
775 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
778 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
780 xbt_test_assert(!data || !strcmp(key, data),
781 "Key(%s) != value(%s). Aborting", key, data);
785 static void search_not_found(xbt_dict_t head, const char *data)
790 xbt_test_add("Search %s (expected not to be found)", data);
793 data = xbt_dict_get(head, data);
794 THROWF(unknown_error, 0,
795 "Found something which shouldn't be there (%s)", data);
798 if (e.category != not_found_error)
799 xbt_test_exception(e);
803 xbt_test_assert(ok, "Exception not raised");
806 static void count(xbt_dict_t dict, int length)
808 xbt_dict_cursor_t cursor;
814 xbt_test_add("Count elements (expecting %d)", length);
815 xbt_test_assert(xbt_dict_length(dict) == length,
816 "Announced length(%d) != %d.", xbt_dict_length(dict),
819 xbt_dict_foreach(dict, cursor, key, data)
822 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
827 static void count_check_get_key(xbt_dict_t dict, int length)
829 xbt_dict_cursor_t cursor;
831 XBT_ATTRIB_UNUSED char *key2;
837 ("Count elements (expecting %d), and test the getkey function",
839 xbt_test_assert(xbt_dict_length(dict) == length,
840 "Announced length(%d) != %d.", xbt_dict_length(dict),
843 xbt_dict_foreach(dict, cursor, key, data) {
845 key2 = xbt_dict_get_key(dict, data);
846 xbt_assert(!strcmp(key, key2),
847 "The data was registered under %s instead of %s as expected",
851 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
857 xbt_dict_t head = NULL;
860 static void basic_test(int homogeneous)
862 void_f_pvoid_t free_f;
864 xbt_test_add("Traversal the null dictionary");
867 xbt_test_add("Traversal and search the empty dictionary");
868 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
871 debuged_remove(head, "12346");
874 if (e.category != not_found_error)
875 xbt_test_exception(e);
878 xbt_dict_free(&head);
880 free_f = homogeneous ? NULL : &free;
882 xbt_test_add("Traverse the full dictionary");
883 fill(&head, homogeneous);
884 count_check_get_key(head, 7);
886 debuged_add_ext(head, "toto", "tutu", free_f);
887 search_ext(head, "toto", "tutu");
888 debuged_remove(head, "toto");
893 xbt_test_add("Free the dictionary (twice)");
894 xbt_dict_free(&head);
895 xbt_dict_free(&head);
898 fill(&head, homogeneous);
899 count_check_get_key(head, 7);
900 xbt_test_add("Change 123 to 'Changed 123'");
901 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
902 count_check_get_key(head, 7);
904 xbt_test_add("Change 123 back to '123'");
905 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
906 count_check_get_key(head, 7);
908 xbt_test_add("Change 12a to 'Dummy 12a'");
909 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
910 count_check_get_key(head, 7);
912 xbt_test_add("Change 12a to '12a'");
913 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
914 count_check_get_key(head, 7);
916 xbt_test_add("Traverse the resulting dictionary");
920 xbt_test_add("Search 123");
921 data = xbt_dict_get(head, "123");
922 xbt_test_assert(data);
923 xbt_test_assert(!strcmp("123", data));
925 search_not_found(head, "Can't be found");
926 search_not_found(head, "123 Can't be found");
927 search_not_found(head, "12345678 NOT");
932 search(head, "123456");
933 search(head, "1234");
934 search(head, "123457");
936 xbt_test_add("Traverse the resulting dictionary");
939 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
941 xbt_test_add("Free the dictionary twice");
942 xbt_dict_free(&head);
943 xbt_dict_free(&head);
945 xbt_test_add("Traverse the resulting dictionary");
949 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
954 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
959 static void remove_test(int homogeneous)
961 fill(&head, homogeneous);
963 xbt_test_add("Remove non existing data");
965 debuged_remove(head, "Does not exist");
968 if (e.category != not_found_error)
969 xbt_test_exception(e);
974 xbt_dict_free(&head);
977 ("Remove each data manually (traversing the resulting dictionary each time)");
978 fill(&head, homogeneous);
979 debuged_remove(head, "12a");
982 debuged_remove(head, "12b");
985 debuged_remove(head, "12");
988 debuged_remove(head, "123456");
992 debuged_remove(head, "12346");
995 if (e.category != not_found_error)
996 xbt_test_exception(e);
1000 debuged_remove(head, "1234");
1002 debuged_remove(head, "123457");
1004 debuged_remove(head, "123");
1007 debuged_remove(head, "12346");
1010 if (e.category != not_found_error)
1011 xbt_test_exception(e);
1017 ("Free dict, create new fresh one, and then reset the dict");
1018 xbt_dict_free(&head);
1019 fill(&head, homogeneous);
1020 xbt_dict_reset(head);
1024 xbt_test_add("Free the dictionary twice");
1025 xbt_dict_free(&head);
1026 xbt_dict_free(&head);
1029 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1034 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1039 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1043 xbt_test_add("Store NULL under 'null'");
1044 xbt_dict_set(head, "null", NULL, NULL);
1045 search_ext(head, "null", NULL);
1047 xbt_test_add("Check whether I see it while traversing...");
1049 xbt_dict_cursor_t cursor = NULL;
1053 xbt_dict_foreach(head, cursor, key, data) {
1054 if (!key || !data || strcmp(key, data)) {
1055 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1057 xbt_test_log("Seen: %s", PRINTF_STR(key));
1060 if (!strcmp(key, "null"))
1063 xbt_test_assert(found,
1064 "the key 'null', associated to NULL is not found");
1066 xbt_dict_free(&head);
1069 #define NB_ELM 20000
1070 #define SIZEOFKEY 1024
1071 static int countelems(xbt_dict_t head)
1073 xbt_dict_cursor_t cursor;
1078 xbt_dict_foreach(head, cursor, key, data) {
1084 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1086 xbt_dict_t head = NULL;
1090 srand((unsigned int) time(NULL));
1092 for (i = 0; i < 10; i++) {
1093 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1095 ("Fill the struct, count its elems and frees the structure");
1097 ("using 1000 elements with %d chars long randomized keys.",
1099 head = xbt_dict_new();
1100 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1101 for (j = 0; j < 1000; j++) {
1103 key = xbt_malloc(SIZEOFKEY);
1106 for (k = 0; k < SIZEOFKEY - 1; k++)
1107 key[k] = rand() % ('z' - 'a') + 'a';
1109 /* printf("[%d %s]\n",j,key); */
1110 data = xbt_dict_get_or_null(head, key);
1111 } while (data != NULL);
1113 xbt_dict_set(head, key, key, &free);
1114 data = xbt_dict_get(head, key);
1115 xbt_test_assert(!strcmp(key, data),
1116 "Retrieved value (%s) != Injected value (%s)", key,
1121 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1123 xbt_dict_free(&head);
1124 xbt_dict_free(&head);
1128 head = xbt_dict_new();
1129 xbt_test_add("Fill %d elements, with keys being the number of element",
1131 for (j = 0; j < NB_ELM; j++) {
1132 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1134 key = xbt_malloc(10);
1136 sprintf(key, "%d", j);
1137 xbt_dict_set(head, key, key, &free);
1139 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1142 ("Count the elements (retrieving the key and data for each)");
1143 i = countelems(head);
1144 xbt_test_log("There is %d elements", i);
1146 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1147 key = xbt_malloc(10);
1148 for (i = 0; i < 20; i++) {
1150 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1151 for (j = 0; j < NB_ELM; j++) {
1153 sprintf(key, "%d", j);
1154 data = xbt_dict_get(head, key);
1155 xbt_test_assert(!strcmp(key, (char *) data),
1156 "with get, key=%s != data=%s", key, (char *) data);
1157 data = xbt_dict_get_ext(head, key, strlen(key));
1158 xbt_test_assert(!strcmp(key, (char *) data),
1159 "with get_ext, key=%s != data=%s", key,
1165 xbt_test_add("Remove my %d elements", NB_ELM);
1166 key = xbt_malloc(10);
1167 for (j = 0; j < NB_ELM; j++) {
1168 /* if (!(j%10000)) printf("."); fflush(stdout); */
1170 sprintf(key, "%d", j);
1171 xbt_dict_remove(head, key);
1176 xbt_test_add("Free the structure (twice)");
1177 xbt_dict_free(&head);
1178 xbt_dict_free(&head);
1181 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1183 xbt_dict_t dict = xbt_dict_new();
1186 xbt_test_add("Insert elements");
1188 for (i = 0; i < count; ++i)
1189 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1190 xbt_test_assert(xbt_dict_size(dict) == count,
1191 "Bad number of elements in the dictionnary");
1193 xbt_test_add("Check elements");
1194 for (i = 0; i < count; ++i) {
1195 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1196 xbt_test_assert(xbt_dict_size(dict) == count,
1197 "Unexpected value at index %i, expected %i but was %i", i, i, res);
1200 xbt_test_add("Free the array");
1201 xbt_dict_free(&dict);
1204 #endif /* SIMGRID_TEST */