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(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(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;
259 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
260 key_len, (char *) current->content,
261 key_len, (char *) data, key_len, (char *) key);
262 /* there is already an element with the same key: overwrite it */
263 if (current->content != NULL && current->free_f != NULL) {
264 current->free_f(current->content);
266 current->content = data;
267 current->free_f = free_ctn;
272 * \brief Add data to the dict (null-terminated key)
274 * \param dict the dict
275 * \param key the key to set the new data
276 * \param data the data to add in the dict
277 * \param free_ctn function to call with (\a data as argument) when
278 * \a data is removed from the dictionary
280 * set the \a data in the structure under the \a key, which is a
281 * null terminated string.
283 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
284 const char *key, void *data,
285 void_f_pvoid_t free_ctn)
288 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
292 * \brief Retrieve data from the dict (arbitrary key)
294 * \param dict the dealer of data
295 * \param key the key to find data
296 * \param key_len the size of the \a key
297 * \return the data that we are looking for
299 * Search the given \a key. Throws not_found_error when not found.
301 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
306 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
307 xbt_dictelm_t current;
311 current = dict->table[hash_code & dict->table_size];
312 while (current != NULL &&
313 (hash_code != current->hash_code || key_len != current->key_len
314 || memcmp(key, current->key, key_len))) {
315 current = current->next;
319 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
321 return current->content;
325 * \brief like xbt_dict_get_ext(), but returning NULL when not found
327 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
331 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
332 xbt_dictelm_t current;
336 current = dict->table[hash_code & dict->table_size];
337 while (current != NULL &&
338 (hash_code != current->hash_code || key_len != current->key_len
339 || memcmp(key, current->key, key_len))) {
340 current = current->next;
346 return current->content;
350 * @brief retrieve the key associated to that object. Warning, that's a linear search
352 * Returns NULL if the object cannot be found
354 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
357 xbt_dictelm_t current;
360 for (i = 0; i <= dict->table_size; i++) {
361 current = dict->table[i];
362 while (current != NULL) {
363 if (current->content == data)
365 current = current->next;
373 * \brief Retrieve data from the dict (null-terminated key)
375 * \param dict the dealer of data
376 * \param key the key to find data
377 * \return the data that we are looking for
379 * Search the given \a key. Throws not_found_error when not found.
380 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
383 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
386 unsigned int hash_code = xbt_dict_hash(key);
387 xbt_dictelm_t current;
391 current = dict->table[hash_code & dict->table_size];
392 while (current != NULL
393 && (hash_code != current->hash_code || strcmp(key, current->key)))
394 current = current->next;
397 THROWF(not_found_error, 0, "key %s not found", key);
399 return current->content;
403 * \brief like xbt_dict_get(), but returning NULL when not found
405 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
407 unsigned int hash_code = xbt_dict_hash(key);
408 xbt_dictelm_t current;
412 current = dict->table[hash_code & dict->table_size];
413 while (current != NULL &&
414 (hash_code != current->hash_code || strcmp(key, current->key)))
415 current = current->next;
420 return current->content;
425 * \brief Remove data from the dict (arbitrary key)
427 * \param dict the trash can
428 * \param key the key of the data to be removed
429 * \param key_len the size of the \a key
431 * Remove the entry associated with the given \a key (throws not_found)
433 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
438 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
439 xbt_dictelm_t current, previous = NULL;
443 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
444 current = dict->table[hash_code & dict->table_size];
445 while (current != NULL &&
446 (hash_code != current->hash_code || key_len != current->key_len
447 || strncmp(key, current->key, key_len))) {
448 previous = current; /* save the previous node */
449 current = current->next;
453 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
455 if (previous != NULL) {
456 previous->next = current->next;
458 dict->table[hash_code & dict->table_size] = current->next;
461 if (!dict->table[hash_code & dict->table_size])
464 xbt_dictelm_free(current);
471 * \brief Remove data from the dict (null-terminated key)
473 * \param dict the dict
474 * \param key the key of the data to be removed
476 * Remove the entry associated with the given \a key
478 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
480 xbt_dict_remove_ext(dict, key, strlen(key));
484 * \brief Add data to the dict (arbitrary key)
485 * \param dict the container
486 * \param key the key to set the new data
487 * \param data the data to add in the dict
489 * Set the \a data in the structure under the \a key.
490 * Both \a data and \a key are considered as uintptr_t.
492 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
493 uintptr_t key, uintptr_t data)
495 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
499 * \brief Retrieve data from the dict (key considered as a uintptr_t)
501 * \param dict the dealer of data
502 * \param key the key to find data
503 * \return the data that we are looking for (or 0 if not found)
505 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
507 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
509 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
512 /** Remove a uintptr_t key from the dict */
513 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
515 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
520 * \brief Remove all data from the dict
521 * \param dict the dict
523 void xbt_dict_reset(xbt_dict_t dict)
527 xbt_dictelm_t current, previous = NULL;
531 if (dict->count == 0)
534 for (i = 0; i <= dict->table_size; i++) {
535 current = dict->table[i];
536 while (current != NULL) {
538 current = current->next;
539 xbt_dictelm_free(previous);
541 dict->table[i] = NULL;
549 * \brief Return the number of elements in the dict.
550 * \param dict a dictionary
552 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
559 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
560 void xbt_dict_dump_output_string(void *s)
566 * \brief test if the dict is empty or not
568 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
570 return !dict || (xbt_dict_length(dict) == 0);
574 * \brief Outputs the content of the structure (debugging purpose)
576 * \param dict the exibitionist
577 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
579 * Outputs the content of the structure. (for debugging purpose). \a output is a
580 * function to output the data. If NULL, data won't be displayed.
583 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
586 xbt_dictelm_t element;
587 printf("Dict %p:\n", dict);
589 for (i = 0; i < dict->table_size; i++) {
590 element = dict->table[i];
593 while (element != NULL) {
594 printf(" %s -> '", element->key);
595 if (output != NULL) {
596 output(element->content);
599 element = element->next;
609 xbt_dynar_t all_sizes = NULL;
610 /** @brief shows some debugging info about the bucklet repartition */
611 void xbt_dict_dump_sizes(xbt_dict_t dict)
617 xbt_dictelm_t element;
618 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
620 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
621 dict->fill, dict->table_size);
623 for (i = 0; i < dict->table_size; i++) {
624 element = dict->table[i];
627 while (element != NULL) {
629 element = element->next;
632 if (xbt_dynar_length(sizes) <= size) {
634 xbt_dynar_set(sizes, size, &prevsize);
637 xbt_dynar_get_cpy(sizes, size, &prevsize);
639 xbt_dynar_set(sizes, size, &prevsize);
643 all_sizes = xbt_dynar_new(sizeof(int), NULL);
645 xbt_dynar_foreach(sizes, count, size) {
646 /* Copy values of this one into all_sizes */
648 if (xbt_dynar_length(all_sizes) <= count) {
650 xbt_dynar_set(all_sizes, count, &prevcount);
652 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
654 xbt_dynar_set(all_sizes, count, &prevcount);
657 /* Report current sizes */
662 printf("%delm x %u cells; ", count, size);
666 xbt_dynar_free(&sizes);
670 * Create the dict mallocators.
671 * This is an internal XBT function called during the lib initialization.
672 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
674 void xbt_dict_preinit(void)
676 if (dict_elm_mallocator != NULL) {
677 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
678 xbt_mallocator_free(dict_elm_mallocator);
681 dict_elm_mallocator = xbt_mallocator_new(256,
682 dict_elm_mallocator_new_f,
683 dict_elm_mallocator_free_f,
684 dict_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;
702 printf("Overall stats:");
703 xbt_dynar_foreach(all_sizes, count, size) {
708 printf("%delm x %d cells; ", count, size);
712 printf("; %f elm per cell\n", avg / (double) total_count);
719 #include "portable.h"
721 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
722 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
724 XBT_TEST_SUITE("dict", "Dict data container");
726 static void print_str(void *str)
728 printf("%s", (char *) PRINTF_STR(str));
731 static void debuged_add_ext(xbt_dict_t head, const char *key,
732 const char *data_to_fill)
734 char *data = xbt_strdup(data_to_fill);
736 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
739 xbt_dict_set(head, key, data, &free);
740 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
741 xbt_dict_dump(head, (void (*)(void *)) &printf);
746 static void debuged_add(xbt_dict_t head, const char *key)
748 debuged_add_ext(head, key, key);
751 static void fill(xbt_dict_t * head)
753 xbt_test_add("Fill in the dictionnary");
755 *head = xbt_dict_new();
756 debuged_add(*head, "12");
757 debuged_add(*head, "12a");
758 debuged_add(*head, "12b");
759 debuged_add(*head, "123");
760 debuged_add(*head, "123456");
761 /* Child becomes child of what to add */
762 debuged_add(*head, "1234");
763 /* Need of common ancestor */
764 debuged_add(*head, "123457");
768 static void search_ext(xbt_dict_t head, const char *key, const char *data)
772 xbt_test_add("Search %s", key);
773 found = xbt_dict_get(head, key);
774 xbt_test_log("Found %s", (char *) found);
776 xbt_test_assert(found,
777 "data do not match expectations: found NULL while searching for %s",
780 xbt_test_assert(!strcmp((char *) data, found),
781 "data do not match expectations: found %s while searching for %s",
782 (char *) found, data);
785 static void search(xbt_dict_t head, const char *key)
787 search_ext(head, key, key);
790 static void debuged_remove(xbt_dict_t head, const char *key)
793 xbt_test_add("Remove '%s'", PRINTF_STR(key));
794 xbt_dict_remove(head, key);
795 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
799 static void traverse(xbt_dict_t head)
801 xbt_dict_cursor_t cursor = NULL;
806 xbt_dict_foreach(head, cursor, key, data) {
807 if (!key || !data || strcmp(key, data)) {
808 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
811 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
813 xbt_test_assert(!data || !strcmp(key, data),
814 "Key(%s) != value(%s). Aborting", key, data);
818 static void search_not_found(xbt_dict_t head, const char *data)
823 xbt_test_add("Search %s (expected not to be found)", data);
826 data = xbt_dict_get(head, data);
827 THROWF(unknown_error, 0,
828 "Found something which shouldn't be there (%s)", data);
831 if (e.category != not_found_error)
832 xbt_test_exception(e);
836 xbt_test_assert(ok, "Exception not raised");
839 static void count(xbt_dict_t dict, int length)
841 xbt_dict_cursor_t cursor;
847 xbt_test_add("Count elements (expecting %d)", length);
848 xbt_test_assert(xbt_dict_length(dict) == length,
849 "Announced length(%d) != %d.", xbt_dict_length(dict),
852 xbt_dict_foreach(dict, cursor, key, data)
855 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
860 static void count_check_get_key(xbt_dict_t dict, int length)
862 xbt_dict_cursor_t cursor;
864 _XBT_GNUC_UNUSED char *key2;
870 ("Count elements (expecting %d), and test the getkey function",
872 xbt_test_assert(xbt_dict_length(dict) == length,
873 "Announced length(%d) != %d.", xbt_dict_length(dict),
876 xbt_dict_foreach(dict, cursor, key, data) {
878 key2 = xbt_dict_get_key(dict, data);
879 xbt_assert(!strcmp(key, key2),
880 "The data was registered under %s instead of %s as expected",
884 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
890 xbt_dict_t head = NULL;
894 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
896 xbt_test_add("Traversal the null dictionary");
899 xbt_test_add("Traversal and search the empty dictionary");
900 head = xbt_dict_new();
903 debuged_remove(head, "12346");
906 if (e.category != not_found_error)
907 xbt_test_exception(e);
910 xbt_dict_free(&head);
912 xbt_test_add("Traverse the full dictionary");
914 count_check_get_key(head, 7);
916 debuged_add_ext(head, "toto", "tutu");
917 search_ext(head, "toto", "tutu");
918 debuged_remove(head, "toto");
923 xbt_test_add("Free the dictionary (twice)");
924 xbt_dict_free(&head);
925 xbt_dict_free(&head);
929 count_check_get_key(head, 7);
930 xbt_test_add("Change 123 to 'Changed 123'");
931 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
932 count_check_get_key(head, 7);
934 xbt_test_add("Change 123 back to '123'");
935 xbt_dict_set(head, "123", strdup("123"), &free);
936 count_check_get_key(head, 7);
938 xbt_test_add("Change 12a to 'Dummy 12a'");
939 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
940 count_check_get_key(head, 7);
942 xbt_test_add("Change 12a to '12a'");
943 xbt_dict_set(head, "12a", strdup("12a"), &free);
944 count_check_get_key(head, 7);
946 xbt_test_add("Traverse the resulting dictionary");
950 xbt_test_add("Search 123");
951 data = xbt_dict_get(head, "123");
952 xbt_test_assert(data);
953 xbt_test_assert(!strcmp("123", data));
955 search_not_found(head, "Can't be found");
956 search_not_found(head, "123 Can't be found");
957 search_not_found(head, "12345678 NOT");
962 search(head, "123456");
963 search(head, "1234");
964 search(head, "123457");
966 xbt_test_add("Traverse the resulting dictionary");
969 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
971 xbt_test_add("Free the dictionary twice");
972 xbt_dict_free(&head);
973 xbt_dict_free(&head);
975 xbt_test_add("Traverse the resulting dictionary");
979 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
983 xbt_test_add("Remove non existing data");
985 debuged_remove(head, "Does not exist");
988 if (e.category != not_found_error)
989 xbt_test_exception(e);
994 xbt_dict_free(&head);
997 ("Remove each data manually (traversing the resulting dictionary each time)");
999 debuged_remove(head, "12a");
1002 debuged_remove(head, "12b");
1005 debuged_remove(head, "12");
1008 debuged_remove(head, "123456");
1012 debuged_remove(head, "12346");
1015 if (e.category != not_found_error)
1016 xbt_test_exception(e);
1020 debuged_remove(head, "1234");
1022 debuged_remove(head, "123457");
1024 debuged_remove(head, "123");
1027 debuged_remove(head, "12346");
1030 if (e.category != not_found_error)
1031 xbt_test_exception(e);
1037 ("Free dict, create new fresh one, and then reset the dict");
1038 xbt_dict_free(&head);
1040 xbt_dict_reset(head);
1044 xbt_test_add("Free the dictionary twice");
1045 xbt_dict_free(&head);
1046 xbt_dict_free(&head);
1049 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1053 xbt_test_add("Store NULL under 'null'");
1054 xbt_dict_set(head, "null", NULL, NULL);
1055 search_ext(head, "null", NULL);
1057 xbt_test_add("Check whether I see it while traversing...");
1059 xbt_dict_cursor_t cursor = NULL;
1063 xbt_dict_foreach(head, cursor, key, data) {
1064 if (!key || !data || strcmp(key, data)) {
1065 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1067 xbt_test_log("Seen: %s", PRINTF_STR(key));
1070 if (!strcmp(key, "null"))
1073 xbt_test_assert(found,
1074 "the key 'null', associated to NULL is not found");
1076 xbt_dict_free(&head);
1079 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1081 uintptr_t stored_data = 0;
1082 xbt_test_log("Add %zu under %zu", data, key);
1084 xbt_dicti_set(head, key, data);
1085 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1086 xbt_dict_dump(head, (void (*)(void *)) &printf);
1089 stored_data = xbt_dicti_get(head, key);
1090 xbt_test_assert(stored_data == data,
1091 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1092 stored_data, data, key);
1095 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1097 xbt_test_add("Fill in the dictionnary");
1099 head = xbt_dict_new();
1100 debuged_addi(head, 12, 12);
1101 debuged_addi(head, 13, 13);
1102 debuged_addi(head, 14, 14);
1103 debuged_addi(head, 15, 15);
1105 debuged_addi(head, 12, 15);
1106 debuged_addi(head, 15, 2000);
1107 debuged_addi(head, 15, 3000);
1109 debuged_addi(head, 0, 1000);
1110 debuged_addi(head, 0, 2000);
1111 debuged_addi(head, 0, 3000);
1113 debuged_addi(head, 12, 0);
1114 debuged_addi(head, 13, 0);
1115 debuged_addi(head, 12, 0);
1116 debuged_addi(head, 0, 0);
1118 xbt_dict_free(&head);
1121 #define NB_ELM 20000
1122 #define SIZEOFKEY 1024
1123 static int countelems(xbt_dict_t head)
1125 xbt_dict_cursor_t cursor;
1130 xbt_dict_foreach(head, cursor, key, data) {
1136 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1138 xbt_dict_t head = NULL;
1143 srand((unsigned int) time(NULL));
1145 for (i = 0; i < 10; i++) {
1146 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1148 ("Fill the struct, count its elems and frees the structure");
1150 ("using 1000 elements with %d chars long randomized keys.",
1152 head = xbt_dict_new();
1153 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1154 for (j = 0; j < 1000; j++) {
1156 key = xbt_malloc(SIZEOFKEY);
1159 for (k = 0; k < SIZEOFKEY - 1; k++)
1160 key[k] = rand() % ('z' - 'a') + 'a';
1162 /* printf("[%d %s]\n",j,key); */
1163 data = xbt_dict_get_or_null(head, key);
1164 } while (data != NULL);
1166 xbt_dict_set(head, key, key, &free);
1167 data = xbt_dict_get(head, key);
1168 xbt_test_assert(!strcmp(key, data),
1169 "Retrieved value (%s) != Injected value (%s)", key,
1174 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1176 xbt_dict_free(&head);
1177 xbt_dict_free(&head);
1181 head = xbt_dict_new();
1182 xbt_test_add("Fill %d elements, with keys being the number of element",
1184 for (j = 0; j < NB_ELM; j++) {
1185 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1187 key = xbt_malloc(10);
1189 sprintf(key, "%d", j);
1190 xbt_dict_set(head, key, key, &free);
1192 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1195 ("Count the elements (retrieving the key and data for each)");
1196 i = countelems(head);
1197 xbt_test_log("There is %d elements", i);
1199 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1200 key = xbt_malloc(10);
1201 for (i = 0; i < 20; i++) {
1202 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1203 for (j = 0; j < NB_ELM; j++) {
1205 sprintf(key, "%d", j);
1206 data = xbt_dict_get(head, key);
1207 xbt_test_assert(!strcmp(key, (char *) data),
1208 "with get, key=%s != data=%s", key, (char *) data);
1209 data = xbt_dict_get_ext(head, key, strlen(key));
1210 xbt_test_assert(!strcmp(key, (char *) data),
1211 "with get_ext, key=%s != data=%s", key,
1217 xbt_test_add("Remove my %d elements", NB_ELM);
1218 key = xbt_malloc(10);
1219 for (j = 0; j < NB_ELM; j++) {
1220 /* if (!(j%10000)) printf("."); fflush(stdout); */
1222 sprintf(key, "%d", j);
1223 xbt_dict_remove(head, key);
1228 xbt_test_add("Free the structure (twice)");
1229 xbt_dict_free(&head);
1230 xbt_dict_free(&head);
1233 static void str_free(void *s)
1235 char *c = *(char **) s;
1239 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1243 #define NB_ELM 100 /*00 */
1245 #define KEY_SIZE 512
1246 #define NB_TEST 20 /*20 */
1249 xbt_dict_t mdict = NULL;
1251 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1256 xbt_test_add("Generic multicache CRASH test");
1258 (" Fill the struct and frees it %d times, using %d elements, "
1259 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1262 for (l = 0; l < DEPTH; l++) {
1263 key = xbt_malloc(KEY_SIZE);
1264 xbt_dynar_push(keys, &key);
1267 for (i = 0; i < NB_TEST; i++) {
1268 mdict = xbt_dict_new();
1269 XBT_VERB("mdict=%p", mdict);
1271 printf("Test %d\n", i);
1272 /* else if (i%10) printf("."); else printf("%d",i/10); */
1274 for (j = 0; j < NB_ELM; j++) {
1278 for (l = 0; l < DEPTH; l++) {
1279 key = *(char **) xbt_dynar_get_ptr(keys, l);
1281 for (k = 0; k < KEY_SIZE - 1; k++)
1282 key[k] = rand() % ('z' - 'a') + 'a';
1287 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1290 printf("in multitree %p.\n", mdict);
1292 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1294 data = xbt_multidict_get(mdict, keys);
1296 xbt_test_assert(data && !strcmp((char *) data, key),
1297 "Retrieved value (%s) does not match the given one (%s)\n",
1298 (char *) data, key);
1300 xbt_dict_free(&mdict);
1303 xbt_dynar_free(&keys);
1306 xbt_dict_dump(mdict,&xbt_dict_print); */
1308 xbt_dict_free(&mdict);
1309 xbt_dynar_free(&keys);
1312 #endif /* SIMGRID_TEST */