1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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");
22 /*####[ Private prototypes ]#################################################*/
24 static xbt_mallocator_t dict_mallocator = NULL;
25 static void *dict_mallocator_new_f(void);
26 #define dict_mallocator_free_f xbt_free_f
27 #define dict_mallocator_reset_f ((void_f_pvoid_t)NULL)
30 /*####[ Code ]###############################################################*/
34 * \return pointer to the destination
35 * \see xbt_dict_free()
37 * Creates and initialize a new dictionary with a default hashtable size.
39 xbt_dict_t xbt_dict_new(void)
43 dict = xbt_mallocator_get(dict_mallocator);
44 dict->table_size = 127;
45 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
54 * \param dict the dictionary to be freed
56 * Frees a dictionary with all the data
58 void xbt_dict_free(xbt_dict_t * dict)
61 xbt_dictelm_t current, previous;
65 // if ( *dict ) xbt_dict_dump_sizes(*dict);
67 if (dict != NULL && *dict != NULL) {
68 table_size = (*dict)->table_size;
69 table = (*dict)->table;
70 /* Warning: the size of the table is 'table_size+1'...
71 * This is because table_size is used as a binary mask in xbt_dict_rehash */
72 for (i = 0; (*dict)->count && i <= table_size; i++) {
74 while (current != NULL) {
76 current = current->next;
77 xbt_dictelm_free(previous);
82 xbt_mallocator_release(dict_mallocator, *dict);
88 * Returns the amount of elements in the dict
90 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
92 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
96 * Returns the hash code of a string.
98 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str,
103 #ifdef DJB2_HASH_FUNCTION
104 /* fast implementation of djb2 algorithm */
106 register unsigned int hash = 5381;
110 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
112 # elif defined(FNV_HASH_FUNCTION)
113 register unsigned int hash = 0x811c9dc5;
114 unsigned char *bp = (unsigned char *) str; /* start of buffer */
115 unsigned char *be = bp + str_len; /* beyond end of buffer */
118 /* multiply by the 32 bit FNV magic prime mod 2^32 */
120 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
123 /* xor the bottom with the current octet */
124 hash ^= (unsigned int) *bp++;
128 register unsigned int hash = 0;
131 hash += (*str) * (*str);
139 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
141 #ifdef DJB2_HASH_FUNCTION
142 /* fast implementation of djb2 algorithm */
144 register unsigned int hash = 5381;
146 while ((c = *str++)) {
147 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
150 # elif defined(FNV_HASH_FUNCTION)
151 register unsigned int hash = 0x811c9dc5;
154 /* multiply by the 32 bit FNV magic prime mod 2^32 */
156 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
159 /* xor the bottom with the current byte */
160 hash ^= (unsigned int) *str++;
164 register unsigned int hash = 0;
167 hash += (*str) * (*str);
174 /* Expend the size of the dict */
175 static void xbt_dict_rehash(xbt_dict_t dict)
177 const int oldsize = dict->table_size + 1;
178 register int newsize = oldsize * 2;
180 register xbt_dictelm_t *currcell;
181 register xbt_dictelm_t *twincell;
182 register xbt_dictelm_t bucklet;
183 register xbt_dictelm_t *pprev;
186 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
187 newsize * sizeof(xbt_dictelm_t));
188 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
189 dict->table_size = --newsize;
190 dict->table = currcell;
191 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
193 for (i = 0; i < oldsize; i++, currcell++) {
194 if (!*currcell) /* empty cell */
196 twincell = currcell + oldsize;
197 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
198 /* Since we use "& size" instead of "%size" and since the size was doubled,
199 each bucklet of this cell must either :
200 - stay in cell i (ie, currcell)
201 - go to the cell i+oldsize (ie, twincell) */
202 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
203 *pprev = bucklet->next;
204 bucklet->next = *twincell;
210 pprev = &bucklet->next;
215 if (!*currcell) /* everything moved */
221 * \brief Add data to the dict (arbitrary key)
222 * \param dict the container
223 * \param key the key to set the new data
224 * \param key_len the size of the \a key
225 * \param data the data to add in the dict
226 * \param free_ctn function to call with (\a data as argument) when
227 * \a data is removed from the dictionary
229 * Set the \a data in the structure under the \a key, which can be any kind
230 * of data, as long as its length is provided in \a key_len.
232 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
233 const char *key, int key_len,
234 void *data, void_f_pvoid_t free_ctn)
237 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
239 xbt_dictelm_t current, previous = NULL;
242 XBT_DEBUG("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
243 dict->table_size, hash_code & dict->table_size);
244 current = dict->table[hash_code & dict->table_size];
245 while (current != NULL &&
246 (hash_code != current->hash_code || key_len != current->key_len
247 || memcmp(key, current->key, key_len))) {
249 current = current->next;
252 if (current == NULL) {
253 /* this key doesn't exist yet */
254 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
256 if (previous == NULL) {
257 dict->table[hash_code & dict->table_size] = current;
259 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
260 xbt_dict_rehash(dict);
262 previous->next = current;
266 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
267 key_len, (char *) current->content,
268 key_len, (char *) data, key_len, (char *) key);
269 /* there is already an element with the same key: overwrite it */
270 if (current->content != NULL && current->free_f != NULL) {
271 current->free_f(current->content);
273 current->content = data;
274 current->free_f = free_ctn;
279 * \brief Add data to the dict (null-terminated key)
281 * \param dict the dict
282 * \param key the key to set the new data
283 * \param data the data to add in the dict
284 * \param free_ctn function to call with (\a data as argument) when
285 * \a data is removed from the dictionary
287 * set the \a data in the structure under the \a key, which is a
288 * null terminated string.
290 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
291 const char *key, void *data,
292 void_f_pvoid_t free_ctn)
295 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
299 * \brief Retrieve data from the dict (arbitrary key)
301 * \param dict the dealer of data
302 * \param key the key to find data
303 * \param key_len the size of the \a key
304 * \return the data that we are looking for
306 * Search the given \a key. Throws not_found_error when not found.
308 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
313 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
314 xbt_dictelm_t current;
318 current = dict->table[hash_code & dict->table_size];
319 while (current != NULL &&
320 (hash_code != current->hash_code || key_len != current->key_len
321 || memcmp(key, current->key, key_len))) {
322 current = current->next;
326 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
328 return current->content;
332 * \brief like xbt_dict_get_ext(), but returning NULL when not found
334 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
338 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
339 xbt_dictelm_t current;
343 current = dict->table[hash_code & dict->table_size];
344 while (current != NULL &&
345 (hash_code != current->hash_code || key_len != current->key_len
346 || memcmp(key, current->key, key_len))) {
347 current = current->next;
353 return current->content;
357 * @brief retrieve the key associated to that object. Warning, that's a linear search
359 * Returns NULL if the object cannot be found
361 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
364 xbt_dictelm_t current;
367 for (i = 0; i <= dict->table_size; i++) {
368 current = dict->table[i];
369 while (current != NULL) {
370 if (current->content == data)
372 current = current->next;
380 * \brief Retrieve data from the dict (null-terminated key)
382 * \param dict the dealer of data
383 * \param key the key to find data
384 * \return the data that we are looking for
386 * Search the given \a key. Throws not_found_error when not found.
387 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
390 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
393 unsigned int hash_code = xbt_dict_hash(key);
394 xbt_dictelm_t current;
398 current = dict->table[hash_code & dict->table_size];
399 while (current != NULL
400 && (hash_code != current->hash_code || strcmp(key, current->key)))
401 current = current->next;
404 THROWF(not_found_error, 0, "key %s not found", key);
406 return current->content;
410 * \brief like xbt_dict_get(), but returning NULL when not found
412 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
414 unsigned int hash_code = xbt_dict_hash(key);
415 xbt_dictelm_t current;
419 current = dict->table[hash_code & dict->table_size];
420 while (current != NULL &&
421 (hash_code != current->hash_code || strcmp(key, current->key)))
422 current = current->next;
427 return current->content;
432 * \brief Remove data from the dict (arbitrary key)
434 * \param dict the trash can
435 * \param key the key of the data to be removed
436 * \param key_len the size of the \a key
438 * Remove the entry associated with the given \a key (throws not_found)
440 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
445 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
446 xbt_dictelm_t current, previous = NULL;
450 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
451 current = dict->table[hash_code & dict->table_size];
452 while (current != NULL &&
453 (hash_code != current->hash_code || key_len != current->key_len
454 || strncmp(key, current->key, key_len))) {
455 previous = current; /* save the previous node */
456 current = current->next;
460 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
462 if (previous != NULL) {
463 previous->next = current->next;
465 dict->table[hash_code & dict->table_size] = current->next;
468 if (!dict->table[hash_code & dict->table_size])
471 xbt_dictelm_free(current);
478 * \brief Remove data from the dict (null-terminated key)
480 * \param dict the dict
481 * \param key the key of the data to be removed
483 * Remove the entry associated with the given \a key
485 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
487 xbt_dict_remove_ext(dict, key, strlen(key));
491 * \brief Add data to the dict (arbitrary key)
492 * \param dict the container
493 * \param key the key to set the new data
494 * \param data the data to add in the dict
496 * Set the \a data in the structure under the \a key.
497 * Both \a data and \a key are considered as uintptr_t.
499 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
500 uintptr_t key, uintptr_t data)
503 unsigned int hash_code =
504 xbt_dict_hash_ext((void *) &key, sizeof(uintptr_t));
506 xbt_dictelm_t current, previous = NULL;
509 XBT_DEBUG("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data,
510 hash_code, dict->table_size, hash_code & dict->table_size);
511 current = dict->table[hash_code & dict->table_size];
512 while (current != NULL &&
513 (hash_code != current->hash_code
514 || sizeof(uintptr_t) != current->key_len
515 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
517 current = current->next;
520 if (current == NULL) {
521 /* this key doesn't exist yet */
522 current = xbt_dictielm_new(key, hash_code, data);
524 if (previous == NULL) {
525 dict->table[hash_code & dict->table_size] = current;
527 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
528 xbt_dict_rehash(dict);
530 previous->next = current;
534 /* there is already an element with the same key: overwrite it */
535 if (current->content != NULL && current->free_f != NULL) {
536 current->free_f(current->content);
538 current->content = (void *) data;
539 current->free_f = NULL;
544 * \brief Retrieve data from the dict (key considered as a uintptr_t)
546 * \param dict the dealer of data
547 * \param key the key to find data
548 * \return the data that we are looking for (or 0 if not found)
550 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
552 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
555 unsigned int hash_code =
556 xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
557 xbt_dictelm_t current;
561 current = dict->table[hash_code & dict->table_size];
562 while (current != NULL &&
563 (hash_code != current->hash_code
564 || sizeof(uintptr_t) != current->key_len
565 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
566 current = current->next;
572 return (uintptr_t) (current->content);
575 /** Remove a uintptr_t key from the dict */
576 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
579 unsigned int hash_code =
580 xbt_dict_hash_ext(((void *) &key), sizeof(uintptr_t));
581 xbt_dictelm_t current, previous = NULL;
584 current = dict->table[hash_code & dict->table_size];
585 while (current != NULL &&
586 (hash_code != current->hash_code
587 || sizeof(uintptr_t) != current->key_len
588 || (((uintptr_t) key) != ((uintptr_t) current->key)))) {
589 previous = current; /* save the previous node */
590 current = current->next;
594 THROWF(not_found_error, 0, "key %zu not found", key);
596 if (previous != NULL) {
597 previous->next = current->next;
599 dict->table[hash_code & dict->table_size] = current->next;
602 if (!dict->table[hash_code & dict->table_size])
605 xbt_dictelm_free(current);
611 * \brief Remove all data from the dict
612 * \param dict the dict
614 void xbt_dict_reset(xbt_dict_t dict)
618 xbt_dictelm_t current, previous = NULL;
622 if (dict->count == 0)
625 for (i = 0; i <= dict->table_size; i++) {
626 current = dict->table[i];
627 while (current != NULL) {
629 current = current->next;
630 xbt_dictelm_free(previous);
632 dict->table[i] = NULL;
640 * \brief Return the number of elements in the dict.
641 * \param dict a dictionary
643 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
650 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
651 void xbt_dict_dump_output_string(void *s)
657 * \brief test if the dict is empty or not
659 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
661 return !dict || (xbt_dict_length(dict) == 0);
665 * \brief Outputs the content of the structure (debugging purpose)
667 * \param dict the exibitionist
668 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
670 * Outputs the content of the structure. (for debugging purpose). \a output is a
671 * function to output the data. If NULL, data won't be displayed.
674 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
677 xbt_dictelm_t element;
678 printf("Dict %p:\n", dict);
680 for (i = 0; i < dict->table_size; i++) {
681 element = dict->table[i];
684 while (element != NULL) {
685 printf(" %s -> '", element->key);
686 if (output != NULL) {
687 output(element->content);
690 element = element->next;
700 xbt_dynar_t all_sizes = NULL;
701 /** @brief shows some debugging info about the bucklet repartition */
702 void xbt_dict_dump_sizes(xbt_dict_t dict)
708 xbt_dictelm_t element;
709 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
711 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
712 dict->fill, dict->table_size);
714 for (i = 0; i < dict->table_size; i++) {
715 element = dict->table[i];
718 while (element != NULL) {
720 element = element->next;
723 if (xbt_dynar_length(sizes) <= size) {
725 xbt_dynar_set(sizes, size, &prevsize);
728 xbt_dynar_get_cpy(sizes, size, &prevsize);
730 xbt_dynar_set(sizes, size, &prevsize);
734 all_sizes = xbt_dynar_new(sizeof(int), NULL);
736 xbt_dynar_foreach(sizes, count, size) {
737 /* Copy values of this one into all_sizes */
739 if (xbt_dynar_length(all_sizes) <= count) {
741 xbt_dynar_set(all_sizes, count, &prevcount);
743 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
745 xbt_dynar_set(all_sizes, count, &prevcount);
748 /* Report current sizes */
753 printf("%delm x %u cells; ", count, size);
757 xbt_dynar_free(&sizes);
761 * Create the dict mallocators.
762 * This is an internal XBT function called during the lib initialization.
763 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
765 void xbt_dict_preinit(void)
767 if (dict_mallocator != NULL) {
768 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
769 xbt_mallocator_free(dict_mallocator);
770 xbt_mallocator_free(dict_elm_mallocator);
773 dict_mallocator = xbt_mallocator_new(256,
774 dict_mallocator_new_f,
775 dict_mallocator_free_f,
776 dict_mallocator_reset_f);
777 dict_elm_mallocator = xbt_mallocator_new(256,
778 dict_elm_mallocator_new_f,
779 dict_elm_mallocator_free_f,
780 dict_elm_mallocator_reset_f);
784 * Destroy the dict mallocators.
785 * This is an internal XBT function during the lib initialization
787 void xbt_dict_postexit(void)
789 if (dict_mallocator != NULL) {
790 xbt_mallocator_free(dict_mallocator);
791 dict_mallocator = NULL;
792 xbt_mallocator_free(dict_elm_mallocator);
793 dict_elm_mallocator = NULL;
800 printf("Overall stats:");
801 xbt_dynar_foreach(all_sizes, count, size) {
806 printf("%delm x %d cells; ", count, size);
810 printf("; %f elm per cell\n", avg / (double) total_count);
814 static void *dict_mallocator_new_f(void)
816 return xbt_new(s_xbt_dict_t, 1);
822 #include "portable.h"
824 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
825 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
827 XBT_TEST_SUITE("dict", "Dict data container");
829 static void print_str(void *str)
831 printf("%s", (char *) PRINTF_STR(str));
834 static void debuged_add_ext(xbt_dict_t head, const char *key,
835 const char *data_to_fill)
837 char *data = xbt_strdup(data_to_fill);
839 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
842 xbt_dict_set(head, key, data, &free);
843 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
844 xbt_dict_dump(head, (void (*)(void *)) &printf);
849 static void debuged_add(xbt_dict_t head, const char *key)
851 debuged_add_ext(head, key, key);
854 static void fill(xbt_dict_t * head)
856 xbt_test_add("Fill in the dictionnary");
858 *head = xbt_dict_new();
859 debuged_add(*head, "12");
860 debuged_add(*head, "12a");
861 debuged_add(*head, "12b");
862 debuged_add(*head, "123");
863 debuged_add(*head, "123456");
864 /* Child becomes child of what to add */
865 debuged_add(*head, "1234");
866 /* Need of common ancestor */
867 debuged_add(*head, "123457");
871 static void search_ext(xbt_dict_t head, const char *key, const char *data)
875 xbt_test_add("Search %s", key);
876 found = xbt_dict_get(head, key);
877 xbt_test_log("Found %s", (char *) found);
879 xbt_test_assert(found,
880 "data do not match expectations: found NULL while searching for %s",
883 xbt_test_assert(!strcmp((char *) data, found),
884 "data do not match expectations: found %s while searching for %s",
885 (char *) found, data);
888 static void search(xbt_dict_t head, const char *key)
890 search_ext(head, key, key);
893 static void debuged_remove(xbt_dict_t head, const char *key)
896 xbt_test_add("Remove '%s'", PRINTF_STR(key));
897 xbt_dict_remove(head, key);
898 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
902 static void traverse(xbt_dict_t head)
904 xbt_dict_cursor_t cursor = NULL;
909 xbt_dict_foreach(head, cursor, key, data) {
910 if (!key || !data || strcmp(key, data)) {
911 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
914 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
916 xbt_test_assert(!data || !strcmp(key, data),
917 "Key(%s) != value(%s). Aborting", key, data);
921 static void search_not_found(xbt_dict_t head, const char *data)
926 xbt_test_add("Search %s (expected not to be found)", data);
929 data = xbt_dict_get(head, data);
930 THROWF(unknown_error, 0,
931 "Found something which shouldn't be there (%s)", data);
934 if (e.category != not_found_error)
935 xbt_test_exception(e);
939 xbt_test_assert(ok, "Exception not raised");
942 static void count(xbt_dict_t dict, int length)
944 xbt_dict_cursor_t cursor;
950 xbt_test_add("Count elements (expecting %d)", length);
951 xbt_test_assert(xbt_dict_length(dict) == length,
952 "Announced length(%d) != %d.", xbt_dict_length(dict),
955 xbt_dict_foreach(dict, cursor, key, data)
958 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
963 static void count_check_get_key(xbt_dict_t dict, int length)
965 xbt_dict_cursor_t cursor;
967 _XBT_GNUC_UNUSED char *key2;
973 ("Count elements (expecting %d), and test the getkey function",
975 xbt_test_assert(xbt_dict_length(dict) == length,
976 "Announced length(%d) != %d.", xbt_dict_length(dict),
979 xbt_dict_foreach(dict, cursor, key, data) {
981 key2 = xbt_dict_get_key(dict, data);
982 xbt_assert(!strcmp(key, key2),
983 "The data was registered under %s instead of %s as expected",
987 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
993 xbt_dict_t head = NULL;
997 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
999 xbt_test_add("Traversal the null dictionary");
1002 xbt_test_add("Traversal and search the empty dictionary");
1003 head = xbt_dict_new();
1006 debuged_remove(head, "12346");
1009 if (e.category != not_found_error)
1010 xbt_test_exception(e);
1013 xbt_dict_free(&head);
1015 xbt_test_add("Traverse the full dictionary");
1017 count_check_get_key(head, 7);
1019 debuged_add_ext(head, "toto", "tutu");
1020 search_ext(head, "toto", "tutu");
1021 debuged_remove(head, "toto");
1023 search(head, "12a");
1026 xbt_test_add("Free the dictionary (twice)");
1027 xbt_dict_free(&head);
1028 xbt_dict_free(&head);
1032 count_check_get_key(head, 7);
1033 xbt_test_add("Change 123 to 'Changed 123'");
1034 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1035 count_check_get_key(head, 7);
1037 xbt_test_add("Change 123 back to '123'");
1038 xbt_dict_set(head, "123", strdup("123"), &free);
1039 count_check_get_key(head, 7);
1041 xbt_test_add("Change 12a to 'Dummy 12a'");
1042 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1043 count_check_get_key(head, 7);
1045 xbt_test_add("Change 12a to '12a'");
1046 xbt_dict_set(head, "12a", strdup("12a"), &free);
1047 count_check_get_key(head, 7);
1049 xbt_test_add("Traverse the resulting dictionary");
1053 xbt_test_add("Search 123");
1054 data = xbt_dict_get(head, "123");
1055 xbt_test_assert(data);
1056 xbt_test_assert(!strcmp("123", data));
1058 search_not_found(head, "Can't be found");
1059 search_not_found(head, "123 Can't be found");
1060 search_not_found(head, "12345678 NOT");
1062 search(head, "12a");
1063 search(head, "12b");
1065 search(head, "123456");
1066 search(head, "1234");
1067 search(head, "123457");
1069 xbt_test_add("Traverse the resulting dictionary");
1072 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1074 xbt_test_add("Free the dictionary twice");
1075 xbt_dict_free(&head);
1076 xbt_dict_free(&head);
1078 xbt_test_add("Traverse the resulting dictionary");
1082 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1086 xbt_test_add("Remove non existing data");
1088 debuged_remove(head, "Does not exist");
1091 if (e.category != not_found_error)
1092 xbt_test_exception(e);
1097 xbt_dict_free(&head);
1100 ("Remove each data manually (traversing the resulting dictionary each time)");
1102 debuged_remove(head, "12a");
1105 debuged_remove(head, "12b");
1108 debuged_remove(head, "12");
1111 debuged_remove(head, "123456");
1115 debuged_remove(head, "12346");
1118 if (e.category != not_found_error)
1119 xbt_test_exception(e);
1123 debuged_remove(head, "1234");
1125 debuged_remove(head, "123457");
1127 debuged_remove(head, "123");
1130 debuged_remove(head, "12346");
1133 if (e.category != not_found_error)
1134 xbt_test_exception(e);
1140 ("Free dict, create new fresh one, and then reset the dict");
1141 xbt_dict_free(&head);
1143 xbt_dict_reset(head);
1147 xbt_test_add("Free the dictionary twice");
1148 xbt_dict_free(&head);
1149 xbt_dict_free(&head);
1152 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1156 xbt_test_add("Store NULL under 'null'");
1157 xbt_dict_set(head, "null", NULL, NULL);
1158 search_ext(head, "null", NULL);
1160 xbt_test_add("Check whether I see it while traversing...");
1162 xbt_dict_cursor_t cursor = NULL;
1166 xbt_dict_foreach(head, cursor, key, data) {
1167 if (!key || !data || strcmp(key, data)) {
1168 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1170 xbt_test_log("Seen: %s", PRINTF_STR(key));
1173 if (!strcmp(key, "null"))
1176 xbt_test_assert(found,
1177 "the key 'null', associated to NULL is not found");
1179 xbt_dict_free(&head);
1182 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1184 uintptr_t stored_data = 0;
1185 xbt_test_log("Add %zu under %zu", data, key);
1187 xbt_dicti_set(head, key, data);
1188 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1189 xbt_dict_dump(head, (void (*)(void *)) &printf);
1192 stored_data = xbt_dicti_get(head, key);
1193 xbt_test_assert(stored_data == data,
1194 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1195 stored_data, data, key);
1198 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1200 xbt_test_add("Fill in the dictionnary");
1202 head = xbt_dict_new();
1203 debuged_addi(head, 12, 12);
1204 debuged_addi(head, 13, 13);
1205 debuged_addi(head, 14, 14);
1206 debuged_addi(head, 15, 15);
1208 debuged_addi(head, 12, 15);
1209 debuged_addi(head, 15, 2000);
1210 debuged_addi(head, 15, 3000);
1212 debuged_addi(head, 0, 1000);
1213 debuged_addi(head, 0, 2000);
1214 debuged_addi(head, 0, 3000);
1216 debuged_addi(head, 12, 0);
1217 debuged_addi(head, 13, 0);
1218 debuged_addi(head, 12, 0);
1219 debuged_addi(head, 0, 0);
1221 xbt_dict_free(&head);
1224 #define NB_ELM 20000
1225 #define SIZEOFKEY 1024
1226 static int countelems(xbt_dict_t head)
1228 xbt_dict_cursor_t cursor;
1233 xbt_dict_foreach(head, cursor, key, data) {
1239 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1241 xbt_dict_t head = NULL;
1246 srand((unsigned int) time(NULL));
1248 for (i = 0; i < 10; i++) {
1249 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1251 ("Fill the struct, count its elems and frees the structure");
1253 ("using 1000 elements with %d chars long randomized keys.",
1255 head = xbt_dict_new();
1256 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1257 for (j = 0; j < 1000; j++) {
1259 key = xbt_malloc(SIZEOFKEY);
1262 for (k = 0; k < SIZEOFKEY - 1; k++)
1263 key[k] = rand() % ('z' - 'a') + 'a';
1265 /* printf("[%d %s]\n",j,key); */
1266 data = xbt_dict_get_or_null(head, key);
1267 } while (data != NULL);
1269 xbt_dict_set(head, key, key, &free);
1270 data = xbt_dict_get(head, key);
1271 xbt_test_assert(!strcmp(key, data),
1272 "Retrieved value (%s) != Injected value (%s)", key,
1277 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1279 xbt_dict_free(&head);
1280 xbt_dict_free(&head);
1284 head = xbt_dict_new();
1285 xbt_test_add("Fill %d elements, with keys being the number of element",
1287 for (j = 0; j < NB_ELM; j++) {
1288 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1290 key = xbt_malloc(10);
1292 sprintf(key, "%d", j);
1293 xbt_dict_set(head, key, key, &free);
1295 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1298 ("Count the elements (retrieving the key and data for each)");
1299 i = countelems(head);
1300 xbt_test_log("There is %d elements", i);
1302 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1303 key = xbt_malloc(10);
1304 for (i = 0; i < 20; i++) {
1305 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1306 for (j = 0; j < NB_ELM; j++) {
1308 sprintf(key, "%d", j);
1309 data = xbt_dict_get(head, key);
1310 xbt_test_assert(!strcmp(key, (char *) data),
1311 "with get, key=%s != data=%s", key, (char *) data);
1312 data = xbt_dict_get_ext(head, key, strlen(key));
1313 xbt_test_assert(!strcmp(key, (char *) data),
1314 "with get_ext, key=%s != data=%s", key,
1320 xbt_test_add("Remove my %d elements", NB_ELM);
1321 key = xbt_malloc(10);
1322 for (j = 0; j < NB_ELM; j++) {
1323 /* if (!(j%10000)) printf("."); fflush(stdout); */
1325 sprintf(key, "%d", j);
1326 xbt_dict_remove(head, key);
1331 xbt_test_add("Free the structure (twice)");
1332 xbt_dict_free(&head);
1333 xbt_dict_free(&head);
1336 static void str_free(void *s)
1338 char *c = *(char **) s;
1342 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1346 #define NB_ELM 100 /*00 */
1348 #define KEY_SIZE 512
1349 #define NB_TEST 20 /*20 */
1352 xbt_dict_t mdict = NULL;
1354 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1359 xbt_test_add("Generic multicache CRASH test");
1361 (" Fill the struct and frees it %d times, using %d elements, "
1362 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1365 for (l = 0; l < DEPTH; l++) {
1366 key = xbt_malloc(KEY_SIZE);
1367 xbt_dynar_push(keys, &key);
1370 for (i = 0; i < NB_TEST; i++) {
1371 mdict = xbt_dict_new();
1372 XBT_VERB("mdict=%p", mdict);
1374 printf("Test %d\n", i);
1375 /* else if (i%10) printf("."); else printf("%d",i/10); */
1377 for (j = 0; j < NB_ELM; j++) {
1381 for (l = 0; l < DEPTH; l++) {
1382 key = *(char **) xbt_dynar_get_ptr(keys, l);
1384 for (k = 0; k < KEY_SIZE - 1; k++)
1385 key[k] = rand() % ('z' - 'a') + 'a';
1390 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1393 printf("in multitree %p.\n", mdict);
1395 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1397 data = xbt_multidict_get(mdict, keys);
1399 xbt_test_assert(data && !strcmp((char *) data, key),
1400 "Retrieved value (%s) does not match the given one (%s)\n",
1401 (char *) data, key);
1403 xbt_dict_free(&mdict);
1406 xbt_dynar_free(&keys);
1409 xbt_dict_dump(mdict,&xbt_dict_print); */
1411 xbt_dict_free(&mdict);
1412 xbt_dynar_free(&keys);
1415 #endif /* SIMGRID_TEST */