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, int str_len)
102 #ifdef DJB2_HASH_FUNCTION
103 /* fast implementation of djb2 algorithm */
105 register unsigned int hash = 5381;
109 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
111 # elif defined(FNV_HASH_FUNCTION)
112 register unsigned int hash = 0x811c9dc5;
113 unsigned char *bp = (unsigned char *) str; /* start of buffer */
114 unsigned char *be = bp + str_len; /* beyond end of buffer */
117 /* multiply by the 32 bit FNV magic prime mod 2^32 */
119 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
121 /* xor the bottom with the current octet */
122 hash ^= (unsigned int) *bp++;
126 register unsigned int hash = 0;
129 hash += (*str) * (*str);
137 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
139 #ifdef DJB2_HASH_FUNCTION
140 /* fast implementation of djb2 algorithm */
142 register unsigned int hash = 5381;
144 while ((c = *str++)) {
145 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
148 # elif defined(FNV_HASH_FUNCTION)
149 register unsigned int hash = 0x811c9dc5;
152 /* multiply by the 32 bit FNV magic prime mod 2^32 */
154 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
156 /* xor the bottom with the current byte */
157 hash ^= (unsigned int) *str++;
161 register unsigned int hash = 0;
164 hash += (*str) * (*str);
171 /* Expend the size of the dict */
172 static void xbt_dict_rehash(xbt_dict_t dict)
174 const int oldsize = dict->table_size + 1;
175 register int newsize = oldsize * 2;
177 register xbt_dictelm_t *currcell;
178 register xbt_dictelm_t *twincell;
179 register xbt_dictelm_t bucklet;
180 register xbt_dictelm_t *pprev;
183 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
184 newsize * sizeof(xbt_dictelm_t));
185 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
186 dict->table_size = --newsize;
187 dict->table = currcell;
188 DEBUG2("REHASH (%d->%d)", oldsize, newsize);
190 for (i = 0; i < oldsize; i++, currcell++) {
191 if (!*currcell) /* empty cell */
193 twincell = currcell + oldsize;
194 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
195 /* Since we use "& size" instead of "%size" and since the size was doubled,
196 each bucklet of this cell must either :
197 - stay in cell i (ie, currcell)
198 - go to the cell i+oldsize (ie, twincell) */
199 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
200 *pprev = bucklet->next;
201 bucklet->next = *twincell;
207 pprev = &bucklet->next;
212 if (!*currcell) /* everything moved */
218 * \brief Add data to the dict (arbitrary key)
219 * \param dict the container
220 * \param key the key to set the new data
221 * \param key_len the size of the \a key
222 * \param data the data to add in the dict
223 * \param free_ctn function to call with (\a key as argument) when
224 * \a key is removed from the dictionary
226 * Set the \a data in the structure under the \a key, which can be any kind
227 * of data, as long as its length is provided in \a key_len.
229 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
230 const char *key, int key_len,
231 void *data, void_f_pvoid_t free_ctn)
234 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
236 xbt_dictelm_t current, previous = NULL;
239 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
240 dict->table_size, hash_code & dict->table_size);
241 current = dict->table[hash_code & dict->table_size];
242 while (current != NULL &&
243 (hash_code != current->hash_code || key_len != current->key_len
244 || memcmp(key, current->key, key_len))) {
246 current = current->next;
249 if (current == NULL) {
250 /* this key doesn't exist yet */
251 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
253 if (previous == NULL) {
254 dict->table[hash_code & dict->table_size] = current;
256 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
257 xbt_dict_rehash(dict);
259 previous->next = current;
263 DEBUG6("Replace %.*s by %.*s under key %.*s",
264 key_len, (char *) current->content,
265 key_len, (char *) data, key_len, (char *) key);
266 /* there is already an element with the same key: overwrite it */
267 if (current->content != NULL && current->free_f != NULL) {
268 current->free_f(current->content);
270 current->content = data;
271 current->free_f = free_ctn;
276 * \brief Add data to the dict (null-terminated key)
278 * \param dict the dict
279 * \param key the key to set the new data
280 * \param data the data to add in the dict
281 * \param free_ctn function to call with (\a key as argument) when
282 * \a key is removed from the dictionary
284 * set the \a data in the structure under the \a key, which is a
285 * null terminated string.
287 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
288 const char *key, void *data, void_f_pvoid_t free_ctn)
291 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
295 * \brief Retrieve data from the dict (arbitrary key)
297 * \param dict the dealer of data
298 * \param key the key to find data
299 * \param key_len the size of the \a key
300 * \return the data that we are looking for
302 * Search the given \a key. Throws not_found_error when not found.
304 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
308 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
309 xbt_dictelm_t current;
313 current = dict->table[hash_code & dict->table_size];
314 while (current != NULL &&
315 (hash_code != current->hash_code || key_len != current->key_len
316 || memcmp(key, current->key, key_len))) {
317 current = current->next;
321 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
323 return current->content;
327 * \brief like xbt_dict_get_ext(), but returning NULL when not found
329 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
332 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
333 xbt_dictelm_t current;
337 current = dict->table[hash_code & dict->table_size];
338 while (current != NULL &&
339 (hash_code != current->hash_code || key_len != current->key_len
340 || memcmp(key, current->key, key_len))) {
341 current = current->next;
347 return current->content;
351 * @brief retrieve the key associated to that object. Warning, that's a linear search
353 * Returns NULL if the object cannot be found
355 char *xbt_dict_get_key(xbt_dict_t dict, const void*data) {
357 xbt_dictelm_t current;
360 for (i = 0; i <= dict->table_size; i++) {
361 current = dict->table[i];
362 while (current != NULL) {
363 if (current->content == data)
365 current = current->next;
373 * \brief Retrieve data from the dict (null-terminated key)
375 * \param dict the dealer of data
376 * \param key the key to find data
377 * \return the data that we are looking for
379 * Search the given \a key. Throws not_found_error when not found.
380 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
383 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
386 unsigned int hash_code = xbt_dict_hash(key);
387 xbt_dictelm_t current;
391 current = dict->table[hash_code & dict->table_size];
392 while (current != NULL
393 && (hash_code != current->hash_code || strcmp(key, current->key)))
394 current = current->next;
397 THROW1(not_found_error, 0, "key %s not found", key);
399 return current->content;
403 * \brief like xbt_dict_get(), but returning NULL when not found
405 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
407 unsigned int hash_code = xbt_dict_hash(key);
408 xbt_dictelm_t current;
412 current = dict->table[hash_code & dict->table_size];
413 while (current != NULL &&
414 hash_code != current->hash_code && strcmp(key, current->key))
415 current = current->next;
420 return current->content;
425 * \brief Remove data from the dict (arbitrary key)
427 * \param dict the trash can
428 * \param key the key of the data to be removed
429 * \param key_len the size of the \a key
431 * Remove the entry associated with the given \a key (throws not_found)
433 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
437 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
438 xbt_dictelm_t current, previous = NULL;
442 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
443 current = dict->table[hash_code & dict->table_size];
444 while (current != NULL &&
445 (hash_code != current->hash_code || key_len != current->key_len
446 || strncmp(key, current->key, key_len))) {
447 previous = current; /* save the previous node */
448 current = current->next;
452 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
454 if (previous != NULL) {
455 previous->next = current->next;
457 dict->table[hash_code & dict->table_size] = current->next;
460 if (!dict->table[hash_code & dict->table_size])
463 xbt_dictelm_free(current);
470 * \brief Remove data from the dict (null-terminated key)
472 * \param dict the dict
473 * \param key the key of the data to be removed
475 * Remove the entry associated with the given \a key
477 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
479 xbt_dict_remove_ext(dict, key, strlen(key));
483 * \brief Add data to the dict (arbitrary key)
484 * \param dict the container
485 * \param key the key to set the new data
486 * \param key_len the size of the \a key
487 * \param data the data to add in the dict
488 * \param free_ctn function to call with (\a key as argument) when
489 * \a key is removed from the dictionary
491 * Set the \a data in the structure under the \a key, which can be any kind
492 * of data, as long as its length is provided in \a key_len.
494 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
495 uintptr_t key, uintptr_t data) {
497 unsigned int hash_code = xbt_dict_hash_ext((void*)&key, sizeof(uintptr_t));
499 xbt_dictelm_t current, previous = NULL;
502 DEBUG5("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data, hash_code,
503 dict->table_size, hash_code & dict->table_size);
504 current = dict->table[hash_code & dict->table_size];
505 while (current != NULL &&
506 (hash_code != current->hash_code || sizeof(uintptr_t) != current->key_len
507 || (((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) {
545 unsigned int hash_code = xbt_dict_hash_ext(((void*)&key), sizeof(uintptr_t));
546 xbt_dictelm_t current;
550 current = dict->table[hash_code & dict->table_size];
551 while (current != NULL &&
552 (hash_code != current->hash_code || sizeof(uintptr_t) != current->key_len
553 || (((uintptr_t)key) != ((uintptr_t)current->key)) )) {
554 current = current->next;
560 return (uintptr_t)(current->content);
563 /** Remove a uintptr_t key from the dict */
564 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key) {
566 unsigned int hash_code = xbt_dict_hash_ext(((void*)&key), sizeof(uintptr_t));
567 xbt_dictelm_t current, previous = NULL;
570 current = dict->table[hash_code & dict->table_size];
571 while (current != NULL &&
572 (hash_code != current->hash_code || sizeof(uintptr_t) != current->key_len
573 || (((uintptr_t)key) != ((uintptr_t)current->key)) )) {
574 previous = current; /* save the previous node */
575 current = current->next;
579 THROW1(not_found_error, 0, "key %zu not found", key);
581 if (previous != NULL) {
582 previous->next = current->next;
584 dict->table[hash_code & dict->table_size] = current->next;
587 if (!dict->table[hash_code & dict->table_size])
590 xbt_dictelm_free(current);
596 * \brief Remove all data from the dict
597 * \param dict the dict
599 void xbt_dict_reset(xbt_dict_t dict)
603 xbt_dictelm_t current, previous = NULL;
607 if (dict->count == 0)
610 for (i = 0; i <= dict->table_size; i++) {
611 current = dict->table[i];
612 while (current != NULL) {
614 current = current->next;
615 xbt_dictelm_free(previous);
617 dict->table[i] = NULL;
625 * \brief Return the number of elements in the dict.
626 * \param dict a dictionary
628 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
635 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
636 void xbt_dict_dump_output_string(void *s)
641 * \brief test if the dict is empty or not
643 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
645 return (xbt_dict_size(dict) == 0);
649 * \brief Outputs the content of the structure (debugging purpose)
651 * \param dict the exibitionist
652 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
654 * Outputs the content of the structure. (for debugging purpose). \a output is a
655 * function to output the data. If NULL, data won't be displayed.
658 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
661 xbt_dictelm_t element;
662 printf("Dict %p:\n", dict);
664 for (i = 0; i < dict->table_size; i++) {
665 element = dict->table[i];
668 while (element != NULL) {
669 printf(" %s -> '", element->key);
670 if (output != NULL) {
671 (*output) (element->content);
674 element = element->next;
684 xbt_dynar_t all_sizes = NULL;
685 /** @brief shows some debugging info about the bucklet repartition */
686 void xbt_dict_dump_sizes(xbt_dict_t dict)
692 xbt_dictelm_t element;
693 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
695 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
696 dict->fill, dict->table_size);
698 for (i = 0; i < dict->table_size; i++) {
699 element = dict->table[i];
702 while (element != NULL) {
704 element = element->next;
707 if (xbt_dynar_length(sizes) <= size) {
709 xbt_dynar_set(sizes, size, &prevsize);
712 xbt_dynar_get_cpy(sizes, size, &prevsize);
714 xbt_dynar_set(sizes, size, &prevsize);
718 all_sizes = xbt_dynar_new(sizeof(int), NULL);
720 xbt_dynar_foreach(sizes, count, size) {
721 /* Copy values of this one into all_sizes */
723 if (xbt_dynar_length(all_sizes) <= count) {
725 xbt_dynar_set(all_sizes, count, &prevcount);
727 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
729 xbt_dynar_set(all_sizes, count, &prevcount);
732 /* Report current sizes */
737 printf("%delm x %u cells; ", count, size);
741 xbt_dynar_free(&sizes);
745 * Create the dict mallocators.
746 * This is an internal XBT function called during the lib initialization.
747 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
749 void xbt_dict_preinit(void) {
750 if (dict_mallocator != NULL) {
751 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
752 xbt_mallocator_free(dict_mallocator);
753 xbt_mallocator_free(dict_elm_mallocator);
756 dict_mallocator = xbt_mallocator_new(256,
757 dict_mallocator_new_f,
758 dict_mallocator_free_f,
759 dict_mallocator_reset_f);
760 dict_elm_mallocator = xbt_mallocator_new(256,
761 dict_elm_mallocator_new_f,
762 dict_elm_mallocator_free_f,
763 dict_elm_mallocator_reset_f);
767 * Destroy the dict mallocators.
768 * This is an internal XBT function during the lib initialization
770 void xbt_dict_postexit(void)
772 if (dict_mallocator != NULL) {
773 xbt_mallocator_free(dict_mallocator);
774 dict_mallocator = NULL;
775 xbt_mallocator_free(dict_elm_mallocator);
776 dict_elm_mallocator = NULL;
783 printf("Overall stats:");
784 xbt_dynar_foreach(all_sizes, count, size) {
789 printf("%delm x %d cells; ", count, size);
793 printf("; %f elm per cell\n", avg / (double) total_count);
797 static void *dict_mallocator_new_f(void)
799 return xbt_new(s_xbt_dict_t, 1);
802 static void dict_mallocator_free_f(void *dict)
807 static void dict_mallocator_reset_f(void *dict)
809 /* nothing to do because all fields are
810 * initialized in xbt_dict_new
817 #include "portable.h"
819 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
820 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
822 XBT_TEST_SUITE("dict", "Dict data container");
824 static void print_str(void *str)
826 printf("%s", (char *) PRINTF_STR(str));
829 static void debuged_add_ext(xbt_dict_t head, const char *key,
830 const char *data_to_fill)
832 char *data = xbt_strdup(data_to_fill);
834 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
836 xbt_dict_set(head, key, data, &free);
837 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
838 xbt_dict_dump(head, (void (*)(void *)) &printf);
843 static void debuged_add(xbt_dict_t head, const char *key)
845 debuged_add_ext(head, key, key);
848 static void fill(xbt_dict_t * head)
850 xbt_test_add0("Fill in the dictionnary");
852 *head = xbt_dict_new();
853 debuged_add(*head, "12");
854 debuged_add(*head, "12a");
855 debuged_add(*head, "12b");
856 debuged_add(*head, "123");
857 debuged_add(*head, "123456");
858 /* Child becomes child of what to add */
859 debuged_add(*head, "1234");
860 /* Need of common ancestor */
861 debuged_add(*head, "123457");
865 static void search_ext(xbt_dict_t head, const char *key, const char *data)
869 xbt_test_add1("Search %s", key);
870 found = xbt_dict_get(head, key);
871 xbt_test_log1("Found %s", (char *) found);
873 xbt_test_assert1(found,
874 "data do not match expectations: found NULL while searching for %s",
877 xbt_test_assert2(!strcmp((char *) data, found),
878 "data do not match expectations: found %s while searching for %s",
879 (char *) found, data);
882 static void search(xbt_dict_t head, const char *key)
884 search_ext(head, key, key);
887 static void debuged_remove(xbt_dict_t head, const char *key)
890 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
891 xbt_dict_remove(head, key);
892 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
896 static void traverse(xbt_dict_t head)
898 xbt_dict_cursor_t cursor = NULL;
903 xbt_dict_foreach(head, cursor, key, data) {
904 if (!key || !data || strcmp(key, data)) {
905 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
908 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
910 xbt_test_assert2(!data || !strcmp(key, data),
911 "Key(%s) != value(%s). Aborting", key, data);
915 static void search_not_found(xbt_dict_t head, const char *data)
920 xbt_test_add1("Search %s (expected not to be found)", data);
923 data = xbt_dict_get(head, data);
924 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
927 if (e.category != not_found_error)
928 xbt_test_exception(e);
932 xbt_test_assert0(ok, "Exception not raised");
935 static void count(xbt_dict_t dict, int length)
937 xbt_dict_cursor_t cursor;
943 xbt_test_add1("Count elements (expecting %d)", length);
944 xbt_test_assert2(xbt_dict_length(dict) == length,
945 "Announced length(%d) != %d.", xbt_dict_length(dict),
948 xbt_dict_foreach(dict, cursor, key, data)
951 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
956 static void count_check_get_key(xbt_dict_t dict, int length)
958 xbt_dict_cursor_t cursor;
964 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
965 xbt_test_assert2(xbt_dict_length(dict) == length,
966 "Announced length(%d) != %d.", xbt_dict_length(dict),
969 xbt_dict_foreach(dict, cursor, key, data) {
971 key2 = xbt_dict_get_key(dict,data);
972 xbt_assert2(!strcmp(key,key2),
973 "The data was registered under %s instead of %s as expected",key2,key);
976 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
982 xbt_dict_t head = NULL;
986 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
988 xbt_test_add0("Traversal the null dictionary");
991 xbt_test_add0("Traversal and search the empty dictionary");
992 head = xbt_dict_new();
995 debuged_remove(head, "12346");
997 if (e.category != not_found_error)
998 xbt_test_exception(e);
1001 xbt_dict_free(&head);
1003 xbt_test_add0("Traverse the full dictionary");
1005 count_check_get_key(head, 7);
1007 debuged_add_ext(head, "toto", "tutu");
1008 search_ext(head, "toto", "tutu");
1009 debuged_remove(head, "toto");
1011 search(head, "12a");
1014 xbt_test_add0("Free the dictionary (twice)");
1015 xbt_dict_free(&head);
1016 xbt_dict_free(&head);
1020 count_check_get_key(head, 7);
1021 xbt_test_add0("Change 123 to 'Changed 123'");
1022 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1023 count_check_get_key(head, 7);
1025 xbt_test_add0("Change 123 back to '123'");
1026 xbt_dict_set(head, "123", strdup("123"), &free);
1027 count_check_get_key(head, 7);
1029 xbt_test_add0("Change 12a to 'Dummy 12a'");
1030 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1031 count_check_get_key(head, 7);
1033 xbt_test_add0("Change 12a to '12a'");
1034 xbt_dict_set(head, "12a", strdup("12a"), &free);
1035 count_check_get_key(head, 7);
1037 xbt_test_add0("Traverse the resulting dictionary");
1041 xbt_test_add0("Search 123");
1042 data = xbt_dict_get(head, "123");
1043 xbt_test_assert(data);
1044 xbt_test_assert(!strcmp("123", data));
1046 search_not_found(head, "Can't be found");
1047 search_not_found(head, "123 Can't be found");
1048 search_not_found(head, "12345678 NOT");
1050 search(head, "12a");
1051 search(head, "12b");
1053 search(head, "123456");
1054 search(head, "1234");
1055 search(head, "123457");
1057 xbt_test_add0("Traverse the resulting dictionary");
1060 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1062 xbt_test_add0("Free the dictionary twice");
1063 xbt_dict_free(&head);
1064 xbt_dict_free(&head);
1066 xbt_test_add0("Traverse the resulting dictionary");
1070 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1074 xbt_test_add0("Remove non existing data");
1076 debuged_remove(head, "Does not exist");
1079 if (e.category != not_found_error)
1080 xbt_test_exception(e);
1085 xbt_dict_free(&head);
1088 ("Remove each data manually (traversing the resulting dictionary each time)");
1090 debuged_remove(head, "12a");
1093 debuged_remove(head, "12b");
1096 debuged_remove(head, "12");
1099 debuged_remove(head, "123456");
1103 debuged_remove(head, "12346");
1106 if (e.category != not_found_error)
1107 xbt_test_exception(e);
1111 debuged_remove(head, "1234");
1113 debuged_remove(head, "123457");
1115 debuged_remove(head, "123");
1118 debuged_remove(head, "12346");
1121 if (e.category != not_found_error)
1122 xbt_test_exception(e);
1127 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
1128 xbt_dict_free(&head);
1130 xbt_dict_reset(head);
1134 xbt_test_add0("Free the dictionary twice");
1135 xbt_dict_free(&head);
1136 xbt_dict_free(&head);
1139 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1143 xbt_test_add0("Store NULL under 'null'");
1144 xbt_dict_set(head, "null", NULL, NULL);
1145 search_ext(head, "null", NULL);
1147 xbt_test_add0("Check whether I see it while traversing...");
1149 xbt_dict_cursor_t cursor = NULL;
1153 xbt_dict_foreach(head, cursor, key, data) {
1154 if (!key || !data || strcmp(key, data)) {
1155 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1157 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1160 if (!strcmp(key, "null"))
1163 xbt_test_assert0(found,
1164 "the key 'null', associated to NULL is not found");
1166 xbt_dict_free(&head);
1169 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
1170 uintptr_t stored_data = 0;
1171 xbt_test_log2("Add %zu under %zu", data, key);
1173 xbt_dicti_set(head, key, data);
1174 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1175 xbt_dict_dump(head, (void (*)(void *)) &printf);
1178 stored_data = xbt_dicti_get(head, key);
1179 xbt_test_assert3(stored_data==data,
1180 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
1183 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1185 xbt_test_add0("Fill in the dictionnary");
1187 head = xbt_dict_new();
1188 debuged_addi(head, 12, 12);
1189 debuged_addi(head, 13, 13);
1190 debuged_addi(head, 14, 14);
1191 debuged_addi(head, 15, 15);
1193 debuged_addi(head, 12, 15);
1194 debuged_addi(head, 15, 2000);
1195 debuged_addi(head, 15, 3000);
1197 debuged_addi(head, 0, 1000);
1198 debuged_addi(head, 0, 2000);
1199 debuged_addi(head, 0, 3000);
1201 debuged_addi(head, 12, 0);
1202 debuged_addi(head, 13, 0);
1203 debuged_addi(head, 12, 0);
1204 debuged_addi(head, 0, 0);
1206 xbt_dict_free(&head);
1209 #define NB_ELM 20000
1210 #define SIZEOFKEY 1024
1211 static int countelems(xbt_dict_t head)
1213 xbt_dict_cursor_t cursor;
1218 xbt_dict_foreach(head, cursor, key, data) {
1224 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1226 xbt_dict_t head = NULL;
1231 srand((unsigned int) time(NULL));
1233 for (i = 0; i < 10; i++) {
1234 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1235 xbt_test_log0("Fill the struct, count its elems and frees the structure");
1236 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
1238 head = xbt_dict_new();
1239 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1241 for (j = 0; j < 1000; j++) {
1243 key = xbt_malloc(SIZEOFKEY);
1246 for (k = 0; k < SIZEOFKEY - 1; k++)
1247 key[k] = rand() % ('z' - 'a') + 'a';
1249 /* printf("[%d %s]\n",j,key); */
1250 data = xbt_dict_get_or_null(head, key);
1251 } while (data != NULL);
1253 xbt_dict_set(head, key, key, &free);
1254 data = xbt_dict_get(head, key);
1255 xbt_test_assert2(!strcmp(key, data),
1256 "Retrieved value (%s) != Injected value (%s)", key,
1261 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1263 xbt_dict_free(&head);
1264 xbt_dict_free(&head);
1268 head = xbt_dict_new();
1269 xbt_test_add1("Fill %d elements, with keys being the number of element",
1271 for (j = 0; j < NB_ELM; j++) {
1272 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1274 key = xbt_malloc(10);
1276 sprintf(key, "%d", j);
1277 xbt_dict_set(head, key, key, &free);
1279 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1281 xbt_test_add0("Count the elements (retrieving the key and data for each)");
1282 i = countelems(head);
1283 xbt_test_log1("There is %d elements", i);
1285 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1286 key = xbt_malloc(10);
1287 for (i = 0; i < 20; i++) {
1288 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1289 for (j = 0; j < NB_ELM; j++) {
1291 sprintf(key, "%d", j);
1292 data = xbt_dict_get(head, key);
1293 xbt_test_assert2(!strcmp(key, (char *) data),
1294 "with get, key=%s != data=%s", key, (char *) data);
1295 data = xbt_dict_get_ext(head, key, strlen(key));
1296 xbt_test_assert2(!strcmp(key, (char *) data),
1297 "with get_ext, key=%s != data=%s", key, (char *) data);
1302 xbt_test_add1("Remove my %d elements", NB_ELM);
1303 key = xbt_malloc(10);
1304 for (j = 0; j < NB_ELM; j++) {
1305 /* if (!(j%10000)) printf("."); fflush(stdout); */
1307 sprintf(key, "%d", j);
1308 xbt_dict_remove(head, key);
1313 xbt_test_add0("Free the structure (twice)");
1314 xbt_dict_free(&head);
1315 xbt_dict_free(&head);
1318 static void str_free(void *s)
1320 char *c = *(char **) s;
1324 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1328 #define NB_ELM 100 /*00 */
1330 #define KEY_SIZE 512
1331 #define NB_TEST 20 /*20 */
1334 xbt_dict_t mdict = NULL;
1336 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1341 xbt_test_add0("Generic multicache CRASH test");
1342 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1343 "depth of multicache=%d, key size=%d",
1344 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
1346 for (l = 0; l < DEPTH; l++) {
1347 key = xbt_malloc(KEY_SIZE);
1348 xbt_dynar_push(keys, &key);
1351 for (i = 0; i < NB_TEST; i++) {
1352 mdict = xbt_dict_new();
1353 VERB1("mdict=%p", mdict);
1355 printf("Test %d\n", i);
1356 /* else if (i%10) printf("."); else printf("%d",i/10); */
1358 for (j = 0; j < NB_ELM; j++) {
1362 for (l = 0; l < DEPTH; l++) {
1363 key = *(char **) xbt_dynar_get_ptr(keys, l);
1365 for (k = 0; k < KEY_SIZE - 1; k++)
1366 key[k] = rand() % ('z' - 'a') + 'a';
1371 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1374 printf("in multitree %p.\n", mdict);
1376 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1378 data = xbt_multidict_get(mdict, keys);
1380 xbt_test_assert2(data && !strcmp((char *) data, key),
1381 "Retrieved value (%s) does not match the given one (%s)\n",
1382 (char *) data, key);
1384 xbt_dict_free(&mdict);
1387 xbt_dynar_free(&keys);
1390 xbt_dict_dump(mdict,&xbt_dict_print); */
1392 xbt_dict_free(&mdict);
1393 xbt_dynar_free(&keys);
1396 #endif /* SIMGRID_TEST */