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;
176 "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
177 dict->table_size, hash_code & dict->table_size);
178 current = dict->table[hash_code & dict->table_size];
179 while (current != NULL &&
180 (hash_code != current->hash_code || key_len != current->key_len
181 || memcmp(key, current->key, key_len))) {
183 current = current->next;
186 if (current == NULL) {
187 /* this key doesn't exist yet */
188 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
190 if (previous == NULL) {
191 dict->table[hash_code & dict->table_size] = current;
193 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
194 xbt_dict_rehash(dict);
196 previous->next = current;
199 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
200 key_len, (char *) current->content,
201 key_len, (char *) data, key_len, (char *) key);
202 /* there is already an element with the same key: overwrite it */
203 xbt_dictelm_set_data(dict, current, data, free_ctn);
208 * \brief Add data to the dict (null-terminated key)
210 * \param dict the dict
211 * \param key the key to set the new data
212 * \param data the data to add in the dict
213 * \param free_ctn function to call with (\a data as argument) when
214 * \a data is removed from the dictionary. This param
215 * will only be considered when the dict was instantiated with
216 * xbt_dict_new() and not xbt_dict_new_homogeneous()
218 * set the \a data in the structure under the \a key, which is a
219 * null terminated string.
221 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
222 const char *key, void *data,
223 void_f_pvoid_t free_ctn)
226 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
230 * \brief Retrieve data from the dict (arbitrary key)
232 * \param dict the dealer of data
233 * \param key the key to find data
234 * \param key_len the size of the \a key
235 * \return the data that we are looking for
237 * Search the given \a key. Throws not_found_error when not found.
239 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
241 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
242 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
244 while (current != NULL &&
245 (hash_code != current->hash_code || key_len != current->key_len
246 || memcmp(key, current->key, key_len))) {
247 current = current->next;
251 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
253 return current->content;
256 /** @brief like xbt_dict_get_ext(), but returning NULL when not found */
257 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
259 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
260 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
262 while (current != NULL &&
263 (hash_code != current->hash_code || key_len != current->key_len
264 || memcmp(key, current->key, key_len))) {
265 current = current->next;
271 return current->content;
275 * @brief retrieve the key associated to that object. Warning, that's a linear search
277 * Returns NULL if the object cannot be found
279 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
281 for (int i = 0; i <= dict->table_size; i++) {
282 xbt_dictelm_t current = dict->table[i];
283 while (current != NULL) {
284 if (current->content == data)
286 current = current->next;
293 /** @brief retrieve the key associated to that xbt_dictelm_t. */
294 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
300 * \brief Retrieve data from the dict (null-terminated key)
302 * \param dict the dealer of data
303 * \param key the key to find data
304 * \return the data that we are looking for
306 * Search the given \a key. Throws not_found_error when not found.
307 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
310 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
312 return xbt_dict_get_elm(dict, key)->content;
315 * \brief Retrieve element from the dict (null-terminated key)
317 * \param dict the dealer of data
318 * \param key the key to find data
319 * \return the s_xbt_dictelm_t that we are looking for
321 * Search the given \a key. Throws not_found_error when not found.
322 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
325 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
327 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
330 THROWF(not_found_error, 0, "key %s not found", key);
336 * \brief like xbt_dict_get(), but returning NULL when not found
338 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
340 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
345 return current->content;
348 * \brief like xbt_dict_get_elm(), but returning NULL when not found
350 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
352 unsigned int hash_code = xbt_str_hash(key);
353 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
355 while (current != NULL &&
356 (hash_code != current->hash_code || strcmp(key, current->key)))
357 current = current->next;
363 * \brief Remove data from the dict (arbitrary key)
365 * \param dict the trash can
366 * \param key the key of the data to be removed
367 * \param key_len the size of the \a key
369 * Remove the entry associated with the given \a key (throws not_found)
371 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
373 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
374 xbt_dictelm_t previous = NULL;
375 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
377 while (current != NULL &&
378 (hash_code != current->hash_code || key_len != current->key_len
379 || strncmp(key, current->key, key_len))) {
380 previous = current; /* save the previous node */
381 current = current->next;
385 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
387 if (previous != NULL) {
388 previous->next = current->next;
390 dict->table[hash_code & dict->table_size] = current->next;
393 if (!dict->table[hash_code & dict->table_size])
396 xbt_dictelm_free(dict, current);
403 * \brief Remove data from the dict (null-terminated key)
405 * \param dict the dict
406 * \param key the key of the data to be removed
408 * Remove the entry associated with the given \a key
410 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
412 xbt_dict_remove_ext(dict, key, strlen(key));
415 #ifdef XBT_USE_DEPRECATED
417 * \brief Add data to the dict (arbitrary key)
418 * \param dict the container
419 * \param key the key to set the new data
420 * \param data the data to add in the dict
422 * Set the \a data in the structure under the \a key.
423 * Both \a data and \a key are considered as uintptr_t.
425 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
426 uintptr_t key, uintptr_t data)
428 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
432 * \brief Retrieve data from the dict (key considered as a uintptr_t)
434 * \param dict the dealer of data
435 * \param key the key to find data
436 * \return the data that we are looking for (or 0 if not found)
438 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
440 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
442 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
445 /** Remove a uintptr_t key from the dict */
446 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
448 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
452 /** @brief Remove all data from the dict */
453 void xbt_dict_reset(xbt_dict_t dict)
455 if (dict->count == 0)
458 for (int i = 0; i <= dict->table_size; i++) {
459 xbt_dictelm_t previous = NULL;
460 xbt_dictelm_t current = dict->table[i];
461 while (current != NULL) {
463 current = current->next;
464 xbt_dictelm_free(dict, previous);
466 dict->table[i] = NULL;
474 * \brief Return the number of elements in the dict.
475 * \param dict a dictionary
477 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
482 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
483 void xbt_dict_dump_output_string(void *s)
489 * \brief test if the dict is empty or not
491 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
493 return !dict || (xbt_dict_length(dict) == 0);
497 * \brief Outputs the content of the structure (debugging purpose)
499 * \param dict the exibitionist
500 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
502 * Outputs the content of the structure. (for debugging purpose). \a output is a
503 * function to output the data. If NULL, data won't be displayed.
506 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
509 xbt_dictelm_t element;
510 printf("Dict %p:\n", dict);
512 for (i = 0; i < dict->table_size; i++) {
513 element = dict->table[i];
516 while (element != NULL) {
517 printf(" %s -> '", element->key);
518 if (output != NULL) {
519 output(element->content);
522 element = element->next;
532 xbt_dynar_t all_sizes = NULL;
533 /** @brief shows some debugging info about the bucklet repartition */
534 void xbt_dict_dump_sizes(xbt_dict_t dict)
539 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
545 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
547 for (int i = 0; i < dict->table_size; i++) {
548 xbt_dictelm_t element = dict->table[i];
551 while (element != NULL) {
553 element = element->next;
556 if (xbt_dynar_length(sizes) <= size) {
558 xbt_dynar_set(sizes, size, &prevsize);
561 xbt_dynar_get_cpy(sizes, size, &prevsize);
563 xbt_dynar_set(sizes, size, &prevsize);
567 all_sizes = xbt_dynar_new(sizeof(int), NULL);
569 xbt_dynar_foreach(sizes, count, size) {
570 /* Copy values of this one into all_sizes */
572 if (xbt_dynar_length(all_sizes) <= count) {
574 xbt_dynar_set(all_sizes, count, &prevcount);
576 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
578 xbt_dynar_set(all_sizes, count, &prevcount);
581 /* Report current sizes */
586 printf("%uelm x %u cells; ", count, size);
589 xbt_dynar_free(&sizes);
593 * Create the dict mallocators.
594 * This is an internal XBT function called during the lib initialization.
595 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
597 void xbt_dict_preinit(void)
599 dict_elm_mallocator = xbt_mallocator_new(256,
600 dict_elm_mallocator_new_f,
601 dict_elm_mallocator_free_f,
602 dict_elm_mallocator_reset_f);
603 dict_het_elm_mallocator = xbt_mallocator_new(256,
604 dict_het_elm_mallocator_new_f,
605 dict_het_elm_mallocator_free_f,
606 dict_het_elm_mallocator_reset_f);
610 * Destroy the dict mallocators.
611 * This is an internal XBT function during the lib initialization
613 void xbt_dict_postexit(void)
615 if (dict_elm_mallocator != NULL) {
616 xbt_mallocator_free(dict_elm_mallocator);
617 dict_elm_mallocator = NULL;
618 xbt_mallocator_free(dict_het_elm_mallocator);
619 dict_het_elm_mallocator = NULL;
626 printf("Overall stats:");
627 xbt_dynar_foreach(all_sizes, count, size) {
632 printf("%uelm x %d cells; ", count, size);
636 printf("; %f elm per cell\n", avg / (double) total_count);
643 #include "src/portable.h"
646 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
648 XBT_TEST_SUITE("dict", "Dict data container");
650 static void debuged_add_ext(xbt_dict_t head, const char *key,
651 const char *data_to_fill, void_f_pvoid_t free_f)
653 char *data = xbt_strdup(data_to_fill);
655 xbt_test_log("Add %s under %s", data_to_fill, key);
657 xbt_dict_set(head, key, data, free_f);
658 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
659 xbt_dict_dump(head, (void (*)(void *)) &printf);
664 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
666 debuged_add_ext(head, key, key, free_f);
669 static void fill(xbt_dict_t * head, int homogeneous)
671 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
673 xbt_test_add("Fill in the dictionnary");
675 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
676 debuged_add(*head, "12", free_f);
677 debuged_add(*head, "12a", free_f);
678 debuged_add(*head, "12b", free_f);
679 debuged_add(*head, "123", free_f);
680 debuged_add(*head, "123456", free_f);
681 /* Child becomes child of what to add */
682 debuged_add(*head, "1234", free_f);
683 /* Need of common ancestor */
684 debuged_add(*head, "123457", free_f);
688 static void search_ext(xbt_dict_t head, const char *key, const char *data)
692 xbt_test_add("Search %s", key);
693 found = xbt_dict_get(head, key);
694 xbt_test_log("Found %s", found);
696 xbt_test_assert(found,
697 "data do not match expectations: found NULL while searching for %s",
700 xbt_test_assert(!strcmp(data, found),
701 "data do not match expectations: found %s while searching for %s",
704 xbt_test_assert(!found,
705 "data do not match expectations: found %s while searching for NULL",
710 static void search(xbt_dict_t head, const char *key)
712 search_ext(head, key, key);
715 static void debuged_remove(xbt_dict_t head, const char *key)
718 xbt_test_add("Remove '%s'", key);
719 xbt_dict_remove(head, key);
720 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
724 static void traverse(xbt_dict_t head)
726 xbt_dict_cursor_t cursor = NULL;
731 xbt_dict_foreach(head, cursor, key, data) {
732 if (!key || !data || strcmp(key, data)) {
733 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
735 xbt_test_log("Seen #%d: %s", ++i, key);
737 xbt_test_assert(!data || !strcmp(key, data),
738 "Key(%s) != value(%s). Aborting", key, data);
742 static void search_not_found(xbt_dict_t head, const char *data)
747 xbt_test_add("Search %s (expected not to be found)", data);
750 data = xbt_dict_get(head, data);
751 THROWF(unknown_error, 0,
752 "Found something which shouldn't be there (%s)", data);
755 if (e.category != not_found_error)
756 xbt_test_exception(e);
760 xbt_test_assert(ok, "Exception not raised");
763 static void count(xbt_dict_t dict, int length)
765 xbt_dict_cursor_t cursor;
771 xbt_test_add("Count elements (expecting %d)", length);
772 xbt_test_assert(xbt_dict_length(dict) == length,
773 "Announced length(%d) != %d.", xbt_dict_length(dict),
776 xbt_dict_foreach(dict, cursor, key, data)
779 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
784 static void count_check_get_key(xbt_dict_t dict, int length)
786 xbt_dict_cursor_t cursor;
788 XBT_ATTRIB_UNUSED char *key2;
794 ("Count elements (expecting %d), and test the getkey function",
796 xbt_test_assert(xbt_dict_length(dict) == length,
797 "Announced length(%d) != %d.", xbt_dict_length(dict),
800 xbt_dict_foreach(dict, cursor, key, data) {
802 key2 = xbt_dict_get_key(dict, data);
803 xbt_assert(!strcmp(key, key2),
804 "The data was registered under %s instead of %s as expected",
808 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
814 xbt_dict_t head = NULL;
817 static void basic_test(int homogeneous)
819 void_f_pvoid_t free_f;
821 xbt_test_add("Traversal the null dictionary");
824 xbt_test_add("Traversal and search the empty dictionary");
825 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
828 debuged_remove(head, "12346");
831 if (e.category != not_found_error)
832 xbt_test_exception(e);
835 xbt_dict_free(&head);
837 free_f = homogeneous ? NULL : &free;
839 xbt_test_add("Traverse the full dictionary");
840 fill(&head, homogeneous);
841 count_check_get_key(head, 7);
843 debuged_add_ext(head, "toto", "tutu", free_f);
844 search_ext(head, "toto", "tutu");
845 debuged_remove(head, "toto");
850 xbt_test_add("Free the dictionary (twice)");
851 xbt_dict_free(&head);
852 xbt_dict_free(&head);
855 fill(&head, homogeneous);
856 count_check_get_key(head, 7);
857 xbt_test_add("Change 123 to 'Changed 123'");
858 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
859 count_check_get_key(head, 7);
861 xbt_test_add("Change 123 back to '123'");
862 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
863 count_check_get_key(head, 7);
865 xbt_test_add("Change 12a to 'Dummy 12a'");
866 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
867 count_check_get_key(head, 7);
869 xbt_test_add("Change 12a to '12a'");
870 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
871 count_check_get_key(head, 7);
873 xbt_test_add("Traverse the resulting dictionary");
877 xbt_test_add("Search 123");
878 data = xbt_dict_get(head, "123");
879 xbt_test_assert(data);
880 xbt_test_assert(!strcmp("123", data));
882 search_not_found(head, "Can't be found");
883 search_not_found(head, "123 Can't be found");
884 search_not_found(head, "12345678 NOT");
889 search(head, "123456");
890 search(head, "1234");
891 search(head, "123457");
893 xbt_test_add("Traverse the resulting dictionary");
896 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
898 xbt_test_add("Free the dictionary twice");
899 xbt_dict_free(&head);
900 xbt_dict_free(&head);
902 xbt_test_add("Traverse the resulting dictionary");
906 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
911 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
916 static void remove_test(int homogeneous)
918 fill(&head, homogeneous);
920 xbt_test_add("Remove non existing data");
922 debuged_remove(head, "Does not exist");
925 if (e.category != not_found_error)
926 xbt_test_exception(e);
931 xbt_dict_free(&head);
934 ("Remove each data manually (traversing the resulting dictionary each time)");
935 fill(&head, homogeneous);
936 debuged_remove(head, "12a");
939 debuged_remove(head, "12b");
942 debuged_remove(head, "12");
945 debuged_remove(head, "123456");
949 debuged_remove(head, "12346");
952 if (e.category != not_found_error)
953 xbt_test_exception(e);
957 debuged_remove(head, "1234");
959 debuged_remove(head, "123457");
961 debuged_remove(head, "123");
964 debuged_remove(head, "12346");
967 if (e.category != not_found_error)
968 xbt_test_exception(e);
974 ("Free dict, create new fresh one, and then reset the dict");
975 xbt_dict_free(&head);
976 fill(&head, homogeneous);
977 xbt_dict_reset(head);
981 xbt_test_add("Free the dictionary twice");
982 xbt_dict_free(&head);
983 xbt_dict_free(&head);
986 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
991 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
996 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1000 xbt_test_add("Store NULL under 'null'");
1001 xbt_dict_set(head, "null", NULL, NULL);
1002 search_ext(head, "null", NULL);
1004 xbt_test_add("Check whether I see it while traversing...");
1006 xbt_dict_cursor_t cursor = NULL;
1010 xbt_dict_foreach(head, cursor, key, data) {
1011 if (!key || !data || strcmp(key, data)) {
1012 xbt_test_log("Seen: %s->%s", key, data);
1014 xbt_test_log("Seen: %s", key);
1017 if (!strcmp(key, "null"))
1020 xbt_test_assert(found,
1021 "the key 'null', associated to NULL is not found");
1023 xbt_dict_free(&head);
1026 #define NB_ELM 20000
1027 #define SIZEOFKEY 1024
1028 static int countelems(xbt_dict_t head)
1030 xbt_dict_cursor_t cursor;
1035 xbt_dict_foreach(head, cursor, key, data) {
1041 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1043 xbt_dict_t head = NULL;
1047 srand((unsigned int) time(NULL));
1049 for (i = 0; i < 10; i++) {
1050 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1052 ("Fill the struct, count its elems and frees the structure");
1054 ("using 1000 elements with %d chars long randomized keys.",
1056 head = xbt_dict_new();
1057 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1058 for (j = 0; j < 1000; j++) {
1060 key = xbt_malloc(SIZEOFKEY);
1063 for (k = 0; k < SIZEOFKEY - 1; k++)
1064 key[k] = rand() % ('z' - 'a') + 'a';
1066 /* printf("[%d %s]\n",j,key); */
1067 data = xbt_dict_get_or_null(head, key);
1068 } while (data != NULL);
1070 xbt_dict_set(head, key, key, &free);
1071 data = xbt_dict_get(head, key);
1072 xbt_test_assert(!strcmp(key, data),
1073 "Retrieved value (%s) != Injected value (%s)", key,
1078 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1080 xbt_dict_free(&head);
1081 xbt_dict_free(&head);
1085 head = xbt_dict_new();
1086 xbt_test_add("Fill %d elements, with keys being the number of element",
1088 for (j = 0; j < NB_ELM; j++) {
1089 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1091 key = xbt_malloc(10);
1093 sprintf(key, "%d", j);
1094 xbt_dict_set(head, key, key, &free);
1096 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1099 ("Count the elements (retrieving the key and data for each)");
1100 i = countelems(head);
1101 xbt_test_log("There is %d elements", i);
1103 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1104 key = xbt_malloc(10);
1105 for (i = 0; i < 20; i++) {
1107 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1108 for (j = 0; j < NB_ELM; j++) {
1110 sprintf(key, "%d", j);
1111 data = xbt_dict_get(head, key);
1112 xbt_test_assert(!strcmp(key, (char *) data),
1113 "with get, key=%s != data=%s", key, (char *) data);
1114 data = xbt_dict_get_ext(head, key, strlen(key));
1115 xbt_test_assert(!strcmp(key, (char *) data),
1116 "with get_ext, key=%s != data=%s", key,
1122 xbt_test_add("Remove my %d elements", NB_ELM);
1123 key = xbt_malloc(10);
1124 for (j = 0; j < NB_ELM; j++) {
1125 /* if (!(j%10000)) printf("."); fflush(stdout); */
1127 sprintf(key, "%d", j);
1128 xbt_dict_remove(head, key);
1133 xbt_test_add("Free the structure (twice)");
1134 xbt_dict_free(&head);
1135 xbt_dict_free(&head);
1138 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1140 xbt_dict_t dict = xbt_dict_new();
1143 xbt_test_add("Insert elements");
1145 for (i = 0; i < count; ++i)
1146 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1147 xbt_test_assert(xbt_dict_size(dict) == count,
1148 "Bad number of elements in the dictionnary");
1150 xbt_test_add("Check elements");
1151 for (i = 0; i < count; ++i) {
1152 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1153 xbt_test_assert(xbt_dict_size(dict) == count,
1154 "Unexpected value at index %i, expected %i but was %i", i, i, res);
1157 xbt_test_add("Free the array");
1158 xbt_dict_free(&dict);
1161 #endif /* SIMGRID_TEST */