1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2011. 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. */
9 #define DJB2_HASH_FUNCTION
10 //#define FNV_HASH_FUNCTION
16 #include "xbt/mallocator.h"
17 #include "xbt_modinter.h"
18 #include "dict_private.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
21 "Dictionaries provide the same functionalities than hash tables");
25 * \return pointer to the destination
26 * \see xbt_dict_free()
28 * Creates and initialize a new dictionary with a default hashtable size.
30 xbt_dict_t xbt_dict_new(void)
34 dict = xbt_new(s_xbt_dict_t, 1);
36 dict->table_size = 127;
37 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
40 dict->homogeneous = 0;
47 * \param dict the dictionary to be freed
49 * Frees a dictionary with all the data
51 void xbt_dict_free(xbt_dict_t * dict)
54 xbt_dictelm_t current, previous;
58 // if ( *dict ) xbt_dict_dump_sizes(*dict);
60 if (dict != NULL && *dict != NULL) {
61 table_size = (*dict)->table_size;
62 table = (*dict)->table;
63 /* Warning: the size of the table is 'table_size+1'...
64 * This is because table_size is used as a binary mask in xbt_dict_rehash */
65 for (i = 0; (*dict)->count && i <= table_size; i++) {
67 while (current != NULL) {
69 current = current->next;
70 xbt_dictelm_free(*dict, previous);
81 * Returns the amount of elements in the dict
83 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
85 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
89 * Returns the hash code of a string.
91 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str,
96 #ifdef DJB2_HASH_FUNCTION
97 /* fast implementation of djb2 algorithm */
99 register unsigned int hash = 5381;
103 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
105 # elif defined(FNV_HASH_FUNCTION)
106 register unsigned int hash = 0x811c9dc5;
107 unsigned char *bp = (unsigned char *) str; /* start of buffer */
108 unsigned char *be = bp + str_len; /* beyond end of buffer */
111 /* multiply by the 32 bit FNV magic prime mod 2^32 */
113 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
116 /* xor the bottom with the current octet */
117 hash ^= (unsigned int) *bp++;
121 register unsigned int hash = 0;
124 hash += (*str) * (*str);
132 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
134 #ifdef DJB2_HASH_FUNCTION
135 /* fast implementation of djb2 algorithm */
137 register unsigned int hash = 5381;
139 while ((c = *str++)) {
140 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
143 # elif defined(FNV_HASH_FUNCTION)
144 register unsigned int hash = 0x811c9dc5;
147 /* multiply by the 32 bit FNV magic prime mod 2^32 */
149 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
152 /* xor the bottom with the current byte */
153 hash ^= (unsigned int) *str++;
157 register unsigned int hash = 0;
160 hash += (*str) * (*str);
167 /* Expend the size of the dict */
168 static void xbt_dict_rehash(xbt_dict_t dict)
170 const int oldsize = dict->table_size + 1;
171 register int newsize = oldsize * 2;
173 register xbt_dictelm_t *currcell;
174 register xbt_dictelm_t *twincell;
175 register xbt_dictelm_t bucklet;
176 register xbt_dictelm_t *pprev;
179 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
180 newsize * sizeof(xbt_dictelm_t));
181 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
182 dict->table_size = --newsize;
183 dict->table = currcell;
184 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
186 for (i = 0; i < oldsize; i++, currcell++) {
187 if (!*currcell) /* empty cell */
189 twincell = currcell + oldsize;
190 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
191 /* Since we use "& size" instead of "%size" and since the size was doubled,
192 each bucklet of this cell must either :
193 - stay in cell i (ie, currcell)
194 - go to the cell i+oldsize (ie, twincell) */
195 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
196 *pprev = bucklet->next;
197 bucklet->next = *twincell;
203 pprev = &bucklet->next;
208 if (!*currcell) /* everything moved */
214 * \brief Add data to the dict (arbitrary key)
215 * \param dict the container
216 * \param key the key to set the new data
217 * \param key_len the size of the \a key
218 * \param data the data to add in the dict
219 * \param free_ctn function to call with (\a data as argument) when
220 * \a data is removed from the dictionary
222 * Set the \a data in the structure under the \a key, which can be any kind
223 * of data, as long as its length is provided in \a key_len.
225 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
226 const char *key, int key_len,
227 void *data, void_f_pvoid_t free_ctn)
230 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
232 xbt_dictelm_t current, previous = NULL;
235 XBT_DEBUG("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
236 dict->table_size, hash_code & dict->table_size);
237 current = dict->table[hash_code & dict->table_size];
238 while (current != NULL &&
239 (hash_code != current->hash_code || key_len != current->key_len
240 || memcmp(key, current->key, key_len))) {
242 current = current->next;
245 if (current == NULL) {
246 /* this key doesn't exist yet */
247 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
249 if (previous == NULL) {
250 dict->table[hash_code & dict->table_size] = current;
252 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
253 xbt_dict_rehash(dict);
255 previous->next = current;
258 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
259 key_len, (char *) current->content,
260 key_len, (char *) data, key_len, (char *) key);
261 /* there is already an element with the same key: overwrite it */
262 xbt_dictelm_set_data(dict, current, data, free_ctn);
267 * \brief Add data to the dict (null-terminated key)
269 * \param dict the dict
270 * \param key the key to set the new data
271 * \param data the data to add in the dict
272 * \param free_ctn function to call with (\a data as argument) when
273 * \a data is removed from the dictionary
275 * set the \a data in the structure under the \a key, which is a
276 * null terminated string.
278 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
279 const char *key, void *data,
280 void_f_pvoid_t free_ctn)
283 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
287 * \brief Retrieve data from the dict (arbitrary key)
289 * \param dict the dealer of data
290 * \param key the key to find data
291 * \param key_len the size of the \a key
292 * \return the data that we are looking for
294 * Search the given \a key. Throws not_found_error when not found.
296 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
301 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
302 xbt_dictelm_t current;
306 current = dict->table[hash_code & dict->table_size];
307 while (current != NULL &&
308 (hash_code != current->hash_code || key_len != current->key_len
309 || memcmp(key, current->key, key_len))) {
310 current = current->next;
314 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
316 return current->content;
320 * \brief like xbt_dict_get_ext(), but returning NULL when not found
322 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
326 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
327 xbt_dictelm_t current;
331 current = dict->table[hash_code & dict->table_size];
332 while (current != NULL &&
333 (hash_code != current->hash_code || key_len != current->key_len
334 || memcmp(key, current->key, key_len))) {
335 current = current->next;
341 return current->content;
345 * @brief retrieve the key associated to that object. Warning, that's a linear search
347 * Returns NULL if the object cannot be found
349 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
352 xbt_dictelm_t current;
355 for (i = 0; i <= dict->table_size; i++) {
356 current = dict->table[i];
357 while (current != NULL) {
358 if (current->content == data)
360 current = current->next;
368 * \brief Retrieve data from the dict (null-terminated key)
370 * \param dict the dealer of data
371 * \param key the key to find data
372 * \return the data that we are looking for
374 * Search the given \a key. Throws not_found_error when not found.
375 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
378 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
381 unsigned int hash_code = xbt_dict_hash(key);
382 xbt_dictelm_t current;
386 current = dict->table[hash_code & dict->table_size];
387 while (current != NULL
388 && (hash_code != current->hash_code || strcmp(key, current->key)))
389 current = current->next;
392 THROWF(not_found_error, 0, "key %s not found", key);
394 return current->content;
398 * \brief like xbt_dict_get(), but returning NULL when not found
400 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
402 unsigned int hash_code = xbt_dict_hash(key);
403 xbt_dictelm_t current;
407 current = dict->table[hash_code & dict->table_size];
408 while (current != NULL &&
409 (hash_code != current->hash_code || strcmp(key, current->key)))
410 current = current->next;
415 return current->content;
420 * \brief Remove data from the dict (arbitrary key)
422 * \param dict the trash can
423 * \param key the key of the data to be removed
424 * \param key_len the size of the \a key
426 * Remove the entry associated with the given \a key (throws not_found)
428 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
433 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
434 xbt_dictelm_t current, previous = NULL;
438 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
439 current = dict->table[hash_code & dict->table_size];
440 while (current != NULL &&
441 (hash_code != current->hash_code || key_len != current->key_len
442 || strncmp(key, current->key, key_len))) {
443 previous = current; /* save the previous node */
444 current = current->next;
448 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
450 if (previous != NULL) {
451 previous->next = current->next;
453 dict->table[hash_code & dict->table_size] = current->next;
456 if (!dict->table[hash_code & dict->table_size])
459 xbt_dictelm_free(dict, current);
466 * \brief Remove data from the dict (null-terminated key)
468 * \param dict the dict
469 * \param key the key of the data to be removed
471 * Remove the entry associated with the given \a key
473 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
475 xbt_dict_remove_ext(dict, key, strlen(key));
479 * \brief Add data to the dict (arbitrary key)
480 * \param dict the container
481 * \param key the key to set the new data
482 * \param data the data to add in the dict
484 * Set the \a data in the structure under the \a key.
485 * Both \a data and \a key are considered as uintptr_t.
487 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
488 uintptr_t key, uintptr_t data)
490 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
494 * \brief Retrieve data from the dict (key considered as a uintptr_t)
496 * \param dict the dealer of data
497 * \param key the key to find data
498 * \return the data that we are looking for (or 0 if not found)
500 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
502 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
504 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
507 /** Remove a uintptr_t key from the dict */
508 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
510 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
515 * \brief Remove all data from the dict
516 * \param dict the dict
518 void xbt_dict_reset(xbt_dict_t dict)
522 xbt_dictelm_t current, previous = NULL;
526 if (dict->count == 0)
529 for (i = 0; i <= dict->table_size; i++) {
530 current = dict->table[i];
531 while (current != NULL) {
533 current = current->next;
534 xbt_dictelm_free(dict, previous);
536 dict->table[i] = NULL;
544 * \brief Return the number of elements in the dict.
545 * \param dict a dictionary
547 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
554 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
555 void xbt_dict_dump_output_string(void *s)
561 * \brief test if the dict is empty or not
563 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
565 return !dict || (xbt_dict_length(dict) == 0);
569 * \brief Outputs the content of the structure (debugging purpose)
571 * \param dict the exibitionist
572 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
574 * Outputs the content of the structure. (for debugging purpose). \a output is a
575 * function to output the data. If NULL, data won't be displayed.
578 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
581 xbt_dictelm_t element;
582 printf("Dict %p:\n", dict);
584 for (i = 0; i < dict->table_size; i++) {
585 element = dict->table[i];
588 while (element != NULL) {
589 printf(" %s -> '", element->key);
590 if (output != NULL) {
591 output(element->content);
594 element = element->next;
604 xbt_dynar_t all_sizes = NULL;
605 /** @brief shows some debugging info about the bucklet repartition */
606 void xbt_dict_dump_sizes(xbt_dict_t dict)
612 xbt_dictelm_t element;
613 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
615 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
616 dict->fill, dict->table_size);
618 for (i = 0; i < dict->table_size; i++) {
619 element = dict->table[i];
622 while (element != NULL) {
624 element = element->next;
627 if (xbt_dynar_length(sizes) <= size) {
629 xbt_dynar_set(sizes, size, &prevsize);
632 xbt_dynar_get_cpy(sizes, size, &prevsize);
634 xbt_dynar_set(sizes, size, &prevsize);
638 all_sizes = xbt_dynar_new(sizeof(int), NULL);
640 xbt_dynar_foreach(sizes, count, size) {
641 /* Copy values of this one into all_sizes */
643 if (xbt_dynar_length(all_sizes) <= count) {
645 xbt_dynar_set(all_sizes, count, &prevcount);
647 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
649 xbt_dynar_set(all_sizes, count, &prevcount);
652 /* Report current sizes */
657 printf("%delm x %u cells; ", count, size);
661 xbt_dynar_free(&sizes);
665 * Create the dict mallocators.
666 * This is an internal XBT function called during the lib initialization.
667 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
669 void xbt_dict_preinit(void)
671 if (dict_elm_mallocator != NULL) {
672 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
673 xbt_mallocator_free(dict_elm_mallocator);
674 xbt_mallocator_free(dict_het_elm_mallocator);
677 dict_elm_mallocator = xbt_mallocator_new(256,
678 dict_elm_mallocator_new_f,
679 dict_elm_mallocator_free_f,
680 dict_elm_mallocator_reset_f);
681 dict_het_elm_mallocator = xbt_mallocator_new(256,
682 dict_het_elm_mallocator_new_f,
683 dict_het_elm_mallocator_free_f,
684 dict_het_elm_mallocator_reset_f);
688 * Destroy the dict mallocators.
689 * This is an internal XBT function during the lib initialization
691 void xbt_dict_postexit(void)
693 if (dict_elm_mallocator != NULL) {
694 xbt_mallocator_free(dict_elm_mallocator);
695 dict_elm_mallocator = NULL;
696 xbt_mallocator_free(dict_het_elm_mallocator);
697 dict_het_elm_mallocator = NULL;
704 printf("Overall stats:");
705 xbt_dynar_foreach(all_sizes, count, size) {
710 printf("%delm x %d cells; ", count, size);
714 printf("; %f elm per cell\n", avg / (double) total_count);
721 #include "portable.h"
723 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
724 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
726 XBT_TEST_SUITE("dict", "Dict data container");
728 static void print_str(void *str)
730 printf("%s", (char *) PRINTF_STR(str));
733 static void debuged_add_ext(xbt_dict_t head, const char *key,
734 const char *data_to_fill)
736 char *data = xbt_strdup(data_to_fill);
738 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
741 xbt_dict_set(head, key, data, &free);
742 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
743 xbt_dict_dump(head, (void (*)(void *)) &printf);
748 static void debuged_add(xbt_dict_t head, const char *key)
750 debuged_add_ext(head, key, key);
753 static void fill(xbt_dict_t * head)
755 xbt_test_add("Fill in the dictionnary");
757 *head = xbt_dict_new();
758 debuged_add(*head, "12");
759 debuged_add(*head, "12a");
760 debuged_add(*head, "12b");
761 debuged_add(*head, "123");
762 debuged_add(*head, "123456");
763 /* Child becomes child of what to add */
764 debuged_add(*head, "1234");
765 /* Need of common ancestor */
766 debuged_add(*head, "123457");
770 static void search_ext(xbt_dict_t head, const char *key, const char *data)
774 xbt_test_add("Search %s", key);
775 found = xbt_dict_get(head, key);
776 xbt_test_log("Found %s", (char *) found);
778 xbt_test_assert(found,
779 "data do not match expectations: found NULL while searching for %s",
782 xbt_test_assert(!strcmp((char *) data, found),
783 "data do not match expectations: found %s while searching for %s",
784 (char *) found, data);
787 static void search(xbt_dict_t head, const char *key)
789 search_ext(head, key, key);
792 static void debuged_remove(xbt_dict_t head, const char *key)
795 xbt_test_add("Remove '%s'", PRINTF_STR(key));
796 xbt_dict_remove(head, key);
797 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
801 static void traverse(xbt_dict_t head)
803 xbt_dict_cursor_t cursor = NULL;
808 xbt_dict_foreach(head, cursor, key, data) {
809 if (!key || !data || strcmp(key, data)) {
810 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
813 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
815 xbt_test_assert(!data || !strcmp(key, data),
816 "Key(%s) != value(%s). Aborting", key, data);
820 static void search_not_found(xbt_dict_t head, const char *data)
825 xbt_test_add("Search %s (expected not to be found)", data);
828 data = xbt_dict_get(head, data);
829 THROWF(unknown_error, 0,
830 "Found something which shouldn't be there (%s)", data);
833 if (e.category != not_found_error)
834 xbt_test_exception(e);
838 xbt_test_assert(ok, "Exception not raised");
841 static void count(xbt_dict_t dict, int length)
843 xbt_dict_cursor_t cursor;
849 xbt_test_add("Count elements (expecting %d)", length);
850 xbt_test_assert(xbt_dict_length(dict) == length,
851 "Announced length(%d) != %d.", xbt_dict_length(dict),
854 xbt_dict_foreach(dict, cursor, key, data)
857 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
862 static void count_check_get_key(xbt_dict_t dict, int length)
864 xbt_dict_cursor_t cursor;
866 _XBT_GNUC_UNUSED char *key2;
872 ("Count elements (expecting %d), and test the getkey function",
874 xbt_test_assert(xbt_dict_length(dict) == length,
875 "Announced length(%d) != %d.", xbt_dict_length(dict),
878 xbt_dict_foreach(dict, cursor, key, data) {
880 key2 = xbt_dict_get_key(dict, data);
881 xbt_assert(!strcmp(key, key2),
882 "The data was registered under %s instead of %s as expected",
886 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
892 xbt_dict_t head = NULL;
896 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
898 xbt_test_add("Traversal the null dictionary");
901 xbt_test_add("Traversal and search the empty dictionary");
902 head = xbt_dict_new();
905 debuged_remove(head, "12346");
908 if (e.category != not_found_error)
909 xbt_test_exception(e);
912 xbt_dict_free(&head);
914 xbt_test_add("Traverse the full dictionary");
916 count_check_get_key(head, 7);
918 debuged_add_ext(head, "toto", "tutu");
919 search_ext(head, "toto", "tutu");
920 debuged_remove(head, "toto");
925 xbt_test_add("Free the dictionary (twice)");
926 xbt_dict_free(&head);
927 xbt_dict_free(&head);
931 count_check_get_key(head, 7);
932 xbt_test_add("Change 123 to 'Changed 123'");
933 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
934 count_check_get_key(head, 7);
936 xbt_test_add("Change 123 back to '123'");
937 xbt_dict_set(head, "123", strdup("123"), &free);
938 count_check_get_key(head, 7);
940 xbt_test_add("Change 12a to 'Dummy 12a'");
941 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
942 count_check_get_key(head, 7);
944 xbt_test_add("Change 12a to '12a'");
945 xbt_dict_set(head, "12a", strdup("12a"), &free);
946 count_check_get_key(head, 7);
948 xbt_test_add("Traverse the resulting dictionary");
952 xbt_test_add("Search 123");
953 data = xbt_dict_get(head, "123");
954 xbt_test_assert(data);
955 xbt_test_assert(!strcmp("123", data));
957 search_not_found(head, "Can't be found");
958 search_not_found(head, "123 Can't be found");
959 search_not_found(head, "12345678 NOT");
964 search(head, "123456");
965 search(head, "1234");
966 search(head, "123457");
968 xbt_test_add("Traverse the resulting dictionary");
971 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
973 xbt_test_add("Free the dictionary twice");
974 xbt_dict_free(&head);
975 xbt_dict_free(&head);
977 xbt_test_add("Traverse the resulting dictionary");
981 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
985 xbt_test_add("Remove non existing data");
987 debuged_remove(head, "Does not exist");
990 if (e.category != not_found_error)
991 xbt_test_exception(e);
996 xbt_dict_free(&head);
999 ("Remove each data manually (traversing the resulting dictionary each time)");
1001 debuged_remove(head, "12a");
1004 debuged_remove(head, "12b");
1007 debuged_remove(head, "12");
1010 debuged_remove(head, "123456");
1014 debuged_remove(head, "12346");
1017 if (e.category != not_found_error)
1018 xbt_test_exception(e);
1022 debuged_remove(head, "1234");
1024 debuged_remove(head, "123457");
1026 debuged_remove(head, "123");
1029 debuged_remove(head, "12346");
1032 if (e.category != not_found_error)
1033 xbt_test_exception(e);
1039 ("Free dict, create new fresh one, and then reset the dict");
1040 xbt_dict_free(&head);
1042 xbt_dict_reset(head);
1046 xbt_test_add("Free the dictionary twice");
1047 xbt_dict_free(&head);
1048 xbt_dict_free(&head);
1051 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1055 xbt_test_add("Store NULL under 'null'");
1056 xbt_dict_set(head, "null", NULL, NULL);
1057 search_ext(head, "null", NULL);
1059 xbt_test_add("Check whether I see it while traversing...");
1061 xbt_dict_cursor_t cursor = NULL;
1065 xbt_dict_foreach(head, cursor, key, data) {
1066 if (!key || !data || strcmp(key, data)) {
1067 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1069 xbt_test_log("Seen: %s", PRINTF_STR(key));
1072 if (!strcmp(key, "null"))
1075 xbt_test_assert(found,
1076 "the key 'null', associated to NULL is not found");
1078 xbt_dict_free(&head);
1081 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1083 uintptr_t stored_data = 0;
1084 xbt_test_log("Add %zu under %zu", data, key);
1086 xbt_dicti_set(head, key, data);
1087 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1088 xbt_dict_dump(head, (void (*)(void *)) &printf);
1091 stored_data = xbt_dicti_get(head, key);
1092 xbt_test_assert(stored_data == data,
1093 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1094 stored_data, data, key);
1097 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1099 xbt_test_add("Fill in the dictionnary");
1101 head = xbt_dict_new();
1102 debuged_addi(head, 12, 12);
1103 debuged_addi(head, 13, 13);
1104 debuged_addi(head, 14, 14);
1105 debuged_addi(head, 15, 15);
1107 debuged_addi(head, 12, 15);
1108 debuged_addi(head, 15, 2000);
1109 debuged_addi(head, 15, 3000);
1111 debuged_addi(head, 0, 1000);
1112 debuged_addi(head, 0, 2000);
1113 debuged_addi(head, 0, 3000);
1115 debuged_addi(head, 12, 0);
1116 debuged_addi(head, 13, 0);
1117 debuged_addi(head, 12, 0);
1118 debuged_addi(head, 0, 0);
1120 xbt_dict_free(&head);
1123 #define NB_ELM 20000
1124 #define SIZEOFKEY 1024
1125 static int countelems(xbt_dict_t head)
1127 xbt_dict_cursor_t cursor;
1132 xbt_dict_foreach(head, cursor, key, data) {
1138 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1140 xbt_dict_t head = NULL;
1145 srand((unsigned int) time(NULL));
1147 for (i = 0; i < 10; i++) {
1148 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1150 ("Fill the struct, count its elems and frees the structure");
1152 ("using 1000 elements with %d chars long randomized keys.",
1154 head = xbt_dict_new();
1155 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1156 for (j = 0; j < 1000; j++) {
1158 key = xbt_malloc(SIZEOFKEY);
1161 for (k = 0; k < SIZEOFKEY - 1; k++)
1162 key[k] = rand() % ('z' - 'a') + 'a';
1164 /* printf("[%d %s]\n",j,key); */
1165 data = xbt_dict_get_or_null(head, key);
1166 } while (data != NULL);
1168 xbt_dict_set(head, key, key, &free);
1169 data = xbt_dict_get(head, key);
1170 xbt_test_assert(!strcmp(key, data),
1171 "Retrieved value (%s) != Injected value (%s)", key,
1176 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1178 xbt_dict_free(&head);
1179 xbt_dict_free(&head);
1183 head = xbt_dict_new();
1184 xbt_test_add("Fill %d elements, with keys being the number of element",
1186 for (j = 0; j < NB_ELM; j++) {
1187 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1189 key = xbt_malloc(10);
1191 sprintf(key, "%d", j);
1192 xbt_dict_set(head, key, key, &free);
1194 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1197 ("Count the elements (retrieving the key and data for each)");
1198 i = countelems(head);
1199 xbt_test_log("There is %d elements", i);
1201 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1202 key = xbt_malloc(10);
1203 for (i = 0; i < 20; i++) {
1204 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1205 for (j = 0; j < NB_ELM; j++) {
1207 sprintf(key, "%d", j);
1208 data = xbt_dict_get(head, key);
1209 xbt_test_assert(!strcmp(key, (char *) data),
1210 "with get, key=%s != data=%s", key, (char *) data);
1211 data = xbt_dict_get_ext(head, key, strlen(key));
1212 xbt_test_assert(!strcmp(key, (char *) data),
1213 "with get_ext, key=%s != data=%s", key,
1219 xbt_test_add("Remove my %d elements", NB_ELM);
1220 key = xbt_malloc(10);
1221 for (j = 0; j < NB_ELM; j++) {
1222 /* if (!(j%10000)) printf("."); fflush(stdout); */
1224 sprintf(key, "%d", j);
1225 xbt_dict_remove(head, key);
1230 xbt_test_add("Free the structure (twice)");
1231 xbt_dict_free(&head);
1232 xbt_dict_free(&head);
1235 static void str_free(void *s)
1237 char *c = *(char **) s;
1241 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1245 #define NB_ELM 100 /*00 */
1247 #define KEY_SIZE 512
1248 #define NB_TEST 20 /*20 */
1251 xbt_dict_t mdict = NULL;
1253 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1258 xbt_test_add("Generic multicache CRASH test");
1260 (" Fill the struct and frees it %d times, using %d elements, "
1261 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1264 for (l = 0; l < DEPTH; l++) {
1265 key = xbt_malloc(KEY_SIZE);
1266 xbt_dynar_push(keys, &key);
1269 for (i = 0; i < NB_TEST; i++) {
1270 mdict = xbt_dict_new();
1271 XBT_VERB("mdict=%p", mdict);
1273 printf("Test %d\n", i);
1274 /* else if (i%10) printf("."); else printf("%d",i/10); */
1276 for (j = 0; j < NB_ELM; j++) {
1280 for (l = 0; l < DEPTH; l++) {
1281 key = *(char **) xbt_dynar_get_ptr(keys, l);
1283 for (k = 0; k < KEY_SIZE - 1; k++)
1284 key[k] = rand() % ('z' - 'a') + 'a';
1289 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1292 printf("in multitree %p.\n", mdict);
1294 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1296 data = xbt_multidict_get(mdict, keys);
1298 xbt_test_assert(data && !strcmp((char *) data, key),
1299 "Retrieved value (%s) does not match the given one (%s)\n",
1300 (char *) data, key);
1302 xbt_dict_free(&mdict);
1305 xbt_dynar_free(&keys);
1308 xbt_dict_dump(mdict,&xbt_dict_print); */
1310 xbt_dict_free(&mdict);
1311 xbt_dynar_free(&keys);
1314 #endif /* SIMGRID_TEST */