1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2003-2010 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #define DJB2_HASH_FUNCTION
9 //#define FNV_HASH_FUNCTION
15 #include "xbt/mallocator.h"
16 #include "xbt_modinter.h"
17 #include "dict_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
20 "Dictionaries provide the same functionalities than hash tables");
21 /*####[ Private prototypes ]#################################################*/
23 static xbt_mallocator_t dict_mallocator = NULL;
24 static void *dict_mallocator_new_f(void);
25 static void dict_mallocator_free_f(void *dict);
26 static void dict_mallocator_reset_f(void *dict);
29 /*####[ Code ]###############################################################*/
33 * \return pointer to the destination
34 * \see xbt_dict_new_ext(), xbt_dict_free()
36 * Creates and initialize a new dictionary with a default hashtable size.
38 xbt_dict_t xbt_dict_new(void)
42 if (dict_mallocator == NULL) {
44 dict_mallocator = xbt_mallocator_new(256,
45 dict_mallocator_new_f,
46 dict_mallocator_free_f,
47 dict_mallocator_reset_f);
48 dict_elm_mallocator = xbt_mallocator_new(256,
49 dict_elm_mallocator_new_f,
50 dict_elm_mallocator_free_f,
51 dict_elm_mallocator_reset_f);
54 dict = xbt_mallocator_get(dict_mallocator);
55 dict->table_size = 127;
56 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
65 * \param dict the dictionary to be freed
67 * Frees a dictionary with all the data
69 void xbt_dict_free(xbt_dict_t * dict)
72 xbt_dictelm_t current, previous;
76 // if ( *dict ) xbt_dict_dump_sizes(*dict);
78 if (dict != NULL && *dict != NULL) {
79 table_size = (*dict)->table_size;
80 table = (*dict)->table;
81 /* Warning: the size of the table is 'table_size+1'...
82 * This is because table_size is used as a binary mask in xbt_dict_rehash */
83 for (i = 0; (*dict)->count && i <= table_size; i++) {
85 while (current != NULL) {
87 current = current->next;
88 xbt_dictelm_free(previous);
93 xbt_mallocator_release(dict_mallocator, *dict);
99 * Returns the amount of elements in the dict
101 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
107 * Returns the hash code of a string.
109 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len)
113 #ifdef DJB2_HASH_FUNCTION
114 /* fast implementation of djb2 algorithm */
116 register unsigned int hash = 5381;
120 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
122 # elif defined(FNV_HASH_FUNCTION)
123 register unsigned int hash = 0x811c9dc5;
124 unsigned char *bp = (unsigned char *) str; /* start of buffer */
125 unsigned char *be = bp + str_len; /* beyond end of buffer */
128 /* multiply by the 32 bit FNV magic prime mod 2^32 */
130 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
132 /* xor the bottom with the current octet */
133 hash ^= (unsigned int) *bp++;
137 register unsigned int hash = 0;
140 hash += (*str) * (*str);
148 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
150 #ifdef DJB2_HASH_FUNCTION
151 /* fast implementation of djb2 algorithm */
153 register unsigned int hash = 5381;
155 while ((c = *str++)) {
156 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
159 # elif defined(FNV_HASH_FUNCTION)
160 register unsigned int hash = 0x811c9dc5;
163 /* multiply by the 32 bit FNV magic prime mod 2^32 */
165 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
167 /* xor the bottom with the current byte */
168 hash ^= (unsigned int) *str++;
172 register unsigned int hash = 0;
175 hash += (*str) * (*str);
182 /* Expend the size of the dict */
183 static void xbt_dict_rehash(xbt_dict_t dict)
185 const int oldsize = dict->table_size + 1;
186 register int newsize = oldsize * 2;
188 register xbt_dictelm_t *currcell;
189 register xbt_dictelm_t *twincell;
190 register xbt_dictelm_t bucklet;
191 register xbt_dictelm_t *pprev;
194 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
195 newsize * sizeof(xbt_dictelm_t));
196 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
197 dict->table_size = --newsize;
198 dict->table = currcell;
199 DEBUG2("REHASH (%d->%d)", oldsize, newsize);
201 for (i = 0; i < oldsize; i++, currcell++) {
202 if (!*currcell) /* empty cell */
204 twincell = currcell + oldsize;
205 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
206 /* Since we use "& size" instead of "%size" and since the size was doubled,
207 each bucklet of this cell must either :
208 - stay in cell i (ie, currcell)
209 - go to the cell i+oldsize (ie, twincell) */
210 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
211 *pprev = bucklet->next;
212 bucklet->next = *twincell;
218 pprev = &bucklet->next;
223 if (!*currcell) /* everything moved */
229 * \brief Add data to the dict (arbitrary key)
230 * \param dict the container
231 * \param key the key to set the new data
232 * \param key_len the size of the \a key
233 * \param data the data to add in the dict
234 * \param free_ctn function to call with (\a key as argument) when
235 * \a key is removed from the dictionary
237 * Set the \a data in the structure under the \a key, which can be any kind
238 * of data, as long as its length is provided in \a key_len.
240 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
241 const char *key, int key_len,
242 void *data, void_f_pvoid_t free_ctn)
245 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
247 xbt_dictelm_t current, previous = NULL;
250 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
251 dict->table_size, hash_code & dict->table_size);
252 current = dict->table[hash_code & dict->table_size];
253 while (current != NULL &&
254 (hash_code != current->hash_code || key_len != current->key_len
255 || memcmp(key, current->key, key_len))) {
257 current = current->next;
260 if (current == NULL) {
261 /* this key doesn't exist yet */
262 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
264 if (previous == NULL) {
265 dict->table[hash_code & dict->table_size] = current;
267 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
268 xbt_dict_rehash(dict);
270 previous->next = current;
274 DEBUG6("Replace %.*s by %.*s under key %.*s",
275 key_len, (char *) current->content,
276 key_len, (char *) data, key_len, (char *) key);
277 /* there is already an element with the same key: overwrite it */
278 if (current->content != NULL && current->free_f != NULL) {
279 current->free_f(current->content);
281 current->content = data;
282 current->free_f = free_ctn;
287 * \brief Add data to the dict (null-terminated key)
289 * \param dict the dict
290 * \param key the key to set the new data
291 * \param data the data to add in the dict
292 * \param free_ctn function to call with (\a key as argument) when
293 * \a key is removed from the dictionary
295 * set the \a data in the structure under the \a key, which is a
296 * null terminated string.
298 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
299 const char *key, void *data, void_f_pvoid_t free_ctn)
302 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
306 * \brief Retrieve data from the dict (arbitrary key)
308 * \param dict the dealer of data
309 * \param key the key to find data
310 * \param key_len the size of the \a key
311 * \return the data that we are looking for
313 * Search the given \a key. Throws not_found_error when not found.
315 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
319 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
320 xbt_dictelm_t current;
324 current = dict->table[hash_code & dict->table_size];
325 while (current != NULL &&
326 (hash_code != current->hash_code || key_len != current->key_len
327 || memcmp(key, current->key, key_len))) {
328 current = current->next;
332 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
334 return current->content;
338 * \brief like xbt_dict_get_ext(), but returning NULL when not found
340 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
343 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
344 xbt_dictelm_t current;
348 current = dict->table[hash_code & dict->table_size];
349 while (current != NULL &&
350 (hash_code != current->hash_code || key_len != current->key_len
351 || memcmp(key, current->key, key_len))) {
352 current = current->next;
358 return current->content;
362 * @brief retrieve the key associated to that object. Warning, that's a linear search
364 * Returns NULL if the object cannot be found
366 char *xbt_dict_get_key(xbt_dict_t dict, const void*data) {
368 xbt_dictelm_t current;
371 for (i = 0; i <= dict->table_size; i++) {
372 current = dict->table[i];
373 while (current != NULL) {
374 if (current->content == data)
376 current = current->next;
384 * \brief Retrieve data from the dict (null-terminated key)
386 * \param dict the dealer of data
387 * \param key the key to find data
388 * \return the data that we are looking for
390 * Search the given \a key. Throws not_found_error when not found.
391 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
394 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
397 unsigned int hash_code = xbt_dict_hash(key);
398 xbt_dictelm_t current;
402 current = dict->table[hash_code & dict->table_size];
403 while (current != NULL
404 && (hash_code != current->hash_code || strcmp(key, current->key)))
405 current = current->next;
408 THROW1(not_found_error, 0, "key %s not found", key);
410 return current->content;
414 * \brief like xbt_dict_get(), but returning NULL when not found
416 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
418 unsigned int hash_code = xbt_dict_hash(key);
419 xbt_dictelm_t current;
423 current = dict->table[hash_code & dict->table_size];
424 while (current != NULL &&
425 hash_code != current->hash_code && strcmp(key, current->key))
426 current = current->next;
431 return current->content;
436 * \brief Remove data from the dict (arbitrary key)
438 * \param dict the trash can
439 * \param key the key of the data to be removed
440 * \param key_len the size of the \a key
442 * Remove the entry associated with the given \a key (throws not_found)
444 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
448 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
449 xbt_dictelm_t current, previous = NULL;
453 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
454 current = dict->table[hash_code & dict->table_size];
455 while (current != NULL &&
456 (hash_code != current->hash_code || key_len != current->key_len
457 || strncmp(key, current->key, key_len))) {
458 previous = current; /* save the previous node */
459 current = current->next;
463 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
465 if (previous != NULL) {
466 previous->next = current->next;
468 dict->table[hash_code & dict->table_size] = current->next;
471 if (!dict->table[hash_code & dict->table_size])
474 xbt_dictelm_free(current);
481 * \brief Remove data from the dict (null-terminated key)
483 * \param dict the dict
484 * \param key the key of the data to be removed
486 * Remove the entry associated with the given \a key
488 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
490 xbt_dict_remove_ext(dict, key, strlen(key));
494 * \brief Add data to the dict (arbitrary key)
495 * \param dict the container
496 * \param key the key to set the new data
497 * \param key_len the size of the \a key
498 * \param data the data to add in the dict
499 * \param free_ctn function to call with (\a key as argument) when
500 * \a key is removed from the dictionary
502 * Set the \a data in the structure under the \a key, which can be any kind
503 * of data, as long as its length is provided in \a key_len.
505 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
506 uintptr_t key, uintptr_t data) {
508 unsigned int hash_code = xbt_dict_hash_ext((void*)&key, sizeof(uintptr_t));
510 xbt_dictelm_t current, previous = NULL;
513 DEBUG5("ADD %zu->%zu; hash = %d, size = %d, & = %d", key, data, hash_code,
514 dict->table_size, hash_code & dict->table_size);
515 current = dict->table[hash_code & dict->table_size];
516 while (current != NULL &&
517 (hash_code != current->hash_code || sizeof(uintptr_t) != current->key_len
518 || (((uintptr_t)key) != ((uintptr_t)current->key)) )) {
519 current = current->next;
522 if (current == NULL) {
523 /* this key doesn't exist yet */
524 current = xbt_dictielm_new(key, hash_code, data);
526 if (previous == NULL) {
527 dict->table[hash_code & dict->table_size] = current;
529 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
530 xbt_dict_rehash(dict);
532 previous->next = current;
536 /* there is already an element with the same key: overwrite it */
537 if (current->content != NULL && current->free_f != NULL) {
538 current->free_f(current->content);
540 current->content = (void*)data;
541 current->free_f = NULL;
546 * \brief Retrieve data from the dict (key considered as a uintptr_t)
548 * \param dict the dealer of data
549 * \param key the key to find data
550 * \return the data that we are looking for (or 0 if not found)
552 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
554 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key) {
556 unsigned int hash_code = 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 || sizeof(uintptr_t) != current->key_len
564 || (((uintptr_t)key) != ((uintptr_t)current->key)) )) {
565 current = current->next;
571 return (uintptr_t)(current->content);
574 /** Remove a uintptr_t key from the dict */
575 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key) {
577 unsigned int hash_code = xbt_dict_hash_ext(((void*)&key), sizeof(uintptr_t));
578 xbt_dictelm_t current, previous = NULL;
581 current = dict->table[hash_code & dict->table_size];
582 while (current != NULL &&
583 (hash_code != current->hash_code || sizeof(uintptr_t) != current->key_len
584 || (((uintptr_t)key) != ((uintptr_t)current->key)) )) {
585 previous = current; /* save the previous node */
586 current = current->next;
590 THROW1(not_found_error, 0, "key %zu not found", key);
592 if (previous != NULL) {
593 previous->next = current->next;
595 dict->table[hash_code & dict->table_size] = current->next;
598 if (!dict->table[hash_code & dict->table_size])
601 xbt_dictelm_free(current);
607 * \brief Remove all data from the dict
608 * \param dict the dict
610 void xbt_dict_reset(xbt_dict_t dict)
614 xbt_dictelm_t current, previous = NULL;
618 if (dict->count == 0)
621 for (i = 0; i <= dict->table_size; i++) {
622 current = dict->table[i];
623 while (current != NULL) {
625 current = current->next;
626 xbt_dictelm_free(previous);
628 dict->table[i] = NULL;
636 * \brief Return the number of elements in the dict.
637 * \param dict a dictionary
639 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
646 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
647 void xbt_dict_dump_output_string(void *s)
654 * \brief Outputs the content of the structure (debugging purpose)
656 * \param dict the exibitionist
657 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
659 * Outputs the content of the structure. (for debugging purpose). \a output is a
660 * function to output the data. If NULL, data won't be displayed.
663 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
666 xbt_dictelm_t element;
667 printf("Dict %p:\n", dict);
669 for (i = 0; i < dict->table_size; i++) {
670 element = dict->table[i];
673 while (element != NULL) {
674 printf(" %s -> '", element->key);
675 if (output != NULL) {
676 (*output) (element->content);
679 element = element->next;
689 xbt_dynar_t all_sizes = NULL;
690 /** @brief shows some debugging info about the bucklet repartition */
691 void xbt_dict_dump_sizes(xbt_dict_t dict)
697 xbt_dictelm_t element;
698 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
700 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
701 dict->fill, dict->table_size);
703 for (i = 0; i < dict->table_size; i++) {
704 element = dict->table[i];
707 while (element != NULL) {
709 element = element->next;
712 if (xbt_dynar_length(sizes) <= size) {
714 xbt_dynar_set(sizes, size, &prevsize);
717 xbt_dynar_get_cpy(sizes, size, &prevsize);
719 xbt_dynar_set(sizes, size, &prevsize);
723 all_sizes = xbt_dynar_new(sizeof(int), NULL);
725 xbt_dynar_foreach(sizes, count, size) {
726 /* Copy values of this one into all_sizes */
728 if (xbt_dynar_length(all_sizes) <= count) {
730 xbt_dynar_set(all_sizes, count, &prevcount);
732 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
734 xbt_dynar_set(all_sizes, count, &prevcount);
737 /* Report current sizes */
742 printf("%delm x %u cells; ", count, size);
746 xbt_dynar_free(&sizes);
750 * Destroy the dict mallocators.
751 * This is an internal XBT function called by xbt_exit().
753 void xbt_dict_exit(void)
755 if (dict_mallocator != NULL) {
756 xbt_mallocator_free(dict_mallocator);
757 dict_mallocator = NULL;
758 xbt_mallocator_free(dict_elm_mallocator);
759 dict_elm_mallocator = NULL;
766 printf("Overall stats:");
767 xbt_dynar_foreach(all_sizes, count, size) {
772 printf("%delm x %d cells; ", count, size);
776 printf("; %f elm per cell\n", avg / (double) total_count);
780 static void *dict_mallocator_new_f(void)
782 return xbt_new(s_xbt_dict_t, 1);
785 static void dict_mallocator_free_f(void *dict)
790 static void dict_mallocator_reset_f(void *dict)
792 /* nothing to do because all fields are
793 * initialized in xbt_dict_new
800 #include "portable.h"
802 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
803 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
805 XBT_TEST_SUITE("dict", "Dict data container");
807 static void print_str(void *str)
809 printf("%s", (char *) PRINTF_STR(str));
812 static void debuged_add_ext(xbt_dict_t head, const char *key,
813 const char *data_to_fill)
815 char *data = xbt_strdup(data_to_fill);
817 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
819 xbt_dict_set(head, key, data, &free);
820 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
821 xbt_dict_dump(head, (void (*)(void *)) &printf);
826 static void debuged_add(xbt_dict_t head, const char *key)
828 debuged_add_ext(head, key, key);
831 static void fill(xbt_dict_t * head)
833 xbt_test_add0("Fill in the dictionnary");
835 *head = xbt_dict_new();
836 debuged_add(*head, "12");
837 debuged_add(*head, "12a");
838 debuged_add(*head, "12b");
839 debuged_add(*head, "123");
840 debuged_add(*head, "123456");
841 /* Child becomes child of what to add */
842 debuged_add(*head, "1234");
843 /* Need of common ancestor */
844 debuged_add(*head, "123457");
848 static void search_ext(xbt_dict_t head, const char *key, const char *data)
852 xbt_test_add1("Search %s", key);
853 found = xbt_dict_get(head, key);
854 xbt_test_log1("Found %s", (char *) found);
856 xbt_test_assert1(found,
857 "data do not match expectations: found NULL while searching for %s",
860 xbt_test_assert2(!strcmp((char *) data, found),
861 "data do not match expectations: found %s while searching for %s",
862 (char *) found, data);
865 static void search(xbt_dict_t head, const char *key)
867 search_ext(head, key, key);
870 static void debuged_remove(xbt_dict_t head, const char *key)
873 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
874 xbt_dict_remove(head, key);
875 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
879 static void traverse(xbt_dict_t head)
881 xbt_dict_cursor_t cursor = NULL;
886 xbt_dict_foreach(head, cursor, key, data) {
887 if (!key || !data || strcmp(key, data)) {
888 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
891 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
893 xbt_test_assert2(!data || !strcmp(key, data),
894 "Key(%s) != value(%s). Aborting", key, data);
898 static void search_not_found(xbt_dict_t head, const char *data)
903 xbt_test_add1("Search %s (expected not to be found)", data);
906 data = xbt_dict_get(head, data);
907 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
910 if (e.category != not_found_error)
911 xbt_test_exception(e);
915 xbt_test_assert0(ok, "Exception not raised");
918 static void count(xbt_dict_t dict, int length)
920 xbt_dict_cursor_t cursor;
926 xbt_test_add1("Count elements (expecting %d)", length);
927 xbt_test_assert2(xbt_dict_length(dict) == length,
928 "Announced length(%d) != %d.", xbt_dict_length(dict),
931 xbt_dict_foreach(dict, cursor, key, data)
934 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
939 static void count_check_get_key(xbt_dict_t dict, int length)
941 xbt_dict_cursor_t cursor;
947 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
948 xbt_test_assert2(xbt_dict_length(dict) == length,
949 "Announced length(%d) != %d.", xbt_dict_length(dict),
952 xbt_dict_foreach(dict, cursor, key, data) {
954 key2 = xbt_dict_get_key(dict,data);
955 xbt_assert2(!strcmp(key,key2),
956 "The data was registered under %s instead of %s as expected",key2,key);
959 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
965 xbt_dict_t head = NULL;
969 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
971 xbt_test_add0("Traversal the null dictionary");
974 xbt_test_add0("Traversal and search the empty dictionary");
975 head = xbt_dict_new();
978 debuged_remove(head, "12346");
980 if (e.category != not_found_error)
981 xbt_test_exception(e);
984 xbt_dict_free(&head);
986 xbt_test_add0("Traverse the full dictionary");
988 count_check_get_key(head, 7);
990 debuged_add_ext(head, "toto", "tutu");
991 search_ext(head, "toto", "tutu");
992 debuged_remove(head, "toto");
997 xbt_test_add0("Free the dictionary (twice)");
998 xbt_dict_free(&head);
999 xbt_dict_free(&head);
1003 count_check_get_key(head, 7);
1004 xbt_test_add0("Change 123 to 'Changed 123'");
1005 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
1006 count_check_get_key(head, 7);
1008 xbt_test_add0("Change 123 back to '123'");
1009 xbt_dict_set(head, "123", strdup("123"), &free);
1010 count_check_get_key(head, 7);
1012 xbt_test_add0("Change 12a to 'Dummy 12a'");
1013 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
1014 count_check_get_key(head, 7);
1016 xbt_test_add0("Change 12a to '12a'");
1017 xbt_dict_set(head, "12a", strdup("12a"), &free);
1018 count_check_get_key(head, 7);
1020 xbt_test_add0("Traverse the resulting dictionary");
1024 xbt_test_add0("Search 123");
1025 data = xbt_dict_get(head, "123");
1026 xbt_test_assert(data);
1027 xbt_test_assert(!strcmp("123", data));
1029 search_not_found(head, "Can't be found");
1030 search_not_found(head, "123 Can't be found");
1031 search_not_found(head, "12345678 NOT");
1033 search(head, "12a");
1034 search(head, "12b");
1036 search(head, "123456");
1037 search(head, "1234");
1038 search(head, "123457");
1040 xbt_test_add0("Traverse the resulting dictionary");
1043 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1045 xbt_test_add0("Free the dictionary twice");
1046 xbt_dict_free(&head);
1047 xbt_dict_free(&head);
1049 xbt_test_add0("Traverse the resulting dictionary");
1053 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
1057 xbt_test_add0("Remove non existing data");
1059 debuged_remove(head, "Does not exist");
1062 if (e.category != not_found_error)
1063 xbt_test_exception(e);
1068 xbt_dict_free(&head);
1071 ("Remove each data manually (traversing the resulting dictionary each time)");
1073 debuged_remove(head, "12a");
1076 debuged_remove(head, "12b");
1079 debuged_remove(head, "12");
1082 debuged_remove(head, "123456");
1086 debuged_remove(head, "12346");
1089 if (e.category != not_found_error)
1090 xbt_test_exception(e);
1094 debuged_remove(head, "1234");
1096 debuged_remove(head, "123457");
1098 debuged_remove(head, "123");
1101 debuged_remove(head, "12346");
1104 if (e.category != not_found_error)
1105 xbt_test_exception(e);
1110 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
1111 xbt_dict_free(&head);
1113 xbt_dict_reset(head);
1117 xbt_test_add0("Free the dictionary twice");
1118 xbt_dict_free(&head);
1119 xbt_dict_free(&head);
1122 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1126 xbt_test_add0("Store NULL under 'null'");
1127 xbt_dict_set(head, "null", NULL, NULL);
1128 search_ext(head, "null", NULL);
1130 xbt_test_add0("Check whether I see it while traversing...");
1132 xbt_dict_cursor_t cursor = NULL;
1136 xbt_dict_foreach(head, cursor, key, data) {
1137 if (!key || !data || strcmp(key, data)) {
1138 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1140 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1143 if (!strcmp(key, "null"))
1146 xbt_test_assert0(found,
1147 "the key 'null', associated to NULL is not found");
1149 xbt_dict_free(&head);
1152 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
1153 xbt_test_log2("Add %ld under %ld", data, key);
1155 xbt_dicti_set(head, key, data);
1156 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
1157 xbt_dict_dump(head, (void (*)(void *)) &printf);
1160 uintptr_t stored_data = xbt_dicti_get(head, key);
1161 xbt_test_assert3(stored_data==data,
1162 "Retrieved data (%ld) is not what I just stored (%ld) under key %ld",stored_data,data,key);
1165 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
1167 xbt_test_add0("Fill in the dictionnary");
1169 head = xbt_dict_new();
1170 debuged_addi(head, 12, 12);
1171 debuged_addi(head, 13, 13);
1172 debuged_addi(head, 14, 14);
1173 debuged_addi(head, 15, 15);
1175 debuged_addi(head, 12, 15);
1176 debuged_addi(head, 15, 2000);
1177 debuged_addi(head, 15, 3000);
1179 debuged_addi(head, 0, 1000);
1180 debuged_addi(head, 0, 2000);
1181 debuged_addi(head, 0, 3000);
1183 debuged_addi(head, 12, 0);
1184 debuged_addi(head, 13, 0);
1185 debuged_addi(head, 12, 0);
1186 debuged_addi(head, 0, 0);
1188 xbt_dict_free(&head);
1191 #define NB_ELM 20000
1192 #define SIZEOFKEY 1024
1193 static int countelems(xbt_dict_t head)
1195 xbt_dict_cursor_t cursor;
1200 xbt_dict_foreach(head, cursor, key, data) {
1206 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1208 xbt_dict_t head = NULL;
1213 srand((unsigned int) time(NULL));
1215 for (i = 0; i < 10; i++) {
1216 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1217 xbt_test_log0("Fill the struct, count its elems and frees the structure");
1218 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
1220 head = xbt_dict_new();
1221 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1223 for (j = 0; j < 1000; j++) {
1225 key = xbt_malloc(SIZEOFKEY);
1228 for (k = 0; k < SIZEOFKEY - 1; k++)
1229 key[k] = rand() % ('z' - 'a') + 'a';
1231 /* printf("[%d %s]\n",j,key); */
1232 data = xbt_dict_get_or_null(head, key);
1233 } while (data != NULL);
1235 xbt_dict_set(head, key, key, &free);
1236 data = xbt_dict_get(head, key);
1237 xbt_test_assert2(!strcmp(key, data),
1238 "Retrieved value (%s) != Injected value (%s)", key,
1243 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1245 xbt_dict_free(&head);
1246 xbt_dict_free(&head);
1250 head = xbt_dict_new();
1251 xbt_test_add1("Fill %d elements, with keys being the number of element",
1253 for (j = 0; j < NB_ELM; j++) {
1254 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1256 key = xbt_malloc(10);
1258 sprintf(key, "%d", j);
1259 xbt_dict_set(head, key, key, &free);
1261 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1263 xbt_test_add0("Count the elements (retrieving the key and data for each)");
1264 i = countelems(head);
1265 xbt_test_log1("There is %d elements", i);
1267 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1268 key = xbt_malloc(10);
1269 for (i = 0; i < 20; i++) {
1270 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1271 for (j = 0; j < NB_ELM; j++) {
1273 sprintf(key, "%d", j);
1274 data = xbt_dict_get(head, key);
1275 xbt_test_assert2(!strcmp(key, (char *) data),
1276 "with get, key=%s != data=%s", key, (char *) data);
1277 data = xbt_dict_get_ext(head, key, strlen(key));
1278 xbt_test_assert2(!strcmp(key, (char *) data),
1279 "with get_ext, key=%s != data=%s", key, (char *) data);
1284 xbt_test_add1("Remove my %d elements", NB_ELM);
1285 key = xbt_malloc(10);
1286 for (j = 0; j < NB_ELM; j++) {
1287 /* if (!(j%10000)) printf("."); fflush(stdout); */
1289 sprintf(key, "%d", j);
1290 xbt_dict_remove(head, key);
1295 xbt_test_add0("Free the structure (twice)");
1296 xbt_dict_free(&head);
1297 xbt_dict_free(&head);
1300 static void str_free(void *s)
1302 char *c = *(char **) s;
1306 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1310 #define NB_ELM 100 /*00 */
1312 #define KEY_SIZE 512
1313 #define NB_TEST 20 /*20 */
1316 xbt_dict_t mdict = NULL;
1318 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1323 xbt_test_add0("Generic multicache CRASH test");
1324 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1325 "depth of multicache=%d, key size=%d",
1326 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
1328 for (l = 0; l < DEPTH; l++) {
1329 key = xbt_malloc(KEY_SIZE);
1330 xbt_dynar_push(keys, &key);
1333 for (i = 0; i < NB_TEST; i++) {
1334 mdict = xbt_dict_new();
1335 VERB1("mdict=%p", mdict);
1337 printf("Test %d\n", i);
1338 /* else if (i%10) printf("."); else printf("%d",i/10); */
1340 for (j = 0; j < NB_ELM; j++) {
1344 for (l = 0; l < DEPTH; l++) {
1345 key = *(char **) xbt_dynar_get_ptr(keys, l);
1347 for (k = 0; k < KEY_SIZE - 1; k++)
1348 key[k] = rand() % ('z' - 'a') + 'a';
1353 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1356 printf("in multitree %p.\n", mdict);
1358 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1360 data = xbt_multidict_get(mdict, keys);
1362 xbt_test_assert2(data && !strcmp((char *) data, key),
1363 "Retrieved value (%s) does not match the given one (%s)\n",
1364 (char *) data, key);
1366 xbt_dict_free(&mdict);
1369 xbt_dynar_free(&keys);
1372 xbt_dict_dump(mdict,&xbt_dict_print); */
1374 xbt_dict_free(&mdict);
1375 xbt_dynar_free(&keys);
1378 #endif /* SIMGRID_TEST */