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. */
14 #include "xbt/mallocator.h"
15 #include "src/xbt_modinter.h"
17 #include "dict_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt, "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 function.
29 xbt_dict_t xbt_dict_new(void)
31 xbt_dict_t dict = xbt_dict_new_homogeneous(NULL);
32 dict->homogeneous = 0;
39 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary
40 * \return pointer to the destination
41 * \see xbt_dict_new(), xbt_dict_free()
43 * Creates and initialize a new dictionary with a default hashtable size.
44 * The dictionary is homogeneous: each element share the same free function.
46 xbt_dict_t xbt_dict_new_homogeneous(void_f_pvoid_t free_ctn)
48 if (dict_elm_mallocator == NULL)
53 dict = xbt_new(s_xbt_dict_t, 1);
54 dict->free_f = free_ctn;
55 dict->table_size = 127;
56 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
59 dict->homogeneous = 1;
66 * \param dict the dictionary to be freed
68 * Frees a dictionary with all the data
70 void xbt_dict_free(xbt_dict_t * dict)
73 xbt_dictelm_t current, previous;
77 // if ( *dict ) xbt_dict_dump_sizes(*dict);
79 if (dict != NULL && *dict != NULL) {
80 table_size = (*dict)->table_size;
81 table = (*dict)->table;
82 /* Warning: the size of the table is 'table_size+1'...
83 * This is because table_size is used as a binary mask in xbt_dict_rehash */
84 for (i = 0; (*dict)->count && i <= table_size; i++) {
86 while (current != NULL) {
88 current = current->next;
89 xbt_dictelm_free(*dict, previous);
99 /** Returns the amount of elements in the dict */
100 unsigned int xbt_dict_size(xbt_dict_t dict)
102 return (dict ? (unsigned int) dict->count : (unsigned int) 0);
105 /* Expend the size of the dict */
106 static void xbt_dict_rehash(xbt_dict_t dict)
108 const unsigned oldsize = dict->table_size + 1;
109 unsigned newsize = oldsize * 2;
111 xbt_dictelm_t *currcell = (xbt_dictelm_t *) xbt_realloc((char *) dict->table, newsize * sizeof(xbt_dictelm_t));
112 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
113 dict->table_size = --newsize;
114 dict->table = currcell;
115 XBT_DEBUG("REHASH (%d->%d)", oldsize, newsize);
117 for (unsigned i = 0; i < oldsize; i++, currcell++) {
118 if (!*currcell) /* empty cell */
121 xbt_dictelm_t *twincell = currcell + oldsize;
122 xbt_dictelm_t *pprev = currcell;
123 xbt_dictelm_t bucklet = *currcell;
124 for (; bucklet != nullptr; bucklet = *pprev) {
125 /* Since we use "& size" instead of "%size" and since the size was doubled, each bucklet of this cell must either:
126 - stay in cell i (ie, currcell)
127 - go to the cell i+oldsize (ie, twincell) */
128 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
129 *pprev = bucklet->next;
130 bucklet->next = *twincell;
136 pprev = &bucklet->next;
140 if (!*currcell) /* everything moved */
146 * \brief Add data to the dict (arbitrary key)
147 * \param dict the container
148 * \param key the key to set the new data
149 * \param key_len the size of the \a key
150 * \param data the data to add in the dict
151 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
152 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
154 * 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
157 void xbt_dict_set_ext(xbt_dict_t dict, const char *key, int key_len, void *data, void_f_pvoid_t free_ctn)
159 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
161 xbt_dictelm_t current, previous = NULL;
163 XBT_CDEBUG(xbt_dict, "ADD %.*s hash = %u, size = %d, & = %u", key_len, key, hash_code,
164 dict->table_size, hash_code & dict->table_size);
165 current = dict->table[hash_code & dict->table_size];
166 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
167 || memcmp(key, current->key, key_len))) {
169 current = current->next;
172 if (current == NULL) {
173 /* this key doesn't exist yet */
174 current = xbt_dictelm_new(dict, key, key_len, hash_code, data, free_ctn);
176 if (previous == NULL) {
177 dict->table[hash_code & dict->table_size] = current;
179 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
180 xbt_dict_rehash(dict);
182 previous->next = current;
185 XBT_CDEBUG(xbt_dict, "Replace %.*s by %.*s under key %.*s",
186 key_len, (char *) current->content, key_len, (char *) data, key_len, (char *) key);
187 /* there is already an element with the same key: overwrite it */
188 xbt_dictelm_set_data(dict, current, data, free_ctn);
193 * \brief Add data to the dict (null-terminated key)
195 * \param dict the dict
196 * \param key the key to set the new data
197 * \param data the data to add in the dict
198 * \param free_ctn function to call with (\a data as argument) when \a data is removed from the dictionary. This param
199 * will only be considered when the dict was instantiated with xbt_dict_new() and not xbt_dict_new_homogeneous()
201 * set the \a data in the structure under the \a key, which is anull terminated string.
203 void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t free_ctn)
205 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
209 * \brief Retrieve data from the dict (arbitrary key)
211 * \param dict the dealer of data
212 * \param key the key to find data
213 * \param key_len the size of the \a key
214 * \return the data that we are looking for
216 * Search the given \a key. Throws not_found_error when not found.
218 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
220 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
221 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
223 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
224 || memcmp(key, current->key, key_len))) {
225 current = current->next;
229 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
231 return current->content;
234 /** @brief like xbt_dict_get_ext(), but returning NULL when not found */
235 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
237 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
238 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
240 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
241 || memcmp(key, current->key, key_len))) {
242 current = current->next;
248 return current->content;
252 * @brief retrieve the key associated to that object. Warning, that's a linear search
254 * Returns NULL if the object cannot be found
256 char *xbt_dict_get_key(xbt_dict_t dict, const void *data)
258 for (int i = 0; i <= dict->table_size; i++) {
259 xbt_dictelm_t current = dict->table[i];
260 while (current != NULL) {
261 if (current->content == data)
263 current = current->next;
269 /** @brief retrieve the key associated to that xbt_dictelm_t. */
270 char *xbt_dict_get_elm_key(xbt_dictelm_t elm)
276 * \brief Retrieve data from the dict (null-terminated key)
278 * \param dict the dealer of data
279 * \param key the key to find data
280 * \return the data that we are looking for
282 * Search the given \a key. Throws not_found_error when not found.
283 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
285 void *xbt_dict_get(xbt_dict_t dict, const char *key)
287 return xbt_dict_get_elm(dict, key)->content;
291 * \brief Retrieve element from the dict (null-terminated key)
293 * \param dict the dealer of data
294 * \param key the key to find data
295 * \return the s_xbt_dictelm_t that we are looking for
297 * Search the given \a key. Throws not_found_error when not found.
298 * Check xbt_dict_get_or_null() for a version returning NULL without exception when not found.
300 xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key)
302 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
305 THROWF(not_found_error, 0, "key %s not found", key);
311 * \brief like xbt_dict_get(), but returning NULL when not found
313 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
315 xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key);
320 return current->content;
324 * \brief like xbt_dict_get_elm(), but returning NULL when not found
326 xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key)
328 unsigned int hash_code = xbt_str_hash(key);
329 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
331 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
332 current = current->next;
337 * \brief Remove data from the dict (arbitrary key)
339 * \param dict the trash can
340 * \param key the key of the data to be removed
341 * \param key_len the size of the \a key
343 * Remove the entry associated with the given \a key (throws not_found)
345 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
347 unsigned int hash_code = xbt_str_hash_ext(key, key_len);
348 xbt_dictelm_t previous = NULL;
349 xbt_dictelm_t current = dict->table[hash_code & dict->table_size];
351 while (current != NULL && (hash_code != current->hash_code || key_len != current->key_len
352 || strncmp(key, current->key, key_len))) {
353 previous = current; /* save the previous node */
354 current = current->next;
358 THROWF(not_found_error, 0, "key %.*s not found", key_len, key);
360 if (previous != NULL) {
361 previous->next = current->next;
363 dict->table[hash_code & dict->table_size] = current->next;
366 if (!dict->table[hash_code & dict->table_size])
369 xbt_dictelm_free(dict, current);
374 * \brief Remove data from the dict (null-terminated key)
376 * \param dict the dict
377 * \param key the key of the data to be removed
379 * Remove the entry associated with the given \a key
381 void xbt_dict_remove(xbt_dict_t dict, const char *key)
383 xbt_dict_remove_ext(dict, key, strlen(key));
386 /** @brief Remove all data from the dict */
387 void xbt_dict_reset(xbt_dict_t dict)
389 if (dict->count == 0)
392 for (int i = 0; i <= dict->table_size; i++) {
393 xbt_dictelm_t previous = NULL;
394 xbt_dictelm_t current = dict->table[i];
395 while (current != NULL) {
397 current = current->next;
398 xbt_dictelm_free(dict, previous);
400 dict->table[i] = NULL;
408 * \brief Return the number of elements in the dict.
409 * \param dict a dictionary
411 int xbt_dict_length(xbt_dict_t dict)
416 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
417 void xbt_dict_dump_output_string(void *s)
419 fputs((char*) s, stdout);
423 * \brief test if the dict is empty or not
425 int xbt_dict_is_empty(xbt_dict_t dict)
427 return !dict || (xbt_dict_length(dict) == 0);
431 * \brief Outputs the content of the structure (debugging purpose)
433 * \param dict the exibitionist
434 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
436 * Outputs the content of the structure. (for debugging purpose). \a output is a function to output the data. If NULL,
437 * data won't be displayed.
439 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
442 xbt_dictelm_t element;
443 printf("Dict %p:\n", dict);
445 for (i = 0; i < dict->table_size; i++) {
446 element = dict->table[i];
449 while (element != NULL) {
450 printf(" %s -> '", element->key);
451 if (output != NULL) {
452 output(element->content);
455 element = element->next;
465 xbt_dynar_t all_sizes = NULL;
466 /** @brief shows some debugging info about the bucklet repartition */
467 void xbt_dict_dump_sizes(xbt_dict_t dict)
472 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill, dict->table_size);
478 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
480 for (int i = 0; i < dict->table_size; i++) {
481 xbt_dictelm_t element = dict->table[i];
484 while (element != NULL) {
486 element = element->next;
489 if (xbt_dynar_length(sizes) <= size) {
491 xbt_dynar_set(sizes, size, &prevsize);
494 xbt_dynar_get_cpy(sizes, size, &prevsize);
496 xbt_dynar_set(sizes, size, &prevsize);
500 all_sizes = xbt_dynar_new(sizeof(int), NULL);
502 xbt_dynar_foreach(sizes, count, size) {
503 /* Copy values of this one into all_sizes */
505 if (xbt_dynar_length(all_sizes) <= count) {
507 xbt_dynar_set(all_sizes, count, &prevcount);
509 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
511 xbt_dynar_set(all_sizes, count, &prevcount);
514 /* Report current sizes */
519 printf("%uelm x %u cells; ", count, size);
522 xbt_dynar_free(&sizes);
526 * Create the dict mallocators.
527 * This is an internal XBT function called during the lib initialization.
528 * It can be used several times to recreate the mallocator, for example when you switch to MC mode
530 void xbt_dict_preinit(void)
532 if (dict_elm_mallocator == NULL)
533 dict_elm_mallocator = xbt_mallocator_new(
534 256, dict_elm_mallocator_new_f, dict_elm_mallocator_free_f,
535 dict_elm_mallocator_reset_f);
536 if (dict_het_elm_mallocator == NULL)
537 dict_het_elm_mallocator = xbt_mallocator_new(
538 256, dict_het_elm_mallocator_new_f, dict_het_elm_mallocator_free_f,
539 dict_het_elm_mallocator_reset_f);
543 * Destroy the dict mallocators.
544 * This is an internal XBT function during the lib initialization
546 void xbt_dict_postexit(void)
548 if (dict_elm_mallocator != NULL) {
549 xbt_mallocator_free(dict_elm_mallocator);
550 dict_elm_mallocator = NULL;
551 xbt_mallocator_free(dict_het_elm_mallocator);
552 dict_het_elm_mallocator = NULL;
559 printf("Overall stats:");
560 xbt_dynar_foreach(all_sizes, count, size) {
565 printf("%uelm x %d cells; ", count, size);
569 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)
621 xbt_test_add("Search %s", key);
622 char *found = (char*) xbt_dict_get(head, key);
623 xbt_test_log("Found %s", found);
625 xbt_test_assert(found, "data do not match expectations: found NULL while searching for %s", data);
627 xbt_test_assert(!strcmp(data, found), "data do not match expectations: found %s while searching for %s",
630 xbt_test_assert(!found, "data do not match expectations: found %s while searching for NULL", found);
634 static void search(xbt_dict_t head, const char *key)
636 search_ext(head, key, key);
639 static void debuged_remove(xbt_dict_t head, const char *key)
641 xbt_test_add("Remove '%s'", key);
642 xbt_dict_remove(head, key);
643 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
646 static void traverse(xbt_dict_t head)
648 xbt_dict_cursor_t cursor = NULL;
653 xbt_dict_foreach(head, cursor, key, data) {
654 if (!key || !data || strcmp(key, data)) {
655 xbt_test_log("Seen #%d: %s->%s", ++i, key, data);
657 xbt_test_log("Seen #%d: %s", ++i, key);
659 xbt_test_assert(!data || !strcmp(key, data), "Key(%s) != value(%s). Aborting", key, data);
663 static void search_not_found(xbt_dict_t head, const char *data)
666 xbt_test_add("Search %s (expected not to be found)", data);
669 data = (const char*) xbt_dict_get(head, data);
670 THROWF(unknown_error, 0, "Found something which shouldn't be there (%s)", data);
673 if (e.category != not_found_error)
674 xbt_test_exception(e);
677 xbt_test_assert(ok, "Exception not raised");
680 static void count(xbt_dict_t dict, int length)
682 xbt_dict_cursor_t cursor;
687 xbt_test_add("Count elements (expecting %d)", length);
688 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
690 xbt_dict_foreach(dict, cursor, key, data)
693 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
696 static void count_check_get_key(xbt_dict_t dict, int length)
698 xbt_dict_cursor_t cursor;
700 XBT_ATTRIB_UNUSED char *key2;
704 xbt_test_add("Count elements (expecting %d), and test the getkey function", length);
705 xbt_test_assert(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
707 xbt_dict_foreach(dict, cursor, key, data) {
709 key2 = xbt_dict_get_key(dict, data);
710 xbt_assert(!strcmp(key, key2), "The data was registered under %s instead of %s as expected", key2, key);
713 xbt_test_assert(effective == length, "Effective length(%d) != %d.", effective, length);
717 xbt_dict_t head = NULL;
720 static void basic_test(int homogeneous)
722 void_f_pvoid_t free_f;
724 xbt_test_add("Traversal the null dictionary");
727 xbt_test_add("Traversal and search the empty dictionary");
728 head = homogeneous ? xbt_dict_new_homogeneous(&free) : xbt_dict_new();
731 debuged_remove(head, "12346");
734 if (e.category != not_found_error)
735 xbt_test_exception(e);
737 xbt_dict_free(&head);
739 free_f = homogeneous ? NULL : &free;
741 xbt_test_add("Traverse the full dictionary");
742 fill(&head, homogeneous);
743 count_check_get_key(head, 7);
745 debuged_add_ext(head, "toto", "tutu", free_f);
746 search_ext(head, "toto", "tutu");
747 debuged_remove(head, "toto");
752 xbt_test_add("Free the dictionary (twice)");
753 xbt_dict_free(&head);
754 xbt_dict_free(&head);
757 fill(&head, homogeneous);
758 count_check_get_key(head, 7);
759 xbt_test_add("Change 123 to 'Changed 123'");
760 xbt_dict_set(head, "123", xbt_strdup("Changed 123"), free_f);
761 count_check_get_key(head, 7);
763 xbt_test_add("Change 123 back to '123'");
764 xbt_dict_set(head, "123", xbt_strdup("123"), free_f);
765 count_check_get_key(head, 7);
767 xbt_test_add("Change 12a to 'Dummy 12a'");
768 xbt_dict_set(head, "12a", xbt_strdup("Dummy 12a"), free_f);
769 count_check_get_key(head, 7);
771 xbt_test_add("Change 12a to '12a'");
772 xbt_dict_set(head, "12a", xbt_strdup("12a"), free_f);
773 count_check_get_key(head, 7);
775 xbt_test_add("Traverse the resulting dictionary");
779 xbt_test_add("Search 123");
780 data = (char*) xbt_dict_get(head, "123");
781 xbt_test_assert(data);
782 xbt_test_assert(!strcmp("123", data));
784 search_not_found(head, "Can't be found");
785 search_not_found(head, "123 Can't be found");
786 search_not_found(head, "12345678 NOT");
791 search(head, "123456");
792 search(head, "1234");
793 search(head, "123457");
795 xbt_test_add("Traverse the resulting dictionary");
798 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
800 xbt_test_add("Free the dictionary twice");
801 xbt_dict_free(&head);
802 xbt_dict_free(&head);
804 xbt_test_add("Traverse the resulting dictionary");
808 XBT_TEST_UNIT("basic_heterogeneous", test_dict_basic_heterogeneous, "Basic usage: change, retrieve, traverse: heterogeneous dict")
813 XBT_TEST_UNIT("basic_homogeneous", test_dict_basic_homogeneous, "Basic usage: change, retrieve, traverse: homogeneous dict")
818 static void remove_test(int homogeneous)
820 fill(&head, homogeneous);
822 xbt_test_add("Remove non existing data");
824 debuged_remove(head, "Does not exist");
827 if (e.category != not_found_error)
828 xbt_test_exception(e);
832 xbt_dict_free(&head);
834 xbt_test_add("Remove each data manually (traversing the resulting dictionary each time)");
835 fill(&head, homogeneous);
836 debuged_remove(head, "12a");
839 debuged_remove(head, "12b");
842 debuged_remove(head, "12");
845 debuged_remove(head, "123456");
849 debuged_remove(head, "12346");
852 if (e.category != not_found_error)
853 xbt_test_exception(e);
856 debuged_remove(head, "1234");
858 debuged_remove(head, "123457");
860 debuged_remove(head, "123");
863 debuged_remove(head, "12346");
866 if (e.category != not_found_error)
867 xbt_test_exception(e);
871 xbt_test_add("Free dict, create new fresh one, and then reset the dict");
872 xbt_dict_free(&head);
873 fill(&head, homogeneous);
874 xbt_dict_reset(head);
878 xbt_test_add("Free the dictionary twice");
879 xbt_dict_free(&head);
880 xbt_dict_free(&head);
883 XBT_TEST_UNIT("remove_heterogeneous", test_dict_remove_heterogeneous, "Removing some values: heterogeneous dict")
888 XBT_TEST_UNIT("remove_homogeneous", test_dict_remove_homogeneous, "Removing some values: homogeneous dict")
893 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
897 xbt_test_add("Store NULL under 'null'");
898 xbt_dict_set(head, "null", NULL, NULL);
899 search_ext(head, "null", NULL);
901 xbt_test_add("Check whether I see it while traversing...");
903 xbt_dict_cursor_t cursor = NULL;
907 xbt_dict_foreach(head, cursor, key, data) {
908 if (!key || !data || strcmp(key, data)) {
909 xbt_test_log("Seen: %s->%s", key, data);
911 xbt_test_log("Seen: %s", key);
914 if (!strcmp(key, "null"))
917 xbt_test_assert(found, "the key 'null', associated to NULL is not found");
919 xbt_dict_free(&head);
923 #define SIZEOFKEY 1024
924 static int countelems(xbt_dict_t head)
926 xbt_dict_cursor_t cursor;
931 xbt_dict_foreach(head, cursor, key, data) {
937 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
939 xbt_dict_t head = NULL;
943 srand((unsigned int) time(NULL));
945 for (i = 0; i < 10; i++) {
946 xbt_test_add("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
947 xbt_test_log("Fill the struct, count its elems and frees the structure");
948 xbt_test_log("using 1000 elements with %d chars long randomized keys.", SIZEOFKEY);
949 head = xbt_dict_new();
950 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
951 for (j = 0; j < 1000; j++) {
953 key = (char*) xbt_malloc(SIZEOFKEY);
956 for (k = 0; k < SIZEOFKEY - 1; k++)
957 key[k] = rand() % ('z' - 'a') + 'a';
959 /* printf("[%d %s]\n",j,key); */
960 data = (char*) xbt_dict_get_or_null(head, key);
961 } while (data != NULL);
963 xbt_dict_set(head, key, key, &free);
964 data = (char*) xbt_dict_get(head, key);
965 xbt_test_assert(!strcmp(key, data), "Retrieved value (%s) != Injected value (%s)", key, data);
969 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
971 xbt_dict_free(&head);
972 xbt_dict_free(&head);
975 head = xbt_dict_new();
976 xbt_test_add("Fill %d elements, with keys being the number of element", NB_ELM);
977 for (j = 0; j < NB_ELM; j++) {
978 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
979 key = (char*) xbt_malloc(10);
981 snprintf(key,10, "%d", j);
982 xbt_dict_set(head, key, key, &free);
984 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
986 xbt_test_add("Count the elements (retrieving the key and data for each)");
987 i = countelems(head);
988 xbt_test_log("There is %d elements", i);
990 xbt_test_add("Search my %d elements 20 times", NB_ELM);
991 key = (char*) xbt_malloc(10);
992 for (i = 0; i < 20; i++) {
994 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
995 for (j = 0; j < NB_ELM; j++) {
996 snprintf(key,10, "%d", j);
997 data = xbt_dict_get(head, key);
998 xbt_test_assert(!strcmp(key, (char *) data), "with get, key=%s != data=%s", key, (char *) data);
999 data = xbt_dict_get_ext(head, key, strlen(key));
1000 xbt_test_assert(!strcmp(key, (char *) data), "with get_ext, key=%s != data=%s", key, (char *) data);
1005 xbt_test_add("Remove my %d elements", NB_ELM);
1006 key = (char*) xbt_malloc(10);
1007 for (j = 0; j < NB_ELM; j++) {
1008 /* if (!(j%10000)) printf("."); fflush(stdout); */
1009 snprintf(key,10, "%d", j);
1010 xbt_dict_remove(head, key);
1014 xbt_test_add("Free the structure (twice)");
1015 xbt_dict_free(&head);
1016 xbt_dict_free(&head);
1019 XBT_TEST_UNIT("ext", test_dict_int, "Test dictionnary with int keys")
1021 xbt_dict_t dict = xbt_dict_new();
1024 xbt_test_add("Insert elements");
1026 for (i = 0; i < count; ++i)
1027 xbt_dict_set_ext(dict, (char*) &i, sizeof(i), (void*) (intptr_t) i, NULL);
1028 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Bad number of elements in the dictionnary");
1030 xbt_test_add("Check elements");
1031 for (i = 0; i < count; ++i) {
1032 int res = (int) (intptr_t) xbt_dict_get_ext(dict, (char*) &i, sizeof(i));
1033 xbt_test_assert(xbt_dict_size(dict) == (unsigned) count, "Unexpected value at index %i, expected %i but was %i", i, i, res);
1036 xbt_test_add("Free the array");
1037 xbt_dict_free(&dict);
1039 #endif /* SIMGRID_TEST */