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 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
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 #define PRINTF_STR(a) (a)?:"(null)"
683 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
685 XBT_TEST_SUITE("dict", "Dict data container");
687 static void debuged_add_ext(xbt_dict_t head, const char *key,
688 const char *data_to_fill, void_f_pvoid_t free_f)
690 char *data = xbt_strdup(data_to_fill);
692 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
695 xbt_dict_set(head, key, data, free_f);
696 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
697 xbt_dict_dump(head, (void (*)(void *)) &printf);
702 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
704 debuged_add_ext(head, key, key, free_f);
707 static void fill(xbt_dict_t * head, int homogeneous)
709 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
711 xbt_test_add("Fill in the dictionnary");
713 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
714 debuged_add(*head, "12", free_f);
715 debuged_add(*head, "12a", free_f);
716 debuged_add(*head, "12b", free_f);
717 debuged_add(*head, "123", free_f);
718 debuged_add(*head, "123456", free_f);
719 /* Child becomes child of what to add */
720 debuged_add(*head, "1234", free_f);
721 /* Need of common ancestor */
722 debuged_add(*head, "123457", free_f);
726 static void search_ext(xbt_dict_t head, const char *key, const char *data)
730 xbt_test_add("Search %s", key);
731 found = xbt_dict_get(head, key);
732 xbt_test_log("Found %s", found);
734 xbt_test_assert(found,
735 "data do not match expectations: found NULL while searching for %s",
738 xbt_test_assert(!strcmp(data, found),
739 "data do not match expectations: found %s while searching for %s",
742 xbt_test_assert(!found,
743 "data do not match expectations: found %s while searching for NULL",
748 static void search(xbt_dict_t head, const char *key)
750 search_ext(head, key, key);
753 static void debuged_remove(xbt_dict_t head, const char *key)
756 xbt_test_add("Remove '%s'", PRINTF_STR(key));
757 xbt_dict_remove(head, key);
758 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
762 static void traverse(xbt_dict_t head)
764 xbt_dict_cursor_t cursor = NULL;
769 xbt_dict_foreach(head, cursor, key, data) {
770 if (!key || !data || strcmp(key, data)) {
771 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
774 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
776 xbt_test_assert(!data || !strcmp(key, data),
777 "Key(%s) != value(%s). Aborting", key, data);
781 static void search_not_found(xbt_dict_t head, const char *data)
786 xbt_test_add("Search %s (expected not to be found)", data);
789 data = xbt_dict_get(head, data);
790 THROWF(unknown_error, 0,
791 "Found something which shouldn't be there (%s)", data);
794 if (e.category != not_found_error)
795 xbt_test_exception(e);
799 xbt_test_assert(ok, "Exception not raised");
802 static void count(xbt_dict_t dict, int length)
804 xbt_dict_cursor_t cursor;
810 xbt_test_add("Count elements (expecting %d)", length);
811 xbt_test_assert(xbt_dict_length(dict) == length,
812 "Announced length(%d) != %d.", xbt_dict_length(dict),
815 xbt_dict_foreach(dict, cursor, key, data)
818 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
823 static void count_check_get_key(xbt_dict_t dict, int length)
825 xbt_dict_cursor_t cursor;
827 _XBT_GNUC_UNUSED char *key2;
833 ("Count elements (expecting %d), and test the getkey function",
835 xbt_test_assert(xbt_dict_length(dict) == length,
836 "Announced length(%d) != %d.", xbt_dict_length(dict),
839 xbt_dict_foreach(dict, cursor, key, data) {
841 key2 = xbt_dict_get_key(dict, data);
842 xbt_assert(!strcmp(key, key2),
843 "The data was registered under %s instead of %s as expected",
847 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
853 xbt_dict_t head = NULL;
856 static void basic_test(int homogeneous)
858 void_f_pvoid_t free_f;
860 xbt_test_add("Traversal the null dictionary");
863 xbt_test_add("Traversal and search the empty dictionary");
864 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
867 debuged_remove(head, "12346");
870 if (e.category != not_found_error)
871 xbt_test_exception(e);
874 xbt_dict_free(&head);
876 free_f = homogeneous ? NULL : &free;
878 xbt_test_add("Traverse the full dictionary");
879 fill(&head, homogeneous);
880 count_check_get_key(head, 7);
882 debuged_add_ext(head, "toto", "tutu", free_f);
883 search_ext(head, "toto", "tutu");
884 debuged_remove(head, "toto");
889 xbt_test_add("Free the dictionary (twice)");
890 xbt_dict_free(&head);
891 xbt_dict_free(&head);
894 fill(&head, homogeneous);
895 count_check_get_key(head, 7);
896 xbt_test_add("Change 123 to 'Changed 123'");
897 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
898 count_check_get_key(head, 7);
900 xbt_test_add("Change 123 back to '123'");
901 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
902 count_check_get_key(head, 7);
904 xbt_test_add("Change 12a to 'Dummy 12a'");
905 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
906 count_check_get_key(head, 7);
908 xbt_test_add("Change 12a to '12a'");
909 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
910 count_check_get_key(head, 7);
912 xbt_test_add("Traverse the resulting dictionary");
916 xbt_test_add("Search 123");
917 data = xbt_dict_get(head, "123");
918 xbt_test_assert(data);
919 xbt_test_assert(!strcmp("123", data));
921 search_not_found(head, "Can't be found");
922 search_not_found(head, "123 Can't be found");
923 search_not_found(head, "12345678 NOT");
928 search(head, "123456");
929 search(head, "1234");
930 search(head, "123457");
932 xbt_test_add("Traverse the resulting dictionary");
935 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
937 xbt_test_add("Free the dictionary twice");
938 xbt_dict_free(&head);
939 xbt_dict_free(&head);
941 xbt_test_add("Traverse the resulting dictionary");
945 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
950 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
955 static void remove_test(int homogeneous)
957 fill(&head, homogeneous);
959 xbt_test_add("Remove non existing data");
961 debuged_remove(head, "Does not exist");
964 if (e.category != not_found_error)
965 xbt_test_exception(e);
970 xbt_dict_free(&head);
973 ("Remove each data manually (traversing the resulting dictionary each time)");
974 fill(&head, homogeneous);
975 debuged_remove(head, "12a");
978 debuged_remove(head, "12b");
981 debuged_remove(head, "12");
984 debuged_remove(head, "123456");
988 debuged_remove(head, "12346");
991 if (e.category != not_found_error)
992 xbt_test_exception(e);
996 debuged_remove(head, "1234");
998 debuged_remove(head, "123457");
1000 debuged_remove(head, "123");
1003 debuged_remove(head, "12346");
1006 if (e.category != not_found_error)
1007 xbt_test_exception(e);
1013 ("Free dict, create new fresh one, and then reset the dict");
1014 xbt_dict_free(&head);
1015 fill(&head, homogeneous);
1016 xbt_dict_reset(head);
1020 xbt_test_add("Free the dictionary twice");
1021 xbt_dict_free(&head);
1022 xbt_dict_free(&head);
1025 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1030 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1035 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1039 xbt_test_add("Store NULL under 'null'");
1040 xbt_dict_set(head, "null", NULL, NULL);
1041 search_ext(head, "null", NULL);
1043 xbt_test_add("Check whether I see it while traversing...");
1045 xbt_dict_cursor_t cursor = NULL;
1049 xbt_dict_foreach(head, cursor, key, data) {
1050 if (!key || !data || strcmp(key, data)) {
1051 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1053 xbt_test_log("Seen: %s", PRINTF_STR(key));
1056 if (!strcmp(key, "null"))
1059 xbt_test_assert(found,
1060 "the key 'null', associated to NULL is not found");
1062 xbt_dict_free(&head);
1065 #define NB_ELM 20000
1066 #define SIZEOFKEY 1024
1067 static int countelems(xbt_dict_t head)
1069 xbt_dict_cursor_t cursor;
1074 xbt_dict_foreach(head, cursor, key, data) {
1080 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1082 xbt_dict_t head = NULL;
1086 srand((unsigned int) time(NULL));
1088 for (i = 0; i < 10; i++) {
1089 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1091 ("Fill the struct, count its elems and frees the structure");
1093 ("using 1000 elements with %d chars long randomized keys.",
1095 head = xbt_dict_new();
1096 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1097 for (j = 0; j < 1000; j++) {
1099 key = xbt_malloc(SIZEOFKEY);
1102 for (k = 0; k < SIZEOFKEY - 1; k++)
1103 key[k] = rand() % ('z' - 'a') + 'a';
1105 /* printf("[%d %s]\n",j,key); */
1106 data = xbt_dict_get_or_null(head, key);
1107 } while (data != NULL);
1109 xbt_dict_set(head, key, key, &free);
1110 data = xbt_dict_get(head, key);
1111 xbt_test_assert(!strcmp(key, data),
1112 "Retrieved value (%s) != Injected value (%s)", key,
1117 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1119 xbt_dict_free(&head);
1120 xbt_dict_free(&head);
1124 head = xbt_dict_new();
1125 xbt_test_add("Fill %d elements, with keys being the number of element",
1127 for (j = 0; j < NB_ELM; j++) {
1128 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1130 key = xbt_malloc(10);
1132 sprintf(key, "%d", j);
1133 xbt_dict_set(head, key, key, &free);
1135 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1138 ("Count the elements (retrieving the key and data for each)");
1139 i = countelems(head);
1140 xbt_test_log("There is %d elements", i);
1142 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1143 key = xbt_malloc(10);
1144 for (i = 0; i < 20; i++) {
1146 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1147 for (j = 0; j < NB_ELM; j++) {
1149 sprintf(key, "%d", j);
1150 data = xbt_dict_get(head, key);
1151 xbt_test_assert(!strcmp(key, (char *) data),
1152 "with get, key=%s != data=%s", key, (char *) data);
1153 data = xbt_dict_get_ext(head, key, strlen(key));
1154 xbt_test_assert(!strcmp(key, (char *) data),
1155 "with get_ext, key=%s != data=%s", key,
1161 xbt_test_add("Remove my %d elements", NB_ELM);
1162 key = xbt_malloc(10);
1163 for (j = 0; j < NB_ELM; j++) {
1164 /* if (!(j%10000)) printf("."); fflush(stdout); */
1166 sprintf(key, "%d", j);
1167 xbt_dict_remove(head, key);
1172 xbt_test_add("Free the structure (twice)");
1173 xbt_dict_free(&head);
1174 xbt_dict_free(&head);
1177 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1179 xbt_dict_t dict = xbt_dict_new();
1182 xbt_test_add("Insert elements");
1184 for (i = 0; i < count; ++i)
1185 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1186 xbt_test_assert(xbt_dict_size(dict) == count,
1187 "Bad number of elements in the dictionnary");
1189 xbt_test_add("Check elements");
1190 for (i = 0; i < count; ++i) {
1191 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1192 xbt_test_assert(xbt_dict_size(dict) == count,
1193 "Unexpected value at index %i, expected %i but was %i", i, i, res);
1196 xbt_test_add("Free the array");
1197 xbt_dict_free(&dict);
1200 #endif /* SIMGRID_TEST */