1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2014. 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;
175 "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
176 dict->table_size, hash_code & dict->table_size);
177 current = dict->table[hash_code & dict->table_size];
178 while (current != NULL &&
179 (hash_code != current->hash_code || key_len != current->key_len
180 || memcmp(key, current->key, key_len))) {
182 current = current->next;
185 if (current == NULL) {
186 /* this key doesn't exist yet */
187 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
189 if (previous == NULL) {
190 dict->table[hash_code & dict->table_size] = current;
192 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
193 xbt_dict_rehash(dict);
195 previous->next = current;
198 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
199 key_len, (char *) current->content,
200 key_len, (char *) data, key_len, (char *) key);
201 /* there is already an element with the same key: overwrite it */
202 xbt_dictelm_set_data(dict, current, data, free_ctn);
207 * \brief Add data to the dict (null-terminated key)
209 * \param dict the dict
210 * \param key the key to set the new data
211 * \param data the data to add in the dict
212 * \param free_ctn function to call with (\a data as argument) when
213 * \a data is removed from the dictionary
215 * set the \a data in the structure under the \a key, which is a
216 * null terminated string.
218 XBT_INLINE void xbt_dict_set(xbt_dict_t dict,
219 const char *key, void *data,
220 void_f_pvoid_t free_ctn)
223 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
227 * \brief Retrieve data from the dict (arbitrary key)
229 * \param dict the dealer of data
230 * \param key the key to find data
231 * \param key_len the size of the \a key
232 * \return the data that we are looking for
234 * Search the given \a key. Throws not_found_error when not found.
236 XBT_INLINE void *xbt_dict_get_ext(xbt_dict_t dict, const char *key,
241 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
242 xbt_dictelm_t current;
246 current = dict->table[hash_code & dict->table_size];
247 while (current != NULL &&
248 (hash_code != current->hash_code || key_len != current->key_len
249 || memcmp(key, current->key, key_len))) {
250 current = current->next;
254 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
256 return current->content;
260 * \brief like xbt_dict_get_ext(), but returning NULL when not found
262 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key,
266 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
267 xbt_dictelm_t current;
271 current = dict->table[hash_code & dict->table_size];
272 while (current != NULL &&
273 (hash_code != current->hash_code || key_len != current->key_len
274 || memcmp(key, current->key, key_len))) {
275 current = current->next;
281 return current->content;
285 * @brief retrieve the key associated to that object. Warning, that's a linear search
287 * Returns NULL if the object cannot be found
289 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
292 xbt_dictelm_t current;
295 for (i = 0; i <= dict->table_size; i++) {
296 current = dict->table[i];
297 while (current != NULL) {
298 if (current->content == data)
300 current = current->next;
308 * \brief Retrieve data from the dict (null-terminated key)
310 * \param dict the dealer of data
311 * \param key the key to find data
312 * \return the data that we are looking for
314 * Search the given \a key. Throws not_found_error when not found.
315 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
318 XBT_INLINE void *xbt_dict_get(xbt_dict_t dict, const char *key)
320 return xbt_dict_get_elm(dict, key)->content;
323 * \brief Retrieve element from the dict (null-terminated key)
325 * \param dict the dealer of data
326 * \param key the key to find data
327 * \return the s_xbt_dictelm_t that we are looking for
329 * Search the given \a key. Throws not_found_error when not found.
330 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
333 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
335 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
338 THROWF(not_found_error, 0, "key %s not found", key);
344 * \brief like xbt_dict_get(), but returning NULL when not found
346 XBT_INLINE void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
348 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
353 return current->content;
356 * \brief like xbt_dict_get_elm(), but returning NULL when not found
358 XBT_INLINE xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
360 unsigned int hash_code = xbt_str_hash(key);
361 xbt_dictelm_t current;
365 current = dict->table[hash_code & dict->table_size];
366 while (current != NULL &&
367 (hash_code != current->hash_code || strcmp(key, current->key)))
368 current = current->next;
374 * \brief Remove data from the dict (arbitrary key)
376 * \param dict the trash can
377 * \param key the key of the data to be removed
378 * \param key_len the size of the \a key
380 * Remove the entry associated with the given \a key (throws not_found)
382 XBT_INLINE void xbt_dict_remove_ext(xbt_dict_t dict, const char *key,
387 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
388 xbt_dictelm_t current, previous = NULL;
392 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
393 current = dict->table[hash_code & dict->table_size];
394 while (current != NULL &&
395 (hash_code != current->hash_code || key_len != current->key_len
396 || strncmp(key, current->key, key_len))) {
397 previous = current; /* save the previous node */
398 current = current->next;
402 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
404 if (previous != NULL) {
405 previous->next = current->next;
407 dict->table[hash_code & dict->table_size] = current->next;
410 if (!dict->table[hash_code & dict->table_size])
413 xbt_dictelm_free(dict, current);
420 * \brief Remove data from the dict (null-terminated key)
422 * \param dict the dict
423 * \param key the key of the data to be removed
425 * Remove the entry associated with the given \a key
427 XBT_INLINE void xbt_dict_remove(xbt_dict_t dict, const char *key)
429 xbt_dict_remove_ext(dict, key, strlen(key));
432 #ifdef XBT_USE_DEPRECATED
434 * \brief Add data to the dict (arbitrary key)
435 * \param dict the container
436 * \param key the key to set the new data
437 * \param data the data to add in the dict
439 * Set the \a data in the structure under the \a key.
440 * Both \a data and \a key are considered as uintptr_t.
442 XBT_INLINE void xbt_dicti_set(xbt_dict_t dict,
443 uintptr_t key, uintptr_t data)
445 xbt_dict_set_ext(dict, (void *)&key, sizeof key, (void*)data, NULL);
449 * \brief Retrieve data from the dict (key considered as a uintptr_t)
451 * \param dict the dealer of data
452 * \param key the key to find data
453 * \return the data that we are looking for (or 0 if not found)
455 * Mixing uintptr_t keys with regular keys in the same dict is discouraged
457 XBT_INLINE uintptr_t xbt_dicti_get(xbt_dict_t dict, uintptr_t key)
459 return (uintptr_t)xbt_dict_get_or_null_ext(dict, (void *)&key, sizeof key);
462 /** Remove a uintptr_t key from the dict */
463 XBT_INLINE void xbt_dicti_remove(xbt_dict_t dict, uintptr_t key)
465 xbt_dict_remove_ext(dict, (void *)&key, sizeof key);
470 * \brief Remove all data from the dict
471 * \param dict the dict
473 void xbt_dict_reset(xbt_dict_t dict)
477 xbt_dictelm_t current, previous = NULL;
481 if (dict->count == 0)
484 for (i = 0; i <= dict->table_size; i++) {
485 current = dict->table[i];
486 while (current != NULL) {
488 current = current->next;
489 xbt_dictelm_free(dict, previous);
491 dict->table[i] = NULL;
499 * \brief Return the number of elements in the dict.
500 * \param dict a dictionary
502 XBT_INLINE int xbt_dict_length(xbt_dict_t dict)
509 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
510 void xbt_dict_dump_output_string(void *s)
516 * \brief test if the dict is empty or not
518 XBT_INLINE int xbt_dict_is_empty(xbt_dict_t dict)
520 return !dict || (xbt_dict_length(dict) == 0);
524 * \brief Outputs the content of the structure (debugging purpose)
526 * \param dict the exibitionist
527 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
529 * Outputs the content of the structure. (for debugging purpose). \a output is a
530 * function to output the data. If NULL, data won't be displayed.
533 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
536 xbt_dictelm_t element;
537 printf("Dict %p:\n", dict);
539 for (i = 0; i < dict->table_size; i++) {
540 element = dict->table[i];
543 while (element != NULL) {
544 printf(" %s -> '", element->key);
545 if (output != NULL) {
546 output(element->content);
549 element = element->next;
559 xbt_dynar_t all_sizes = NULL;
560 /** @brief shows some debugging info about the bucklet repartition */
561 void xbt_dict_dump_sizes(xbt_dict_t dict)
567 xbt_dictelm_t element;
568 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
570 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
571 dict->fill, dict->table_size);
573 for (i = 0; i < dict->table_size; i++) {
574 element = dict->table[i];
577 while (element != NULL) {
579 element = element->next;
582 if (xbt_dynar_length(sizes) <= size) {
584 xbt_dynar_set(sizes, size, &prevsize);
587 xbt_dynar_get_cpy(sizes, size, &prevsize);
589 xbt_dynar_set(sizes, size, &prevsize);
593 all_sizes = xbt_dynar_new(sizeof(int), NULL);
595 xbt_dynar_foreach(sizes, count, size) {
596 /* Copy values of this one into all_sizes */
598 if (xbt_dynar_length(all_sizes) <= count) {
600 xbt_dynar_set(all_sizes, count, &prevcount);
602 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
604 xbt_dynar_set(all_sizes, count, &prevcount);
607 /* Report current sizes */
612 printf("%uelm x %u cells; ", count, size);
616 xbt_dynar_free(&sizes);
620 * Create the dict mallocators.
621 * This is an internal XBT function called during the lib initialization.
622 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
624 void xbt_dict_preinit(void)
626 dict_elm_mallocator = xbt_mallocator_new(256,
627 dict_elm_mallocator_new_f,
628 dict_elm_mallocator_free_f,
629 dict_elm_mallocator_reset_f);
630 dict_het_elm_mallocator = xbt_mallocator_new(256,
631 dict_het_elm_mallocator_new_f,
632 dict_het_elm_mallocator_free_f,
633 dict_het_elm_mallocator_reset_f);
637 * Destroy the dict mallocators.
638 * This is an internal XBT function during the lib initialization
640 void xbt_dict_postexit(void)
642 if (dict_elm_mallocator != NULL) {
643 xbt_mallocator_free(dict_elm_mallocator);
644 dict_elm_mallocator = NULL;
645 xbt_mallocator_free(dict_het_elm_mallocator);
646 dict_het_elm_mallocator = NULL;
653 printf("Overall stats:");
654 xbt_dynar_foreach(all_sizes, count, size) {
659 printf("%uelm x %d cells; ", count, size);
663 printf("; %f elm per cell\n", avg / (double) total_count);
670 #include "portable.h"
672 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
674 XBT_TEST_SUITE("dict", "Dict data container");
676 static void debuged_add_ext(xbt_dict_t head, const char *key,
677 const char *data_to_fill, void_f_pvoid_t free_f)
679 char *data = xbt_strdup(data_to_fill);
681 xbt_test_log("Add %s under %s", PRINTF_STR(data_to_fill),
684 xbt_dict_set(head, key, data, free_f);
685 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
686 xbt_dict_dump(head, (void (*)(void *)) &printf);
691 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
693 debuged_add_ext(head, key, key, free_f);
696 static void fill(xbt_dict_t * head, int homogeneous)
698 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
700 xbt_test_add("Fill in the dictionnary");
702 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
703 debuged_add(*head, "12", free_f);
704 debuged_add(*head, "12a", free_f);
705 debuged_add(*head, "12b", free_f);
706 debuged_add(*head, "123", free_f);
707 debuged_add(*head, "123456", free_f);
708 /* Child becomes child of what to add */
709 debuged_add(*head, "1234", free_f);
710 /* Need of common ancestor */
711 debuged_add(*head, "123457", free_f);
715 static void search_ext(xbt_dict_t head, const char *key, const char *data)
719 xbt_test_add("Search %s", key);
720 found = xbt_dict_get(head, key);
721 xbt_test_log("Found %s", found);
723 xbt_test_assert(found,
724 "data do not match expectations: found NULL while searching for %s",
727 xbt_test_assert(!strcmp(data, found),
728 "data do not match expectations: found %s while searching for %s",
731 xbt_test_assert(!found,
732 "data do not match expectations: found %s while searching for NULL",
737 static void search(xbt_dict_t head, const char *key)
739 search_ext(head, key, key);
742 static void debuged_remove(xbt_dict_t head, const char *key)
745 xbt_test_add("Remove '%s'", PRINTF_STR(key));
746 xbt_dict_remove(head, key);
747 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
751 static void traverse(xbt_dict_t head)
753 xbt_dict_cursor_t cursor = NULL;
758 xbt_dict_foreach(head, cursor, key, data) {
759 if (!key || !data || strcmp(key, data)) {
760 xbt_test_log("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
763 xbt_test_log("Seen #%d: %s", ++i, PRINTF_STR(key));
765 xbt_test_assert(!data || !strcmp(key, data),
766 "Key(%s) != value(%s). Aborting", key, data);
770 static void search_not_found(xbt_dict_t head, const char *data)
775 xbt_test_add("Search %s (expected not to be found)", data);
778 data = xbt_dict_get(head, data);
779 THROWF(unknown_error, 0,
780 "Found something which shouldn't be there (%s)", data);
783 if (e.category != not_found_error)
784 xbt_test_exception(e);
788 xbt_test_assert(ok, "Exception not raised");
791 static void count(xbt_dict_t dict, int length)
793 xbt_dict_cursor_t cursor;
799 xbt_test_add("Count elements (expecting %d)", length);
800 xbt_test_assert(xbt_dict_length(dict) == length,
801 "Announced length(%d) != %d.", xbt_dict_length(dict),
804 xbt_dict_foreach(dict, cursor, key, data)
807 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
812 static void count_check_get_key(xbt_dict_t dict, int length)
814 xbt_dict_cursor_t cursor;
816 _XBT_GNUC_UNUSED char *key2;
822 ("Count elements (expecting %d), and test the getkey function",
824 xbt_test_assert(xbt_dict_length(dict) == length,
825 "Announced length(%d) != %d.", xbt_dict_length(dict),
828 xbt_dict_foreach(dict, cursor, key, data) {
830 key2 = xbt_dict_get_key(dict, data);
831 xbt_assert(!strcmp(key, key2),
832 "The data was registered under %s instead of %s as expected",
836 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
842 xbt_dict_t head = NULL;
845 static void basic_test(int homogeneous)
847 void_f_pvoid_t free_f;
849 xbt_test_add("Traversal the null dictionary");
852 xbt_test_add("Traversal and search the empty dictionary");
853 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
856 debuged_remove(head, "12346");
859 if (e.category != not_found_error)
860 xbt_test_exception(e);
863 xbt_dict_free(&head);
865 free_f = homogeneous ? NULL : &free;
867 xbt_test_add("Traverse the full dictionary");
868 fill(&head, homogeneous);
869 count_check_get_key(head, 7);
871 debuged_add_ext(head, "toto", "tutu", free_f);
872 search_ext(head, "toto", "tutu");
873 debuged_remove(head, "toto");
878 xbt_test_add("Free the dictionary (twice)");
879 xbt_dict_free(&head);
880 xbt_dict_free(&head);
883 fill(&head, homogeneous);
884 count_check_get_key(head, 7);
885 xbt_test_add("Change 123 to 'Changed 123'");
886 xbt_dict_set(head, "123", strdup("Changed 123"), free_f);
887 count_check_get_key(head, 7);
889 xbt_test_add("Change 123 back to '123'");
890 xbt_dict_set(head, "123", strdup("123"), free_f);
891 count_check_get_key(head, 7);
893 xbt_test_add("Change 12a to 'Dummy 12a'");
894 xbt_dict_set(head, "12a", strdup("Dummy 12a"), free_f);
895 count_check_get_key(head, 7);
897 xbt_test_add("Change 12a to '12a'");
898 xbt_dict_set(head, "12a", strdup("12a"), free_f);
899 count_check_get_key(head, 7);
901 xbt_test_add("Traverse the resulting dictionary");
905 xbt_test_add("Search 123");
906 data = xbt_dict_get(head, "123");
907 xbt_test_assert(data);
908 xbt_test_assert(!strcmp("123", data));
910 search_not_found(head, "Can't be found");
911 search_not_found(head, "123 Can't be found");
912 search_not_found(head, "12345678 NOT");
917 search(head, "123456");
918 search(head, "1234");
919 search(head, "123457");
921 xbt_test_add("Traverse the resulting dictionary");
924 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
926 xbt_test_add("Free the dictionary twice");
927 xbt_dict_free(&head);
928 xbt_dict_free(&head);
930 xbt_test_add("Traverse the resulting dictionary");
934 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
939 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
944 static void remove_test(int homogeneous)
946 fill(&head, homogeneous);
948 xbt_test_add("Remove non existing data");
950 debuged_remove(head, "Does not exist");
953 if (e.category != not_found_error)
954 xbt_test_exception(e);
959 xbt_dict_free(&head);
962 ("Remove each data manually (traversing the resulting dictionary each time)");
963 fill(&head, homogeneous);
964 debuged_remove(head, "12a");
967 debuged_remove(head, "12b");
970 debuged_remove(head, "12");
973 debuged_remove(head, "123456");
977 debuged_remove(head, "12346");
980 if (e.category != not_found_error)
981 xbt_test_exception(e);
985 debuged_remove(head, "1234");
987 debuged_remove(head, "123457");
989 debuged_remove(head, "123");
992 debuged_remove(head, "12346");
995 if (e.category != not_found_error)
996 xbt_test_exception(e);
1002 ("Free dict, create new fresh one, and then reset the dict");
1003 xbt_dict_free(&head);
1004 fill(&head, homogeneous);
1005 xbt_dict_reset(head);
1009 xbt_test_add("Free the dictionary twice");
1010 xbt_dict_free(&head);
1011 xbt_dict_free(&head);
1014 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
1019 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
1024 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1028 xbt_test_add("Store NULL under 'null'");
1029 xbt_dict_set(head, "null", NULL, NULL);
1030 search_ext(head, "null", NULL);
1032 xbt_test_add("Check whether I see it while traversing...");
1034 xbt_dict_cursor_t cursor = NULL;
1038 xbt_dict_foreach(head, cursor, key, data) {
1039 if (!key || !data || strcmp(key, data)) {
1040 xbt_test_log("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1042 xbt_test_log("Seen: %s", PRINTF_STR(key));
1045 if (!strcmp(key, "null"))
1048 xbt_test_assert(found,
1049 "the key 'null', associated to NULL is not found");
1051 xbt_dict_free(&head);
1054 #define NB_ELM 20000
1055 #define SIZEOFKEY 1024
1056 static int countelems(xbt_dict_t head)
1058 xbt_dict_cursor_t cursor;
1063 xbt_dict_foreach(head, cursor, key, data) {
1069 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1071 xbt_dict_t head = NULL;
1075 srand((unsigned int) time(NULL));
1077 for (i = 0; i < 10; i++) {
1078 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1080 ("Fill the struct, count its elems and frees the structure");
1082 ("using 1000 elements with %d chars long randomized keys.",
1084 head = xbt_dict_new();
1085 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1086 for (j = 0; j < 1000; j++) {
1088 key = xbt_malloc(SIZEOFKEY);
1091 for (k = 0; k < SIZEOFKEY - 1; k++)
1092 key[k] = rand() % ('z' - 'a') + 'a';
1094 /* printf("[%d %s]\n",j,key); */
1095 data = xbt_dict_get_or_null(head, key);
1096 } while (data != NULL);
1098 xbt_dict_set(head, key, key, &free);
1099 data = xbt_dict_get(head, key);
1100 xbt_test_assert(!strcmp(key, data),
1101 "Retrieved value (%s) != Injected value (%s)", key,
1106 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1108 xbt_dict_free(&head);
1109 xbt_dict_free(&head);
1113 head = xbt_dict_new();
1114 xbt_test_add("Fill %d elements, with keys being the number of element",
1116 for (j = 0; j < NB_ELM; j++) {
1117 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1119 key = xbt_malloc(10);
1121 sprintf(key, "%d", j);
1122 xbt_dict_set(head, key, key, &free);
1124 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1127 ("Count the elements (retrieving the key and data for each)");
1128 i = countelems(head);
1129 xbt_test_log("There is %d elements", i);
1131 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1132 key = xbt_malloc(10);
1133 for (i = 0; i < 20; i++) {
1135 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1136 for (j = 0; j < NB_ELM; j++) {
1138 sprintf(key, "%d", j);
1139 data = xbt_dict_get(head, key);
1140 xbt_test_assert(!strcmp(key, (char *) data),
1141 "with get, key=%s != data=%s", key, (char *) data);
1142 data = xbt_dict_get_ext(head, key, strlen(key));
1143 xbt_test_assert(!strcmp(key, (char *) data),
1144 "with get_ext, key=%s != data=%s", key,
1150 xbt_test_add("Remove my %d elements", NB_ELM);
1151 key = xbt_malloc(10);
1152 for (j = 0; j < NB_ELM; j++) {
1153 /* if (!(j%10000)) printf("."); fflush(stdout); */
1155 sprintf(key, "%d", j);
1156 xbt_dict_remove(head, key);
1161 xbt_test_add("Free the structure (twice)");
1162 xbt_dict_free(&head);
1163 xbt_dict_free(&head);
1166 #endif /* SIMGRID_TEST */