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 static void dict_mallocator_free_f(void *dict);
27 static void dict_mallocator_reset_f(void *dict);
30 /*####[ Code ]###############################################################*/
34 * \return pointer to the destination
35 * \see xbt_dict_new_ext(), 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 DEBUG2("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 key as argument) when
227 * \a key 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 DEBUG5("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 DEBUG6("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 key as argument) when
285 * \a key 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 THROW2(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 THROW1(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 THROW2(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 DEBUG5("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 THROW1(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 (xbt_dict_size(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);
819 static void dict_mallocator_free_f(void *dict)
824 static void dict_mallocator_reset_f(void *dict)
826 /* nothing to do because all fields are
827 * initialized in xbt_dict_new
834 #include "portable.h"
836 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
837 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
839 XBT_TEST_SUITE("dict", "Dict data container");
841 static void print_str(void *str)
843 printf("%s", (char *) PRINTF_STR(str));
846 static void debuged_add_ext(xbt_dict_t head, const char *key,
847 const char *data_to_fill)
849 char *data = xbt_strdup(data_to_fill);
851 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill),
854 xbt_dict_set(head, key, data, &free);
855 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
856 xbt_dict_dump(head, (void (*)(void *)) &printf);
861 static void debuged_add(xbt_dict_t head, const char *key)
863 debuged_add_ext(head, key, key);
866 static void fill(xbt_dict_t * head)
868 xbt_test_add0("Fill in the dictionnary");
870 *head = xbt_dict_new();
871 debuged_add(*head, "12");
872 debuged_add(*head, "12a");
873 debuged_add(*head, "12b");
874 debuged_add(*head, "123");
875 debuged_add(*head, "123456");
876 /* Child becomes child of what to add */
877 debuged_add(*head, "1234");
878 /* Need of common ancestor */
879 debuged_add(*head, "123457");
883 static void search_ext(xbt_dict_t head, const char *key, const char *data)
887 xbt_test_add1("Search %s", key);
888 found = xbt_dict_get(head, key);
889 xbt_test_log1("Found %s", (char *) found);
891 xbt_test_assert1(found,
892 "data do not match expectations: found NULL while searching for %s",
895 xbt_test_assert2(!strcmp((char *) data, found),
896 "data do not match expectations: found %s while searching for %s",
897 (char *) found, data);
900 static void search(xbt_dict_t head, const char *key)
902 search_ext(head, key, key);
905 static void debuged_remove(xbt_dict_t head, const char *key)
908 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
909 xbt_dict_remove(head, key);
910 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
914 static void traverse(xbt_dict_t head)
916 xbt_dict_cursor_t cursor = NULL;
921 xbt_dict_foreach(head, cursor, key, data) {
922 if (!key || !data || strcmp(key, data)) {
923 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
926 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
928 xbt_test_assert2(!data || !strcmp(key, data),
929 "Key(%s) != value(%s). Aborting", key, data);
933 static void search_not_found(xbt_dict_t head, const char *data)
938 xbt_test_add1("Search %s (expected not to be found)", data);
941 data = xbt_dict_get(head, data);
942 THROW1(unknown_error, 0,
943 "Found something which shouldn't be there (%s)", data);
945 if (e.category != not_found_error)
946 xbt_test_exception(e);
950 xbt_test_assert0(ok, "Exception not raised");
953 static void count(xbt_dict_t dict, int length)
955 xbt_dict_cursor_t cursor;
961 xbt_test_add1("Count elements (expecting %d)", length);
962 xbt_test_assert2(xbt_dict_length(dict) == length,
963 "Announced length(%d) != %d.", xbt_dict_length(dict),
966 xbt_dict_foreach(dict, cursor, key, data)
969 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
974 static void count_check_get_key(xbt_dict_t dict, int length)
976 xbt_dict_cursor_t cursor;
983 ("Count elements (expecting %d), and test the getkey function",
985 xbt_test_assert2(xbt_dict_length(dict) == length,
986 "Announced length(%d) != %d.", xbt_dict_length(dict),
989 xbt_dict_foreach(dict, cursor, key, data) {
991 key2 = xbt_dict_get_key(dict, data);
992 xbt_assert2(!strcmp(key, key2),
993 "The data was registered under %s instead of %s as expected",
997 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
1003 xbt_dict_t head = NULL;
1007 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
1009 xbt_test_add0("Traversal the null dictionary");
1012 xbt_test_add0("Traversal and search the empty dictionary");
1013 head = xbt_dict_new();
1016 debuged_remove(head, "12346");
1018 if (e.category != not_found_error)
1019 xbt_test_exception(e);
1022 xbt_dict_free(&head);
1024 xbt_test_add0("Traverse the full dictionary");
1026 count_check_get_key(head, 7);
1028 debuged_add_ext(head, "toto", "tutu");
1029 search_ext(head, "toto", "tutu");
1030 debuged_remove(head, "toto");
1032 search(head, "12a");
1035 xbt_test_add0("Free the dictionary (twice)");
1036 xbt_dict_free(&head);
1037 xbt_dict_free(&head);
1041 count_check_get_key(head, 7);
1042 xbt_test_add0("Change 123 to 'Changed 123'");
1043 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1044 count_check_get_key(head, 7);
1046 xbt_test_add0("Change 123 back to '123'");
1047 xbt_dict_set(head, "123", strdup("123"), &free);
1048 count_check_get_key(head, 7);
1050 xbt_test_add0("Change 12a to 'Dummy 12a'");
1051 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1052 count_check_get_key(head, 7);
1054 xbt_test_add0("Change 12a to '12a'");
1055 xbt_dict_set(head, "12a", strdup("12a"), &free);
1056 count_check_get_key(head, 7);
1058 xbt_test_add0("Traverse the resulting dictionary");
1062 xbt_test_add0("Search 123");
1063 data = xbt_dict_get(head, "123");
1064 xbt_test_assert(data);
1065 xbt_test_assert(!strcmp("123", data));
1067 search_not_found(head, "Can't be found");
1068 search_not_found(head, "123 Can't be found");
1069 search_not_found(head, "12345678 NOT");
1071 search(head, "12a");
1072 search(head, "12b");
1074 search(head, "123456");
1075 search(head, "1234");
1076 search(head, "123457");
1078 xbt_test_add0("Traverse the resulting dictionary");
1081 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1083 xbt_test_add0("Free the dictionary twice");
1084 xbt_dict_free(&head);
1085 xbt_dict_free(&head);
1087 xbt_test_add0("Traverse the resulting dictionary");
1091 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1095 xbt_test_add0("Remove non existing data");
1097 debuged_remove(head, "Does not exist");
1100 if (e.category != not_found_error)
1101 xbt_test_exception(e);
1106 xbt_dict_free(&head);
1109 ("Remove each data manually (traversing the resulting dictionary each time)");
1111 debuged_remove(head, "12a");
1114 debuged_remove(head, "12b");
1117 debuged_remove(head, "12");
1120 debuged_remove(head, "123456");
1124 debuged_remove(head, "12346");
1127 if (e.category != not_found_error)
1128 xbt_test_exception(e);
1132 debuged_remove(head, "1234");
1134 debuged_remove(head, "123457");
1136 debuged_remove(head, "123");
1139 debuged_remove(head, "12346");
1142 if (e.category != not_found_error)
1143 xbt_test_exception(e);
1149 ("Free dict, create new fresh one, and then reset the dict");
1150 xbt_dict_free(&head);
1152 xbt_dict_reset(head);
1156 xbt_test_add0("Free the dictionary twice");
1157 xbt_dict_free(&head);
1158 xbt_dict_free(&head);
1161 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1165 xbt_test_add0("Store NULL under 'null'");
1166 xbt_dict_set(head, "null", NULL, NULL);
1167 search_ext(head, "null", NULL);
1169 xbt_test_add0("Check whether I see it while traversing...");
1171 xbt_dict_cursor_t cursor = NULL;
1175 xbt_dict_foreach(head, cursor, key, data) {
1176 if (!key || !data || strcmp(key, data)) {
1177 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1179 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1182 if (!strcmp(key, "null"))
1185 xbt_test_assert0(found,
1186 "the key 'null', associated to NULL is not found");
1188 xbt_dict_free(&head);
1191 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
1193 uintptr_t stored_data = 0;
1194 xbt_test_log2("Add %zu under %zu", data, key);
1196 xbt_dicti_set(head, key, data);
1197 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1198 xbt_dict_dump(head, (void (*)(void *)) &printf);
1201 stored_data = xbt_dicti_get(head, key);
1202 xbt_test_assert3(stored_data == data,
1203 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",
1204 stored_data, data, key);
1207 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1209 xbt_test_add0("Fill in the dictionnary");
1211 head = xbt_dict_new();
1212 debuged_addi(head, 12, 12);
1213 debuged_addi(head, 13, 13);
1214 debuged_addi(head, 14, 14);
1215 debuged_addi(head, 15, 15);
1217 debuged_addi(head, 12, 15);
1218 debuged_addi(head, 15, 2000);
1219 debuged_addi(head, 15, 3000);
1221 debuged_addi(head, 0, 1000);
1222 debuged_addi(head, 0, 2000);
1223 debuged_addi(head, 0, 3000);
1225 debuged_addi(head, 12, 0);
1226 debuged_addi(head, 13, 0);
1227 debuged_addi(head, 12, 0);
1228 debuged_addi(head, 0, 0);
1230 xbt_dict_free(&head);
1233 #define NB_ELM 20000
1234 #define SIZEOFKEY 1024
1235 static int countelems(xbt_dict_t head)
1237 xbt_dict_cursor_t cursor;
1242 xbt_dict_foreach(head, cursor, key, data) {
1248 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1250 xbt_dict_t head = NULL;
1255 srand((unsigned int) time(NULL));
1257 for (i = 0; i < 10; i++) {
1258 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1260 ("Fill the struct, count its elems and frees the structure");
1262 ("using 1000 elements with %d chars long randomized keys.",
1264 head = xbt_dict_new();
1265 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1267 for (j = 0; j < 1000; j++) {
1269 key = xbt_malloc(SIZEOFKEY);
1272 for (k = 0; k < SIZEOFKEY - 1; k++)
1273 key[k] = rand() % ('z' - 'a') + 'a';
1275 /* printf("[%d %s]\n",j,key); */
1276 data = xbt_dict_get_or_null(head, key);
1277 } while (data != NULL);
1279 xbt_dict_set(head, key, key, &free);
1280 data = xbt_dict_get(head, key);
1281 xbt_test_assert2(!strcmp(key, data),
1282 "Retrieved value (%s) != Injected value (%s)", key,
1287 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1289 xbt_dict_free(&head);
1290 xbt_dict_free(&head);
1294 head = xbt_dict_new();
1295 xbt_test_add1("Fill %d elements, with keys being the number of element",
1297 for (j = 0; j < NB_ELM; j++) {
1298 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1300 key = xbt_malloc(10);
1302 sprintf(key, "%d", j);
1303 xbt_dict_set(head, key, key, &free);
1305 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1308 ("Count the elements (retrieving the key and data for each)");
1309 i = countelems(head);
1310 xbt_test_log1("There is %d elements", i);
1312 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1313 key = xbt_malloc(10);
1314 for (i = 0; i < 20; i++) {
1315 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1316 for (j = 0; j < NB_ELM; j++) {
1318 sprintf(key, "%d", j);
1319 data = xbt_dict_get(head, key);
1320 xbt_test_assert2(!strcmp(key, (char *) data),
1321 "with get, key=%s != data=%s", key, (char *) data);
1322 data = xbt_dict_get_ext(head, key, strlen(key));
1323 xbt_test_assert2(!strcmp(key, (char *) data),
1324 "with get_ext, key=%s != data=%s", key,
1330 xbt_test_add1("Remove my %d elements", NB_ELM);
1331 key = xbt_malloc(10);
1332 for (j = 0; j < NB_ELM; j++) {
1333 /* if (!(j%10000)) printf("."); fflush(stdout); */
1335 sprintf(key, "%d", j);
1336 xbt_dict_remove(head, key);
1341 xbt_test_add0("Free the structure (twice)");
1342 xbt_dict_free(&head);
1343 xbt_dict_free(&head);
1346 static void str_free(void *s)
1348 char *c = *(char **) s;
1352 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1356 #define NB_ELM 100 /*00 */
1358 #define KEY_SIZE 512
1359 #define NB_TEST 20 /*20 */
1362 xbt_dict_t mdict = NULL;
1364 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1369 xbt_test_add0("Generic multicache CRASH test");
1371 (" Fill the struct and frees it %d times, using %d elements, "
1372 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1375 for (l = 0; l < DEPTH; l++) {
1376 key = xbt_malloc(KEY_SIZE);
1377 xbt_dynar_push(keys, &key);
1380 for (i = 0; i < NB_TEST; i++) {
1381 mdict = xbt_dict_new();
1382 VERB1("mdict=%p", mdict);
1384 printf("Test %d\n", i);
1385 /* else if (i%10) printf("."); else printf("%d",i/10); */
1387 for (j = 0; j < NB_ELM; j++) {
1391 for (l = 0; l < DEPTH; l++) {
1392 key = *(char **) xbt_dynar_get_ptr(keys, l);
1394 for (k = 0; k < KEY_SIZE - 1; k++)
1395 key[k] = rand() % ('z' - 'a') + 'a';
1400 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1403 printf("in multitree %p.\n", mdict);
1405 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1407 data = xbt_multidict_get(mdict, keys);
1409 xbt_test_assert2(data && !strcmp((char *) data, key),
1410 "Retrieved value (%s) does not match the given one (%s)\n",
1411 (char *) data, key);
1413 xbt_dict_free(&mdict);
1416 xbt_dynar_free(&keys);
1419 xbt_dict_dump(mdict,&xbt_dict_print); */
1421 xbt_dict_free(&mdict);
1422 xbt_dynar_free(&keys);
1425 #endif /* SIMGRID_TEST */