1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2014. 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 "xbt_modinter.h"
16 #include "dict_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
19 "Dictionaries provide the same functionalities than hash tables");
23 * \return pointer to the destination
24 * \see 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 register int newsize = oldsize * 2;
112 register xbt_dictelm_t *currcell;
113 register xbt_dictelm_t *twincell;
114 register xbt_dictelm_t bucklet;
115 register 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
161 * Set the \a data in the structure under the \a key, which can be any kind
162 * of data, as long as its length is provided in \a key_len.
164 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
165 const char *key, int key_len,
166 void *data, void_f_pvoid_t free_ctn)
169 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
171 xbt_dictelm_t current, previous = NULL;
175 "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
176 dict->table_size, hash_code & dict->table_size);
177 current = dict->table[hash_code & dict->table_size];
178 while (current != NULL &&
179 (hash_code != current->hash_code || key_len != current->key_len
180 || memcmp(key, current->key, key_len))) {
182 current = current->next;
185 if (current == NULL) {
186 /* this key doesn't exist yet */
187 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
189 if (previous == NULL) {
190 dict->table[hash_code & dict->table_size] = current;
192 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
193 xbt_dict_rehash(dict);
195 previous->next = current;
198 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
199 key_len, (char *) current->content,
200 key_len, (char *) data, key_len, (char *) key);
201 /* there is already an element with the same key: overwrite it */
202 xbt_dictelm_set_data(dict, current, data, free_ctn);
207 * \brief Add data to the dict (null-terminated key)
209 * \param dict the dict
210 * \param key the key to set the new data
211 * \param data the data to add in the dict
212 * \param free_ctn function to call with (\a data as argument) when
213 * \a data is removed from the dictionary
215 * set the \a data in the structure under the \a key, which is a
216 * null terminated string.
218 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
219 const char *key, void *data,
220 void_f_pvoid_t free_ctn)
223 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
227 * \brief Retrieve data from the dict (arbitrary key)
229 * \param dict the dealer of data
230 * \param key the key to find data
231 * \param key_len the size of the \a key
232 * \return the data that we are looking for
234 * Search the given \a key. Throws not_found_error when not found.
236 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
241 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
242 xbt_dictelm_t current;
246 current = dict->table[hash_code & dict->table_size];
247 while (current != NULL &&
248 (hash_code != current->hash_code || key_len != current->key_len
249 || memcmp(key, current->key, key_len))) {
250 current = current->next;
254 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
256 return current->content;
260 * \brief like xbt_dict_get_ext(), but returning NULL when not found
262 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
266 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
267 xbt_dictelm_t current;
271 current = dict->table[hash_code & dict->table_size];
272 while (current != NULL &&
273 (hash_code != current->hash_code || key_len != current->key_len
274 || memcmp(key, current->key, key_len))) {
275 current = current->next;
281 return current->content;
285 * @brief retrieve the key associated to that object. Warning, that's a linear search
287 * Returns NULL if the object cannot be found
289 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
292 xbt_dictelm_t current;
295 for (i = 0; i <= dict->table_size; i++) {
296 current = dict->table[i];
297 while (current != NULL) {
298 if (current->content == data)
300 current = current->next;
308 * @brief retrieve the key associated to that xbt_dictelm_t.
311 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
317 * \brief Retrieve data from the dict (null-terminated key)
319 * \param dict the dealer of data
320 * \param key the key to find data
321 * \return the data that we are looking for
323 * Search the given \a key. Throws not_found_error when not found.
324 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
327 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
329 return xbt_dict_get_elm(dict, key)->content;
332 * \brief Retrieve element from the dict (null-terminated key)
334 * \param dict the dealer of data
335 * \param key the key to find data
336 * \return the s_xbt_dictelm_t that we are looking for
338 * Search the given \a key. Throws not_found_error when not found.
339 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
342 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
344 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
347 THROWF(not_found_error, 0, "key %s not found", key);
353 * \brief like xbt_dict_get(), but returning NULL when not found
355 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
357 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
362 return current->content;
365 * \brief like xbt_dict_get_elm(), but returning NULL when not found
367 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
369 unsigned int hash_code = xbt_str_hash(key);
370 xbt_dictelm_t current;
374 current = dict->table[hash_code & dict->table_size];
375 while (current != NULL &&
376 (hash_code != current->hash_code || strcmp(key, current->key)))
377 current = current->next;
383 * \brief Remove data from the dict (arbitrary key)
385 * \param dict the trash can
386 * \param key the key of the data to be removed
387 * \param key_len the size of the \a key
389 * Remove the entry associated with the given \a key (throws not_found)
391 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
396 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
397 xbt_dictelm_t current, previous = NULL;
401 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
402 current = dict->table[hash_code & dict->table_size];
403 while (current != NULL &&
404 (hash_code != current->hash_code || key_len != current->key_len
405 || strncmp(key, current->key, key_len))) {
406 previous = current; /* save the previous node */
407 current = current->next;
411 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
413 if (previous != NULL) {
414 previous->next = current->next;
416 dict->table[hash_code & dict->table_size] = current->next;
419 if (!dict->table[hash_code & dict->table_size])
422 xbt_dictelm_free(dict, current);
429 * \brief Remove data from the dict (null-terminated key)
431 * \param dict the dict
432 * \param key the key of the data to be removed
434 * Remove the entry associated with the given \a key
436 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
438 xbt_dict_remove_ext(dict, key, strlen(key));
441 #ifdef XBT_USE_DEPRECATED
443 * \brief Add data to the dict (arbitrary key)
444 * \param dict the container
445 * \param key the key to set the new data
446 * \param data the data to add in the dict
448 * Set the \a data in the structure under the \a key.
449 * Both \a data and \a key are considered as uintptr_t.
451 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
452 uintptr_t key, uintptr_t data)
454 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
458 * \brief Retrieve data from the dict (key considered as a uintptr_t)
460 * \param dict the dealer of data
461 * \param key the key to find data
462 * \return the data that we are looking for (or 0 if not found)
464 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
466 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
468 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
471 /** Remove a uintptr_t key from the dict */
472 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
474 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
479 * \brief Remove all data from the dict
480 * \param dict the dict
482 void xbt_dict_reset(xbt_dict_t dict)
486 xbt_dictelm_t current, previous = NULL;
490 if (dict->count == 0)
493 for (i = 0; i <= dict->table_size; i++) {
494 current = dict->table[i];
495 while (current != NULL) {
497 current = current->next;
498 xbt_dictelm_free(dict, previous);
500 dict->table[i] = NULL;
508 * \brief Return the number of elements in the dict.
509 * \param dict a dictionary
511 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
518 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
519 void xbt_dict_dump_output_string(void *s)
525 * \brief test if the dict is empty or not
527 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
529 return !dict || (xbt_dict_length(dict) == 0);
533 * \brief Outputs the content of the structure (debugging purpose)
535 * \param dict the exibitionist
536 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
538 * Outputs the content of the structure. (for debugging purpose). \a output is a
539 * function to output the data. If NULL, data won't be displayed.
542 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
545 xbt_dictelm_t element;
546 printf("Dict %p:\n", dict);
548 for (i = 0; i < dict->table_size; i++) {
549 element = dict->table[i];
552 while (element != NULL) {
553 printf(" %s -> '", element->key);
554 if (output != NULL) {
555 output(element->content);
558 element = element->next;
568 xbt_dynar_t all_sizes = NULL;
569 /** @brief shows some debugging info about the bucklet repartition */
570 void xbt_dict_dump_sizes(xbt_dict_t dict)
576 xbt_dictelm_t element;
577 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
579 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
580 dict->fill, dict->table_size);
582 for (i = 0; i < dict->table_size; i++) {
583 element = dict->table[i];
586 while (element != NULL) {
588 element = element->next;
591 if (xbt_dynar_length(sizes) <= size) {
593 xbt_dynar_set(sizes, size, &prevsize);
596 xbt_dynar_get_cpy(sizes, size, &prevsize);
598 xbt_dynar_set(sizes, size, &prevsize);
602 all_sizes = xbt_dynar_new(sizeof(int), NULL);
604 xbt_dynar_foreach(sizes, count, size) {
605 /* Copy values of this one into all_sizes */
607 if (xbt_dynar_length(all_sizes) <= count) {
609 xbt_dynar_set(all_sizes, count, &prevcount);
611 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
613 xbt_dynar_set(all_sizes, count, &prevcount);
616 /* Report current sizes */
621 printf("%uelm x %u cells; ", count, size);
625 xbt_dynar_free(&sizes);
629 * Create the dict mallocators.
630 * This is an internal XBT function called during the lib initialization.
631 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
633 void xbt_dict_preinit(void)
635 dict_elm_mallocator = xbt_mallocator_new(256,
636 dict_elm_mallocator_new_f,
637 dict_elm_mallocator_free_f,
638 dict_elm_mallocator_reset_f);
639 dict_het_elm_mallocator = xbt_mallocator_new(256,
640 dict_het_elm_mallocator_new_f,
641 dict_het_elm_mallocator_free_f,
642 dict_het_elm_mallocator_reset_f);
646 * Destroy the dict mallocators.
647 * This is an internal XBT function during the lib initialization
649 void xbt_dict_postexit(void)
651 if (dict_elm_mallocator != NULL) {
652 xbt_mallocator_free(dict_elm_mallocator);
653 dict_elm_mallocator = NULL;
654 xbt_mallocator_free(dict_het_elm_mallocator);
655 dict_het_elm_mallocator = NULL;
662 printf("Overall stats:");
663 xbt_dynar_foreach(all_sizes, count, size) {
668 printf("%uelm x %d cells; ", count, size);
672 printf("; %f elm per cell\n", avg / (double) total_count);
679 #include "portable.h"
681 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
683 XBT_TEST_SUITE("dict", "Dict data container");
685 static void debuged_add_ext(xbt_dict_t head, const char *key,
686 const char *data_to_fill, void_f_pvoid_t free_f)
688 char *data = xbt_strdup(data_to_fill);
690 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
693 xbt_dict_set(head, key, data, free_f);
694 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
695 xbt_dict_dump(head, (void (*)(void *)) &printf);
700 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
702 debuged_add_ext(head, key, key, free_f);
705 static void fill(xbt_dict_t * head, int homogeneous)
707 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
709 xbt_test_add("Fill in the dictionnary");
711 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
712 debuged_add(*head, "12", free_f);
713 debuged_add(*head, "12a", free_f);
714 debuged_add(*head, "12b", free_f);
715 debuged_add(*head, "123", free_f);
716 debuged_add(*head, "123456", free_f);
717 /* Child becomes child of what to add */
718 debuged_add(*head, "1234", free_f);
719 /* Need of common ancestor */
720 debuged_add(*head, "123457", free_f);
724 static void search_ext(xbt_dict_t head, const char *key, const char *data)
728 xbt_test_add("Search %s", key);
729 found = xbt_dict_get(head, key);
730 xbt_test_log("Found %s", found);
732 xbt_test_assert(found,
733 "data do not match expectations: found NULL while searching for %s",
736 xbt_test_assert(!strcmp(data, found),
737 "data do not match expectations: found %s while searching for %s",
740 xbt_test_assert(!found,
741 "data do not match expectations: found %s while searching for NULL",
746 static void search(xbt_dict_t head, const char *key)
748 search_ext(head, key, key);
751 static void debuged_remove(xbt_dict_t head, const char *key)
754 xbt_test_add("Remove '%s'", PRINTF_STR(key));
755 xbt_dict_remove(head, key);
756 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
760 static void traverse(xbt_dict_t head)
762 xbt_dict_cursor_t cursor = NULL;
767 xbt_dict_foreach(head, cursor, key, data) {
768 if (!key || !data || strcmp(key, data)) {
769 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
772 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
774 xbt_test_assert(!data || !strcmp(key, data),
775 "Key(%s) != value(%s). Aborting", key, data);
779 static void search_not_found(xbt_dict_t head, const char *data)
784 xbt_test_add("Search %s (expected not to be found)", data);
787 data = xbt_dict_get(head, data);
788 THROWF(unknown_error, 0,
789 "Found something which shouldn't be there (%s)", data);
792 if (e.category != not_found_error)
793 xbt_test_exception(e);
797 xbt_test_assert(ok, "Exception not raised");
800 static void count(xbt_dict_t dict, int length)
802 xbt_dict_cursor_t cursor;
808 xbt_test_add("Count elements (expecting %d)", length);
809 xbt_test_assert(xbt_dict_length(dict) == length,
810 "Announced length(%d) != %d.", xbt_dict_length(dict),
813 xbt_dict_foreach(dict, cursor, key, data)
816 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
821 static void count_check_get_key(xbt_dict_t dict, int length)
823 xbt_dict_cursor_t cursor;
825 _XBT_GNUC_UNUSED char *key2;
831 ("Count elements (expecting %d), and test the getkey function",
833 xbt_test_assert(xbt_dict_length(dict) == length,
834 "Announced length(%d) != %d.", xbt_dict_length(dict),
837 xbt_dict_foreach(dict, cursor, key, data) {
839 key2 = xbt_dict_get_key(dict, data);
840 xbt_assert(!strcmp(key, key2),
841 "The data was registered under %s instead of %s as expected",
845 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
851 xbt_dict_t head = NULL;
854 static void basic_test(int homogeneous)
856 void_f_pvoid_t free_f;
858 xbt_test_add("Traversal the null dictionary");
861 xbt_test_add("Traversal and search the empty dictionary");
862 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
865 debuged_remove(head, "12346");
868 if (e.category != not_found_error)
869 xbt_test_exception(e);
872 xbt_dict_free(&head);
874 free_f = homogeneous ? NULL : &free;
876 xbt_test_add("Traverse the full dictionary");
877 fill(&head, homogeneous);
878 count_check_get_key(head, 7);
880 debuged_add_ext(head, "toto", "tutu", free_f);
881 search_ext(head, "toto", "tutu");
882 debuged_remove(head, "toto");
887 xbt_test_add("Free the dictionary (twice)");
888 xbt_dict_free(&head);
889 xbt_dict_free(&head);
892 fill(&head, homogeneous);
893 count_check_get_key(head, 7);
894 xbt_test_add("Change 123 to 'Changed 123'");
895 xbt_dict_set(head, "123", strdup("Changed 123"), free_f);
896 count_check_get_key(head, 7);
898 xbt_test_add("Change 123 back to '123'");
899 xbt_dict_set(head, "123", strdup("123"), free_f);
900 count_check_get_key(head, 7);
902 xbt_test_add("Change 12a to 'Dummy 12a'");
903 xbt_dict_set(head, "12a", strdup("Dummy 12a"), free_f);
904 count_check_get_key(head, 7);
906 xbt_test_add("Change 12a to '12a'");
907 xbt_dict_set(head, "12a", strdup("12a"), free_f);
908 count_check_get_key(head, 7);
910 xbt_test_add("Traverse the resulting dictionary");
914 xbt_test_add("Search 123");
915 data = xbt_dict_get(head, "123");
916 xbt_test_assert(data);
917 xbt_test_assert(!strcmp("123", data));
919 search_not_found(head, "Can't be found");
920 search_not_found(head, "123 Can't be found");
921 search_not_found(head, "12345678 NOT");
926 search(head, "123456");
927 search(head, "1234");
928 search(head, "123457");
930 xbt_test_add("Traverse the resulting dictionary");
933 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
935 xbt_test_add("Free the dictionary twice");
936 xbt_dict_free(&head);
937 xbt_dict_free(&head);
939 xbt_test_add("Traverse the resulting dictionary");
943 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
948 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
953 static void remove_test(int homogeneous)
955 fill(&head, homogeneous);
957 xbt_test_add("Remove non existing data");
959 debuged_remove(head, "Does not exist");
962 if (e.category != not_found_error)
963 xbt_test_exception(e);
968 xbt_dict_free(&head);
971 ("Remove each data manually (traversing the resulting dictionary each time)");
972 fill(&head, homogeneous);
973 debuged_remove(head, "12a");
976 debuged_remove(head, "12b");
979 debuged_remove(head, "12");
982 debuged_remove(head, "123456");
986 debuged_remove(head, "12346");
989 if (e.category != not_found_error)
990 xbt_test_exception(e);
994 debuged_remove(head, "1234");
996 debuged_remove(head, "123457");
998 debuged_remove(head, "123");
1001 debuged_remove(head, "12346");
1004 if (e.category != not_found_error)
1005 xbt_test_exception(e);
1011 ("Free dict, create new fresh one, and then reset the dict");
1012 xbt_dict_free(&head);
1013 fill(&head, homogeneous);
1014 xbt_dict_reset(head);
1018 xbt_test_add("Free the dictionary twice");
1019 xbt_dict_free(&head);
1020 xbt_dict_free(&head);
1023 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1028 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1033 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1037 xbt_test_add("Store NULL under 'null'");
1038 xbt_dict_set(head, "null", NULL, NULL);
1039 search_ext(head, "null", NULL);
1041 xbt_test_add("Check whether I see it while traversing...");
1043 xbt_dict_cursor_t cursor = NULL;
1047 xbt_dict_foreach(head, cursor, key, data) {
1048 if (!key || !data || strcmp(key, data)) {
1049 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1051 xbt_test_log("Seen: %s", PRINTF_STR(key));
1054 if (!strcmp(key, "null"))
1057 xbt_test_assert(found,
1058 "the key 'null', associated to NULL is not found");
1060 xbt_dict_free(&head);
1063 #define NB_ELM 20000
1064 #define SIZEOFKEY 1024
1065 static int countelems(xbt_dict_t head)
1067 xbt_dict_cursor_t cursor;
1072 xbt_dict_foreach(head, cursor, key, data) {
1078 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1080 xbt_dict_t head = NULL;
1084 srand((unsigned int) time(NULL));
1086 for (i = 0; i < 10; i++) {
1087 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1089 ("Fill the struct, count its elems and frees the structure");
1091 ("using 1000 elements with %d chars long randomized keys.",
1093 head = xbt_dict_new();
1094 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1095 for (j = 0; j < 1000; j++) {
1097 key = xbt_malloc(SIZEOFKEY);
1100 for (k = 0; k < SIZEOFKEY - 1; k++)
1101 key[k] = rand() % ('z' - 'a') + 'a';
1103 /* printf("[%d %s]\n",j,key); */
1104 data = xbt_dict_get_or_null(head, key);
1105 } while (data != NULL);
1107 xbt_dict_set(head, key, key, &free);
1108 data = xbt_dict_get(head, key);
1109 xbt_test_assert(!strcmp(key, data),
1110 "Retrieved value (%s) != Injected value (%s)", key,
1115 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1117 xbt_dict_free(&head);
1118 xbt_dict_free(&head);
1122 head = xbt_dict_new();
1123 xbt_test_add("Fill %d elements, with keys being the number of element",
1125 for (j = 0; j < NB_ELM; j++) {
1126 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1128 key = xbt_malloc(10);
1130 sprintf(key, "%d", j);
1131 xbt_dict_set(head, key, key, &free);
1133 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1136 ("Count the elements (retrieving the key and data for each)");
1137 i = countelems(head);
1138 xbt_test_log("There is %d elements", i);
1140 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1141 key = xbt_malloc(10);
1142 for (i = 0; i < 20; i++) {
1144 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1145 for (j = 0; j < NB_ELM; j++) {
1147 sprintf(key, "%d", j);
1148 data = xbt_dict_get(head, key);
1149 xbt_test_assert(!strcmp(key, (char *) data),
1150 "with get, key=%s != data=%s", key, (char *) data);
1151 data = xbt_dict_get_ext(head, key, strlen(key));
1152 xbt_test_assert(!strcmp(key, (char *) data),
1153 "with get_ext, key=%s != data=%s", key,
1159 xbt_test_add("Remove my %d elements", NB_ELM);
1160 key = xbt_malloc(10);
1161 for (j = 0; j < NB_ELM; j++) {
1162 /* if (!(j%10000)) printf("."); fflush(stdout); */
1164 sprintf(key, "%d", j);
1165 xbt_dict_remove(head, key);
1170 xbt_test_add("Free the structure (twice)");
1171 xbt_dict_free(&head);
1172 xbt_dict_free(&head);
1175 #endif /* SIMGRID_TEST */