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, "Dictionaries provide the same functionalities as hash tables");
22 * \return pointer to the destination
23 * \see xbt_dict_new_homogenous(), xbt_dict_free()
25 * Creates and initialize a new dictionary with a default hashtable size.
26 * The dictionary is heterogeneous: each element can have a different free function.
28 xbt_dict_t xbt_dict_new(void)
30 xbt_dict_t dict = xbt_dict_new_homogeneous(NULL);
31 dict->homogeneous = 0;
38 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary
39 * \return pointer to the destination
40 * \see xbt_dict_new(), xbt_dict_free()
42 * Creates and initialize a new dictionary with a default hashtable size.
43 * The dictionary is homogeneous: each element share the same free function.
45 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
47 if (dict_elm_mallocator == NULL)
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);
98 /** Returns the amount of elements in the dict */
99 inline unsigned int xbt_dict_size(xbt_dict_t dict)
101 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
104 /* Expend the size of the dict */
105 static void xbt_dict_rehash(xbt_dict_t dict)
107 const int oldsize = dict->table_size + 1;
108 int newsize = oldsize * 2;
110 xbt_dictelm_t *currcell;
111 xbt_dictelm_t *twincell;
112 xbt_dictelm_t bucklet;
113 xbt_dictelm_t *pprev;
115 currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
116 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
117 dict->table_size = --newsize;
118 dict->table = currcell;
119 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
121 for (i = 0; i < oldsize; i++, currcell++) {
122 if (!*currcell) /* empty cell */
124 twincell = currcell + oldsize;
125 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
126 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
127 - stay in cell i (ie, currcell)
128 - go to the cell i+oldsize (ie, twincell) */
129 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
130 *pprev = bucklet->next;
131 bucklet->next = *twincell;
137 pprev = &bucklet->next;
141 if (!*currcell) /* everything moved */
147 * \brief Add data to the dict (arbitrary key)
148 * \param dict the container
149 * \param key the key to set the new data
150 * \param key_len the size of the \a key
151 * \param data the data to add in the dict
152 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
153 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
155 * Set the \a data in the structure under the \a key, which can be any kind of data, as long as its length is provided
158 inline void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn)
160 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
162 xbt_dictelm_t current, previous = NULL;
164 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
165 dict->table_size, hash_code & dict->table_size);
166 current = dict->table[hash_code & dict->table_size];
167 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
168 || memcmp(key, current->key, key_len))) {
170 current = current->next;
173 if (current == NULL) {
174 /* this key doesn't exist yet */
175 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
177 if (previous == NULL) {
178 dict->table[hash_code & dict->table_size] = current;
180 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
181 xbt_dict_rehash(dict);
183 previous->next = current;
186 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
187 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
188 /* there is already an element with the same key: overwrite it */
189 xbt_dictelm_set_data(dict, current, data, free_ctn);
194 * \brief Add data to the dict (null-terminated key)
196 * \param dict the dict
197 * \param key the key to set the new data
198 * \param data the data to add in the dict
199 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
200 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
202 * set the \a data in the structure under the \a key, which is anull terminated string.
204 inline void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
206 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
210 * \brief Retrieve data from the dict (arbitrary key)
212 * \param dict the dealer of data
213 * \param key the key to find data
214 * \param key_len the size of the \a key
215 * \return the data that we are looking for
217 * Search the given \a key. Throws not_found_error when not found.
219 inline void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
221 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
222 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
224 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
225 || memcmp(key, current->key, key_len))) {
226 current = current->next;
230 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
232 return current->content;
235 /** @brief like xbt_dict_get_ext(), but returning NULL when not found */
236 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
238 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
239 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
241 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
242 || memcmp(key, current->key, key_len))) {
243 current = current->next;
249 return current->content;
253 * @brief retrieve the key associated to that object. Warning, that's a linear search
255 * Returns NULL if the object cannot be found
257 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
259 for (int i = 0; i <= dict->table_size; i++) {
260 xbt_dictelm_t current = dict->table[i];
261 while (current != NULL) {
262 if (current->content == data)
264 current = current->next;
270 /** @brief retrieve the key associated to that xbt_dictelm_t. */
271 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
277 * \brief Retrieve data from the dict (null-terminated key)
279 * \param dict the dealer of data
280 * \param key the key to find data
281 * \return the data that we are looking for
283 * Search the given \a key. Throws not_found_error when not found.
284 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
286 inline void *xbt_dict_get(xbt_dict_t dict, const char *key)
288 return xbt_dict_get_elm(dict, key)->content;
292 * \brief Retrieve element from the dict (null-terminated key)
294 * \param dict the dealer of data
295 * \param key the key to find data
296 * \return the s_xbt_dictelm_t that we are looking for
298 * Search the given \a key. Throws not_found_error when not found.
299 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
301 inline xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
303 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
306 THROWF(not_found_error, 0, "key %s not found", key);
312 * \brief like xbt_dict_get(), but returning NULL when not found
314 inline void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
316 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
321 return current->content;
325 * \brief like xbt_dict_get_elm(), but returning NULL when not found
327 inline xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
329 unsigned int hash_code = xbt_str_hash(key);
330 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
332 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
333 current = current->next;
338 * \brief Remove data from the dict (arbitrary key)
340 * \param dict the trash can
341 * \param key the key of the data to be removed
342 * \param key_len the size of the \a key
344 * Remove the entry associated with the given \a key (throws not_found)
346 inline void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
348 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
349 xbt_dictelm_t previous = NULL;
350 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
352 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
353 || strncmp(key, current->key, key_len))) {
354 previous = current; /* save the previous node */
355 current = current->next;
359 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
361 if (previous != NULL) {
362 previous->next = current->next;
364 dict->table[hash_code & dict->table_size] = current->next;
367 if (!dict->table[hash_code & dict->table_size])
370 xbt_dictelm_free(dict, current);
375 * \brief Remove data from the dict (null-terminated key)
377 * \param dict the dict
378 * \param key the key of the data to be removed
380 * Remove the entry associated with the given \a key
382 inline void xbt_dict_remove(xbt_dict_t dict, const char *key)
384 xbt_dict_remove_ext(dict, key, strlen(key));
387 /** @brief Remove all data from the dict */
388 void xbt_dict_reset(xbt_dict_t dict)
390 if (dict->count == 0)
393 for (int i = 0; i <= dict->table_size; i++) {
394 xbt_dictelm_t previous = NULL;
395 xbt_dictelm_t current = dict->table[i];
396 while (current != NULL) {
398 current = current->next;
399 xbt_dictelm_free(dict, previous);
401 dict->table[i] = NULL;
409 * \brief Return the number of elements in the dict.
410 * \param dict a dictionary
412 inline int xbt_dict_length(xbt_dict_t dict)
417 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
418 void xbt_dict_dump_output_string(void *s)
424 * \brief test if the dict is empty or not
426 inline int xbt_dict_is_empty(xbt_dict_t dict)
428 return !dict || (xbt_dict_length(dict) == 0);
432 * \brief Outputs the content of the structure (debugging purpose)
434 * \param dict the exibitionist
435 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
437 * Outputs the content of the structure. (for debugging purpose). \a output is a function to output the data. If NULL,
438 * data won't be displayed.
440 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
443 xbt_dictelm_t element;
444 printf("Dict %p:\n", dict);
446 for (i = 0; i < dict->table_size; i++) {
447 element = dict->table[i];
450 while (element != NULL) {
451 printf(" %s -> '", element->key);
452 if (output != NULL) {
453 output(element->content);
456 element = element->next;
466 xbt_dynar_t all_sizes = NULL;
467 /** @brief shows some debugging info about the bucklet repartition */
468 void xbt_dict_dump_sizes(xbt_dict_t dict)
473 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
479 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
481 for (int i = 0; i < dict->table_size; i++) {
482 xbt_dictelm_t element = dict->table[i];
485 while (element != NULL) {
487 element = element->next;
490 if (xbt_dynar_length(sizes) <= size) {
492 xbt_dynar_set(sizes, size, &prevsize);
495 xbt_dynar_get_cpy(sizes, size, &prevsize);
497 xbt_dynar_set(sizes, size, &prevsize);
501 all_sizes = xbt_dynar_new(sizeof(int), NULL);
503 xbt_dynar_foreach(sizes, count, size) {
504 /* Copy values of this one into all_sizes */
506 if (xbt_dynar_length(all_sizes) <= count) {
508 xbt_dynar_set(all_sizes, count, &prevcount);
510 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
512 xbt_dynar_set(all_sizes, count, &prevcount);
515 /* Report current sizes */
520 printf("%uelm x %u cells; ", count, size);
523 xbt_dynar_free(&sizes);
527 * Create the dict mallocators.
528 * This is an internal XBT function called during the lib initialization.
529 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
531 void xbt_dict_preinit(void)
533 if (dict_elm_mallocator == NULL)
534 dict_elm_mallocator = xbt_mallocator_new(
535 256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
536 dict_elm_mallocator_reset_f);
537 if (dict_het_elm_mallocator == NULL)
538 dict_het_elm_mallocator = xbt_mallocator_new(
539 256, dict_het_elm_mallocator_new_f, dict_het_elm_mallocator_free_f,
540 dict_het_elm_mallocator_reset_f);
544 * Destroy the dict mallocators.
545 * This is an internal XBT function during the lib initialization
547 void xbt_dict_postexit(void)
549 if (dict_elm_mallocator != NULL) {
550 xbt_mallocator_free(dict_elm_mallocator);
551 dict_elm_mallocator = NULL;
552 xbt_mallocator_free(dict_het_elm_mallocator);
553 dict_het_elm_mallocator = NULL;
560 printf("Overall stats:");
561 xbt_dynar_foreach(all_sizes, count, size) {
566 printf("%uelm x %d cells; ", count, size);
570 printf("; %f elm per cell\n", avg / (double) total_count);
577 #include "src/internal_config.h"
579 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
581 XBT_TEST_SUITE("dict", "Dict data container");
583 static void debuged_add_ext(xbt_dict_t head, const char *key, const char *data_to_fill, void_f_pvoid_t free_f)
585 char *data = xbt_strdup(data_to_fill);
587 xbt_test_log("Add %s under %s", data_to_fill, key);
589 xbt_dict_set(head, key, data, free_f);
590 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
591 xbt_dict_dump(head, (void (*)(void *)) &printf);
596 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
598 debuged_add_ext(head, key, key, free_f);
601 static void fill(xbt_dict_t * head, int homogeneous)
603 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
605 xbt_test_add("Fill in the dictionnary");
607 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
608 debuged_add(*head, "12", free_f);
609 debuged_add(*head, "12a", free_f);
610 debuged_add(*head, "12b", free_f);
611 debuged_add(*head, "123", free_f);
612 debuged_add(*head, "123456", free_f);
613 /* Child becomes child of what to add */
614 debuged_add(*head, "1234", free_f);
615 /* Need of common ancestor */
616 debuged_add(*head, "123457", free_f);
619 static void search_ext(xbt_dict_t head, const char *key, const char *data)
623 xbt_test_add("Search %s", key);
624 found = xbt_dict_get(head, key);
625 xbt_test_log("Found %s", found);
627 xbt_test_assert(found, "data do not match expectations: found NULL while searching for %s", data);
629 xbt_test_assert(!strcmp(data, found), "data do not match expectations: found %s while searching for %s",
632 xbt_test_assert(!found, "data do not match expectations: found %s while searching for NULL", found);
636 static void search(xbt_dict_t head, const char *key)
638 search_ext(head, key, key);
641 static void debuged_remove(xbt_dict_t head, const char *key)
643 xbt_test_add("Remove '%s'", key);
644 xbt_dict_remove(head, key);
645 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
648 static void traverse(xbt_dict_t head)
650 xbt_dict_cursor_t cursor = NULL;
655 xbt_dict_foreach(head, cursor, key, data) {
656 if (!key || !data || strcmp(key, data)) {
657 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
659 xbt_test_log("Seen #%d: %s", ++i, key);
661 xbt_test_assert(!data || !strcmp(key, data), "Key(%s) != value(%s). Aborting", key, data);
665 static void search_not_found(xbt_dict_t head, const char *data)
670 xbt_test_add("Search %s (expected not to be found)", data);
673 data = xbt_dict_get(head, data);
674 THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
676 if (e.category != not_found_error)
677 xbt_test_exception(e);
681 xbt_test_assert(ok, "Exception not raised");
684 static void count(xbt_dict_t dict, int length)
686 xbt_dict_cursor_t cursor;
691 xbt_test_add("Count elements (expecting %d)", length);
692 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
694 xbt_dict_foreach(dict, cursor, key, data)
697 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
700 static void count_check_get_key(xbt_dict_t dict, int length)
702 xbt_dict_cursor_t cursor;
704 XBT_ATTRIB_UNUSED char *key2;
708 xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
709 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
711 xbt_dict_foreach(dict, cursor, key, data) {
713 key2 = xbt_dict_get_key(dict, data);
714 xbt_assert(!strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
717 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
721 xbt_dict_t head = NULL;
724 static void basic_test(int homogeneous)
726 void_f_pvoid_t free_f;
728 xbt_test_add("Traversal the null dictionary");
731 xbt_test_add("Traversal and search the empty dictionary");
732 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
735 debuged_remove(head, "12346");
737 if (e.category != not_found_error)
738 xbt_test_exception(e);
741 xbt_dict_free(&head);
743 free_f = homogeneous ? NULL : &free;
745 xbt_test_add("Traverse the full dictionary");
746 fill(&head, homogeneous);
747 count_check_get_key(head, 7);
749 debuged_add_ext(head, "toto", "tutu", free_f);
750 search_ext(head, "toto", "tutu");
751 debuged_remove(head, "toto");
756 xbt_test_add("Free the dictionary (twice)");
757 xbt_dict_free(&head);
758 xbt_dict_free(&head);
761 fill(&head, homogeneous);
762 count_check_get_key(head, 7);
763 xbt_test_add("Change 123 to 'Changed 123'");
764 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
765 count_check_get_key(head, 7);
767 xbt_test_add("Change 123 back to '123'");
768 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
769 count_check_get_key(head, 7);
771 xbt_test_add("Change 12a to 'Dummy 12a'");
772 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
773 count_check_get_key(head, 7);
775 xbt_test_add("Change 12a to '12a'");
776 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
777 count_check_get_key(head, 7);
779 xbt_test_add("Traverse the resulting dictionary");
783 xbt_test_add("Search 123");
784 data = xbt_dict_get(head, "123");
785 xbt_test_assert(data);
786 xbt_test_assert(!strcmp("123", data));
788 search_not_found(head, "Can't be found");
789 search_not_found(head, "123 Can't be found");
790 search_not_found(head, "12345678 NOT");
795 search(head, "123456");
796 search(head, "1234");
797 search(head, "123457");
799 xbt_test_add("Traverse the resulting dictionary");
802 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
804 xbt_test_add("Free the dictionary twice");
805 xbt_dict_free(&head);
806 xbt_dict_free(&head);
808 xbt_test_add("Traverse the resulting dictionary");
812 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
817 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
822 static void remove_test(int homogeneous)
824 fill(&head, homogeneous);
826 xbt_test_add("Remove non existing data");
828 debuged_remove(head, "Does not exist");
830 if (e.category != not_found_error)
831 xbt_test_exception(e);
836 xbt_dict_free(&head);
838 xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
839 fill(&head, homogeneous);
840 debuged_remove(head, "12a");
843 debuged_remove(head, "12b");
846 debuged_remove(head, "12");
849 debuged_remove(head, "123456");
853 debuged_remove(head, "12346");
855 if (e.category != not_found_error)
856 xbt_test_exception(e);
860 debuged_remove(head, "1234");
862 debuged_remove(head, "123457");
864 debuged_remove(head, "123");
867 debuged_remove(head, "12346");
869 if (e.category != not_found_error)
870 xbt_test_exception(e);
875 xbt_test_add("Free dict, create new fresh one, and then reset the dict");
876 xbt_dict_free(&head);
877 fill(&head, homogeneous);
878 xbt_dict_reset(head);
882 xbt_test_add("Free the dictionary twice");
883 xbt_dict_free(&head);
884 xbt_dict_free(&head);
887 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
892 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
897 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
901 xbt_test_add("Store NULL under 'null'");
902 xbt_dict_set(head, "null", NULL, NULL);
903 search_ext(head, "null", NULL);
905 xbt_test_add("Check whether I see it while traversing...");
907 xbt_dict_cursor_t cursor = NULL;
911 xbt_dict_foreach(head, cursor, key, data) {
912 if (!key || !data || strcmp(key, data)) {
913 xbt_test_log("Seen: %s->%s", key, data);
915 xbt_test_log("Seen: %s", key);
918 if (!strcmp(key, "null"))
921 xbt_test_assert(found, "the key 'null', associated to NULL is not found");
923 xbt_dict_free(&head);
927 #define SIZEOFKEY 1024
928 static int countelems(xbt_dict_t head)
930 xbt_dict_cursor_t cursor;
935 xbt_dict_foreach(head, cursor, key, data) {
941 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
943 xbt_dict_t head = NULL;
947 srand((unsigned int) time(NULL));
949 for (i = 0; i < 10; i++) {
950 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
951 xbt_test_log("Fill the struct, count its elems and frees the structure");
952 xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
953 head = xbt_dict_new();
954 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
955 for (j = 0; j < 1000; j++) {
957 key = xbt_malloc(SIZEOFKEY);
960 for (k = 0; k < SIZEOFKEY - 1; k++)
961 key[k] = rand() % ('z' - 'a') + 'a';
963 /* printf("[%d %s]\n",j,key); */
964 data = xbt_dict_get_or_null(head, key);
965 } while (data != NULL);
967 xbt_dict_set(head, key, key, &free);
968 data = xbt_dict_get(head, key);
969 xbt_test_assert(!strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
973 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
975 xbt_dict_free(&head);
976 xbt_dict_free(&head);
979 head = xbt_dict_new();
980 xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
981 for (j = 0; j < NB_ELM; j++) {
982 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
983 key = xbt_malloc(10);
985 sprintf(key, "%d", j);
986 xbt_dict_set(head, key, key, &free);
988 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
990 xbt_test_add("Count the elements (retrieving the key and data for each)");
991 i = countelems(head);
992 xbt_test_log("There is %d elements", i);
994 xbt_test_add("Search my %d elements 20 times", NB_ELM);
995 key = xbt_malloc(10);
996 for (i = 0; i < 20; i++) {
998 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
999 for (j = 0; j < NB_ELM; j++) {
1000 sprintf(key, "%d", j);
1001 data = xbt_dict_get(head, key);
1002 xbt_test_assert(!strcmp(key, (char *) data), "with get, key=%s != data=%s", key, (char *) data);
1003 data = xbt_dict_get_ext(head, key, strlen(key));
1004 xbt_test_assert(!strcmp(key, (char *) data), "with get_ext, key=%s != data=%s", key, (char *) data);
1009 xbt_test_add("Remove my %d elements", NB_ELM);
1010 key = xbt_malloc(10);
1011 for (j = 0; j < NB_ELM; j++) {
1012 /* if (!(j%10000)) printf("."); fflush(stdout); */
1013 sprintf(key, "%d", j);
1014 xbt_dict_remove(head, key);
1018 xbt_test_add("Free the structure (twice)");
1019 xbt_dict_free(&head);
1020 xbt_dict_free(&head);
1023 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1025 xbt_dict_t dict = xbt_dict_new();
1028 xbt_test_add("Insert elements");
1030 for (i = 0; i < count; ++i)
1031 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1032 xbt_test_assert(xbt_dict_size(dict) == count, "Bad number of elements in the dictionnary");
1034 xbt_test_add("Check elements");
1035 for (i = 0; i < count; ++i) {
1036 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1037 xbt_test_assert(xbt_dict_size(dict) == count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
1040 xbt_test_add("Free the array");
1041 xbt_dict_free(&dict);
1043 #endif /* SIMGRID_TEST */