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);
35 dict->table_size = 127;
36 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
45 * \param dict the dictionary to be freed
47 * Frees a dictionary with all the data
49 void xbt_dict_free(xbt_dict_t * dict)
52 xbt_dictelm_t current, previous;
56 // if ( *dict ) xbt_dict_dump_sizes(*dict);
58 if (dict != NULL && *dict != NULL) {
59 table_size = (*dict)->table_size;
60 table = (*dict)->table;
61 /* Warning: the size of the table is 'table_size+1'...
62 * This is because table_size is used as a binary mask in xbt_dict_rehash */
63 for (i = 0; (*dict)->count && i <= table_size; i++) {
65 while (current != NULL) {
67 current = current->next;
68 xbt_dictelm_free(previous);
79 * Returns the amount of elements in the dict
81 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
83 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
87 * Returns the hash code of a string.
89 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str,
94 #ifdef DJB2_HASH_FUNCTION
95 /* fast implementation of djb2 algorithm */
97 register unsigned int hash = 5381;
101 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
103 # elif defined(FNV_HASH_FUNCTION)
104 register unsigned int hash = 0x811c9dc5;
105 unsigned char *bp = (unsigned char *) str; /* start of buffer */
106 unsigned char *be = bp + str_len; /* beyond end of buffer */
109 /* multiply by the 32 bit FNV magic prime mod 2^32 */
111 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
114 /* xor the bottom with the current octet */
115 hash ^= (unsigned int) *bp++;
119 register unsigned int hash = 0;
122 hash += (*str) * (*str);
130 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
132 #ifdef DJB2_HASH_FUNCTION
133 /* fast implementation of djb2 algorithm */
135 register unsigned int hash = 5381;
137 while ((c = *str++)) {
138 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
141 # elif defined(FNV_HASH_FUNCTION)
142 register unsigned int hash = 0x811c9dc5;
145 /* multiply by the 32 bit FNV magic prime mod 2^32 */
147 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
150 /* xor the bottom with the current byte */
151 hash ^= (unsigned int) *str++;
155 register unsigned int hash = 0;
158 hash += (*str) * (*str);
165 /* Expend the size of the dict */
166 static void xbt_dict_rehash(xbt_dict_t dict)
168 const int oldsize = dict->table_size + 1;
169 register int newsize = oldsize * 2;
171 register xbt_dictelm_t *currcell;
172 register xbt_dictelm_t *twincell;
173 register xbt_dictelm_t bucklet;
174 register xbt_dictelm_t *pprev;
177 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
178 newsize * sizeof(xbt_dictelm_t));
179 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
180 dict->table_size = --newsize;
181 dict->table = currcell;
182 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
184 for (i = 0; i < oldsize; i++, currcell++) {
185 if (!*currcell) /* empty cell */
187 twincell = currcell + oldsize;
188 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
189 /* Since we use "& size" instead of "%size" and since the size was doubled,
190 each bucklet of this cell must either :
191 - stay in cell i (ie, currcell)
192 - go to the cell i+oldsize (ie, twincell) */
193 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
194 *pprev = bucklet->next;
195 bucklet->next = *twincell;
201 pprev = &bucklet->next;
206 if (!*currcell) /* everything moved */
212 * \brief Add data to the dict (arbitrary key)
213 * \param dict the container
214 * \param key the key to set the new data
215 * \param key_len the size of the \a key
216 * \param data the data to add in the dict
217 * \param free_ctn function to call with (\a data as argument) when
218 * \a data is removed from the dictionary
220 * Set the \a data in the structure under the \a key, which can be any kind
221 * of data, as long as its length is provided in \a key_len.
223 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
224 const char *key, int key_len,
225 void *data, void_f_pvoid_t free_ctn)
228 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
230 xbt_dictelm_t current, previous = NULL;
233 XBT_DEBUG("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
234 dict->table_size, hash_code & dict->table_size);
235 current = dict->table[hash_code & dict->table_size];
236 while (current != NULL &&
237 (hash_code != current->hash_code || key_len != current->key_len
238 || memcmp(key, current->key, key_len))) {
240 current = current->next;
243 if (current == NULL) {
244 /* this key doesn't exist yet */
245 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
247 if (previous == NULL) {
248 dict->table[hash_code & dict->table_size] = current;
250 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
251 xbt_dict_rehash(dict);
253 previous->next = current;
257 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
258 key_len, (char *) current->content,
259 key_len, (char *) data, key_len, (char *) key);
260 /* there is already an element with the same key: overwrite it */
261 if (current->content != NULL && current->free_f != NULL) {
262 current->free_f(current->content);
264 current->content = data;
265 current->free_f = free_ctn;
270 * \brief Add data to the dict (null-terminated key)
272 * \param dict the dict
273 * \param key the key to set the new data
274 * \param data the data to add in the dict
275 * \param free_ctn function to call with (\a data as argument) when
276 * \a data is removed from the dictionary
278 * set the \a data in the structure under the \a key, which is a
279 * null terminated string.
281 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
282 const char *key, void *data,
283 void_f_pvoid_t free_ctn)
286 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
290 * \brief Retrieve data from the dict (arbitrary key)
292 * \param dict the dealer of data
293 * \param key the key to find data
294 * \param key_len the size of the \a key
295 * \return the data that we are looking for
297 * Search the given \a key. Throws not_found_error when not found.
299 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
304 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
305 xbt_dictelm_t current;
309 current = dict->table[hash_code & dict->table_size];
310 while (current != NULL &&
311 (hash_code != current->hash_code || key_len != current->key_len
312 || memcmp(key, current->key, key_len))) {
313 current = current->next;
317 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
319 return current->content;
323 * \brief like xbt_dict_get_ext(), but returning NULL when not found
325 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
329 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
330 xbt_dictelm_t current;
334 current = dict->table[hash_code & dict->table_size];
335 while (current != NULL &&
336 (hash_code != current->hash_code || key_len != current->key_len
337 || memcmp(key, current->key, key_len))) {
338 current = current->next;
344 return current->content;
348 * @brief retrieve the key associated to that object. Warning, that's a linear search
350 * Returns NULL if the object cannot be found
352 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
355 xbt_dictelm_t current;
358 for (i = 0; i <= dict->table_size; i++) {
359 current = dict->table[i];
360 while (current != NULL) {
361 if (current->content == data)
363 current = current->next;
371 * \brief Retrieve data from the dict (null-terminated key)
373 * \param dict the dealer of data
374 * \param key the key to find data
375 * \return the data that we are looking for
377 * Search the given \a key. Throws not_found_error when not found.
378 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
381 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
384 unsigned int hash_code = xbt_dict_hash(key);
385 xbt_dictelm_t current;
389 current = dict->table[hash_code & dict->table_size];
390 while (current != NULL
391 && (hash_code != current->hash_code || strcmp(key, current->key)))
392 current = current->next;
395 THROWF(not_found_error, 0, "key %s not found", key);
397 return current->content;
401 * \brief like xbt_dict_get(), but returning NULL when not found
403 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
405 unsigned int hash_code = xbt_dict_hash(key);
406 xbt_dictelm_t current;
410 current = dict->table[hash_code & dict->table_size];
411 while (current != NULL &&
412 (hash_code != current->hash_code || strcmp(key, current->key)))
413 current = current->next;
418 return current->content;
423 * \brief Remove data from the dict (arbitrary key)
425 * \param dict the trash can
426 * \param key the key of the data to be removed
427 * \param key_len the size of the \a key
429 * Remove the entry associated with the given \a key (throws not_found)
431 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
436 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
437 xbt_dictelm_t current, previous = NULL;
441 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
442 current = dict->table[hash_code & dict->table_size];
443 while (current != NULL &&
444 (hash_code != current->hash_code || key_len != current->key_len
445 || strncmp(key, current->key, key_len))) {
446 previous = current; /* save the previous node */
447 current = current->next;
451 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
453 if (previous != NULL) {
454 previous->next = current->next;
456 dict->table[hash_code & dict->table_size] = current->next;
459 if (!dict->table[hash_code & dict->table_size])
462 xbt_dictelm_free(current);
469 * \brief Remove data from the dict (null-terminated key)
471 * \param dict the dict
472 * \param key the key of the data to be removed
474 * Remove the entry associated with the given \a key
476 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
478 xbt_dict_remove_ext(dict, key, strlen(key));
482 * \brief Add data to the dict (arbitrary key)
483 * \param dict the container
484 * \param key the key to set the new data
485 * \param data the data to add in the dict
487 * Set the \a data in the structure under the \a key.
488 * Both \a data and \a key are considered as uintptr_t.
490 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
491 uintptr_t key, uintptr_t data)
494 unsigned int hash_code =
495 xbt_dict_hash_ext((void *) &key, sizeof(uintptr_t));
497 xbt_dictelm_t current, previous = NULL;
500 XBT_DEBUG("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data,
501 hash_code, dict->table_size, hash_code & dict->table_size);
502 current = dict->table[hash_code & dict->table_size];
503 while (current != NULL &&
504 (hash_code != current->hash_code
505 || sizeof(uintptr_t) != current->key_len
506 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
508 current = current->next;
511 if (current == NULL) {
512 /* this key doesn't exist yet */
513 current = xbt_dictielm_new(key, hash_code, data);
515 if (previous == NULL) {
516 dict->table[hash_code & dict->table_size] = current;
518 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
519 xbt_dict_rehash(dict);
521 previous->next = current;
525 /* there is already an element with the same key: overwrite it */
526 if (current->content != NULL && current->free_f != NULL) {
527 current->free_f(current->content);
529 current->content = (void *) data;
530 current->free_f = NULL;
535 * \brief Retrieve data from the dict (key considered as a uintptr_t)
537 * \param dict the dealer of data
538 * \param key the key to find data
539 * \return the data that we are looking for (or 0 if not found)
541 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
543 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
546 unsigned int hash_code =
547 xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
548 xbt_dictelm_t current;
552 current = dict->table[hash_code & dict->table_size];
553 while (current != NULL &&
554 (hash_code != current->hash_code
555 || sizeof(uintptr_t) != current->key_len
556 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
557 current = current->next;
563 return (uintptr_t) (current->content);
566 /** Remove a uintptr_t key from the dict */
567 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
570 unsigned int hash_code =
571 xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
572 xbt_dictelm_t current, previous = NULL;
575 current = dict->table[hash_code & dict->table_size];
576 while (current != NULL &&
577 (hash_code != current->hash_code
578 || sizeof(uintptr_t) != current->key_len
579 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
580 previous = current; /* save the previous node */
581 current = current->next;
585 THROWF(not_found_error, 0, "key %zu not found", key);
587 if (previous != NULL) {
588 previous->next = current->next;
590 dict->table[hash_code & dict->table_size] = current->next;
593 if (!dict->table[hash_code & dict->table_size])
596 xbt_dictelm_free(current);
602 * \brief Remove all data from the dict
603 * \param dict the dict
605 void xbt_dict_reset(xbt_dict_t dict)
609 xbt_dictelm_t current, previous = NULL;
613 if (dict->count == 0)
616 for (i = 0; i <= dict->table_size; i++) {
617 current = dict->table[i];
618 while (current != NULL) {
620 current = current->next;
621 xbt_dictelm_free(previous);
623 dict->table[i] = NULL;
631 * \brief Return the number of elements in the dict.
632 * \param dict a dictionary
634 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
641 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
642 void xbt_dict_dump_output_string(void *s)
648 * \brief test if the dict is empty or not
650 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
652 return !dict || (xbt_dict_length(dict) == 0);
656 * \brief Outputs the content of the structure (debugging purpose)
658 * \param dict the exibitionist
659 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
661 * Outputs the content of the structure. (for debugging purpose). \a output is a
662 * function to output the data. If NULL, data won't be displayed.
665 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
668 xbt_dictelm_t element;
669 printf("Dict %p:\n", dict);
671 for (i = 0; i < dict->table_size; i++) {
672 element = dict->table[i];
675 while (element != NULL) {
676 printf(" %s -> '", element->key);
677 if (output != NULL) {
678 output(element->content);
681 element = element->next;
691 xbt_dynar_t all_sizes = NULL;
692 /** @brief shows some debugging info about the bucklet repartition */
693 void xbt_dict_dump_sizes(xbt_dict_t dict)
699 xbt_dictelm_t element;
700 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
702 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
703 dict->fill, dict->table_size);
705 for (i = 0; i < dict->table_size; i++) {
706 element = dict->table[i];
709 while (element != NULL) {
711 element = element->next;
714 if (xbt_dynar_length(sizes) <= size) {
716 xbt_dynar_set(sizes, size, &prevsize);
719 xbt_dynar_get_cpy(sizes, size, &prevsize);
721 xbt_dynar_set(sizes, size, &prevsize);
725 all_sizes = xbt_dynar_new(sizeof(int), NULL);
727 xbt_dynar_foreach(sizes, count, size) {
728 /* Copy values of this one into all_sizes */
730 if (xbt_dynar_length(all_sizes) <= count) {
732 xbt_dynar_set(all_sizes, count, &prevcount);
734 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
736 xbt_dynar_set(all_sizes, count, &prevcount);
739 /* Report current sizes */
744 printf("%delm x %u cells; ", count, size);
748 xbt_dynar_free(&sizes);
752 * Create the dict mallocators.
753 * This is an internal XBT function called during the lib initialization.
754 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
756 void xbt_dict_preinit(void)
758 if (dict_elm_mallocator != NULL) {
759 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
760 xbt_mallocator_free(dict_elm_mallocator);
763 dict_elm_mallocator = xbt_mallocator_new(256,
764 dict_elm_mallocator_new_f,
765 dict_elm_mallocator_free_f,
766 dict_elm_mallocator_reset_f);
770 * Destroy the dict mallocators.
771 * This is an internal XBT function during the lib initialization
773 void xbt_dict_postexit(void)
775 if (dict_elm_mallocator != NULL) {
776 xbt_mallocator_free(dict_elm_mallocator);
777 dict_elm_mallocator = NULL;
784 printf("Overall stats:");
785 xbt_dynar_foreach(all_sizes, count, size) {
790 printf("%delm x %d cells; ", count, size);
794 printf("; %f elm per cell\n", avg / (double) total_count);
801 #include "portable.h"
803 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
804 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
806 XBT_TEST_SUITE("dict", "Dict data container");
808 static void print_str(void *str)
810 printf("%s", (char *) PRINTF_STR(str));
813 static void debuged_add_ext(xbt_dict_t head, const char *key,
814 const char *data_to_fill)
816 char *data = xbt_strdup(data_to_fill);
818 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
821 xbt_dict_set(head, key, data, &free);
822 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
823 xbt_dict_dump(head, (void (*)(void *)) &printf);
828 static void debuged_add(xbt_dict_t head, const char *key)
830 debuged_add_ext(head, key, key);
833 static void fill(xbt_dict_t * head)
835 xbt_test_add("Fill in the dictionnary");
837 *head = xbt_dict_new();
838 debuged_add(*head, "12");
839 debuged_add(*head, "12a");
840 debuged_add(*head, "12b");
841 debuged_add(*head, "123");
842 debuged_add(*head, "123456");
843 /* Child becomes child of what to add */
844 debuged_add(*head, "1234");
845 /* Need of common ancestor */
846 debuged_add(*head, "123457");
850 static void search_ext(xbt_dict_t head, const char *key, const char *data)
854 xbt_test_add("Search %s", key);
855 found = xbt_dict_get(head, key);
856 xbt_test_log("Found %s", (char *) found);
858 xbt_test_assert(found,
859 "data do not match expectations: found NULL while searching for %s",
862 xbt_test_assert(!strcmp((char *) data, found),
863 "data do not match expectations: found %s while searching for %s",
864 (char *) found, data);
867 static void search(xbt_dict_t head, const char *key)
869 search_ext(head, key, key);
872 static void debuged_remove(xbt_dict_t head, const char *key)
875 xbt_test_add("Remove '%s'", PRINTF_STR(key));
876 xbt_dict_remove(head, key);
877 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
881 static void traverse(xbt_dict_t head)
883 xbt_dict_cursor_t cursor = NULL;
888 xbt_dict_foreach(head, cursor, key, data) {
889 if (!key || !data || strcmp(key, data)) {
890 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
893 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
895 xbt_test_assert(!data || !strcmp(key, data),
896 "Key(%s) != value(%s). Aborting", key, data);
900 static void search_not_found(xbt_dict_t head, const char *data)
905 xbt_test_add("Search %s (expected not to be found)", data);
908 data = xbt_dict_get(head, data);
909 THROWF(unknown_error, 0,
910 "Found something which shouldn't be there (%s)", data);
913 if (e.category != not_found_error)
914 xbt_test_exception(e);
918 xbt_test_assert(ok, "Exception not raised");
921 static void count(xbt_dict_t dict, int length)
923 xbt_dict_cursor_t cursor;
929 xbt_test_add("Count elements (expecting %d)", length);
930 xbt_test_assert(xbt_dict_length(dict) == length,
931 "Announced length(%d) != %d.", xbt_dict_length(dict),
934 xbt_dict_foreach(dict, cursor, key, data)
937 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
942 static void count_check_get_key(xbt_dict_t dict, int length)
944 xbt_dict_cursor_t cursor;
946 _XBT_GNUC_UNUSED char *key2;
952 ("Count elements (expecting %d), and test the getkey function",
954 xbt_test_assert(xbt_dict_length(dict) == length,
955 "Announced length(%d) != %d.", xbt_dict_length(dict),
958 xbt_dict_foreach(dict, cursor, key, data) {
960 key2 = xbt_dict_get_key(dict, data);
961 xbt_assert(!strcmp(key, key2),
962 "The data was registered under %s instead of %s as expected",
966 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
972 xbt_dict_t head = NULL;
976 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
978 xbt_test_add("Traversal the null dictionary");
981 xbt_test_add("Traversal and search the empty dictionary");
982 head = xbt_dict_new();
985 debuged_remove(head, "12346");
988 if (e.category != not_found_error)
989 xbt_test_exception(e);
992 xbt_dict_free(&head);
994 xbt_test_add("Traverse the full dictionary");
996 count_check_get_key(head, 7);
998 debuged_add_ext(head, "toto", "tutu");
999 search_ext(head, "toto", "tutu");
1000 debuged_remove(head, "toto");
1002 search(head, "12a");
1005 xbt_test_add("Free the dictionary (twice)");
1006 xbt_dict_free(&head);
1007 xbt_dict_free(&head);
1011 count_check_get_key(head, 7);
1012 xbt_test_add("Change 123 to 'Changed 123'");
1013 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1014 count_check_get_key(head, 7);
1016 xbt_test_add("Change 123 back to '123'");
1017 xbt_dict_set(head, "123", strdup("123"), &free);
1018 count_check_get_key(head, 7);
1020 xbt_test_add("Change 12a to 'Dummy 12a'");
1021 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1022 count_check_get_key(head, 7);
1024 xbt_test_add("Change 12a to '12a'");
1025 xbt_dict_set(head, "12a", strdup("12a"), &free);
1026 count_check_get_key(head, 7);
1028 xbt_test_add("Traverse the resulting dictionary");
1032 xbt_test_add("Search 123");
1033 data = xbt_dict_get(head, "123");
1034 xbt_test_assert(data);
1035 xbt_test_assert(!strcmp("123", data));
1037 search_not_found(head, "Can't be found");
1038 search_not_found(head, "123 Can't be found");
1039 search_not_found(head, "12345678 NOT");
1041 search(head, "12a");
1042 search(head, "12b");
1044 search(head, "123456");
1045 search(head, "1234");
1046 search(head, "123457");
1048 xbt_test_add("Traverse the resulting dictionary");
1051 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1053 xbt_test_add("Free the dictionary twice");
1054 xbt_dict_free(&head);
1055 xbt_dict_free(&head);
1057 xbt_test_add("Traverse the resulting dictionary");
1061 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1065 xbt_test_add("Remove non existing data");
1067 debuged_remove(head, "Does not exist");
1070 if (e.category != not_found_error)
1071 xbt_test_exception(e);
1076 xbt_dict_free(&head);
1079 ("Remove each data manually (traversing the resulting dictionary each time)");
1081 debuged_remove(head, "12a");
1084 debuged_remove(head, "12b");
1087 debuged_remove(head, "12");
1090 debuged_remove(head, "123456");
1094 debuged_remove(head, "12346");
1097 if (e.category != not_found_error)
1098 xbt_test_exception(e);
1102 debuged_remove(head, "1234");
1104 debuged_remove(head, "123457");
1106 debuged_remove(head, "123");
1109 debuged_remove(head, "12346");
1112 if (e.category != not_found_error)
1113 xbt_test_exception(e);
1119 ("Free dict, create new fresh one, and then reset the dict");
1120 xbt_dict_free(&head);
1122 xbt_dict_reset(head);
1126 xbt_test_add("Free the dictionary twice");
1127 xbt_dict_free(&head);
1128 xbt_dict_free(&head);
1131 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1135 xbt_test_add("Store NULL under 'null'");
1136 xbt_dict_set(head, "null", NULL, NULL);
1137 search_ext(head, "null", NULL);
1139 xbt_test_add("Check whether I see it while traversing...");
1141 xbt_dict_cursor_t cursor = NULL;
1145 xbt_dict_foreach(head, cursor, key, data) {
1146 if (!key || !data || strcmp(key, data)) {
1147 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1149 xbt_test_log("Seen: %s", PRINTF_STR(key));
1152 if (!strcmp(key, "null"))
1155 xbt_test_assert(found,
1156 "the key 'null', associated to NULL is not found");
1158 xbt_dict_free(&head);
1161 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1163 uintptr_t stored_data = 0;
1164 xbt_test_log("Add %zu under %zu", data, key);
1166 xbt_dicti_set(head, key, data);
1167 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1168 xbt_dict_dump(head, (void (*)(void *)) &printf);
1171 stored_data = xbt_dicti_get(head, key);
1172 xbt_test_assert(stored_data == data,
1173 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1174 stored_data, data, key);
1177 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1179 xbt_test_add("Fill in the dictionnary");
1181 head = xbt_dict_new();
1182 debuged_addi(head, 12, 12);
1183 debuged_addi(head, 13, 13);
1184 debuged_addi(head, 14, 14);
1185 debuged_addi(head, 15, 15);
1187 debuged_addi(head, 12, 15);
1188 debuged_addi(head, 15, 2000);
1189 debuged_addi(head, 15, 3000);
1191 debuged_addi(head, 0, 1000);
1192 debuged_addi(head, 0, 2000);
1193 debuged_addi(head, 0, 3000);
1195 debuged_addi(head, 12, 0);
1196 debuged_addi(head, 13, 0);
1197 debuged_addi(head, 12, 0);
1198 debuged_addi(head, 0, 0);
1200 xbt_dict_free(&head);
1203 #define NB_ELM 20000
1204 #define SIZEOFKEY 1024
1205 static int countelems(xbt_dict_t head)
1207 xbt_dict_cursor_t cursor;
1212 xbt_dict_foreach(head, cursor, key, data) {
1218 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1220 xbt_dict_t head = NULL;
1225 srand((unsigned int) time(NULL));
1227 for (i = 0; i < 10; i++) {
1228 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1230 ("Fill the struct, count its elems and frees the structure");
1232 ("using 1000 elements with %d chars long randomized keys.",
1234 head = xbt_dict_new();
1235 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1236 for (j = 0; j < 1000; j++) {
1238 key = xbt_malloc(SIZEOFKEY);
1241 for (k = 0; k < SIZEOFKEY - 1; k++)
1242 key[k] = rand() % ('z' - 'a') + 'a';
1244 /* printf("[%d %s]\n",j,key); */
1245 data = xbt_dict_get_or_null(head, key);
1246 } while (data != NULL);
1248 xbt_dict_set(head, key, key, &free);
1249 data = xbt_dict_get(head, key);
1250 xbt_test_assert(!strcmp(key, data),
1251 "Retrieved value (%s) != Injected value (%s)", key,
1256 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1258 xbt_dict_free(&head);
1259 xbt_dict_free(&head);
1263 head = xbt_dict_new();
1264 xbt_test_add("Fill %d elements, with keys being the number of element",
1266 for (j = 0; j < NB_ELM; j++) {
1267 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1269 key = xbt_malloc(10);
1271 sprintf(key, "%d", j);
1272 xbt_dict_set(head, key, key, &free);
1274 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1277 ("Count the elements (retrieving the key and data for each)");
1278 i = countelems(head);
1279 xbt_test_log("There is %d elements", i);
1281 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1282 key = xbt_malloc(10);
1283 for (i = 0; i < 20; i++) {
1284 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1285 for (j = 0; j < NB_ELM; j++) {
1287 sprintf(key, "%d", j);
1288 data = xbt_dict_get(head, key);
1289 xbt_test_assert(!strcmp(key, (char *) data),
1290 "with get, key=%s != data=%s", key, (char *) data);
1291 data = xbt_dict_get_ext(head, key, strlen(key));
1292 xbt_test_assert(!strcmp(key, (char *) data),
1293 "with get_ext, key=%s != data=%s", key,
1299 xbt_test_add("Remove my %d elements", NB_ELM);
1300 key = xbt_malloc(10);
1301 for (j = 0; j < NB_ELM; j++) {
1302 /* if (!(j%10000)) printf("."); fflush(stdout); */
1304 sprintf(key, "%d", j);
1305 xbt_dict_remove(head, key);
1310 xbt_test_add("Free the structure (twice)");
1311 xbt_dict_free(&head);
1312 xbt_dict_free(&head);
1315 static void str_free(void *s)
1317 char *c = *(char **) s;
1321 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1325 #define NB_ELM 100 /*00 */
1327 #define KEY_SIZE 512
1328 #define NB_TEST 20 /*20 */
1331 xbt_dict_t mdict = NULL;
1333 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1338 xbt_test_add("Generic multicache CRASH test");
1340 (" Fill the struct and frees it %d times, using %d elements, "
1341 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1344 for (l = 0; l < DEPTH; l++) {
1345 key = xbt_malloc(KEY_SIZE);
1346 xbt_dynar_push(keys, &key);
1349 for (i = 0; i < NB_TEST; i++) {
1350 mdict = xbt_dict_new();
1351 XBT_VERB("mdict=%p", mdict);
1353 printf("Test %d\n", i);
1354 /* else if (i%10) printf("."); else printf("%d",i/10); */
1356 for (j = 0; j < NB_ELM; j++) {
1360 for (l = 0; l < DEPTH; l++) {
1361 key = *(char **) xbt_dynar_get_ptr(keys, l);
1363 for (k = 0; k < KEY_SIZE - 1; k++)
1364 key[k] = rand() % ('z' - 'a') + 'a';
1369 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1372 printf("in multitree %p.\n", mdict);
1374 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1376 data = xbt_multidict_get(mdict, keys);
1378 xbt_test_assert(data && !strcmp((char *) data, key),
1379 "Retrieved value (%s) does not match the given one (%s)\n",
1380 (char *) data, key);
1382 xbt_dict_free(&mdict);
1385 xbt_dynar_free(&keys);
1388 xbt_dict_dump(mdict,&xbt_dict_print); */
1390 xbt_dict_free(&mdict);
1391 xbt_dynar_free(&keys);
1394 #endif /* SIMGRID_TEST */