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. */
13 #include "xbt/mallocator.h"
14 #include "xbt_modinter.h"
16 #include "dict_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
19 "Dictionaries provide the same functionalities than hash tables");
23 * \return pointer to the destination
24 * \see xbt_dict_free()
26 * Creates and initialize a new dictionary with a default hashtable size.
27 * The dictionary is heterogeneous: each element can have a different free
30 xbt_dict_t xbt_dict_new(void)
32 xbt_dict_t dict = xbt_dict_new_homogeneous(NULL);
33 dict->homogeneous = 0;
40 * \param free_ctn function to call with (\a data as argument) when
41 * \a data is removed from the dictionary
42 * \return pointer to the destination
43 * \see xbt_dict_new(), xbt_dict_free()
45 * Creates and initialize a new dictionary with a default hashtable size.
46 * The dictionary is homogeneous: each element share the same free function.
48 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
52 dict = xbt_new(s_xbt_dict_t, 1);
53 dict->free_f = free_ctn;
54 dict->table_size = 127;
55 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
58 dict->homogeneous = 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(*dict, previous);
99 * Returns the amount of elements in the dict
101 XBT_INLINE unsigned int xbt_dict_size(xbt_dict_t dict)
103 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
106 /* Expend the size of the dict */
107 static void xbt_dict_rehash(xbt_dict_t dict)
109 const int oldsize = dict->table_size + 1;
110 register int newsize = oldsize * 2;
112 register xbt_dictelm_t *currcell;
113 register xbt_dictelm_t *twincell;
114 register xbt_dictelm_t bucklet;
115 register xbt_dictelm_t *pprev;
118 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
119 newsize * sizeof(xbt_dictelm_t));
120 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
121 dict->table_size = --newsize;
122 dict->table = currcell;
123 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
125 for (i = 0; i < oldsize; i++, currcell++) {
126 if (!*currcell) /* empty cell */
128 twincell = currcell + oldsize;
129 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
130 /* Since we use "& size" instead of "%size" and since the size was doubled,
131 each bucklet of this cell must either :
132 - stay in cell i (ie, currcell)
133 - go to the cell i+oldsize (ie, twincell) */
134 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
135 *pprev = bucklet->next;
136 bucklet->next = *twincell;
142 pprev = &bucklet->next;
147 if (!*currcell) /* everything moved */
153 * \brief Add data to the dict (arbitrary key)
154 * \param dict the container
155 * \param key the key to set the new data
156 * \param key_len the size of the \a key
157 * \param data the data to add in the dict
158 * \param free_ctn function to call with (\a data as argument) when
159 * \a data is removed from the dictionary
161 * Set the \a data in the structure under the \a key, which can be any kind
162 * of data, as long as its length is provided in \a key_len.
164 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
165 const char *key, int key_len,
166 void *data, void_f_pvoid_t free_ctn)
169 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
171 xbt_dictelm_t current, previous = NULL;
174 XBT_DEBUG("ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
175 dict->table_size, hash_code & dict->table_size);
176 current = dict->table[hash_code & dict->table_size];
177 while (current != NULL &&
178 (hash_code != current->hash_code || key_len != current->key_len
179 || memcmp(key, current->key, key_len))) {
181 current = current->next;
184 if (current == NULL) {
185 /* this key doesn't exist yet */
186 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
188 if (previous == NULL) {
189 dict->table[hash_code & dict->table_size] = current;
191 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
192 xbt_dict_rehash(dict);
194 previous->next = current;
197 XBT_DEBUG("Replace %.*s by %.*s under key %.*s",
198 key_len, (char *) current->content,
199 key_len, (char *) data, key_len, (char *) key);
200 /* there is already an element with the same key: overwrite it */
201 xbt_dictelm_set_data(dict, current, data, free_ctn);
206 * \brief Add data to the dict (null-terminated key)
208 * \param dict the dict
209 * \param key the key to set the new data
210 * \param data the data to add in the dict
211 * \param free_ctn function to call with (\a data as argument) when
212 * \a data is removed from the dictionary
214 * set the \a data in the structure under the \a key, which is a
215 * null terminated string.
217 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
218 const char *key, void *data,
219 void_f_pvoid_t free_ctn)
222 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
226 * \brief Retrieve data from the dict (arbitrary key)
228 * \param dict the dealer of data
229 * \param key the key to find data
230 * \param key_len the size of the \a key
231 * \return the data that we are looking for
233 * Search the given \a key. Throws not_found_error when not found.
235 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
240 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
241 xbt_dictelm_t current;
245 current = dict->table[hash_code & dict->table_size];
246 while (current != NULL &&
247 (hash_code != current->hash_code || key_len != current->key_len
248 || memcmp(key, current->key, key_len))) {
249 current = current->next;
253 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
255 return current->content;
259 * \brief like xbt_dict_get_ext(), but returning NULL when not found
261 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
265 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
266 xbt_dictelm_t current;
270 current = dict->table[hash_code & dict->table_size];
271 while (current != NULL &&
272 (hash_code != current->hash_code || key_len != current->key_len
273 || memcmp(key, current->key, key_len))) {
274 current = current->next;
280 return current->content;
284 * @brief retrieve the key associated to that object. Warning, that's a linear search
286 * Returns NULL if the object cannot be found
288 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
291 xbt_dictelm_t current;
294 for (i = 0; i <= dict->table_size; i++) {
295 current = dict->table[i];
296 while (current != NULL) {
297 if (current->content == data)
299 current = current->next;
307 * \brief Retrieve data from the dict (null-terminated key)
309 * \param dict the dealer of data
310 * \param key the key to find data
311 * \return the data that we are looking for
313 * Search the given \a key. Throws not_found_error when not found.
314 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
317 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
319 return xbt_dict_get_elm(dict, key)->content;
322 * \brief Retrieve element from the dict (null-terminated key)
324 * \param dict the dealer of data
325 * \param key the key to find data
326 * \return the s_xbt_dictelm_t that we are looking for
328 * Search the given \a key. Throws not_found_error when not found.
329 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
332 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
334 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
337 THROWF(not_found_error, 0, "key %s not found", key);
343 * \brief like xbt_dict_get(), but returning NULL when not found
345 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
347 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
352 return current->content;
355 * \brief like xbt_dict_get_elm(), but returning NULL when not found
357 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
359 unsigned int hash_code = xbt_str_hash(key);
360 xbt_dictelm_t current;
364 current = dict->table[hash_code & dict->table_size];
365 while (current != NULL &&
366 (hash_code != current->hash_code || strcmp(key, current->key)))
367 current = current->next;
373 * \brief Remove data from the dict (arbitrary key)
375 * \param dict the trash can
376 * \param key the key of the data to be removed
377 * \param key_len the size of the \a key
379 * Remove the entry associated with the given \a key (throws not_found)
381 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
386 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
387 xbt_dictelm_t current, previous = NULL;
391 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
392 current = dict->table[hash_code & dict->table_size];
393 while (current != NULL &&
394 (hash_code != current->hash_code || key_len != current->key_len
395 || strncmp(key, current->key, key_len))) {
396 previous = current; /* save the previous node */
397 current = current->next;
401 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
403 if (previous != NULL) {
404 previous->next = current->next;
406 dict->table[hash_code & dict->table_size] = current->next;
409 if (!dict->table[hash_code & dict->table_size])
412 xbt_dictelm_free(dict, current);
419 * \brief Remove data from the dict (null-terminated key)
421 * \param dict the dict
422 * \param key the key of the data to be removed
424 * Remove the entry associated with the given \a key
426 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
428 xbt_dict_remove_ext(dict, key, strlen(key));
431 #ifdef XBT_USE_DEPRECATED
433 * \brief Add data to the dict (arbitrary key)
434 * \param dict the container
435 * \param key the key to set the new data
436 * \param data the data to add in the dict
438 * Set the \a data in the structure under the \a key.
439 * Both \a data and \a key are considered as uintptr_t.
441 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
442 uintptr_t key, uintptr_t data)
444 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
448 * \brief Retrieve data from the dict (key considered as a uintptr_t)
450 * \param dict the dealer of data
451 * \param key the key to find data
452 * \return the data that we are looking for (or 0 if not found)
454 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
456 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
458 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
461 /** Remove a uintptr_t key from the dict */
462 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
464 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
469 * \brief Remove all data from the dict
470 * \param dict the dict
472 void xbt_dict_reset(xbt_dict_t dict)
476 xbt_dictelm_t current, previous = NULL;
480 if (dict->count == 0)
483 for (i = 0; i <= dict->table_size; i++) {
484 current = dict->table[i];
485 while (current != NULL) {
487 current = current->next;
488 xbt_dictelm_free(dict, previous);
490 dict->table[i] = NULL;
498 * \brief Return the number of elements in the dict.
499 * \param dict a dictionary
501 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
508 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
509 void xbt_dict_dump_output_string(void *s)
515 * \brief test if the dict is empty or not
517 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
519 return !dict || (xbt_dict_length(dict) == 0);
523 * \brief Outputs the content of the structure (debugging purpose)
525 * \param dict the exibitionist
526 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
528 * Outputs the content of the structure. (for debugging purpose). \a output is a
529 * function to output the data. If NULL, data won't be displayed.
532 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
535 xbt_dictelm_t element;
536 printf("Dict %p:\n", dict);
538 for (i = 0; i < dict->table_size; i++) {
539 element = dict->table[i];
542 while (element != NULL) {
543 printf(" %s -> '", element->key);
544 if (output != NULL) {
545 output(element->content);
548 element = element->next;
558 xbt_dynar_t all_sizes = NULL;
559 /** @brief shows some debugging info about the bucklet repartition */
560 void xbt_dict_dump_sizes(xbt_dict_t dict)
566 xbt_dictelm_t element;
567 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
569 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
570 dict->fill, dict->table_size);
572 for (i = 0; i < dict->table_size; i++) {
573 element = dict->table[i];
576 while (element != NULL) {
578 element = element->next;
581 if (xbt_dynar_length(sizes) <= size) {
583 xbt_dynar_set(sizes, size, &prevsize);
586 xbt_dynar_get_cpy(sizes, size, &prevsize);
588 xbt_dynar_set(sizes, size, &prevsize);
592 all_sizes = xbt_dynar_new(sizeof(int), NULL);
594 xbt_dynar_foreach(sizes, count, size) {
595 /* Copy values of this one into all_sizes */
597 if (xbt_dynar_length(all_sizes) <= count) {
599 xbt_dynar_set(all_sizes, count, &prevcount);
601 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
603 xbt_dynar_set(all_sizes, count, &prevcount);
606 /* Report current sizes */
611 printf("%uelm x %u cells; ", count, size);
615 xbt_dynar_free(&sizes);
619 * Create the dict mallocators.
620 * This is an internal XBT function called during the lib initialization.
621 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
623 void xbt_dict_preinit(void)
625 dict_elm_mallocator = xbt_mallocator_new(256,
626 dict_elm_mallocator_new_f,
627 dict_elm_mallocator_free_f,
628 dict_elm_mallocator_reset_f);
629 dict_het_elm_mallocator = xbt_mallocator_new(256,
630 dict_het_elm_mallocator_new_f,
631 dict_het_elm_mallocator_free_f,
632 dict_het_elm_mallocator_reset_f);
636 * Destroy the dict mallocators.
637 * This is an internal XBT function during the lib initialization
639 void xbt_dict_postexit(void)
641 if (dict_elm_mallocator != NULL) {
642 xbt_mallocator_free(dict_elm_mallocator);
643 dict_elm_mallocator = NULL;
644 xbt_mallocator_free(dict_het_elm_mallocator);
645 dict_het_elm_mallocator = NULL;
652 printf("Overall stats:");
653 xbt_dynar_foreach(all_sizes, count, size) {
658 printf("%uelm x %d cells; ", count, size);
662 printf("; %f elm per cell\n", avg / (double) total_count);
669 #include "portable.h"
671 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
673 XBT_TEST_SUITE("dict", "Dict data container");
675 static void debuged_add_ext(xbt_dict_t head, const char *key,
676 const char *data_to_fill, void_f_pvoid_t free_f)
678 char *data = xbt_strdup(data_to_fill);
680 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
683 xbt_dict_set(head, key, data, free_f);
684 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
685 xbt_dict_dump(head, (void (*)(void *)) &printf);
690 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
692 debuged_add_ext(head, key, key, free_f);
695 static void fill(xbt_dict_t * head, int homogeneous)
697 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
699 xbt_test_add("Fill in the dictionnary");
701 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
702 debuged_add(*head, "12", free_f);
703 debuged_add(*head, "12a", free_f);
704 debuged_add(*head, "12b", free_f);
705 debuged_add(*head, "123", free_f);
706 debuged_add(*head, "123456", free_f);
707 /* Child becomes child of what to add */
708 debuged_add(*head, "1234", free_f);
709 /* Need of common ancestor */
710 debuged_add(*head, "123457", free_f);
714 static void search_ext(xbt_dict_t head, const char *key, const char *data)
718 xbt_test_add("Search %s", key);
719 found = xbt_dict_get(head, key);
720 xbt_test_log("Found %s", (char *) found);
722 xbt_test_assert(found,
723 "data do not match expectations: found NULL while searching for %s",
726 xbt_test_assert(!strcmp((char *) data, found),
727 "data do not match expectations: found %s while searching for %s",
728 (char *) found, data);
731 static void search(xbt_dict_t head, const char *key)
733 search_ext(head, key, key);
736 static void debuged_remove(xbt_dict_t head, const char *key)
739 xbt_test_add("Remove '%s'", PRINTF_STR(key));
740 xbt_dict_remove(head, key);
741 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
745 static void traverse(xbt_dict_t head)
747 xbt_dict_cursor_t cursor = NULL;
752 xbt_dict_foreach(head, cursor, key, data) {
753 if (!key || !data || strcmp(key, data)) {
754 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
757 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
759 xbt_test_assert(!data || !strcmp(key, data),
760 "Key(%s) != value(%s). Aborting", key, data);
764 static void search_not_found(xbt_dict_t head, const char *data)
769 xbt_test_add("Search %s (expected not to be found)", data);
772 data = xbt_dict_get(head, data);
773 THROWF(unknown_error, 0,
774 "Found something which shouldn't be there (%s)", data);
777 if (e.category != not_found_error)
778 xbt_test_exception(e);
782 xbt_test_assert(ok, "Exception not raised");
785 static void count(xbt_dict_t dict, int length)
787 xbt_dict_cursor_t cursor;
793 xbt_test_add("Count elements (expecting %d)", length);
794 xbt_test_assert(xbt_dict_length(dict) == length,
795 "Announced length(%d) != %d.", xbt_dict_length(dict),
798 xbt_dict_foreach(dict, cursor, key, data)
801 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
806 static void count_check_get_key(xbt_dict_t dict, int length)
808 xbt_dict_cursor_t cursor;
810 _XBT_GNUC_UNUSED char *key2;
816 ("Count elements (expecting %d), and test the getkey function",
818 xbt_test_assert(xbt_dict_length(dict) == length,
819 "Announced length(%d) != %d.", xbt_dict_length(dict),
822 xbt_dict_foreach(dict, cursor, key, data) {
824 key2 = xbt_dict_get_key(dict, data);
825 xbt_assert(!strcmp(key, key2),
826 "The data was registered under %s instead of %s as expected",
830 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
836 xbt_dict_t head = NULL;
839 static void basic_test(int homogeneous)
841 void_f_pvoid_t free_f;
843 xbt_test_add("Traversal the null dictionary");
846 xbt_test_add("Traversal and search the empty dictionary");
847 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
850 debuged_remove(head, "12346");
853 if (e.category != not_found_error)
854 xbt_test_exception(e);
857 xbt_dict_free(&head);
859 free_f = homogeneous ? NULL : &free;
861 xbt_test_add("Traverse the full dictionary");
862 fill(&head, homogeneous);
863 count_check_get_key(head, 7);
865 debuged_add_ext(head, "toto", "tutu", free_f);
866 search_ext(head, "toto", "tutu");
867 debuged_remove(head, "toto");
872 xbt_test_add("Free the dictionary (twice)");
873 xbt_dict_free(&head);
874 xbt_dict_free(&head);
877 fill(&head, homogeneous);
878 count_check_get_key(head, 7);
879 xbt_test_add("Change 123 to 'Changed 123'");
880 xbt_dict_set(head, "123", strdup("Changed 123"), free_f);
881 count_check_get_key(head, 7);
883 xbt_test_add("Change 123 back to '123'");
884 xbt_dict_set(head, "123", strdup("123"), free_f);
885 count_check_get_key(head, 7);
887 xbt_test_add("Change 12a to 'Dummy 12a'");
888 xbt_dict_set(head, "12a", strdup("Dummy 12a"), free_f);
889 count_check_get_key(head, 7);
891 xbt_test_add("Change 12a to '12a'");
892 xbt_dict_set(head, "12a", strdup("12a"), free_f);
893 count_check_get_key(head, 7);
895 xbt_test_add("Traverse the resulting dictionary");
899 xbt_test_add("Search 123");
900 data = xbt_dict_get(head, "123");
901 xbt_test_assert(data);
902 xbt_test_assert(!strcmp("123", data));
904 search_not_found(head, "Can't be found");
905 search_not_found(head, "123 Can't be found");
906 search_not_found(head, "12345678 NOT");
911 search(head, "123456");
912 search(head, "1234");
913 search(head, "123457");
915 xbt_test_add("Traverse the resulting dictionary");
918 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
920 xbt_test_add("Free the dictionary twice");
921 xbt_dict_free(&head);
922 xbt_dict_free(&head);
924 xbt_test_add("Traverse the resulting dictionary");
928 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
933 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
938 static void remove_test(int homogeneous)
940 fill(&head, homogeneous);
942 xbt_test_add("Remove non existing data");
944 debuged_remove(head, "Does not exist");
947 if (e.category != not_found_error)
948 xbt_test_exception(e);
953 xbt_dict_free(&head);
956 ("Remove each data manually (traversing the resulting dictionary each time)");
957 fill(&head, homogeneous);
958 debuged_remove(head, "12a");
961 debuged_remove(head, "12b");
964 debuged_remove(head, "12");
967 debuged_remove(head, "123456");
971 debuged_remove(head, "12346");
974 if (e.category != not_found_error)
975 xbt_test_exception(e);
979 debuged_remove(head, "1234");
981 debuged_remove(head, "123457");
983 debuged_remove(head, "123");
986 debuged_remove(head, "12346");
989 if (e.category != not_found_error)
990 xbt_test_exception(e);
996 ("Free dict, create new fresh one, and then reset the dict");
997 xbt_dict_free(&head);
998 fill(&head, homogeneous);
999 xbt_dict_reset(head);
1003 xbt_test_add("Free the dictionary twice");
1004 xbt_dict_free(&head);
1005 xbt_dict_free(&head);
1008 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1013 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1018 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1022 xbt_test_add("Store NULL under 'null'");
1023 xbt_dict_set(head, "null", NULL, NULL);
1024 search_ext(head, "null", NULL);
1026 xbt_test_add("Check whether I see it while traversing...");
1028 xbt_dict_cursor_t cursor = NULL;
1032 xbt_dict_foreach(head, cursor, key, data) {
1033 if (!key || !data || strcmp(key, data)) {
1034 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1036 xbt_test_log("Seen: %s", PRINTF_STR(key));
1039 if (!strcmp(key, "null"))
1042 xbt_test_assert(found,
1043 "the key 'null', associated to NULL is not found");
1045 xbt_dict_free(&head);
1048 #define NB_ELM 20000
1049 #define SIZEOFKEY 1024
1050 static int countelems(xbt_dict_t head)
1052 xbt_dict_cursor_t cursor;
1057 xbt_dict_foreach(head, cursor, key, data) {
1063 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1065 xbt_dict_t head = NULL;
1069 srand((unsigned int) time(NULL));
1071 for (i = 0; i < 10; i++) {
1072 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1074 ("Fill the struct, count its elems and frees the structure");
1076 ("using 1000 elements with %d chars long randomized keys.",
1078 head = xbt_dict_new();
1079 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1080 for (j = 0; j < 1000; j++) {
1082 key = xbt_malloc(SIZEOFKEY);
1085 for (k = 0; k < SIZEOFKEY - 1; k++)
1086 key[k] = rand() % ('z' - 'a') + 'a';
1088 /* printf("[%d %s]\n",j,key); */
1089 data = xbt_dict_get_or_null(head, key);
1090 } while (data != NULL);
1092 xbt_dict_set(head, key, key, &free);
1093 data = xbt_dict_get(head, key);
1094 xbt_test_assert(!strcmp(key, data),
1095 "Retrieved value (%s) != Injected value (%s)", key,
1100 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1102 xbt_dict_free(&head);
1103 xbt_dict_free(&head);
1107 head = xbt_dict_new();
1108 xbt_test_add("Fill %d elements, with keys being the number of element",
1110 for (j = 0; j < NB_ELM; j++) {
1111 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1113 key = xbt_malloc(10);
1115 sprintf(key, "%d", j);
1116 xbt_dict_set(head, key, key, &free);
1118 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1121 ("Count the elements (retrieving the key and data for each)");
1122 i = countelems(head);
1123 xbt_test_log("There is %d elements", i);
1125 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1126 key = xbt_malloc(10);
1127 for (i = 0; i < 20; i++) {
1129 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1130 for (j = 0; j < NB_ELM; j++) {
1132 sprintf(key, "%d", j);
1133 data = xbt_dict_get(head, key);
1134 xbt_test_assert(!strcmp(key, (char *) data),
1135 "with get, key=%s != data=%s", key, (char *) data);
1136 data = xbt_dict_get_ext(head, key, strlen(key));
1137 xbt_test_assert(!strcmp(key, (char *) data),
1138 "with get_ext, key=%s != data=%s", key,
1144 xbt_test_add("Remove my %d elements", NB_ELM);
1145 key = xbt_malloc(10);
1146 for (j = 0; j < NB_ELM; j++) {
1147 /* if (!(j%10000)) printf("."); fflush(stdout); */
1149 sprintf(key, "%d", j);
1150 xbt_dict_remove(head, key);
1155 xbt_test_add("Free the structure (twice)");
1156 xbt_dict_free(&head);
1157 xbt_dict_free(&head);
1160 #endif /* SIMGRID_TEST */