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);
676 dict_elm_mallocator = xbt_mallocator_new(256,
677 dict_elm_mallocator_new_f,
678 dict_elm_mallocator_free_f,
679 dict_elm_mallocator_reset_f);
683 * Destroy the dict mallocators.
684 * This is an internal XBT function during the lib initialization
686 void xbt_dict_postexit(void)
688 if (dict_elm_mallocator != NULL) {
689 xbt_mallocator_free(dict_elm_mallocator);
690 dict_elm_mallocator = NULL;
697 printf("Overall stats:");
698 xbt_dynar_foreach(all_sizes, count, size) {
703 printf("%delm x %d cells; ", count, size);
707 printf("; %f elm per cell\n", avg / (double) total_count);
714 #include "portable.h"
716 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
717 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
719 XBT_TEST_SUITE("dict", "Dict data container");
721 static void print_str(void *str)
723 printf("%s", (char *) PRINTF_STR(str));
726 static void debuged_add_ext(xbt_dict_t head, const char *key,
727 const char *data_to_fill)
729 char *data = xbt_strdup(data_to_fill);
731 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
734 xbt_dict_set(head, key, data, &free);
735 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
736 xbt_dict_dump(head, (void (*)(void *)) &printf);
741 static void debuged_add(xbt_dict_t head, const char *key)
743 debuged_add_ext(head, key, key);
746 static void fill(xbt_dict_t * head)
748 xbt_test_add("Fill in the dictionnary");
750 *head = xbt_dict_new();
751 debuged_add(*head, "12");
752 debuged_add(*head, "12a");
753 debuged_add(*head, "12b");
754 debuged_add(*head, "123");
755 debuged_add(*head, "123456");
756 /* Child becomes child of what to add */
757 debuged_add(*head, "1234");
758 /* Need of common ancestor */
759 debuged_add(*head, "123457");
763 static void search_ext(xbt_dict_t head, const char *key, const char *data)
767 xbt_test_add("Search %s", key);
768 found = xbt_dict_get(head, key);
769 xbt_test_log("Found %s", (char *) found);
771 xbt_test_assert(found,
772 "data do not match expectations: found NULL while searching for %s",
775 xbt_test_assert(!strcmp((char *) data, found),
776 "data do not match expectations: found %s while searching for %s",
777 (char *) found, data);
780 static void search(xbt_dict_t head, const char *key)
782 search_ext(head, key, key);
785 static void debuged_remove(xbt_dict_t head, const char *key)
788 xbt_test_add("Remove '%s'", PRINTF_STR(key));
789 xbt_dict_remove(head, key);
790 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
794 static void traverse(xbt_dict_t head)
796 xbt_dict_cursor_t cursor = NULL;
801 xbt_dict_foreach(head, cursor, key, data) {
802 if (!key || !data || strcmp(key, data)) {
803 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
806 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
808 xbt_test_assert(!data || !strcmp(key, data),
809 "Key(%s) != value(%s). Aborting", key, data);
813 static void search_not_found(xbt_dict_t head, const char *data)
818 xbt_test_add("Search %s (expected not to be found)", data);
821 data = xbt_dict_get(head, data);
822 THROWF(unknown_error, 0,
823 "Found something which shouldn't be there (%s)", data);
826 if (e.category != not_found_error)
827 xbt_test_exception(e);
831 xbt_test_assert(ok, "Exception not raised");
834 static void count(xbt_dict_t dict, int length)
836 xbt_dict_cursor_t cursor;
842 xbt_test_add("Count elements (expecting %d)", length);
843 xbt_test_assert(xbt_dict_length(dict) == length,
844 "Announced length(%d) != %d.", xbt_dict_length(dict),
847 xbt_dict_foreach(dict, cursor, key, data)
850 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
855 static void count_check_get_key(xbt_dict_t dict, int length)
857 xbt_dict_cursor_t cursor;
859 _XBT_GNUC_UNUSED char *key2;
865 ("Count elements (expecting %d), and test the getkey function",
867 xbt_test_assert(xbt_dict_length(dict) == length,
868 "Announced length(%d) != %d.", xbt_dict_length(dict),
871 xbt_dict_foreach(dict, cursor, key, data) {
873 key2 = xbt_dict_get_key(dict, data);
874 xbt_assert(!strcmp(key, key2),
875 "The data was registered under %s instead of %s as expected",
879 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
885 xbt_dict_t head = NULL;
889 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
891 xbt_test_add("Traversal the null dictionary");
894 xbt_test_add("Traversal and search the empty dictionary");
895 head = xbt_dict_new();
898 debuged_remove(head, "12346");
901 if (e.category != not_found_error)
902 xbt_test_exception(e);
905 xbt_dict_free(&head);
907 xbt_test_add("Traverse the full dictionary");
909 count_check_get_key(head, 7);
911 debuged_add_ext(head, "toto", "tutu");
912 search_ext(head, "toto", "tutu");
913 debuged_remove(head, "toto");
918 xbt_test_add("Free the dictionary (twice)");
919 xbt_dict_free(&head);
920 xbt_dict_free(&head);
924 count_check_get_key(head, 7);
925 xbt_test_add("Change 123 to 'Changed 123'");
926 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
927 count_check_get_key(head, 7);
929 xbt_test_add("Change 123 back to '123'");
930 xbt_dict_set(head, "123", strdup("123"), &free);
931 count_check_get_key(head, 7);
933 xbt_test_add("Change 12a to 'Dummy 12a'");
934 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
935 count_check_get_key(head, 7);
937 xbt_test_add("Change 12a to '12a'");
938 xbt_dict_set(head, "12a", strdup("12a"), &free);
939 count_check_get_key(head, 7);
941 xbt_test_add("Traverse the resulting dictionary");
945 xbt_test_add("Search 123");
946 data = xbt_dict_get(head, "123");
947 xbt_test_assert(data);
948 xbt_test_assert(!strcmp("123", data));
950 search_not_found(head, "Can't be found");
951 search_not_found(head, "123 Can't be found");
952 search_not_found(head, "12345678 NOT");
957 search(head, "123456");
958 search(head, "1234");
959 search(head, "123457");
961 xbt_test_add("Traverse the resulting dictionary");
964 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
966 xbt_test_add("Free the dictionary twice");
967 xbt_dict_free(&head);
968 xbt_dict_free(&head);
970 xbt_test_add("Traverse the resulting dictionary");
974 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
978 xbt_test_add("Remove non existing data");
980 debuged_remove(head, "Does not exist");
983 if (e.category != not_found_error)
984 xbt_test_exception(e);
989 xbt_dict_free(&head);
992 ("Remove each data manually (traversing the resulting dictionary each time)");
994 debuged_remove(head, "12a");
997 debuged_remove(head, "12b");
1000 debuged_remove(head, "12");
1003 debuged_remove(head, "123456");
1007 debuged_remove(head, "12346");
1010 if (e.category != not_found_error)
1011 xbt_test_exception(e);
1015 debuged_remove(head, "1234");
1017 debuged_remove(head, "123457");
1019 debuged_remove(head, "123");
1022 debuged_remove(head, "12346");
1025 if (e.category != not_found_error)
1026 xbt_test_exception(e);
1032 ("Free dict, create new fresh one, and then reset the dict");
1033 xbt_dict_free(&head);
1035 xbt_dict_reset(head);
1039 xbt_test_add("Free the dictionary twice");
1040 xbt_dict_free(&head);
1041 xbt_dict_free(&head);
1044 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1048 xbt_test_add("Store NULL under 'null'");
1049 xbt_dict_set(head, "null", NULL, NULL);
1050 search_ext(head, "null", NULL);
1052 xbt_test_add("Check whether I see it while traversing...");
1054 xbt_dict_cursor_t cursor = NULL;
1058 xbt_dict_foreach(head, cursor, key, data) {
1059 if (!key || !data || strcmp(key, data)) {
1060 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1062 xbt_test_log("Seen: %s", PRINTF_STR(key));
1065 if (!strcmp(key, "null"))
1068 xbt_test_assert(found,
1069 "the key 'null', associated to NULL is not found");
1071 xbt_dict_free(&head);
1074 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1076 uintptr_t stored_data = 0;
1077 xbt_test_log("Add %zu under %zu", data, key);
1079 xbt_dicti_set(head, key, data);
1080 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1081 xbt_dict_dump(head, (void (*)(void *)) &printf);
1084 stored_data = xbt_dicti_get(head, key);
1085 xbt_test_assert(stored_data == data,
1086 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1087 stored_data, data, key);
1090 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1092 xbt_test_add("Fill in the dictionnary");
1094 head = xbt_dict_new();
1095 debuged_addi(head, 12, 12);
1096 debuged_addi(head, 13, 13);
1097 debuged_addi(head, 14, 14);
1098 debuged_addi(head, 15, 15);
1100 debuged_addi(head, 12, 15);
1101 debuged_addi(head, 15, 2000);
1102 debuged_addi(head, 15, 3000);
1104 debuged_addi(head, 0, 1000);
1105 debuged_addi(head, 0, 2000);
1106 debuged_addi(head, 0, 3000);
1108 debuged_addi(head, 12, 0);
1109 debuged_addi(head, 13, 0);
1110 debuged_addi(head, 12, 0);
1111 debuged_addi(head, 0, 0);
1113 xbt_dict_free(&head);
1116 #define NB_ELM 20000
1117 #define SIZEOFKEY 1024
1118 static int countelems(xbt_dict_t head)
1120 xbt_dict_cursor_t cursor;
1125 xbt_dict_foreach(head, cursor, key, data) {
1131 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1133 xbt_dict_t head = NULL;
1138 srand((unsigned int) time(NULL));
1140 for (i = 0; i < 10; i++) {
1141 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1143 ("Fill the struct, count its elems and frees the structure");
1145 ("using 1000 elements with %d chars long randomized keys.",
1147 head = xbt_dict_new();
1148 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1149 for (j = 0; j < 1000; j++) {
1151 key = xbt_malloc(SIZEOFKEY);
1154 for (k = 0; k < SIZEOFKEY - 1; k++)
1155 key[k] = rand() % ('z' - 'a') + 'a';
1157 /* printf("[%d %s]\n",j,key); */
1158 data = xbt_dict_get_or_null(head, key);
1159 } while (data != NULL);
1161 xbt_dict_set(head, key, key, &free);
1162 data = xbt_dict_get(head, key);
1163 xbt_test_assert(!strcmp(key, data),
1164 "Retrieved value (%s) != Injected value (%s)", key,
1169 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1171 xbt_dict_free(&head);
1172 xbt_dict_free(&head);
1176 head = xbt_dict_new();
1177 xbt_test_add("Fill %d elements, with keys being the number of element",
1179 for (j = 0; j < NB_ELM; j++) {
1180 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1182 key = xbt_malloc(10);
1184 sprintf(key, "%d", j);
1185 xbt_dict_set(head, key, key, &free);
1187 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1190 ("Count the elements (retrieving the key and data for each)");
1191 i = countelems(head);
1192 xbt_test_log("There is %d elements", i);
1194 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1195 key = xbt_malloc(10);
1196 for (i = 0; i < 20; i++) {
1197 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1198 for (j = 0; j < NB_ELM; j++) {
1200 sprintf(key, "%d", j);
1201 data = xbt_dict_get(head, key);
1202 xbt_test_assert(!strcmp(key, (char *) data),
1203 "with get, key=%s != data=%s", key, (char *) data);
1204 data = xbt_dict_get_ext(head, key, strlen(key));
1205 xbt_test_assert(!strcmp(key, (char *) data),
1206 "with get_ext, key=%s != data=%s", key,
1212 xbt_test_add("Remove my %d elements", NB_ELM);
1213 key = xbt_malloc(10);
1214 for (j = 0; j < NB_ELM; j++) {
1215 /* if (!(j%10000)) printf("."); fflush(stdout); */
1217 sprintf(key, "%d", j);
1218 xbt_dict_remove(head, key);
1223 xbt_test_add("Free the structure (twice)");
1224 xbt_dict_free(&head);
1225 xbt_dict_free(&head);
1228 static void str_free(void *s)
1230 char *c = *(char **) s;
1234 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1238 #define NB_ELM 100 /*00 */
1240 #define KEY_SIZE 512
1241 #define NB_TEST 20 /*20 */
1244 xbt_dict_t mdict = NULL;
1246 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1251 xbt_test_add("Generic multicache CRASH test");
1253 (" Fill the struct and frees it %d times, using %d elements, "
1254 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1257 for (l = 0; l < DEPTH; l++) {
1258 key = xbt_malloc(KEY_SIZE);
1259 xbt_dynar_push(keys, &key);
1262 for (i = 0; i < NB_TEST; i++) {
1263 mdict = xbt_dict_new();
1264 XBT_VERB("mdict=%p", mdict);
1266 printf("Test %d\n", i);
1267 /* else if (i%10) printf("."); else printf("%d",i/10); */
1269 for (j = 0; j < NB_ELM; j++) {
1273 for (l = 0; l < DEPTH; l++) {
1274 key = *(char **) xbt_dynar_get_ptr(keys, l);
1276 for (k = 0; k < KEY_SIZE - 1; k++)
1277 key[k] = rand() % ('z' - 'a') + 'a';
1282 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1285 printf("in multitree %p.\n", mdict);
1287 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1289 data = xbt_multidict_get(mdict, keys);
1291 xbt_test_assert(data && !strcmp((char *) data, key),
1292 "Retrieved value (%s) does not match the given one (%s)\n",
1293 (char *) data, key);
1295 xbt_dict_free(&mdict);
1298 xbt_dynar_free(&keys);
1301 xbt_dict_dump(mdict,&xbt_dict_print); */
1303 xbt_dict_free(&mdict);
1304 xbt_dynar_free(&keys);
1307 #endif /* SIMGRID_TEST */