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)
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)
643 * \brief Outputs the content of the structure (debugging purpose)
645 * \param dict the exibitionist
646 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
648 * Outputs the content of the structure. (for debugging purpose). \a output is a
649 * function to output the data. If NULL, data won't be displayed.
652 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
655 xbt_dictelm_t element;
656 printf("Dict %p:\n", dict);
658 for (i = 0; i < dict->table_size; i++) {
659 element = dict->table[i];
662 while (element != NULL) {
663 printf(" %s -> '", element->key);
664 if (output != NULL) {
665 (*output) (element->content);
668 element = element->next;
678 xbt_dynar_t all_sizes = NULL;
679 /** @brief shows some debugging info about the bucklet repartition */
680 void xbt_dict_dump_sizes(xbt_dict_t dict)
686 xbt_dictelm_t element;
687 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
689 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
690 dict->fill, dict->table_size);
692 for (i = 0; i < dict->table_size; i++) {
693 element = dict->table[i];
696 while (element != NULL) {
698 element = element->next;
701 if (xbt_dynar_length(sizes) <= size) {
703 xbt_dynar_set(sizes, size, &prevsize);
706 xbt_dynar_get_cpy(sizes, size, &prevsize);
708 xbt_dynar_set(sizes, size, &prevsize);
712 all_sizes = xbt_dynar_new(sizeof(int), NULL);
714 xbt_dynar_foreach(sizes, count, size) {
715 /* Copy values of this one into all_sizes */
717 if (xbt_dynar_length(all_sizes) <= count) {
719 xbt_dynar_set(all_sizes, count, &prevcount);
721 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
723 xbt_dynar_set(all_sizes, count, &prevcount);
726 /* Report current sizes */
731 printf("%delm x %u cells; ", count, size);
735 xbt_dynar_free(&sizes);
739 * Create the dict mallocators.
740 * This is an internal XBT function called during the lib initialization.
741 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
743 void xbt_dict_preinit(void) {
744 if (dict_mallocator != NULL) {
745 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
746 xbt_mallocator_free(dict_mallocator);
747 xbt_mallocator_free(dict_elm_mallocator);
750 dict_mallocator = xbt_mallocator_new(256,
751 dict_mallocator_new_f,
752 dict_mallocator_free_f,
753 dict_mallocator_reset_f);
754 dict_elm_mallocator = xbt_mallocator_new(256,
755 dict_elm_mallocator_new_f,
756 dict_elm_mallocator_free_f,
757 dict_elm_mallocator_reset_f);
761 * Destroy the dict mallocators.
762 * This is an internal XBT function during the lib initialization
764 void xbt_dict_postexit(void)
766 if (dict_mallocator != NULL) {
767 xbt_mallocator_free(dict_mallocator);
768 dict_mallocator = NULL;
769 xbt_mallocator_free(dict_elm_mallocator);
770 dict_elm_mallocator = NULL;
777 printf("Overall stats:");
778 xbt_dynar_foreach(all_sizes, count, size) {
783 printf("%delm x %d cells; ", count, size);
787 printf("; %f elm per cell\n", avg / (double) total_count);
791 static void *dict_mallocator_new_f(void)
793 return xbt_new(s_xbt_dict_t, 1);
796 static void dict_mallocator_free_f(void *dict)
801 static void dict_mallocator_reset_f(void *dict)
803 /* nothing to do because all fields are
804 * initialized in xbt_dict_new
811 #include "portable.h"
813 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
814 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
816 XBT_TEST_SUITE("dict", "Dict data container");
818 static void print_str(void *str)
820 printf("%s", (char *) PRINTF_STR(str));
823 static void debuged_add_ext(xbt_dict_t head, const char *key,
824 const char *data_to_fill)
826 char *data = xbt_strdup(data_to_fill);
828 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
830 xbt_dict_set(head, key, data, &free);
831 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
832 xbt_dict_dump(head, (void (*)(void *)) &printf);
837 static void debuged_add(xbt_dict_t head, const char *key)
839 debuged_add_ext(head, key, key);
842 static void fill(xbt_dict_t * head)
844 xbt_test_add0("Fill in the dictionnary");
846 *head = xbt_dict_new();
847 debuged_add(*head, "12");
848 debuged_add(*head, "12a");
849 debuged_add(*head, "12b");
850 debuged_add(*head, "123");
851 debuged_add(*head, "123456");
852 /* Child becomes child of what to add */
853 debuged_add(*head, "1234");
854 /* Need of common ancestor */
855 debuged_add(*head, "123457");
859 static void search_ext(xbt_dict_t head, const char *key, const char *data)
863 xbt_test_add1("Search %s", key);
864 found = xbt_dict_get(head, key);
865 xbt_test_log1("Found %s", (char *) found);
867 xbt_test_assert1(found,
868 "data do not match expectations: found NULL while searching for %s",
871 xbt_test_assert2(!strcmp((char *) data, found),
872 "data do not match expectations: found %s while searching for %s",
873 (char *) found, data);
876 static void search(xbt_dict_t head, const char *key)
878 search_ext(head, key, key);
881 static void debuged_remove(xbt_dict_t head, const char *key)
884 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
885 xbt_dict_remove(head, key);
886 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
890 static void traverse(xbt_dict_t head)
892 xbt_dict_cursor_t cursor = NULL;
897 xbt_dict_foreach(head, cursor, key, data) {
898 if (!key || !data || strcmp(key, data)) {
899 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
902 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
904 xbt_test_assert2(!data || !strcmp(key, data),
905 "Key(%s) != value(%s). Aborting", key, data);
909 static void search_not_found(xbt_dict_t head, const char *data)
914 xbt_test_add1("Search %s (expected not to be found)", data);
917 data = xbt_dict_get(head, data);
918 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
921 if (e.category != not_found_error)
922 xbt_test_exception(e);
926 xbt_test_assert0(ok, "Exception not raised");
929 static void count(xbt_dict_t dict, int length)
931 xbt_dict_cursor_t cursor;
937 xbt_test_add1("Count elements (expecting %d)", length);
938 xbt_test_assert2(xbt_dict_length(dict) == length,
939 "Announced length(%d) != %d.", xbt_dict_length(dict),
942 xbt_dict_foreach(dict, cursor, key, data)
945 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
950 static void count_check_get_key(xbt_dict_t dict, int length)
952 xbt_dict_cursor_t cursor;
958 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
959 xbt_test_assert2(xbt_dict_length(dict) == length,
960 "Announced length(%d) != %d.", xbt_dict_length(dict),
963 xbt_dict_foreach(dict, cursor, key, data) {
965 key2 = xbt_dict_get_key(dict,data);
966 xbt_assert2(!strcmp(key,key2),
967 "The data was registered under %s instead of %s as expected",key2,key);
970 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
976 xbt_dict_t head = NULL;
980 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
982 xbt_test_add0("Traversal the null dictionary");
985 xbt_test_add0("Traversal and search the empty dictionary");
986 head = xbt_dict_new();
989 debuged_remove(head, "12346");
991 if (e.category != not_found_error)
992 xbt_test_exception(e);
995 xbt_dict_free(&head);
997 xbt_test_add0("Traverse the full dictionary");
999 count_check_get_key(head, 7);
1001 debuged_add_ext(head, "toto", "tutu");
1002 search_ext(head, "toto", "tutu");
1003 debuged_remove(head, "toto");
1005 search(head, "12a");
1008 xbt_test_add0("Free the dictionary (twice)");
1009 xbt_dict_free(&head);
1010 xbt_dict_free(&head);
1014 count_check_get_key(head, 7);
1015 xbt_test_add0("Change 123 to 'Changed 123'");
1016 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1017 count_check_get_key(head, 7);
1019 xbt_test_add0("Change 123 back to '123'");
1020 xbt_dict_set(head, "123", strdup("123"), &free);
1021 count_check_get_key(head, 7);
1023 xbt_test_add0("Change 12a to 'Dummy 12a'");
1024 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1025 count_check_get_key(head, 7);
1027 xbt_test_add0("Change 12a to '12a'");
1028 xbt_dict_set(head, "12a", strdup("12a"), &free);
1029 count_check_get_key(head, 7);
1031 xbt_test_add0("Traverse the resulting dictionary");
1035 xbt_test_add0("Search 123");
1036 data = xbt_dict_get(head, "123");
1037 xbt_test_assert(data);
1038 xbt_test_assert(!strcmp("123", data));
1040 search_not_found(head, "Can't be found");
1041 search_not_found(head, "123 Can't be found");
1042 search_not_found(head, "12345678 NOT");
1044 search(head, "12a");
1045 search(head, "12b");
1047 search(head, "123456");
1048 search(head, "1234");
1049 search(head, "123457");
1051 xbt_test_add0("Traverse the resulting dictionary");
1054 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1056 xbt_test_add0("Free the dictionary twice");
1057 xbt_dict_free(&head);
1058 xbt_dict_free(&head);
1060 xbt_test_add0("Traverse the resulting dictionary");
1064 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1068 xbt_test_add0("Remove non existing data");
1070 debuged_remove(head, "Does not exist");
1073 if (e.category != not_found_error)
1074 xbt_test_exception(e);
1079 xbt_dict_free(&head);
1082 ("Remove each data manually (traversing the resulting dictionary each time)");
1084 debuged_remove(head, "12a");
1087 debuged_remove(head, "12b");
1090 debuged_remove(head, "12");
1093 debuged_remove(head, "123456");
1097 debuged_remove(head, "12346");
1100 if (e.category != not_found_error)
1101 xbt_test_exception(e);
1105 debuged_remove(head, "1234");
1107 debuged_remove(head, "123457");
1109 debuged_remove(head, "123");
1112 debuged_remove(head, "12346");
1115 if (e.category != not_found_error)
1116 xbt_test_exception(e);
1121 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
1122 xbt_dict_free(&head);
1124 xbt_dict_reset(head);
1128 xbt_test_add0("Free the dictionary twice");
1129 xbt_dict_free(&head);
1130 xbt_dict_free(&head);
1133 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1137 xbt_test_add0("Store NULL under 'null'");
1138 xbt_dict_set(head, "null", NULL, NULL);
1139 search_ext(head, "null", NULL);
1141 xbt_test_add0("Check whether I see it while traversing...");
1143 xbt_dict_cursor_t cursor = NULL;
1147 xbt_dict_foreach(head, cursor, key, data) {
1148 if (!key || !data || strcmp(key, data)) {
1149 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1151 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1154 if (!strcmp(key, "null"))
1157 xbt_test_assert0(found,
1158 "the key 'null', associated to NULL is not found");
1160 xbt_dict_free(&head);
1163 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
1164 uintptr_t stored_data = 0;
1165 xbt_test_log2("Add %zu under %zu", data, key);
1167 xbt_dicti_set(head, key, data);
1168 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1169 xbt_dict_dump(head, (void (*)(void *)) &printf);
1172 stored_data = xbt_dicti_get(head, key);
1173 xbt_test_assert3(stored_data==data,
1174 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
1177 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1179 xbt_test_add0("Fill in the dictionnary");
1181 head = xbt_dict_new();
1182 debuged_addi(head, 12, 12);
1183 debuged_addi(head, 13, 13);
1184 debuged_addi(head, 14, 14);
1185 debuged_addi(head, 15, 15);
1187 debuged_addi(head, 12, 15);
1188 debuged_addi(head, 15, 2000);
1189 debuged_addi(head, 15, 3000);
1191 debuged_addi(head, 0, 1000);
1192 debuged_addi(head, 0, 2000);
1193 debuged_addi(head, 0, 3000);
1195 debuged_addi(head, 12, 0);
1196 debuged_addi(head, 13, 0);
1197 debuged_addi(head, 12, 0);
1198 debuged_addi(head, 0, 0);
1200 xbt_dict_free(&head);
1203 #define NB_ELM 20000
1204 #define SIZEOFKEY 1024
1205 static int countelems(xbt_dict_t head)
1207 xbt_dict_cursor_t cursor;
1212 xbt_dict_foreach(head, cursor, key, data) {
1218 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1220 xbt_dict_t head = NULL;
1225 srand((unsigned int) time(NULL));
1227 for (i = 0; i < 10; i++) {
1228 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1229 xbt_test_log0("Fill the struct, count its elems and frees the structure");
1230 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
1232 head = xbt_dict_new();
1233 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1235 for (j = 0; j < 1000; j++) {
1237 key = xbt_malloc(SIZEOFKEY);
1240 for (k = 0; k < SIZEOFKEY - 1; k++)
1241 key[k] = rand() % ('z' - 'a') + 'a';
1243 /* printf("[%d %s]\n",j,key); */
1244 data = xbt_dict_get_or_null(head, key);
1245 } while (data != NULL);
1247 xbt_dict_set(head, key, key, &free);
1248 data = xbt_dict_get(head, key);
1249 xbt_test_assert2(!strcmp(key, data),
1250 "Retrieved value (%s) != Injected value (%s)", key,
1255 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1257 xbt_dict_free(&head);
1258 xbt_dict_free(&head);
1262 head = xbt_dict_new();
1263 xbt_test_add1("Fill %d elements, with keys being the number of element",
1265 for (j = 0; j < NB_ELM; j++) {
1266 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1268 key = xbt_malloc(10);
1270 sprintf(key, "%d", j);
1271 xbt_dict_set(head, key, key, &free);
1273 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1275 xbt_test_add0("Count the elements (retrieving the key and data for each)");
1276 i = countelems(head);
1277 xbt_test_log1("There is %d elements", i);
1279 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1280 key = xbt_malloc(10);
1281 for (i = 0; i < 20; i++) {
1282 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1283 for (j = 0; j < NB_ELM; j++) {
1285 sprintf(key, "%d", j);
1286 data = xbt_dict_get(head, key);
1287 xbt_test_assert2(!strcmp(key, (char *) data),
1288 "with get, key=%s != data=%s", key, (char *) data);
1289 data = xbt_dict_get_ext(head, key, strlen(key));
1290 xbt_test_assert2(!strcmp(key, (char *) data),
1291 "with get_ext, key=%s != data=%s", key, (char *) data);
1296 xbt_test_add1("Remove my %d elements", NB_ELM);
1297 key = xbt_malloc(10);
1298 for (j = 0; j < NB_ELM; j++) {
1299 /* if (!(j%10000)) printf("."); fflush(stdout); */
1301 sprintf(key, "%d", j);
1302 xbt_dict_remove(head, key);
1307 xbt_test_add0("Free the structure (twice)");
1308 xbt_dict_free(&head);
1309 xbt_dict_free(&head);
1312 static void str_free(void *s)
1314 char *c = *(char **) s;
1318 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1322 #define NB_ELM 100 /*00 */
1324 #define KEY_SIZE 512
1325 #define NB_TEST 20 /*20 */
1328 xbt_dict_t mdict = NULL;
1330 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1335 xbt_test_add0("Generic multicache CRASH test");
1336 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1337 "depth of multicache=%d, key size=%d",
1338 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
1340 for (l = 0; l < DEPTH; l++) {
1341 key = xbt_malloc(KEY_SIZE);
1342 xbt_dynar_push(keys, &key);
1345 for (i = 0; i < NB_TEST; i++) {
1346 mdict = xbt_dict_new();
1347 VERB1("mdict=%p", mdict);
1349 printf("Test %d\n", i);
1350 /* else if (i%10) printf("."); else printf("%d",i/10); */
1352 for (j = 0; j < NB_ELM; j++) {
1356 for (l = 0; l < DEPTH; l++) {
1357 key = *(char **) xbt_dynar_get_ptr(keys, l);
1359 for (k = 0; k < KEY_SIZE - 1; k++)
1360 key[k] = rand() % ('z' - 'a') + 'a';
1365 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1368 printf("in multitree %p.\n", mdict);
1370 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1372 data = xbt_multidict_get(mdict, keys);
1374 xbt_test_assert2(data && !strcmp((char *) data, key),
1375 "Retrieved value (%s) does not match the given one (%s)\n",
1376 (char *) data, key);
1378 xbt_dict_free(&mdict);
1381 xbt_dynar_free(&keys);
1384 xbt_dict_dump(mdict,&xbt_dict_print); */
1386 xbt_dict_free(&mdict);
1387 xbt_dynar_free(&keys);
1390 #endif /* SIMGRID_TEST */