1 /* dict - a generic dictionary, variation over hash table */
3 /* Copyright (c) 2004-2015. 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 "src/xbt_modinter.h"
16 #include "dict_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
19 "Dictionaries provide the same functionalities as hash tables");
23 * \return pointer to the destination
24 * \see xbt_dict_new_homogenous(), 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 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 int newsize = oldsize * 2;
112 xbt_dictelm_t *currcell;
113 xbt_dictelm_t *twincell;
114 xbt_dictelm_t bucklet;
115 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. This param
160 * will only be considered when the dict was instantiated with
161 * xbt_dict_new() and not xbt_dict_new_homogeneous()
163 * Set the \a data in the structure under the \a key, which can be any kind
164 * of data, as long as its length is provided in \a key_len.
166 inline void xbt_dict_set_ext(xbt_dict_t dict,
167 const char *key, int key_len,
168 void *data, void_f_pvoid_t free_ctn)
171 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
173 xbt_dictelm_t current, previous = NULL;
176 "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
177 dict->table_size, hash_code & dict->table_size);
178 current = dict->table[hash_code & dict->table_size];
179 while (current != NULL &&
180 (hash_code != current->hash_code || key_len != current->key_len
181 || memcmp(key, current->key, key_len))) {
183 current = current->next;
186 if (current == NULL) {
187 /* this key doesn't exist yet */
188 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
190 if (previous == NULL) {
191 dict->table[hash_code & dict->table_size] = current;
193 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
194 xbt_dict_rehash(dict);
196 previous->next = current;
199 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
200 key_len, (char *) current->content,
201 key_len, (char *) data, key_len, (char *) key);
202 /* there is already an element with the same key: overwrite it */
203 xbt_dictelm_set_data(dict, current, data, free_ctn);
208 * \brief Add data to the dict (null-terminated key)
210 * \param dict the dict
211 * \param key the key to set the new data
212 * \param data the data to add in the dict
213 * \param free_ctn function to call with (\a data as argument) when
214 * \a data is removed from the dictionary. This param
215 * will only be considered when the dict was instantiated with
216 * xbt_dict_new() and not xbt_dict_new_homogeneous()
218 * set the \a data in the structure under the \a key, which is a
219 * null terminated string.
221 inline void xbt_dict_set(xbt_dict_t dict,
222 const char *key, void *data,
223 void_f_pvoid_t free_ctn)
226 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
230 * \brief Retrieve data from the dict (arbitrary key)
232 * \param dict the dealer of data
233 * \param key the key to find data
234 * \param key_len the size of the \a key
235 * \return the data that we are looking for
237 * Search the given \a key. Throws not_found_error when not found.
239 inline void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
241 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
242 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
244 while (current != NULL &&
245 (hash_code != current->hash_code || key_len != current->key_len
246 || memcmp(key, current->key, key_len))) {
247 current = current->next;
251 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
253 return current->content;
256 /** @brief like xbt_dict_get_ext(), but returning NULL when not found */
257 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
259 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
260 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
262 while (current != NULL &&
263 (hash_code != current->hash_code || key_len != current->key_len
264 || memcmp(key, current->key, key_len))) {
265 current = current->next;
271 return current->content;
275 * @brief retrieve the key associated to that object. Warning, that's a linear search
277 * Returns NULL if the object cannot be found
279 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
281 for (int i = 0; i <= dict->table_size; i++) {
282 xbt_dictelm_t current = dict->table[i];
283 while (current != NULL) {
284 if (current->content == data)
286 current = current->next;
293 /** @brief retrieve the key associated to that xbt_dictelm_t. */
294 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
300 * \brief Retrieve data from the dict (null-terminated key)
302 * \param dict the dealer of data
303 * \param key the key to find data
304 * \return the data that we are looking for
306 * Search the given \a key. Throws not_found_error when not found.
307 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
310 inline void *xbt_dict_get(xbt_dict_t dict, const char *key)
312 return xbt_dict_get_elm(dict, key)->content;
315 * \brief Retrieve element from the dict (null-terminated key)
317 * \param dict the dealer of data
318 * \param key the key to find data
319 * \return the s_xbt_dictelm_t that we are looking for
321 * Search the given \a key. Throws not_found_error when not found.
322 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
325 inline xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
327 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
330 THROWF(not_found_error, 0, "key %s not found", key);
336 * \brief like xbt_dict_get(), but returning NULL when not found
338 inline void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
340 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
345 return current->content;
348 * \brief like xbt_dict_get_elm(), but returning NULL when not found
350 inline xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
352 unsigned int hash_code = xbt_str_hash(key);
353 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
355 while (current != NULL &&
356 (hash_code != current->hash_code || strcmp(key, current->key)))
357 current = current->next;
363 * \brief Remove data from the dict (arbitrary key)
365 * \param dict the trash can
366 * \param key the key of the data to be removed
367 * \param key_len the size of the \a key
369 * Remove the entry associated with the given \a key (throws not_found)
371 inline void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
373 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
374 xbt_dictelm_t previous = NULL;
375 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
377 while (current != NULL &&
378 (hash_code != current->hash_code || key_len != current->key_len
379 || strncmp(key, current->key, key_len))) {
380 previous = current; /* save the previous node */
381 current = current->next;
385 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
387 if (previous != NULL) {
388 previous->next = current->next;
390 dict->table[hash_code & dict->table_size] = current->next;
393 if (!dict->table[hash_code & dict->table_size])
396 xbt_dictelm_free(dict, current);
403 * \brief Remove data from the dict (null-terminated key)
405 * \param dict the dict
406 * \param key the key of the data to be removed
408 * Remove the entry associated with the given \a key
410 inline void xbt_dict_remove(xbt_dict_t dict, const char *key)
412 xbt_dict_remove_ext(dict, key, strlen(key));
415 /** @brief Remove all data from the dict */
416 void xbt_dict_reset(xbt_dict_t dict)
418 if (dict->count == 0)
421 for (int i = 0; i <= dict->table_size; i++) {
422 xbt_dictelm_t previous = NULL;
423 xbt_dictelm_t current = dict->table[i];
424 while (current != NULL) {
426 current = current->next;
427 xbt_dictelm_free(dict, previous);
429 dict->table[i] = NULL;
437 * \brief Return the number of elements in the dict.
438 * \param dict a dictionary
440 inline int xbt_dict_length(xbt_dict_t dict)
445 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
446 void xbt_dict_dump_output_string(void *s)
452 * \brief test if the dict is empty or not
454 inline int xbt_dict_is_empty(xbt_dict_t dict)
456 return !dict || (xbt_dict_length(dict) == 0);
460 * \brief Outputs the content of the structure (debugging purpose)
462 * \param dict the exibitionist
463 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
465 * Outputs the content of the structure. (for debugging purpose). \a output is a
466 * function to output the data. If NULL, data won't be displayed.
469 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
472 xbt_dictelm_t element;
473 printf("Dict %p:\n", dict);
475 for (i = 0; i < dict->table_size; i++) {
476 element = dict->table[i];
479 while (element != NULL) {
480 printf(" %s -> '", element->key);
481 if (output != NULL) {
482 output(element->content);
485 element = element->next;
495 xbt_dynar_t all_sizes = NULL;
496 /** @brief shows some debugging info about the bucklet repartition */
497 void xbt_dict_dump_sizes(xbt_dict_t dict)
502 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
508 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
510 for (int i = 0; i < dict->table_size; i++) {
511 xbt_dictelm_t element = dict->table[i];
514 while (element != NULL) {
516 element = element->next;
519 if (xbt_dynar_length(sizes) <= size) {
521 xbt_dynar_set(sizes, size, &prevsize);
524 xbt_dynar_get_cpy(sizes, size, &prevsize);
526 xbt_dynar_set(sizes, size, &prevsize);
530 all_sizes = xbt_dynar_new(sizeof(int), NULL);
532 xbt_dynar_foreach(sizes, count, size) {
533 /* Copy values of this one into all_sizes */
535 if (xbt_dynar_length(all_sizes) <= count) {
537 xbt_dynar_set(all_sizes, count, &prevcount);
539 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
541 xbt_dynar_set(all_sizes, count, &prevcount);
544 /* Report current sizes */
549 printf("%uelm x %u cells; ", count, size);
552 xbt_dynar_free(&sizes);
556 * Create the dict mallocators.
557 * This is an internal XBT function called during the lib initialization.
558 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
560 void xbt_dict_preinit(void)
562 dict_elm_mallocator = xbt_mallocator_new(256,
563 dict_elm_mallocator_new_f,
564 dict_elm_mallocator_free_f,
565 dict_elm_mallocator_reset_f);
566 dict_het_elm_mallocator = xbt_mallocator_new(256,
567 dict_het_elm_mallocator_new_f,
568 dict_het_elm_mallocator_free_f,
569 dict_het_elm_mallocator_reset_f);
573 * Destroy the dict mallocators.
574 * This is an internal XBT function during the lib initialization
576 void xbt_dict_postexit(void)
578 if (dict_elm_mallocator != NULL) {
579 xbt_mallocator_free(dict_elm_mallocator);
580 dict_elm_mallocator = NULL;
581 xbt_mallocator_free(dict_het_elm_mallocator);
582 dict_het_elm_mallocator = NULL;
589 printf("Overall stats:");
590 xbt_dynar_foreach(all_sizes, count, size) {
595 printf("%uelm x %d cells; ", count, size);
599 printf("; %f elm per cell\n", avg / (double) total_count);
606 #include "src/portable.h"
609 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
611 XBT_TEST_SUITE("dict", "Dict data container");
613 static void debuged_add_ext(xbt_dict_t head, const char *key,
614 const char *data_to_fill, void_f_pvoid_t free_f)
616 char *data = xbt_strdup(data_to_fill);
618 xbt_test_log("Add %s under %s", data_to_fill, key);
620 xbt_dict_set(head, key, data, free_f);
621 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
622 xbt_dict_dump(head, (void (*)(void *)) &printf);
627 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
629 debuged_add_ext(head, key, key, free_f);
632 static void fill(xbt_dict_t * head, int homogeneous)
634 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
636 xbt_test_add("Fill in the dictionnary");
638 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
639 debuged_add(*head, "12", free_f);
640 debuged_add(*head, "12a", free_f);
641 debuged_add(*head, "12b", free_f);
642 debuged_add(*head, "123", free_f);
643 debuged_add(*head, "123456", free_f);
644 /* Child becomes child of what to add */
645 debuged_add(*head, "1234", free_f);
646 /* Need of common ancestor */
647 debuged_add(*head, "123457", free_f);
651 static void search_ext(xbt_dict_t head, const char *key, const char *data)
655 xbt_test_add("Search %s", key);
656 found = xbt_dict_get(head, key);
657 xbt_test_log("Found %s", found);
659 xbt_test_assert(found,
660 "data do not match expectations: found NULL while searching for %s",
663 xbt_test_assert(!strcmp(data, found),
664 "data do not match expectations: found %s while searching for %s",
667 xbt_test_assert(!found,
668 "data do not match expectations: found %s while searching for NULL",
673 static void search(xbt_dict_t head, const char *key)
675 search_ext(head, key, key);
678 static void debuged_remove(xbt_dict_t head, const char *key)
681 xbt_test_add("Remove '%s'", key);
682 xbt_dict_remove(head, key);
683 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
687 static void traverse(xbt_dict_t head)
689 xbt_dict_cursor_t cursor = NULL;
694 xbt_dict_foreach(head, cursor, key, data) {
695 if (!key || !data || strcmp(key, data)) {
696 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
698 xbt_test_log("Seen #%d: %s", ++i, key);
700 xbt_test_assert(!data || !strcmp(key, data),
701 "Key(%s) != value(%s). Aborting", key, data);
705 static void search_not_found(xbt_dict_t head, const char *data)
710 xbt_test_add("Search %s (expected not to be found)", data);
713 data = xbt_dict_get(head, data);
714 THROWF(unknown_error, 0,
715 "Found something which shouldn't be there (%s)", data);
718 if (e.category != not_found_error)
719 xbt_test_exception(e);
723 xbt_test_assert(ok, "Exception not raised");
726 static void count(xbt_dict_t dict, int length)
728 xbt_dict_cursor_t cursor;
734 xbt_test_add("Count elements (expecting %d)", length);
735 xbt_test_assert(xbt_dict_length(dict) == length,
736 "Announced length(%d) != %d.", xbt_dict_length(dict),
739 xbt_dict_foreach(dict, cursor, key, data)
742 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
747 static void count_check_get_key(xbt_dict_t dict, int length)
749 xbt_dict_cursor_t cursor;
751 XBT_ATTRIB_UNUSED char *key2;
757 ("Count elements (expecting %d), and test the getkey function",
759 xbt_test_assert(xbt_dict_length(dict) == length,
760 "Announced length(%d) != %d.", xbt_dict_length(dict),
763 xbt_dict_foreach(dict, cursor, key, data) {
765 key2 = xbt_dict_get_key(dict, data);
766 xbt_assert(!strcmp(key, key2),
767 "The data was registered under %s instead of %s as expected",
771 xbt_test_assert(effective == length, "Effective length(%d) != %d.",
777 xbt_dict_t head = NULL;
780 static void basic_test(int homogeneous)
782 void_f_pvoid_t free_f;
784 xbt_test_add("Traversal the null dictionary");
787 xbt_test_add("Traversal and search the empty dictionary");
788 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
791 debuged_remove(head, "12346");
794 if (e.category != not_found_error)
795 xbt_test_exception(e);
798 xbt_dict_free(&head);
800 free_f = homogeneous ? NULL : &free;
802 xbt_test_add("Traverse the full dictionary");
803 fill(&head, homogeneous);
804 count_check_get_key(head, 7);
806 debuged_add_ext(head, "toto", "tutu", free_f);
807 search_ext(head, "toto", "tutu");
808 debuged_remove(head, "toto");
813 xbt_test_add("Free the dictionary (twice)");
814 xbt_dict_free(&head);
815 xbt_dict_free(&head);
818 fill(&head, homogeneous);
819 count_check_get_key(head, 7);
820 xbt_test_add("Change 123 to 'Changed 123'");
821 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
822 count_check_get_key(head, 7);
824 xbt_test_add("Change 123 back to '123'");
825 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
826 count_check_get_key(head, 7);
828 xbt_test_add("Change 12a to 'Dummy 12a'");
829 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
830 count_check_get_key(head, 7);
832 xbt_test_add("Change 12a to '12a'");
833 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
834 count_check_get_key(head, 7);
836 xbt_test_add("Traverse the resulting dictionary");
840 xbt_test_add("Search 123");
841 data = xbt_dict_get(head, "123");
842 xbt_test_assert(data);
843 xbt_test_assert(!strcmp("123", data));
845 search_not_found(head, "Can't be found");
846 search_not_found(head, "123 Can't be found");
847 search_not_found(head, "12345678 NOT");
852 search(head, "123456");
853 search(head, "1234");
854 search(head, "123457");
856 xbt_test_add("Traverse the resulting dictionary");
859 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
861 xbt_test_add("Free the dictionary twice");
862 xbt_dict_free(&head);
863 xbt_dict_free(&head);
865 xbt_test_add("Traverse the resulting dictionary");
869 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
874 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
879 static void remove_test(int homogeneous)
881 fill(&head, homogeneous);
883 xbt_test_add("Remove non existing data");
885 debuged_remove(head, "Does not exist");
888 if (e.category != not_found_error)
889 xbt_test_exception(e);
894 xbt_dict_free(&head);
897 ("Remove each data manually (traversing the resulting dictionary each time)");
898 fill(&head, homogeneous);
899 debuged_remove(head, "12a");
902 debuged_remove(head, "12b");
905 debuged_remove(head, "12");
908 debuged_remove(head, "123456");
912 debuged_remove(head, "12346");
915 if (e.category != not_found_error)
916 xbt_test_exception(e);
920 debuged_remove(head, "1234");
922 debuged_remove(head, "123457");
924 debuged_remove(head, "123");
927 debuged_remove(head, "12346");
930 if (e.category != not_found_error)
931 xbt_test_exception(e);
937 ("Free dict, create new fresh one, and then reset the dict");
938 xbt_dict_free(&head);
939 fill(&head, homogeneous);
940 xbt_dict_reset(head);
944 xbt_test_add("Free the dictionary twice");
945 xbt_dict_free(&head);
946 xbt_dict_free(&head);
949 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
954 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
959 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
963 xbt_test_add("Store NULL under 'null'");
964 xbt_dict_set(head, "null", NULL, NULL);
965 search_ext(head, "null", NULL);
967 xbt_test_add("Check whether I see it while traversing...");
969 xbt_dict_cursor_t cursor = NULL;
973 xbt_dict_foreach(head, cursor, key, data) {
974 if (!key || !data || strcmp(key, data)) {
975 xbt_test_log("Seen: %s->%s", key, data);
977 xbt_test_log("Seen: %s", key);
980 if (!strcmp(key, "null"))
983 xbt_test_assert(found,
984 "the key 'null', associated to NULL is not found");
986 xbt_dict_free(&head);
990 #define SIZEOFKEY 1024
991 static int countelems(xbt_dict_t head)
993 xbt_dict_cursor_t cursor;
998 xbt_dict_foreach(head, cursor, key, data) {
1004 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1006 xbt_dict_t head = NULL;
1010 srand((unsigned int) time(NULL));
1012 for (i = 0; i < 10; i++) {
1013 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1015 ("Fill the struct, count its elems and frees the structure");
1017 ("using 1000 elements with %d chars long randomized keys.",
1019 head = xbt_dict_new();
1020 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1021 for (j = 0; j < 1000; j++) {
1023 key = xbt_malloc(SIZEOFKEY);
1026 for (k = 0; k < SIZEOFKEY - 1; k++)
1027 key[k] = rand() % ('z' - 'a') + 'a';
1029 /* printf("[%d %s]\n",j,key); */
1030 data = xbt_dict_get_or_null(head, key);
1031 } while (data != NULL);
1033 xbt_dict_set(head, key, key, &free);
1034 data = xbt_dict_get(head, key);
1035 xbt_test_assert(!strcmp(key, data),
1036 "Retrieved value (%s) != Injected value (%s)", key,
1041 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1043 xbt_dict_free(&head);
1044 xbt_dict_free(&head);
1048 head = xbt_dict_new();
1049 xbt_test_add("Fill %d elements, with keys being the number of element",
1051 for (j = 0; j < NB_ELM; j++) {
1052 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1054 key = xbt_malloc(10);
1056 sprintf(key, "%d", j);
1057 xbt_dict_set(head, key, key, &free);
1059 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1062 ("Count the elements (retrieving the key and data for each)");
1063 i = countelems(head);
1064 xbt_test_log("There is %d elements", i);
1066 xbt_test_add("Search my %d elements 20 times", NB_ELM);
1067 key = xbt_malloc(10);
1068 for (i = 0; i < 20; i++) {
1070 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1071 for (j = 0; j < NB_ELM; j++) {
1073 sprintf(key, "%d", j);
1074 data = xbt_dict_get(head, key);
1075 xbt_test_assert(!strcmp(key, (char *) data),
1076 "with get, key=%s != data=%s", key, (char *) data);
1077 data = xbt_dict_get_ext(head, key, strlen(key));
1078 xbt_test_assert(!strcmp(key, (char *) data),
1079 "with get_ext, key=%s != data=%s", key,
1085 xbt_test_add("Remove my %d elements", NB_ELM);
1086 key = xbt_malloc(10);
1087 for (j = 0; j < NB_ELM; j++) {
1088 /* if (!(j%10000)) printf("."); fflush(stdout); */
1090 sprintf(key, "%d", j);
1091 xbt_dict_remove(head, key);
1096 xbt_test_add("Free the structure (twice)");
1097 xbt_dict_free(&head);
1098 xbt_dict_free(&head);
1101 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1103 xbt_dict_t dict = xbt_dict_new();
1106 xbt_test_add("Insert elements");
1108 for (i = 0; i < count; ++i)
1109 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1110 xbt_test_assert(xbt_dict_size(dict) == count,
1111 "Bad number of elements in the dictionnary");
1113 xbt_test_add("Check elements");
1114 for (i = 0; i < count; ++i) {
1115 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1116 xbt_test_assert(xbt_dict_size(dict) == count,
1117 "Unexpected value at index %i, expected %i but was %i", i, i, res);
1120 xbt_test_add("Free the array");
1121 xbt_dict_free(&dict);
1124 #endif /* SIMGRID_TEST */