3 /* dict - a generic dictionary, variation over the B-tree concept */
5 /* Copyright (c) 2003,2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #define DJB2_HASH_FUNCTION
11 //#define FNV_HASH_FUNCTION
17 #include "xbt/mallocator.h"
18 #include "xbt_modinter.h"
19 #include "dict_private.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
22 "Dictionaries provide the same functionalities than hash tables");
23 /*####[ Private prototypes ]#################################################*/
25 static xbt_mallocator_t dict_mallocator = NULL;
26 static void *dict_mallocator_new_f(void);
27 static void dict_mallocator_free_f(void *dict);
28 static void dict_mallocator_reset_f(void *dict);
31 /*####[ Code ]###############################################################*/
35 * \return pointer to the destination
36 * \see xbt_dict_new_ext(), xbt_dict_free()
38 * Creates and initialize a new dictionary with a default hashtable size.
40 xbt_dict_t xbt_dict_new(void)
44 if (dict_mallocator == NULL) {
46 dict_mallocator = xbt_mallocator_new(256,
47 dict_mallocator_new_f,
48 dict_mallocator_free_f,
49 dict_mallocator_reset_f);
50 dict_elm_mallocator = xbt_mallocator_new(256,
51 dict_elm_mallocator_new_f,
52 dict_elm_mallocator_free_f,
53 dict_elm_mallocator_reset_f);
56 dict = xbt_mallocator_get(dict_mallocator);
57 dict->table_size = 127;
58 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size + 1);
67 * \param dict the dictionary to be freed
69 * Frees a dictionary with all the data
71 void xbt_dict_free(xbt_dict_t * dict)
74 xbt_dictelm_t current, previous;
78 // if ( *dict ) xbt_dict_dump_sizes(*dict);
80 if (dict != NULL && *dict != NULL) {
81 table_size = (*dict)->table_size;
82 table = (*dict)->table;
83 for (i = 0; (*dict)->count && i < table_size; i++) {
85 while (current != NULL) {
87 current = current->next;
88 xbt_dictelm_free(previous);
93 xbt_mallocator_release(dict_mallocator, *dict);
99 * Returns the amount of elements in the dict
101 unsigned int xbt_dict_size(xbt_dict_t dict)
107 * Returns the hash code of a string.
109 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len)
113 #ifdef DJB2_HASH_FUNCTION
114 /* fast implementation of djb2 algorithm */
116 register unsigned int hash = 5381;
120 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
122 # elif defined(FNV_HASH_FUNCTION)
123 register unsigned int hash = 0x811c9dc5;
124 unsigned char *bp = (unsigned char *) str; /* start of buffer */
125 unsigned char *be = bp + str_len; /* beyond end of buffer */
128 /* multiply by the 32 bit FNV magic prime mod 2^32 */
130 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
132 /* xor the bottom with the current octet */
133 hash ^= (unsigned int) *bp++;
137 register unsigned int hash = 0;
140 hash += (*str) * (*str);
148 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
150 #ifdef DJB2_HASH_FUNCTION
151 /* fast implementation of djb2 algorithm */
153 register unsigned int hash = 5381;
155 while ((c = *str++)) {
156 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
159 # elif defined(FNV_HASH_FUNCTION)
160 register unsigned int hash = 0x811c9dc5;
163 /* multiply by the 32 bit FNV magic prime mod 2^32 */
165 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
167 /* xor the bottom with the current byte */
168 hash ^= (unsigned int) *str++;
172 register unsigned int hash = 0;
175 hash += (*str) * (*str);
182 /* Expend the size of the dict */
183 static void xbt_dict_rehash(xbt_dict_t dict)
185 const int oldsize = dict->table_size + 1;
186 register int newsize = oldsize * 2;
188 register xbt_dictelm_t *currcell;
189 register xbt_dictelm_t *twincell;
190 register xbt_dictelm_t bucklet;
191 register xbt_dictelm_t *pprev;
194 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
195 newsize * sizeof(xbt_dictelm_t));
196 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
197 dict->table_size = --newsize;
198 dict->table = currcell;
199 DEBUG2("REHASH (%d->%d)", oldsize, newsize);
201 for (i = 0; i < oldsize; i++, currcell++) {
202 if (!*currcell) /* empty cell */
204 twincell = currcell + oldsize;
205 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
206 /* Since we use "& size" instead of "%size" and since the size was doubled,
207 each bucklet of this cell must either :
208 - stay in cell i (ie, currcell)
209 - go to the cell i+oldsize (ie, twincell) */
210 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
211 *pprev = bucklet->next;
212 bucklet->next = *twincell;
218 pprev = &bucklet->next;
223 if (!*currcell) /* everything moved */
229 * \brief Add data to the dict (arbitrary key)
230 * \param dict the container
231 * \param key the key to set the new data
232 * \param key_len the size of the \a key
233 * \param data the data to add in the dict
234 * \param free_ctn function to call with (\a key as argument) when
235 * \a key is removed from the dictionary
237 * Set the \a data in the structure under the \a key, which can be any kind
238 * of data, as long as its length is provided in \a key_len.
240 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
241 const char *key, int key_len,
242 void *data, void_f_pvoid_t free_ctn)
245 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
247 xbt_dictelm_t current, previous = NULL;
250 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
251 dict->table_size, hash_code & dict->table_size);
252 current = dict->table[hash_code & dict->table_size];
253 while (current != NULL &&
254 (hash_code != current->hash_code || key_len != current->key_len
255 || memcmp(key, current->key, key_len))) {
257 current = current->next;
260 if (current == NULL) {
261 /* this key doesn't exist yet */
262 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
264 if (previous == NULL) {
265 dict->table[hash_code & dict->table_size] = current;
267 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
268 xbt_dict_rehash(dict);
270 previous->next = current;
274 DEBUG6("Replace %.*s by %.*s under key %.*s",
275 key_len, (char *) current->content,
276 key_len, (char *) data, key_len, (char *) key);
277 /* there is already an element with the same key: overwrite it */
278 if (current->content != NULL && current->free_f != NULL) {
279 current->free_f(current->content);
281 current->content = data;
282 current->free_f = free_ctn;
287 * \brief Add data to the dict (null-terminated key)
289 * \param dict the dict
290 * \param key the key to set the new data
291 * \param data the data to add in the dict
292 * \param free_ctn function to call with (\a key as argument) when
293 * \a key is removed from the dictionary
295 * set the \a data in the structure under the \a key, which is a
296 * null terminated string.
298 void xbt_dict_set(xbt_dict_t dict,
299 const char *key, void *data, void_f_pvoid_t free_ctn)
302 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
306 * \brief Retrieve data from the dict (arbitrary key)
308 * \param dict the dealer of data
309 * \param key the key to find data
310 * \param key_len the size of the \a key
311 * \return the data that we are looking for
313 * Search the given \a key. Throws not_found_error when not found.
315 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
319 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
320 xbt_dictelm_t current;
324 current = dict->table[hash_code & dict->table_size];
325 while (current != NULL &&
326 (hash_code != current->hash_code || key_len != current->key_len
327 || memcmp(key, current->key, key_len))) {
328 current = current->next;
332 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
334 return current->content;
338 * \brief like xbt_dict_get_ext(), but returning NULL when not found
340 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
342 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
343 xbt_dictelm_t current;
347 current = dict->table[hash_code & dict->table_size];
348 while (current != NULL &&
349 (hash_code != current->hash_code || key_len != current->key_len
350 || memcmp(key, current->key, key_len))) {
351 current = current->next;
357 return current->content;
361 * @brief retrieve the key associated to that object. Warning, that's a linear search
363 * Returns NULL if the object cannot be found
365 char *xbt_dict_get_key(xbt_dict_t dict, const void*data) {
367 xbt_dictelm_t current;
370 for (i = 0; i <= dict->table_size; i++) {
371 current = dict->table[i];
372 while (current != NULL) {
373 if (current->content == data)
375 current = current->next;
383 * \brief Retrieve data from the dict (null-terminated key)
385 * \param dict the dealer of data
386 * \param key the key to find data
387 * \return the data that we are looking for
389 * Search the given \a key. Throws not_found_error when not found.
390 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
393 void *xbt_dict_get(xbt_dict_t dict, const char *key)
396 unsigned int hash_code = xbt_dict_hash(key);
397 xbt_dictelm_t current;
401 current = dict->table[hash_code & dict->table_size];
402 while (current != NULL
403 && (hash_code != current->hash_code || strcmp(key, current->key)))
404 current = current->next;
407 THROW1(not_found_error, 0, "key %s not found", key);
409 return current->content;
413 * \brief like xbt_dict_get(), but returning NULL when not found
415 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
417 unsigned int hash_code = xbt_dict_hash(key);
418 xbt_dictelm_t current;
422 current = dict->table[hash_code & dict->table_size];
423 while (current != NULL &&
424 hash_code != current->hash_code && strcmp(key, current->key))
425 current = current->next;
430 return current->content;
435 * \brief Remove data from the dict (arbitrary key)
437 * \param dict the trash can
438 * \param key the key of the data to be removed
439 * \param key_len the size of the \a key
441 * Remove the entry associated with the given \a key (throws not_found)
443 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
447 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
448 xbt_dictelm_t current, previous = NULL;
452 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
453 current = dict->table[hash_code & dict->table_size];
454 while (current != NULL &&
455 (hash_code != current->hash_code || key_len != current->key_len
456 || strncmp(key, current->key, key_len))) {
457 previous = current; /* save the previous node */
458 current = current->next;
462 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
464 if (previous != NULL) {
465 previous->next = current->next;
467 dict->table[hash_code & dict->table_size] = current->next;
470 if (!dict->table[hash_code & dict->table_size])
473 xbt_dictelm_free(current);
478 * \brief Remove data from the dict (null-terminated key)
480 * \param dict the dict
481 * \param key the key of the data to be removed
483 * Remove the entry associated with the given \a key
485 void xbt_dict_remove(xbt_dict_t dict, const char *key)
487 xbt_dict_remove_ext(dict, key, strlen(key));
491 * \brief Remove all data from the dict
492 * \param dict the dict
494 void xbt_dict_reset(xbt_dict_t dict)
498 xbt_dictelm_t current, previous = NULL;
502 if (dict->count == 0)
505 for (i = 0; i <= dict->table_size; i++) {
506 current = dict->table[i];
507 while (current != NULL) {
509 current = current->next;
510 xbt_dictelm_free(previous);
512 dict->table[i] = NULL;
520 * \brief Return the number of elements in the dict.
521 * \param dict a dictionary
523 int xbt_dict_length(xbt_dict_t dict)
530 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
531 void xbt_dict_dump_output_string(void *s)
538 * \brief Outputs the content of the structure (debugging purpose)
540 * \param dict the exibitionist
541 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
543 * Outputs the content of the structure. (for debugging purpose). \a output is a
544 * function to output the data. If NULL, data won't be displayed.
547 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
550 xbt_dictelm_t element;
551 printf("Dict %p:\n", dict);
553 for (i = 0; i < dict->table_size; i++) {
554 element = dict->table[i];
557 while (element != NULL) {
558 printf(" %s -> '", element->key);
559 if (output != NULL) {
560 (*output) (element->content);
563 element = element->next;
573 xbt_dynar_t all_sizes = NULL;
574 /** @brief shows some debugging info about the bucklet repartition */
575 void xbt_dict_dump_sizes(xbt_dict_t dict)
581 xbt_dictelm_t element;
582 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
584 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
585 dict->fill, dict->table_size);
587 for (i = 0; i < dict->table_size; i++) {
588 element = dict->table[i];
591 while (element != NULL) {
593 element = element->next;
596 if (xbt_dynar_length(sizes) <= size) {
598 xbt_dynar_set(sizes, size, &prevsize);
601 xbt_dynar_get_cpy(sizes, size, &prevsize);
603 xbt_dynar_set(sizes, size, &prevsize);
607 all_sizes = xbt_dynar_new(sizeof(int), NULL);
609 xbt_dynar_foreach(sizes, count, size) {
610 /* Copy values of this one into all_sizes */
612 if (xbt_dynar_length(all_sizes) <= count) {
614 xbt_dynar_set(all_sizes, count, &prevcount);
616 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
618 xbt_dynar_set(all_sizes, count, &prevcount);
621 /* Report current sizes */
626 printf("%delm x %u cells; ", count, size);
630 xbt_dynar_free(&sizes);
634 * Destroy the dict mallocators.
635 * This is an internal XBT function called by xbt_exit().
637 void xbt_dict_exit(void)
639 if (dict_mallocator != NULL) {
640 xbt_mallocator_free(dict_mallocator);
641 dict_mallocator = NULL;
642 xbt_mallocator_free(dict_elm_mallocator);
643 dict_elm_mallocator = NULL;
650 printf("Overall stats:");
651 xbt_dynar_foreach(all_sizes, count, size) {
656 printf("%delm x %d cells; ", count, size);
660 printf("; %f elm per cell\n", avg / (double) total_count);
664 static void *dict_mallocator_new_f(void)
666 return xbt_new(s_xbt_dict_t, 1);
669 static void dict_mallocator_free_f(void *dict)
674 static void dict_mallocator_reset_f(void *dict)
676 /* nothing to do because all fields are
677 * initialized in xbt_dict_new
684 #include "portable.h"
686 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
687 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
689 XBT_TEST_SUITE("dict", "Dict data container");
691 static void print_str(void *str)
693 printf("%s", (char *) PRINTF_STR(str));
696 static void debuged_add_ext(xbt_dict_t head, const char *key,
697 const char *data_to_fill)
699 char *data = xbt_strdup(data_to_fill);
701 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
703 xbt_dict_set(head, key, data, &free);
704 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
705 xbt_dict_dump(head, (void (*)(void *)) &printf);
710 static void debuged_add(xbt_dict_t head, const char *key)
712 debuged_add_ext(head, key, key);
715 static void fill(xbt_dict_t * head)
717 xbt_test_add0("Fill in the dictionnary");
719 *head = xbt_dict_new();
720 debuged_add(*head, "12");
721 debuged_add(*head, "12a");
722 debuged_add(*head, "12b");
723 debuged_add(*head, "123");
724 debuged_add(*head, "123456");
725 /* Child becomes child of what to add */
726 debuged_add(*head, "1234");
727 /* Need of common ancestor */
728 debuged_add(*head, "123457");
732 static void search_ext(xbt_dict_t head, const char *key, const char *data)
736 xbt_test_add1("Search %s", key);
737 found = xbt_dict_get(head, key);
738 xbt_test_log1("Found %s", (char *) found);
740 xbt_test_assert1(found,
741 "data do not match expectations: found NULL while searching for %s",
744 xbt_test_assert2(!strcmp((char *) data, found),
745 "data do not match expectations: found %s while searching for %s",
746 (char *) found, data);
749 static void search(xbt_dict_t head, const char *key)
751 search_ext(head, key, key);
754 static void debuged_remove(xbt_dict_t head, const char *key)
757 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
758 xbt_dict_remove(head, key);
759 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
763 static void traverse(xbt_dict_t head)
765 xbt_dict_cursor_t cursor = NULL;
770 xbt_dict_foreach(head, cursor, key, data) {
771 if (!key || !data || strcmp(key, data)) {
772 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
775 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
777 xbt_test_assert2(!data || !strcmp(key, data),
778 "Key(%s) != value(%s). Aborting", key, data);
782 static void search_not_found(xbt_dict_t head, const char *data)
787 xbt_test_add1("Search %s (expected not to be found)", data);
790 data = xbt_dict_get(head, data);
791 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
794 if (e.category != not_found_error)
795 xbt_test_exception(e);
799 xbt_test_assert0(ok, "Exception not raised");
802 static void count(xbt_dict_t dict, int length)
804 xbt_dict_cursor_t cursor;
810 xbt_test_add1("Count elements (expecting %d)", length);
811 xbt_test_assert2(xbt_dict_length(dict) == length,
812 "Announced length(%d) != %d.", xbt_dict_length(dict),
815 xbt_dict_foreach(dict, cursor, key, data)
818 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
823 static void count_check_get_key(xbt_dict_t dict, int length)
825 xbt_dict_cursor_t cursor;
831 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
832 xbt_test_assert2(xbt_dict_length(dict) == length,
833 "Announced length(%d) != %d.", xbt_dict_length(dict),
836 xbt_dict_foreach(dict, cursor, key, data) {
838 key2 = xbt_dict_get_key(dict,data);
839 xbt_assert2(!strcmp(key,key2),
840 "The data was registered under %s instead of %s as expected",key2,key);
843 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
849 xbt_dict_t head = NULL;
853 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
855 xbt_test_add0("Traversal the null dictionary");
858 xbt_test_add0("Traversal and search the empty dictionary");
859 head = xbt_dict_new();
862 debuged_remove(head, "12346");
864 if (e.category != not_found_error)
865 xbt_test_exception(e);
868 xbt_dict_free(&head);
870 xbt_test_add0("Traverse the full dictionary");
872 count_check_get_key(head, 7);
874 debuged_add_ext(head, "toto", "tutu");
875 search_ext(head, "toto", "tutu");
876 debuged_remove(head, "toto");
881 xbt_test_add0("Free the dictionary (twice)");
882 xbt_dict_free(&head);
883 xbt_dict_free(&head);
887 count_check_get_key(head, 7);
888 xbt_test_add0("Change 123 to 'Changed 123'");
889 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
890 count_check_get_key(head, 7);
892 xbt_test_add0("Change 123 back to '123'");
893 xbt_dict_set(head, "123", strdup("123"), &free);
894 count_check_get_key(head, 7);
896 xbt_test_add0("Change 12a to 'Dummy 12a'");
897 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
898 count_check_get_key(head, 7);
900 xbt_test_add0("Change 12a to '12a'");
901 xbt_dict_set(head, "12a", strdup("12a"), &free);
902 count_check_get_key(head, 7);
904 xbt_test_add0("Traverse the resulting dictionary");
908 xbt_test_add0("Search 123");
909 data = xbt_dict_get(head, "123");
910 xbt_test_assert(data);
911 xbt_test_assert(!strcmp("123", data));
913 search_not_found(head, "Can't be found");
914 search_not_found(head, "123 Can't be found");
915 search_not_found(head, "12345678 NOT");
920 search(head, "123456");
921 search(head, "1234");
922 search(head, "123457");
924 xbt_test_add0("Traverse the resulting dictionary");
927 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
929 xbt_test_add0("Free the dictionary twice");
930 xbt_dict_free(&head);
931 xbt_dict_free(&head);
933 xbt_test_add0("Traverse the resulting dictionary");
937 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
941 xbt_test_add0("Remove non existing data");
943 debuged_remove(head, "Does not exist");
946 if (e.category != not_found_error)
947 xbt_test_exception(e);
952 xbt_dict_free(&head);
955 ("Remove each data manually (traversing the resulting dictionary each time)");
957 debuged_remove(head, "12a");
960 debuged_remove(head, "12b");
963 debuged_remove(head, "12");
966 debuged_remove(head, "123456");
970 debuged_remove(head, "12346");
973 if (e.category != not_found_error)
974 xbt_test_exception(e);
978 debuged_remove(head, "1234");
980 debuged_remove(head, "123457");
982 debuged_remove(head, "123");
985 debuged_remove(head, "12346");
988 if (e.category != not_found_error)
989 xbt_test_exception(e);
994 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
995 xbt_dict_free(&head);
997 xbt_dict_reset(head);
1001 xbt_test_add0("Free the dictionary twice");
1002 xbt_dict_free(&head);
1003 xbt_dict_free(&head);
1006 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1010 xbt_test_add0("Store NULL under 'null'");
1011 xbt_dict_set(head, "null", NULL, NULL);
1012 search_ext(head, "null", NULL);
1014 xbt_test_add0("Check whether I see it while traversing...");
1016 xbt_dict_cursor_t cursor = NULL;
1020 xbt_dict_foreach(head, cursor, key, data) {
1021 if (!key || !data || strcmp(key, data)) {
1022 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1024 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1027 if (!strcmp(key, "null"))
1030 xbt_test_assert0(found,
1031 "the key 'null', associated to NULL is not found");
1033 xbt_dict_free(&head);
1036 #define NB_ELM 20000
1037 #define SIZEOFKEY 1024
1038 static int countelems(xbt_dict_t head)
1040 xbt_dict_cursor_t cursor;
1045 xbt_dict_foreach(head, cursor, key, data) {
1051 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1053 xbt_dict_t head = NULL;
1058 srand((unsigned int) time(NULL));
1060 for (i = 0; i < 10; i++) {
1061 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1062 xbt_test_log0("Fill the struct, count its elems and frees the structure");
1063 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
1065 head = xbt_dict_new();
1066 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1068 for (j = 0; j < 1000; j++) {
1070 key = xbt_malloc(SIZEOFKEY);
1073 for (k = 0; k < SIZEOFKEY - 1; k++)
1074 key[k] = rand() % ('z' - 'a') + 'a';
1076 /* printf("[%d %s]\n",j,key); */
1077 data = xbt_dict_get_or_null(head, key);
1078 } while (data != NULL);
1080 xbt_dict_set(head, key, key, &free);
1081 data = xbt_dict_get(head, key);
1082 xbt_test_assert2(!strcmp(key, data),
1083 "Retrieved value (%s) != Injected value (%s)", key,
1088 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1090 xbt_dict_free(&head);
1091 xbt_dict_free(&head);
1095 head = xbt_dict_new();
1096 xbt_test_add1("Fill %d elements, with keys being the number of element",
1098 for (j = 0; j < NB_ELM; j++) {
1099 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1101 key = xbt_malloc(10);
1103 sprintf(key, "%d", j);
1104 xbt_dict_set(head, key, key, &free);
1106 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1108 xbt_test_add0("Count the elements (retrieving the key and data for each)");
1109 i = countelems(head);
1110 xbt_test_log1("There is %d elements", i);
1112 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1113 key = xbt_malloc(10);
1114 for (i = 0; i < 20; i++) {
1115 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1116 for (j = 0; j < NB_ELM; j++) {
1118 sprintf(key, "%d", j);
1119 data = xbt_dict_get(head, key);
1120 xbt_test_assert2(!strcmp(key, (char *) data),
1121 "with get, key=%s != data=%s", key, (char *) data);
1122 data = xbt_dict_get_ext(head, key, strlen(key));
1123 xbt_test_assert2(!strcmp(key, (char *) data),
1124 "with get_ext, key=%s != data=%s", key, (char *) data);
1129 xbt_test_add1("Remove my %d elements", NB_ELM);
1130 key = xbt_malloc(10);
1131 for (j = 0; j < NB_ELM; j++) {
1132 /* if (!(j%10000)) printf("."); fflush(stdout); */
1134 sprintf(key, "%d", j);
1135 xbt_dict_remove(head, key);
1140 xbt_test_add0("Free the structure (twice)");
1141 xbt_dict_free(&head);
1142 xbt_dict_free(&head);
1145 static void str_free(void *s)
1147 char *c = *(char **) s;
1151 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1155 #define NB_ELM 100 /*00 */
1157 #define KEY_SIZE 512
1158 #define NB_TEST 20 /*20 */
1161 xbt_dict_t mdict = NULL;
1163 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1168 xbt_test_add0("Generic multicache CRASH test");
1169 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1170 "depth of multicache=%d, key size=%d",
1171 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
1173 for (l = 0; l < DEPTH; l++) {
1174 key = xbt_malloc(KEY_SIZE);
1175 xbt_dynar_push(keys, &key);
1178 for (i = 0; i < NB_TEST; i++) {
1179 mdict = xbt_dict_new();
1180 VERB1("mdict=%p", mdict);
1182 printf("Test %d\n", i);
1183 /* else if (i%10) printf("."); else printf("%d",i/10); */
1185 for (j = 0; j < NB_ELM; j++) {
1189 for (l = 0; l < DEPTH; l++) {
1190 key = *(char **) xbt_dynar_get_ptr(keys, l);
1192 for (k = 0; k < KEY_SIZE - 1; k++)
1193 key[k] = rand() % ('z' - 'a') + 'a';
1198 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1201 printf("in multitree %p.\n", mdict);
1203 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1205 data = xbt_multidict_get(mdict, keys);
1207 xbt_test_assert2(data && !strcmp((char *) data, key),
1208 "Retrieved value (%s) does not match the given one (%s)\n",
1209 (char *) data, key);
1211 xbt_dict_free(&mdict);
1214 xbt_dynar_free(&keys);
1217 xbt_dict_dump(mdict,&xbt_dict_print); */
1219 xbt_dict_free(&mdict);
1220 xbt_dynar_free(&keys);
1223 #endif /* SIMGRID_TEST */