1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2011. 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");
25 * \return pointer to the destination
26 * \see xbt_dict_free()
28 * Creates and initialize a new dictionary with a default hashtable size.
29 * The dictionary is heterogeneous: each element can have a different free
32 xbt_dict_t xbt_dict_new(void)
34 xbt_dict_t dict = xbt_dict_new_homogeneous(NULL);
35 dict->homogeneous = 0;
42 * \param free_ctn function to call with (\a data as argument) when
43 * \a data is removed from the dictionary
44 * \return pointer to the destination
45 * \see xbt_dict_new(), xbt_dict_free()
47 * Creates and initialize a new dictionary with a default hashtable size.
48 * The dictionary is homogeneous: each element share the same free function.
50 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
54 dict = xbt_new(s_xbt_dict_t, 1);
55 dict->free_f = free_ctn;
56 dict->table_size = 127;
57 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
60 dict->homogeneous = 1;
67 * \param dict the dictionary to be freed
69 * Frees a dictionary with all the data
71 void xbt_dict_free(xbt_dict_t * dict)
74 xbt_dictelm_t current, previous;
78 // if ( *dict ) xbt_dict_dump_sizes(*dict);
80 if (dict != NULL && *dict != NULL) {
81 table_size = (*dict)->table_size;
82 table = (*dict)->table;
83 /* Warning: the size of the table is 'table_size+1'...
84 * This is because table_size is used as a binary mask in xbt_dict_rehash */
85 for (i = 0; (*dict)->count && i <= table_size; i++) {
87 while (current != NULL) {
89 current = current->next;
90 xbt_dictelm_free(*dict, previous);
101 * Returns the amount of elements in the dict
103 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
105 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
109 * Returns the hash code of a string.
111 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str,
116 #ifdef DJB2_HASH_FUNCTION
117 /* fast implementation of djb2 algorithm */
119 register unsigned int hash = 5381;
123 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
125 # elif defined(FNV_HASH_FUNCTION)
126 register unsigned int hash = 0x811c9dc5;
127 unsigned char *bp = (unsigned char *) str; /* start of buffer */
128 unsigned char *be = bp + str_len; /* beyond end of buffer */
131 /* multiply by the 32 bit FNV magic prime mod 2^32 */
133 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
136 /* xor the bottom with the current octet */
137 hash ^= (unsigned int) *bp++;
141 register unsigned int hash = 0;
144 hash += (*str) * (*str);
152 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
154 #ifdef DJB2_HASH_FUNCTION
155 /* fast implementation of djb2 algorithm */
157 register unsigned int hash = 5381;
159 while ((c = *str++)) {
160 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
163 # elif defined(FNV_HASH_FUNCTION)
164 register unsigned int hash = 0x811c9dc5;
167 /* multiply by the 32 bit FNV magic prime mod 2^32 */
169 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) +
172 /* xor the bottom with the current byte */
173 hash ^= (unsigned int) *str++;
177 register unsigned int hash = 0;
180 hash += (*str) * (*str);
187 /* Expend the size of the dict */
188 static void xbt_dict_rehash(xbt_dict_t dict)
190 const int oldsize = dict->table_size + 1;
191 register int newsize = oldsize * 2;
193 register xbt_dictelm_t *currcell;
194 register xbt_dictelm_t *twincell;
195 register xbt_dictelm_t bucklet;
196 register xbt_dictelm_t *pprev;
199 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
200 newsize * sizeof(xbt_dictelm_t));
201 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
202 dict->table_size = --newsize;
203 dict->table = currcell;
204 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
206 for (i = 0; i < oldsize; i++, currcell++) {
207 if (!*currcell) /* empty cell */
209 twincell = currcell + oldsize;
210 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
211 /* Since we use "& size" instead of "%size" and since the size was doubled,
212 each bucklet of this cell must either :
213 - stay in cell i (ie, currcell)
214 - go to the cell i+oldsize (ie, twincell) */
215 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
216 *pprev = bucklet->next;
217 bucklet->next = *twincell;
223 pprev = &bucklet->next;
228 if (!*currcell) /* everything moved */
234 * \brief Add data to the dict (arbitrary key)
235 * \param dict the container
236 * \param key the key to set the new data
237 * \param key_len the size of the \a key
238 * \param data the data to add in the dict
239 * \param free_ctn function to call with (\a data as argument) when
240 * \a data is removed from the dictionary
242 * Set the \a data in the structure under the \a key, which can be any kind
243 * of data, as long as its length is provided in \a key_len.
245 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
246 const char *key, int key_len,
247 void *data, void_f_pvoid_t free_ctn)
250 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
252 xbt_dictelm_t current, previous = NULL;
255 XBT_DEBUG("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
256 dict->table_size, hash_code & dict->table_size);
257 current = dict->table[hash_code & dict->table_size];
258 while (current != NULL &&
259 (hash_code != current->hash_code || key_len != current->key_len
260 || memcmp(key, current->key, key_len))) {
262 current = current->next;
265 if (current == NULL) {
266 /* this key doesn't exist yet */
267 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
269 if (previous == NULL) {
270 dict->table[hash_code & dict->table_size] = current;
272 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
273 xbt_dict_rehash(dict);
275 previous->next = current;
278 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
279 key_len, (char *) current->content,
280 key_len, (char *) data, key_len, (char *) key);
281 /* there is already an element with the same key: overwrite it */
282 xbt_dictelm_set_data(dict, current, data, 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 data as argument) when
293 * \a data 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,
300 void_f_pvoid_t free_ctn)
303 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
307 * \brief Retrieve data from the dict (arbitrary key)
309 * \param dict the dealer of data
310 * \param key the key to find data
311 * \param key_len the size of the \a key
312 * \return the data that we are looking for
314 * Search the given \a key. Throws not_found_error when not found.
316 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
321 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
322 xbt_dictelm_t current;
326 current = dict->table[hash_code & dict->table_size];
327 while (current != NULL &&
328 (hash_code != current->hash_code || key_len != current->key_len
329 || memcmp(key, current->key, key_len))) {
330 current = current->next;
334 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
336 return current->content;
340 * \brief like xbt_dict_get_ext(), but returning NULL when not found
342 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
346 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
347 xbt_dictelm_t current;
351 current = dict->table[hash_code & dict->table_size];
352 while (current != NULL &&
353 (hash_code != current->hash_code || key_len != current->key_len
354 || memcmp(key, current->key, key_len))) {
355 current = current->next;
361 return current->content;
365 * @brief retrieve the key associated to that object. Warning, that's a linear search
367 * Returns NULL if the object cannot be found
369 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
372 xbt_dictelm_t current;
375 for (i = 0; i <= dict->table_size; i++) {
376 current = dict->table[i];
377 while (current != NULL) {
378 if (current->content == data)
380 current = current->next;
388 * \brief Retrieve data from the dict (null-terminated key)
390 * \param dict the dealer of data
391 * \param key the key to find data
392 * \return the data that we are looking for
394 * Search the given \a key. Throws not_found_error when not found.
395 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
398 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
401 unsigned int hash_code = xbt_dict_hash(key);
402 xbt_dictelm_t current;
406 current = dict->table[hash_code & dict->table_size];
407 while (current != NULL
408 && (hash_code != current->hash_code || strcmp(key, current->key)))
409 current = current->next;
412 THROWF(not_found_error, 0, "key %s not found", key);
414 return current->content;
418 * \brief like xbt_dict_get(), but returning NULL when not found
420 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
422 unsigned int hash_code = xbt_dict_hash(key);
423 xbt_dictelm_t current;
427 current = dict->table[hash_code & dict->table_size];
428 while (current != NULL &&
429 (hash_code != current->hash_code || strcmp(key, current->key)))
430 current = current->next;
435 return current->content;
440 * \brief Remove data from the dict (arbitrary key)
442 * \param dict the trash can
443 * \param key the key of the data to be removed
444 * \param key_len the size of the \a key
446 * Remove the entry associated with the given \a key (throws not_found)
448 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
453 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
454 xbt_dictelm_t current, previous = NULL;
458 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
459 current = dict->table[hash_code & dict->table_size];
460 while (current != NULL &&
461 (hash_code != current->hash_code || key_len != current->key_len
462 || strncmp(key, current->key, key_len))) {
463 previous = current; /* save the previous node */
464 current = current->next;
468 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
470 if (previous != NULL) {
471 previous->next = current->next;
473 dict->table[hash_code & dict->table_size] = current->next;
476 if (!dict->table[hash_code & dict->table_size])
479 xbt_dictelm_free(dict, current);
486 * \brief Remove data from the dict (null-terminated key)
488 * \param dict the dict
489 * \param key the key of the data to be removed
491 * Remove the entry associated with the given \a key
493 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
495 xbt_dict_remove_ext(dict, key, strlen(key));
498 #ifdef XBT_USE_DEPRECATED
500 * \brief Add data to the dict (arbitrary key)
501 * \param dict the container
502 * \param key the key to set the new data
503 * \param data the data to add in the dict
505 * Set the \a data in the structure under the \a key.
506 * Both \a data and \a key are considered as uintptr_t.
508 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
509 uintptr_t key, uintptr_t data)
511 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
515 * \brief Retrieve data from the dict (key considered as a uintptr_t)
517 * \param dict the dealer of data
518 * \param key the key to find data
519 * \return the data that we are looking for (or 0 if not found)
521 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
523 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
525 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
528 /** Remove a uintptr_t key from the dict */
529 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
531 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
536 * \brief Remove all data from the dict
537 * \param dict the dict
539 void xbt_dict_reset(xbt_dict_t dict)
543 xbt_dictelm_t current, previous = NULL;
547 if (dict->count == 0)
550 for (i = 0; i <= dict->table_size; i++) {
551 current = dict->table[i];
552 while (current != NULL) {
554 current = current->next;
555 xbt_dictelm_free(dict, previous);
557 dict->table[i] = NULL;
565 * \brief Return the number of elements in the dict.
566 * \param dict a dictionary
568 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
575 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
576 void xbt_dict_dump_output_string(void *s)
582 * \brief test if the dict is empty or not
584 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
586 return !dict || (xbt_dict_length(dict) == 0);
590 * \brief Outputs the content of the structure (debugging purpose)
592 * \param dict the exibitionist
593 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
595 * Outputs the content of the structure. (for debugging purpose). \a output is a
596 * function to output the data. If NULL, data won't be displayed.
599 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
602 xbt_dictelm_t element;
603 printf("Dict %p:\n", dict);
605 for (i = 0; i < dict->table_size; i++) {
606 element = dict->table[i];
609 while (element != NULL) {
610 printf(" %s -> '", element->key);
611 if (output != NULL) {
612 output(element->content);
615 element = element->next;
625 xbt_dynar_t all_sizes = NULL;
626 /** @brief shows some debugging info about the bucklet repartition */
627 void xbt_dict_dump_sizes(xbt_dict_t dict)
633 xbt_dictelm_t element;
634 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
636 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
637 dict->fill, dict->table_size);
639 for (i = 0; i < dict->table_size; i++) {
640 element = dict->table[i];
643 while (element != NULL) {
645 element = element->next;
648 if (xbt_dynar_length(sizes) <= size) {
650 xbt_dynar_set(sizes, size, &prevsize);
653 xbt_dynar_get_cpy(sizes, size, &prevsize);
655 xbt_dynar_set(sizes, size, &prevsize);
659 all_sizes = xbt_dynar_new(sizeof(int), NULL);
661 xbt_dynar_foreach(sizes, count, size) {
662 /* Copy values of this one into all_sizes */
664 if (xbt_dynar_length(all_sizes) <= count) {
666 xbt_dynar_set(all_sizes, count, &prevcount);
668 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
670 xbt_dynar_set(all_sizes, count, &prevcount);
673 /* Report current sizes */
678 printf("%delm x %u cells; ", count, size);
682 xbt_dynar_free(&sizes);
686 * Create the dict mallocators.
687 * This is an internal XBT function called during the lib initialization.
688 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
690 void xbt_dict_preinit(void)
692 if (dict_elm_mallocator != NULL) {
693 /* Already created. I guess we want to switch to MC mode, so kill the previously created mallocator */
694 xbt_mallocator_free(dict_elm_mallocator);
695 xbt_mallocator_free(dict_het_elm_mallocator);
698 dict_elm_mallocator = xbt_mallocator_new(256,
699 dict_elm_mallocator_new_f,
700 dict_elm_mallocator_free_f,
701 dict_elm_mallocator_reset_f);
702 dict_het_elm_mallocator = xbt_mallocator_new(256,
703 dict_het_elm_mallocator_new_f,
704 dict_het_elm_mallocator_free_f,
705 dict_het_elm_mallocator_reset_f);
709 * Destroy the dict mallocators.
710 * This is an internal XBT function during the lib initialization
712 void xbt_dict_postexit(void)
714 if (dict_elm_mallocator != NULL) {
715 xbt_mallocator_free(dict_elm_mallocator);
716 dict_elm_mallocator = NULL;
717 xbt_mallocator_free(dict_het_elm_mallocator);
718 dict_het_elm_mallocator = NULL;
725 printf("Overall stats:");
726 xbt_dynar_foreach(all_sizes, count, size) {
731 printf("%delm x %d cells; ", count, size);
735 printf("; %f elm per cell\n", avg / (double) total_count);
742 #include "portable.h"
744 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
745 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
747 XBT_TEST_SUITE("dict", "Dict data container");
749 static void print_str(void *str)
751 printf("%s", (char *) PRINTF_STR(str));
754 static void debuged_add_ext(xbt_dict_t head, const char *key,
755 const char *data_to_fill, void_f_pvoid_t free_f)
757 char *data = xbt_strdup(data_to_fill);
759 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
762 xbt_dict_set(head, key, data, free_f);
763 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
764 xbt_dict_dump(head, (void (*)(void *)) &printf);
769 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
771 debuged_add_ext(head, key, key, free_f);
774 static void fill(xbt_dict_t * head, int homogeneous)
776 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
778 xbt_test_add("Fill in the dictionnary");
780 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
781 debuged_add(*head, "12", free_f);
782 debuged_add(*head, "12a", free_f);
783 debuged_add(*head, "12b", free_f);
784 debuged_add(*head, "123", free_f);
785 debuged_add(*head, "123456", free_f);
786 /* Child becomes child of what to add */
787 debuged_add(*head, "1234", free_f);
788 /* Need of common ancestor */
789 debuged_add(*head, "123457", free_f);
793 static void search_ext(xbt_dict_t head, const char *key, const char *data)
797 xbt_test_add("Search %s", key);
798 found = xbt_dict_get(head, key);
799 xbt_test_log("Found %s", (char *) found);
801 xbt_test_assert(found,
802 "data do not match expectations: found NULL while searching for %s",
805 xbt_test_assert(!strcmp((char *) data, found),
806 "data do not match expectations: found %s while searching for %s",
807 (char *) found, data);
810 static void search(xbt_dict_t head, const char *key)
812 search_ext(head, key, key);
815 static void debuged_remove(xbt_dict_t head, const char *key)
818 xbt_test_add("Remove '%s'", PRINTF_STR(key));
819 xbt_dict_remove(head, key);
820 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
824 static void traverse(xbt_dict_t head)
826 xbt_dict_cursor_t cursor = NULL;
831 xbt_dict_foreach(head, cursor, key, data) {
832 if (!key || !data || strcmp(key, data)) {
833 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
836 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
838 xbt_test_assert(!data || !strcmp(key, data),
839 "Key(%s) != value(%s). Aborting", key, data);
843 static void search_not_found(xbt_dict_t head, const char *data)
848 xbt_test_add("Search %s (expected not to be found)", data);
851 data = xbt_dict_get(head, data);
852 THROWF(unknown_error, 0,
853 "Found something which shouldn't be there (%s)", data);
856 if (e.category != not_found_error)
857 xbt_test_exception(e);
861 xbt_test_assert(ok, "Exception not raised");
864 static void count(xbt_dict_t dict, int length)
866 xbt_dict_cursor_t cursor;
872 xbt_test_add("Count elements (expecting %d)", length);
873 xbt_test_assert(xbt_dict_length(dict) == length,
874 "Announced length(%d) != %d.", xbt_dict_length(dict),
877 xbt_dict_foreach(dict, cursor, key, data)
880 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
885 static void count_check_get_key(xbt_dict_t dict, int length)
887 xbt_dict_cursor_t cursor;
889 _XBT_GNUC_UNUSED char *key2;
895 ("Count elements (expecting %d), and test the getkey function",
897 xbt_test_assert(xbt_dict_length(dict) == length,
898 "Announced length(%d) != %d.", xbt_dict_length(dict),
901 xbt_dict_foreach(dict, cursor, key, data) {
903 key2 = xbt_dict_get_key(dict, data);
904 xbt_assert(!strcmp(key, key2),
905 "The data was registered under %s instead of %s as expected",
909 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
915 xbt_dict_t head = NULL;
918 static void basic_test(int homogeneous)
920 void_f_pvoid_t free_f;
922 xbt_test_add("Traversal the null dictionary");
925 xbt_test_add("Traversal and search the empty dictionary");
926 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
929 debuged_remove(head, "12346");
932 if (e.category != not_found_error)
933 xbt_test_exception(e);
936 xbt_dict_free(&head);
938 free_f = homogeneous ? NULL : &free;
940 xbt_test_add("Traverse the full dictionary");
941 fill(&head, homogeneous);
942 count_check_get_key(head, 7);
944 debuged_add_ext(head, "toto", "tutu", free_f);
945 search_ext(head, "toto", "tutu");
946 debuged_remove(head, "toto");
951 xbt_test_add("Free the dictionary (twice)");
952 xbt_dict_free(&head);
953 xbt_dict_free(&head);
956 fill(&head, homogeneous);
957 count_check_get_key(head, 7);
958 xbt_test_add("Change 123 to 'Changed 123'");
959 xbt_dict_set(head, "123", strdup("Changed 123"), free_f);
960 count_check_get_key(head, 7);
962 xbt_test_add("Change 123 back to '123'");
963 xbt_dict_set(head, "123", strdup("123"), free_f);
964 count_check_get_key(head, 7);
966 xbt_test_add("Change 12a to 'Dummy 12a'");
967 xbt_dict_set(head, "12a", strdup("Dummy 12a"), free_f);
968 count_check_get_key(head, 7);
970 xbt_test_add("Change 12a to '12a'");
971 xbt_dict_set(head, "12a", strdup("12a"), free_f);
972 count_check_get_key(head, 7);
974 xbt_test_add("Traverse the resulting dictionary");
978 xbt_test_add("Search 123");
979 data = xbt_dict_get(head, "123");
980 xbt_test_assert(data);
981 xbt_test_assert(!strcmp("123", data));
983 search_not_found(head, "Can't be found");
984 search_not_found(head, "123 Can't be found");
985 search_not_found(head, "12345678 NOT");
990 search(head, "123456");
991 search(head, "1234");
992 search(head, "123457");
994 xbt_test_add("Traverse the resulting dictionary");
997 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
999 xbt_test_add("Free the dictionary twice");
1000 xbt_dict_free(&head);
1001 xbt_dict_free(&head);
1003 xbt_test_add("Traverse the resulting dictionary");
1007 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
1012 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
1017 static void remove_test(int homogeneous)
1019 fill(&head, homogeneous);
1021 xbt_test_add("Remove non existing data");
1023 debuged_remove(head, "Does not exist");
1026 if (e.category != not_found_error)
1027 xbt_test_exception(e);
1032 xbt_dict_free(&head);
1035 ("Remove each data manually (traversing the resulting dictionary each time)");
1036 fill(&head, homogeneous);
1037 debuged_remove(head, "12a");
1040 debuged_remove(head, "12b");
1043 debuged_remove(head, "12");
1046 debuged_remove(head, "123456");
1050 debuged_remove(head, "12346");
1053 if (e.category != not_found_error)
1054 xbt_test_exception(e);
1058 debuged_remove(head, "1234");
1060 debuged_remove(head, "123457");
1062 debuged_remove(head, "123");
1065 debuged_remove(head, "12346");
1068 if (e.category != not_found_error)
1069 xbt_test_exception(e);
1075 ("Free dict, create new fresh one, and then reset the dict");
1076 xbt_dict_free(&head);
1077 fill(&head, homogeneous);
1078 xbt_dict_reset(head);
1082 xbt_test_add("Free the dictionary twice");
1083 xbt_dict_free(&head);
1084 xbt_dict_free(&head);
1087 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1092 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1097 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1101 xbt_test_add("Store NULL under 'null'");
1102 xbt_dict_set(head, "null", NULL, NULL);
1103 search_ext(head, "null", NULL);
1105 xbt_test_add("Check whether I see it while traversing...");
1107 xbt_dict_cursor_t cursor = NULL;
1111 xbt_dict_foreach(head, cursor, key, data) {
1112 if (!key || !data || strcmp(key, data)) {
1113 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1115 xbt_test_log("Seen: %s", PRINTF_STR(key));
1118 if (!strcmp(key, "null"))
1121 xbt_test_assert(found,
1122 "the key 'null', associated to NULL is not found");
1124 xbt_dict_free(&head);
1127 #define NB_ELM 20000
1128 #define SIZEOFKEY 1024
1129 static int countelems(xbt_dict_t head)
1131 xbt_dict_cursor_t cursor;
1136 xbt_dict_foreach(head, cursor, key, data) {
1142 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1144 xbt_dict_t head = NULL;
1149 srand((unsigned int) time(NULL));
1151 for (i = 0; i < 10; i++) {
1152 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1154 ("Fill the struct, count its elems and frees the structure");
1156 ("using 1000 elements with %d chars long randomized keys.",
1158 head = xbt_dict_new();
1159 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1160 for (j = 0; j < 1000; j++) {
1162 key = xbt_malloc(SIZEOFKEY);
1165 for (k = 0; k < SIZEOFKEY - 1; k++)
1166 key[k] = rand() % ('z' - 'a') + 'a';
1168 /* printf("[%d %s]\n",j,key); */
1169 data = xbt_dict_get_or_null(head, key);
1170 } while (data != NULL);
1172 xbt_dict_set(head, key, key, &free);
1173 data = xbt_dict_get(head, key);
1174 xbt_test_assert(!strcmp(key, data),
1175 "Retrieved value (%s) != Injected value (%s)", key,
1180 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1182 xbt_dict_free(&head);
1183 xbt_dict_free(&head);
1187 head = xbt_dict_new();
1188 xbt_test_add("Fill %d elements, with keys being the number of element",
1190 for (j = 0; j < NB_ELM; j++) {
1191 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1193 key = xbt_malloc(10);
1195 sprintf(key, "%d", j);
1196 xbt_dict_set(head, key, key, &free);
1198 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1201 ("Count the elements (retrieving the key and data for each)");
1202 i = countelems(head);
1203 xbt_test_log("There is %d elements", i);
1205 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1206 key = xbt_malloc(10);
1207 for (i = 0; i < 20; i++) {
1208 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1209 for (j = 0; j < NB_ELM; j++) {
1211 sprintf(key, "%d", j);
1212 data = xbt_dict_get(head, key);
1213 xbt_test_assert(!strcmp(key, (char *) data),
1214 "with get, key=%s != data=%s", key, (char *) data);
1215 data = xbt_dict_get_ext(head, key, strlen(key));
1216 xbt_test_assert(!strcmp(key, (char *) data),
1217 "with get_ext, key=%s != data=%s", key,
1223 xbt_test_add("Remove my %d elements", NB_ELM);
1224 key = xbt_malloc(10);
1225 for (j = 0; j < NB_ELM; j++) {
1226 /* if (!(j%10000)) printf("."); fflush(stdout); */
1228 sprintf(key, "%d", j);
1229 xbt_dict_remove(head, key);
1234 xbt_test_add("Free the structure (twice)");
1235 xbt_dict_free(&head);
1236 xbt_dict_free(&head);
1239 static void str_free(void *s)
1241 char *c = *(char **) s;
1245 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1249 #define NB_ELM 100 /*00 */
1251 #define KEY_SIZE 512
1252 #define NB_TEST 20 /*20 */
1255 xbt_dict_t mdict = NULL;
1257 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1262 xbt_test_add("Generic multicache CRASH test");
1264 (" Fill the struct and frees it %d times, using %d elements, "
1265 "depth of multicache=%d, key size=%d", NB_TEST, NB_ELM, DEPTH,
1268 for (l = 0; l < DEPTH; l++) {
1269 key = xbt_malloc(KEY_SIZE);
1270 xbt_dynar_push(keys, &key);
1273 for (i = 0; i < NB_TEST; i++) {
1274 mdict = xbt_dict_new();
1275 XBT_VERB("mdict=%p", mdict);
1277 printf("Test %d\n", i);
1278 /* else if (i%10) printf("."); else printf("%d",i/10); */
1280 for (j = 0; j < NB_ELM; j++) {
1284 for (l = 0; l < DEPTH; l++) {
1285 key = *(char **) xbt_dynar_get_ptr(keys, l);
1287 for (k = 0; k < KEY_SIZE - 1; k++)
1288 key[k] = rand() % ('z' - 'a') + 'a';
1293 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1296 printf("in multitree %p.\n", mdict);
1298 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1300 data = xbt_multidict_get(mdict, keys);
1302 xbt_test_assert(data && !strcmp((char *) data, key),
1303 "Retrieved value (%s) does not match the given one (%s)\n",
1304 (char *) data, key);
1306 xbt_dict_free(&mdict);
1309 xbt_dynar_free(&keys);
1312 xbt_dict_dump(mdict,&xbt_dict_print); */
1314 xbt_dict_free(&mdict);
1315 xbt_dynar_free(&keys);
1318 #endif /* SIMGRID_TEST */