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)
49 dict = xbt_new(s_xbt_dict_t, 1);
50 dict->free_f = free_ctn;
51 dict->table_size = 127;
52 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
55 dict->homogeneous = 1;
62 * \param dict the dictionary to be freed
64 * Frees a dictionary with all the data
66 void xbt_dict_free(xbt_dict_t * dict)
69 xbt_dictelm_t current, previous;
73 // if ( *dict ) xbt_dict_dump_sizes(*dict);
75 if (dict != NULL && *dict != NULL) {
76 table_size = (*dict)->table_size;
77 table = (*dict)->table;
78 /* Warning: the size of the table is 'table_size+1'...
79 * This is because table_size is used as a binary mask in xbt_dict_rehash */
80 for (i = 0; (*dict)->count && i <= table_size; i++) {
82 while (current != NULL) {
84 current = current->next;
85 xbt_dictelm_free(*dict, previous);
95 /** Returns the amount of elements in the dict */
96 inline unsigned int xbt_dict_size(xbt_dict_t dict)
98 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
101 /* Expend the size of the dict */
102 static void xbt_dict_rehash(xbt_dict_t dict)
104 const int oldsize = dict->table_size + 1;
105 int newsize = oldsize * 2;
107 xbt_dictelm_t *currcell;
108 xbt_dictelm_t *twincell;
109 xbt_dictelm_t bucklet;
110 xbt_dictelm_t *pprev;
112 currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
113 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
114 dict->table_size = --newsize;
115 dict->table = currcell;
116 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
118 for (i = 0; i < oldsize; i++, currcell++) {
119 if (!*currcell) /* empty cell */
121 twincell = currcell + oldsize;
122 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
123 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
124 - stay in cell i (ie, currcell)
125 - go to the cell i+oldsize (ie, twincell) */
126 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
127 *pprev = bucklet->next;
128 bucklet->next = *twincell;
134 pprev = &bucklet->next;
138 if (!*currcell) /* everything moved */
144 * \brief Add data to the dict (arbitrary key)
145 * \param dict the container
146 * \param key the key to set the new data
147 * \param key_len the size of the \a key
148 * \param data the data to add in the dict
149 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
150 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
152 * 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
155 inline void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn)
157 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
159 xbt_dictelm_t current, previous = NULL;
161 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
162 dict->table_size, hash_code & dict->table_size);
163 current = dict->table[hash_code & dict->table_size];
164 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
165 || memcmp(key, current->key, key_len))) {
167 current = current->next;
170 if (current == NULL) {
171 /* this key doesn't exist yet */
172 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
174 if (previous == NULL) {
175 dict->table[hash_code & dict->table_size] = current;
177 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
178 xbt_dict_rehash(dict);
180 previous->next = current;
183 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
184 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
185 /* there is already an element with the same key: overwrite it */
186 xbt_dictelm_set_data(dict, current, data, free_ctn);
191 * \brief Add data to the dict (null-terminated key)
193 * \param dict the dict
194 * \param key the key to set the new data
195 * \param data the data to add in the dict
196 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
197 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
199 * set the \a data in the structure under the \a key, which is anull terminated string.
201 inline void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
203 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
207 * \brief Retrieve data from the dict (arbitrary key)
209 * \param dict the dealer of data
210 * \param key the key to find data
211 * \param key_len the size of the \a key
212 * \return the data that we are looking for
214 * Search the given \a key. Throws not_found_error when not found.
216 inline void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
218 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
219 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
221 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
222 || memcmp(key, current->key, key_len))) {
223 current = current->next;
227 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
229 return current->content;
232 /** @brief like xbt_dict_get_ext(), but returning NULL when not found */
233 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
235 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
236 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
238 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
239 || memcmp(key, current->key, key_len))) {
240 current = current->next;
246 return current->content;
250 * @brief retrieve the key associated to that object. Warning, that's a linear search
252 * Returns NULL if the object cannot be found
254 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
256 for (int i = 0; i <= dict->table_size; i++) {
257 xbt_dictelm_t current = dict->table[i];
258 while (current != NULL) {
259 if (current->content == data)
261 current = current->next;
267 /** @brief retrieve the key associated to that xbt_dictelm_t. */
268 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
274 * \brief Retrieve data from the dict (null-terminated key)
276 * \param dict the dealer of data
277 * \param key the key to find data
278 * \return the data that we are looking for
280 * Search the given \a key. Throws not_found_error when not found.
281 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
283 inline void *xbt_dict_get(xbt_dict_t dict, const char *key)
285 return xbt_dict_get_elm(dict, key)->content;
289 * \brief Retrieve element from the dict (null-terminated key)
291 * \param dict the dealer of data
292 * \param key the key to find data
293 * \return the s_xbt_dictelm_t that we are looking for
295 * Search the given \a key. Throws not_found_error when not found.
296 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
298 inline xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
300 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
303 THROWF(not_found_error, 0, "key %s not found", key);
309 * \brief like xbt_dict_get(), but returning NULL when not found
311 inline void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
313 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
318 return current->content;
322 * \brief like xbt_dict_get_elm(), but returning NULL when not found
324 inline xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
326 unsigned int hash_code = xbt_str_hash(key);
327 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
329 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
330 current = current->next;
335 * \brief Remove data from the dict (arbitrary key)
337 * \param dict the trash can
338 * \param key the key of the data to be removed
339 * \param key_len the size of the \a key
341 * Remove the entry associated with the given \a key (throws not_found)
343 inline void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
345 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
346 xbt_dictelm_t previous = NULL;
347 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
349 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
350 || strncmp(key, current->key, key_len))) {
351 previous = current; /* save the previous node */
352 current = current->next;
356 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
358 if (previous != NULL) {
359 previous->next = current->next;
361 dict->table[hash_code & dict->table_size] = current->next;
364 if (!dict->table[hash_code & dict->table_size])
367 xbt_dictelm_free(dict, current);
372 * \brief Remove data from the dict (null-terminated key)
374 * \param dict the dict
375 * \param key the key of the data to be removed
377 * Remove the entry associated with the given \a key
379 inline void xbt_dict_remove(xbt_dict_t dict, const char *key)
381 xbt_dict_remove_ext(dict, key, strlen(key));
384 /** @brief Remove all data from the dict */
385 void xbt_dict_reset(xbt_dict_t dict)
387 if (dict->count == 0)
390 for (int i = 0; i <= dict->table_size; i++) {
391 xbt_dictelm_t previous = NULL;
392 xbt_dictelm_t current = dict->table[i];
393 while (current != NULL) {
395 current = current->next;
396 xbt_dictelm_free(dict, previous);
398 dict->table[i] = NULL;
406 * \brief Return the number of elements in the dict.
407 * \param dict a dictionary
409 inline int xbt_dict_length(xbt_dict_t dict)
414 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
415 void xbt_dict_dump_output_string(void *s)
421 * \brief test if the dict is empty or not
423 inline int xbt_dict_is_empty(xbt_dict_t dict)
425 return !dict || (xbt_dict_length(dict) == 0);
429 * \brief Outputs the content of the structure (debugging purpose)
431 * \param dict the exibitionist
432 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
434 * Outputs the content of the structure. (for debugging purpose). \a output is a function to output the data. If NULL,
435 * data won't be displayed.
437 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
440 xbt_dictelm_t element;
441 printf("Dict %p:\n", dict);
443 for (i = 0; i < dict->table_size; i++) {
444 element = dict->table[i];
447 while (element != NULL) {
448 printf(" %s -> '", element->key);
449 if (output != NULL) {
450 output(element->content);
453 element = element->next;
463 xbt_dynar_t all_sizes = NULL;
464 /** @brief shows some debugging info about the bucklet repartition */
465 void xbt_dict_dump_sizes(xbt_dict_t dict)
470 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
476 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
478 for (int i = 0; i < dict->table_size; i++) {
479 xbt_dictelm_t element = dict->table[i];
482 while (element != NULL) {
484 element = element->next;
487 if (xbt_dynar_length(sizes) <= size) {
489 xbt_dynar_set(sizes, size, &prevsize);
492 xbt_dynar_get_cpy(sizes, size, &prevsize);
494 xbt_dynar_set(sizes, size, &prevsize);
498 all_sizes = xbt_dynar_new(sizeof(int), NULL);
500 xbt_dynar_foreach(sizes, count, size) {
501 /* Copy values of this one into all_sizes */
503 if (xbt_dynar_length(all_sizes) <= count) {
505 xbt_dynar_set(all_sizes, count, &prevcount);
507 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
509 xbt_dynar_set(all_sizes, count, &prevcount);
512 /* Report current sizes */
517 printf("%uelm x %u cells; ", count, size);
520 xbt_dynar_free(&sizes);
524 * Create the dict mallocators.
525 * This is an internal XBT function called during the lib initialization.
526 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
528 void xbt_dict_preinit(void)
530 dict_elm_mallocator = xbt_mallocator_new(256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
531 dict_elm_mallocator_reset_f);
532 dict_het_elm_mallocator = xbt_mallocator_new(256, dict_het_elm_mallocator_new_f, dict_het_elm_mallocator_free_f,
533 dict_het_elm_mallocator_reset_f);
537 * Destroy the dict mallocators.
538 * This is an internal XBT function during the lib initialization
540 void xbt_dict_postexit(void)
542 if (dict_elm_mallocator != NULL) {
543 xbt_mallocator_free(dict_elm_mallocator);
544 dict_elm_mallocator = NULL;
545 xbt_mallocator_free(dict_het_elm_mallocator);
546 dict_het_elm_mallocator = NULL;
553 printf("Overall stats:");
554 xbt_dynar_foreach(all_sizes, count, size) {
559 printf("%uelm x %d cells; ", count, size);
563 printf("; %f elm per cell\n", avg / (double) total_count);
570 #include "src/internal_config.h"
572 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dict);
574 XBT_TEST_SUITE("dict", "Dict data container");
576 static void debuged_add_ext(xbt_dict_t head, const char *key, const char *data_to_fill, void_f_pvoid_t free_f)
578 char *data = xbt_strdup(data_to_fill);
580 xbt_test_log("Add %s under %s", data_to_fill, key);
582 xbt_dict_set(head, key, data, free_f);
583 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
584 xbt_dict_dump(head, (void (*)(void *)) &printf);
589 static void debuged_add(xbt_dict_t head, const char *key, void_f_pvoid_t free_f)
591 debuged_add_ext(head, key, key, free_f);
594 static void fill(xbt_dict_t * head, int homogeneous)
596 void_f_pvoid_t free_f = homogeneous ? NULL : &free;
598 xbt_test_add("Fill in the dictionnary");
600 *head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
601 debuged_add(*head, "12", free_f);
602 debuged_add(*head, "12a", free_f);
603 debuged_add(*head, "12b", free_f);
604 debuged_add(*head, "123", free_f);
605 debuged_add(*head, "123456", free_f);
606 /* Child becomes child of what to add */
607 debuged_add(*head, "1234", free_f);
608 /* Need of common ancestor */
609 debuged_add(*head, "123457", free_f);
612 static void search_ext(xbt_dict_t head, const char *key, const char *data)
616 xbt_test_add("Search %s", key);
617 found = xbt_dict_get(head, key);
618 xbt_test_log("Found %s", found);
620 xbt_test_assert(found, "data do not match expectations: found NULL while searching for %s", data);
622 xbt_test_assert(!strcmp(data, found), "data do not match expectations: found %s while searching for %s",
625 xbt_test_assert(!found, "data do not match expectations: found %s while searching for NULL", found);
629 static void search(xbt_dict_t head, const char *key)
631 search_ext(head, key, key);
634 static void debuged_remove(xbt_dict_t head, const char *key)
636 xbt_test_add("Remove '%s'", key);
637 xbt_dict_remove(head, key);
638 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
641 static void traverse(xbt_dict_t head)
643 xbt_dict_cursor_t cursor = NULL;
648 xbt_dict_foreach(head, cursor, key, data) {
649 if (!key || !data || strcmp(key, data)) {
650 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
652 xbt_test_log("Seen #%d: %s", ++i, key);
654 xbt_test_assert(!data || !strcmp(key, data), "Key(%s) != value(%s). Aborting", key, data);
658 static void search_not_found(xbt_dict_t head, const char *data)
663 xbt_test_add("Search %s (expected not to be found)", data);
666 data = xbt_dict_get(head, data);
667 THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
669 if (e.category != not_found_error)
670 xbt_test_exception(e);
674 xbt_test_assert(ok, "Exception not raised");
677 static void count(xbt_dict_t dict, int length)
679 xbt_dict_cursor_t cursor;
684 xbt_test_add("Count elements (expecting %d)", length);
685 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
687 xbt_dict_foreach(dict, cursor, key, data)
690 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
693 static void count_check_get_key(xbt_dict_t dict, int length)
695 xbt_dict_cursor_t cursor;
697 XBT_ATTRIB_UNUSED char *key2;
701 xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
702 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
704 xbt_dict_foreach(dict, cursor, key, data) {
706 key2 = xbt_dict_get_key(dict, data);
707 xbt_assert(!strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
710 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
714 xbt_dict_t head = NULL;
717 static void basic_test(int homogeneous)
719 void_f_pvoid_t free_f;
721 xbt_test_add("Traversal the null dictionary");
724 xbt_test_add("Traversal and search the empty dictionary");
725 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
728 debuged_remove(head, "12346");
730 if (e.category != not_found_error)
731 xbt_test_exception(e);
734 xbt_dict_free(&head);
736 free_f = homogeneous ? NULL : &free;
738 xbt_test_add("Traverse the full dictionary");
739 fill(&head, homogeneous);
740 count_check_get_key(head, 7);
742 debuged_add_ext(head, "toto", "tutu", free_f);
743 search_ext(head, "toto", "tutu");
744 debuged_remove(head, "toto");
749 xbt_test_add("Free the dictionary (twice)");
750 xbt_dict_free(&head);
751 xbt_dict_free(&head);
754 fill(&head, homogeneous);
755 count_check_get_key(head, 7);
756 xbt_test_add("Change 123 to 'Changed 123'");
757 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
758 count_check_get_key(head, 7);
760 xbt_test_add("Change 123 back to '123'");
761 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
762 count_check_get_key(head, 7);
764 xbt_test_add("Change 12a to 'Dummy 12a'");
765 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
766 count_check_get_key(head, 7);
768 xbt_test_add("Change 12a to '12a'");
769 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
770 count_check_get_key(head, 7);
772 xbt_test_add("Traverse the resulting dictionary");
776 xbt_test_add("Search 123");
777 data = xbt_dict_get(head, "123");
778 xbt_test_assert(data);
779 xbt_test_assert(!strcmp("123", data));
781 search_not_found(head, "Can't be found");
782 search_not_found(head, "123 Can't be found");
783 search_not_found(head, "12345678 NOT");
788 search(head, "123456");
789 search(head, "1234");
790 search(head, "123457");
792 xbt_test_add("Traverse the resulting dictionary");
795 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
797 xbt_test_add("Free the dictionary twice");
798 xbt_dict_free(&head);
799 xbt_dict_free(&head);
801 xbt_test_add("Traverse the resulting dictionary");
805 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
810 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
815 static void remove_test(int homogeneous)
817 fill(&head, homogeneous);
819 xbt_test_add("Remove non existing data");
821 debuged_remove(head, "Does not exist");
823 if (e.category != not_found_error)
824 xbt_test_exception(e);
829 xbt_dict_free(&head);
831 xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
832 fill(&head, homogeneous);
833 debuged_remove(head, "12a");
836 debuged_remove(head, "12b");
839 debuged_remove(head, "12");
842 debuged_remove(head, "123456");
846 debuged_remove(head, "12346");
848 if (e.category != not_found_error)
849 xbt_test_exception(e);
853 debuged_remove(head, "1234");
855 debuged_remove(head, "123457");
857 debuged_remove(head, "123");
860 debuged_remove(head, "12346");
862 if (e.category != not_found_error)
863 xbt_test_exception(e);
868 xbt_test_add("Free dict, create new fresh one, and then reset the dict");
869 xbt_dict_free(&head);
870 fill(&head, homogeneous);
871 xbt_dict_reset(head);
875 xbt_test_add("Free the dictionary twice");
876 xbt_dict_free(&head);
877 xbt_dict_free(&head);
880 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
885 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
890 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
894 xbt_test_add("Store NULL under 'null'");
895 xbt_dict_set(head, "null", NULL, NULL);
896 search_ext(head, "null", NULL);
898 xbt_test_add("Check whether I see it while traversing...");
900 xbt_dict_cursor_t cursor = NULL;
904 xbt_dict_foreach(head, cursor, key, data) {
905 if (!key || !data || strcmp(key, data)) {
906 xbt_test_log("Seen: %s->%s", key, data);
908 xbt_test_log("Seen: %s", key);
911 if (!strcmp(key, "null"))
914 xbt_test_assert(found, "the key 'null', associated to NULL is not found");
916 xbt_dict_free(&head);
920 #define SIZEOFKEY 1024
921 static int countelems(xbt_dict_t head)
923 xbt_dict_cursor_t cursor;
928 xbt_dict_foreach(head, cursor, key, data) {
934 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
936 xbt_dict_t head = NULL;
940 srand((unsigned int) time(NULL));
942 for (i = 0; i < 10; i++) {
943 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
944 xbt_test_log("Fill the struct, count its elems and frees the structure");
945 xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
946 head = xbt_dict_new();
947 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
948 for (j = 0; j < 1000; j++) {
950 key = xbt_malloc(SIZEOFKEY);
953 for (k = 0; k < SIZEOFKEY - 1; k++)
954 key[k] = rand() % ('z' - 'a') + 'a';
956 /* printf("[%d %s]\n",j,key); */
957 data = xbt_dict_get_or_null(head, key);
958 } while (data != NULL);
960 xbt_dict_set(head, key, key, &free);
961 data = xbt_dict_get(head, key);
962 xbt_test_assert(!strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
966 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
968 xbt_dict_free(&head);
969 xbt_dict_free(&head);
972 head = xbt_dict_new();
973 xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
974 for (j = 0; j < NB_ELM; j++) {
975 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
976 key = xbt_malloc(10);
978 sprintf(key, "%d", j);
979 xbt_dict_set(head, key, key, &free);
981 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
983 xbt_test_add("Count the elements (retrieving the key and data for each)");
984 i = countelems(head);
985 xbt_test_log("There is %d elements", i);
987 xbt_test_add("Search my %d elements 20 times", NB_ELM);
988 key = xbt_malloc(10);
989 for (i = 0; i < 20; i++) {
991 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
992 for (j = 0; j < NB_ELM; j++) {
993 sprintf(key, "%d", j);
994 data = xbt_dict_get(head, key);
995 xbt_test_assert(!strcmp(key, (char *) data), "with get, key=%s != data=%s", key, (char *) data);
996 data = xbt_dict_get_ext(head, key, strlen(key));
997 xbt_test_assert(!strcmp(key, (char *) data), "with get_ext, key=%s != data=%s", key, (char *) data);
1002 xbt_test_add("Remove my %d elements", NB_ELM);
1003 key = xbt_malloc(10);
1004 for (j = 0; j < NB_ELM; j++) {
1005 /* if (!(j%10000)) printf("."); fflush(stdout); */
1006 sprintf(key, "%d", j);
1007 xbt_dict_remove(head, key);
1011 xbt_test_add("Free the structure (twice)");
1012 xbt_dict_free(&head);
1013 xbt_dict_free(&head);
1016 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1018 xbt_dict_t dict = xbt_dict_new();
1021 xbt_test_add("Insert elements");
1023 for (i = 0; i < count; ++i)
1024 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1025 xbt_test_assert(xbt_dict_size(dict) == count, "Bad number of elements in the dictionnary");
1027 xbt_test_add("Check elements");
1028 for (i = 0; i < count; ++i) {
1029 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1030 xbt_test_assert(xbt_dict_size(dict) == count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
1033 xbt_test_add("Free the array");
1034 xbt_dict_free(&dict);
1036 #endif /* SIMGRID_TEST */