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 /* Warning: the size of the table is 'table_size+1'...
84 * This is because table_size is used as a binary mask in xbt_dict_rehash */
85 for (i = 0; (*dict)->count && i <= table_size; i++) {
87 while (current != NULL) {
89 current = current->next;
90 xbt_dictelm_free(previous);
95 xbt_mallocator_release(dict_mallocator, *dict);
101 * Returns the amount of elements in the dict
103 unsigned int xbt_dict_size(xbt_dict_t dict)
109 * Returns the hash code of a string.
111 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len)
115 #ifdef DJB2_HASH_FUNCTION
116 /* fast implementation of djb2 algorithm */
118 register unsigned int hash = 5381;
122 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
124 # elif defined(FNV_HASH_FUNCTION)
125 register unsigned int hash = 0x811c9dc5;
126 unsigned char *bp = (unsigned char *) str; /* start of buffer */
127 unsigned char *be = bp + str_len; /* beyond end of buffer */
130 /* multiply by the 32 bit FNV magic prime mod 2^32 */
132 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
134 /* xor the bottom with the current octet */
135 hash ^= (unsigned int) *bp++;
139 register unsigned int hash = 0;
142 hash += (*str) * (*str);
150 static XBT_INLINE unsigned int xbt_dict_hash(const char *str)
152 #ifdef DJB2_HASH_FUNCTION
153 /* fast implementation of djb2 algorithm */
155 register unsigned int hash = 5381;
157 while ((c = *str++)) {
158 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
161 # elif defined(FNV_HASH_FUNCTION)
162 register unsigned int hash = 0x811c9dc5;
165 /* multiply by the 32 bit FNV magic prime mod 2^32 */
167 (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
169 /* xor the bottom with the current byte */
170 hash ^= (unsigned int) *str++;
174 register unsigned int hash = 0;
177 hash += (*str) * (*str);
184 /* Expend the size of the dict */
185 static void xbt_dict_rehash(xbt_dict_t dict)
187 const int oldsize = dict->table_size + 1;
188 register int newsize = oldsize * 2;
190 register xbt_dictelm_t *currcell;
191 register xbt_dictelm_t *twincell;
192 register xbt_dictelm_t bucklet;
193 register xbt_dictelm_t *pprev;
196 (xbt_dictelm_t *) xbt_realloc((char *) dict->table,
197 newsize * sizeof(xbt_dictelm_t));
198 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
199 dict->table_size = --newsize;
200 dict->table = currcell;
201 DEBUG2("REHASH (%d->%d)", oldsize, newsize);
203 for (i = 0; i < oldsize; i++, currcell++) {
204 if (!*currcell) /* empty cell */
206 twincell = currcell + oldsize;
207 for (pprev = currcell, bucklet = *currcell; bucklet; bucklet = *pprev) {
208 /* Since we use "& size" instead of "%size" and since the size was doubled,
209 each bucklet of this cell must either :
210 - stay in cell i (ie, currcell)
211 - go to the cell i+oldsize (ie, twincell) */
212 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
213 *pprev = bucklet->next;
214 bucklet->next = *twincell;
220 pprev = &bucklet->next;
225 if (!*currcell) /* everything moved */
231 * \brief Add data to the dict (arbitrary key)
232 * \param dict the container
233 * \param key the key to set the new data
234 * \param key_len the size of the \a key
235 * \param data the data to add in the dict
236 * \param free_ctn function to call with (\a key as argument) when
237 * \a key is removed from the dictionary
239 * Set the \a data in the structure under the \a key, which can be any kind
240 * of data, as long as its length is provided in \a key_len.
242 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
243 const char *key, int key_len,
244 void *data, void_f_pvoid_t free_ctn)
247 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
249 xbt_dictelm_t current, previous = NULL;
252 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d", key_len, key, hash_code,
253 dict->table_size, hash_code & dict->table_size);
254 current = dict->table[hash_code & dict->table_size];
255 while (current != NULL &&
256 (hash_code != current->hash_code || key_len != current->key_len
257 || memcmp(key, current->key, key_len))) {
259 current = current->next;
262 if (current == NULL) {
263 /* this key doesn't exist yet */
264 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
266 if (previous == NULL) {
267 dict->table[hash_code & dict->table_size] = current;
269 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
270 xbt_dict_rehash(dict);
272 previous->next = current;
276 DEBUG6("Replace %.*s by %.*s under key %.*s",
277 key_len, (char *) current->content,
278 key_len, (char *) data, key_len, (char *) key);
279 /* there is already an element with the same key: overwrite it */
280 if (current->content != NULL && current->free_f != NULL) {
281 current->free_f(current->content);
283 current->content = data;
284 current->free_f = free_ctn;
289 * \brief Add data to the dict (null-terminated key)
291 * \param dict the dict
292 * \param key the key to set the new data
293 * \param data the data to add in the dict
294 * \param free_ctn function to call with (\a key as argument) when
295 * \a key is removed from the dictionary
297 * set the \a data in the structure under the \a key, which is a
298 * null terminated string.
300 void xbt_dict_set(xbt_dict_t dict,
301 const char *key, void *data, void_f_pvoid_t free_ctn)
304 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
308 * \brief Retrieve data from the dict (arbitrary key)
310 * \param dict the dealer of data
311 * \param key the key to find data
312 * \param key_len the size of the \a key
313 * \return the data that we are looking for
315 * Search the given \a key. Throws not_found_error when not found.
317 void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len)
321 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
322 xbt_dictelm_t current;
326 current = dict->table[hash_code & dict->table_size];
327 while (current != NULL &&
328 (hash_code != current->hash_code || key_len != current->key_len
329 || memcmp(key, current->key, key_len))) {
330 current = current->next;
334 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
336 return current->content;
340 * \brief like xbt_dict_get_ext(), but returning NULL when not found
342 void *xbt_dict_get_or_null_ext(xbt_dict_t dict, const char *key, int key_len)
344 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
345 xbt_dictelm_t current;
349 current = dict->table[hash_code & dict->table_size];
350 while (current != NULL &&
351 (hash_code != current->hash_code || key_len != current->key_len
352 || memcmp(key, current->key, key_len))) {
353 current = current->next;
359 return current->content;
363 * @brief retrieve the key associated to that object. Warning, that's a linear search
365 * Returns NULL if the object cannot be found
367 char *xbt_dict_get_key(xbt_dict_t dict, const void*data) {
369 xbt_dictelm_t current;
372 for (i = 0; i <= dict->table_size; i++) {
373 current = dict->table[i];
374 while (current != NULL) {
375 if (current->content == data)
377 current = current->next;
385 * \brief Retrieve data from the dict (null-terminated key)
387 * \param dict the dealer of data
388 * \param key the key to find data
389 * \return the data that we are looking for
391 * Search the given \a key. Throws not_found_error when not found.
392 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
395 void *xbt_dict_get(xbt_dict_t dict, const char *key)
398 unsigned int hash_code = xbt_dict_hash(key);
399 xbt_dictelm_t current;
403 current = dict->table[hash_code & dict->table_size];
404 while (current != NULL
405 && (hash_code != current->hash_code || strcmp(key, current->key)))
406 current = current->next;
409 THROW1(not_found_error, 0, "key %s not found", key);
411 return current->content;
415 * \brief like xbt_dict_get(), but returning NULL when not found
417 void *xbt_dict_get_or_null(xbt_dict_t dict, const char *key)
419 unsigned int hash_code = xbt_dict_hash(key);
420 xbt_dictelm_t current;
424 current = dict->table[hash_code & dict->table_size];
425 while (current != NULL &&
426 hash_code != current->hash_code && strcmp(key, current->key))
427 current = current->next;
432 return current->content;
437 * \brief Remove data from the dict (arbitrary key)
439 * \param dict the trash can
440 * \param key the key of the data to be removed
441 * \param key_len the size of the \a key
443 * Remove the entry associated with the given \a key (throws not_found)
445 void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len)
449 unsigned int hash_code = xbt_dict_hash_ext(key, key_len);
450 xbt_dictelm_t current, previous = NULL;
454 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
455 current = dict->table[hash_code & dict->table_size];
456 while (current != NULL &&
457 (hash_code != current->hash_code || key_len != current->key_len
458 || strncmp(key, current->key, key_len))) {
459 previous = current; /* save the previous node */
460 current = current->next;
464 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
466 if (previous != NULL) {
467 previous->next = current->next;
469 dict->table[hash_code & dict->table_size] = current->next;
472 if (!dict->table[hash_code & dict->table_size])
475 xbt_dictelm_free(current);
480 * \brief Remove data from the dict (null-terminated key)
482 * \param dict the dict
483 * \param key the key of the data to be removed
485 * Remove the entry associated with the given \a key
487 void xbt_dict_remove(xbt_dict_t dict, const char *key)
489 xbt_dict_remove_ext(dict, key, strlen(key));
493 * \brief Remove all data from the dict
494 * \param dict the dict
496 void xbt_dict_reset(xbt_dict_t dict)
500 xbt_dictelm_t current, previous = NULL;
504 if (dict->count == 0)
507 for (i = 0; i <= dict->table_size; i++) {
508 current = dict->table[i];
509 while (current != NULL) {
511 current = current->next;
512 xbt_dictelm_free(previous);
514 dict->table[i] = NULL;
522 * \brief Return the number of elements in the dict.
523 * \param dict a dictionary
525 int xbt_dict_length(xbt_dict_t dict)
532 /** @brief function to be used in xbt_dict_dump as long as the stored values are strings */
533 void xbt_dict_dump_output_string(void *s)
540 * \brief Outputs the content of the structure (debugging purpose)
542 * \param dict the exibitionist
543 * \param output a function to dump each data in the tree (check @ref xbt_dict_dump_output_string)
545 * Outputs the content of the structure. (for debugging purpose). \a output is a
546 * function to output the data. If NULL, data won't be displayed.
549 void xbt_dict_dump(xbt_dict_t dict, void_f_pvoid_t output)
552 xbt_dictelm_t element;
553 printf("Dict %p:\n", dict);
555 for (i = 0; i < dict->table_size; i++) {
556 element = dict->table[i];
559 while (element != NULL) {
560 printf(" %s -> '", element->key);
561 if (output != NULL) {
562 (*output) (element->content);
565 element = element->next;
575 xbt_dynar_t all_sizes = NULL;
576 /** @brief shows some debugging info about the bucklet repartition */
577 void xbt_dict_dump_sizes(xbt_dict_t dict)
583 xbt_dictelm_t element;
584 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int), NULL);
586 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count,
587 dict->fill, dict->table_size);
589 for (i = 0; i < dict->table_size; i++) {
590 element = dict->table[i];
593 while (element != NULL) {
595 element = element->next;
598 if (xbt_dynar_length(sizes) <= size) {
600 xbt_dynar_set(sizes, size, &prevsize);
603 xbt_dynar_get_cpy(sizes, size, &prevsize);
605 xbt_dynar_set(sizes, size, &prevsize);
609 all_sizes = xbt_dynar_new(sizeof(int), NULL);
611 xbt_dynar_foreach(sizes, count, size) {
612 /* Copy values of this one into all_sizes */
614 if (xbt_dynar_length(all_sizes) <= count) {
616 xbt_dynar_set(all_sizes, count, &prevcount);
618 xbt_dynar_get_cpy(all_sizes, count, &prevcount);
620 xbt_dynar_set(all_sizes, count, &prevcount);
623 /* Report current sizes */
628 printf("%delm x %u cells; ", count, size);
632 xbt_dynar_free(&sizes);
636 * Destroy the dict mallocators.
637 * This is an internal XBT function called by xbt_exit().
639 void xbt_dict_exit(void)
641 if (dict_mallocator != NULL) {
642 xbt_mallocator_free(dict_mallocator);
643 dict_mallocator = NULL;
644 xbt_mallocator_free(dict_elm_mallocator);
645 dict_elm_mallocator = NULL;
652 printf("Overall stats:");
653 xbt_dynar_foreach(all_sizes, count, size) {
658 printf("%delm x %d cells; ", count, size);
662 printf("; %f elm per cell\n", avg / (double) total_count);
666 static void *dict_mallocator_new_f(void)
668 return xbt_new(s_xbt_dict_t, 1);
671 static void dict_mallocator_free_f(void *dict)
676 static void dict_mallocator_reset_f(void *dict)
678 /* nothing to do because all fields are
679 * initialized in xbt_dict_new
686 #include "portable.h"
688 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
689 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
691 XBT_TEST_SUITE("dict", "Dict data container");
693 static void print_str(void *str)
695 printf("%s", (char *) PRINTF_STR(str));
698 static void debuged_add_ext(xbt_dict_t head, const char *key,
699 const char *data_to_fill)
701 char *data = xbt_strdup(data_to_fill);
703 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
705 xbt_dict_set(head, key, data, &free);
706 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
707 xbt_dict_dump(head, (void (*)(void *)) &printf);
712 static void debuged_add(xbt_dict_t head, const char *key)
714 debuged_add_ext(head, key, key);
717 static void fill(xbt_dict_t * head)
719 xbt_test_add0("Fill in the dictionnary");
721 *head = xbt_dict_new();
722 debuged_add(*head, "12");
723 debuged_add(*head, "12a");
724 debuged_add(*head, "12b");
725 debuged_add(*head, "123");
726 debuged_add(*head, "123456");
727 /* Child becomes child of what to add */
728 debuged_add(*head, "1234");
729 /* Need of common ancestor */
730 debuged_add(*head, "123457");
734 static void search_ext(xbt_dict_t head, const char *key, const char *data)
738 xbt_test_add1("Search %s", key);
739 found = xbt_dict_get(head, key);
740 xbt_test_log1("Found %s", (char *) found);
742 xbt_test_assert1(found,
743 "data do not match expectations: found NULL while searching for %s",
746 xbt_test_assert2(!strcmp((char *) data, found),
747 "data do not match expectations: found %s while searching for %s",
748 (char *) found, data);
751 static void search(xbt_dict_t head, const char *key)
753 search_ext(head, key, key);
756 static void debuged_remove(xbt_dict_t head, const char *key)
759 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
760 xbt_dict_remove(head, key);
761 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
765 static void traverse(xbt_dict_t head)
767 xbt_dict_cursor_t cursor = NULL;
772 xbt_dict_foreach(head, cursor, key, data) {
773 if (!key || !data || strcmp(key, data)) {
774 xbt_test_log3("Seen #%d: %s->%s", ++i, PRINTF_STR(key),
777 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
779 xbt_test_assert2(!data || !strcmp(key, data),
780 "Key(%s) != value(%s). Aborting", key, data);
784 static void search_not_found(xbt_dict_t head, const char *data)
789 xbt_test_add1("Search %s (expected not to be found)", data);
792 data = xbt_dict_get(head, data);
793 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
796 if (e.category != not_found_error)
797 xbt_test_exception(e);
801 xbt_test_assert0(ok, "Exception not raised");
804 static void count(xbt_dict_t dict, int length)
806 xbt_dict_cursor_t cursor;
812 xbt_test_add1("Count elements (expecting %d)", length);
813 xbt_test_assert2(xbt_dict_length(dict) == length,
814 "Announced length(%d) != %d.", xbt_dict_length(dict),
817 xbt_dict_foreach(dict, cursor, key, data)
820 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
825 static void count_check_get_key(xbt_dict_t dict, int length)
827 xbt_dict_cursor_t cursor;
833 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
834 xbt_test_assert2(xbt_dict_length(dict) == length,
835 "Announced length(%d) != %d.", xbt_dict_length(dict),
838 xbt_dict_foreach(dict, cursor, key, data) {
840 key2 = xbt_dict_get_key(dict,data);
841 xbt_assert2(!strcmp(key,key2),
842 "The data was registered under %s instead of %s as expected",key2,key);
845 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
851 xbt_dict_t head = NULL;
855 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
857 xbt_test_add0("Traversal the null dictionary");
860 xbt_test_add0("Traversal and search the empty dictionary");
861 head = xbt_dict_new();
864 debuged_remove(head, "12346");
866 if (e.category != not_found_error)
867 xbt_test_exception(e);
870 xbt_dict_free(&head);
872 xbt_test_add0("Traverse the full dictionary");
874 count_check_get_key(head, 7);
876 debuged_add_ext(head, "toto", "tutu");
877 search_ext(head, "toto", "tutu");
878 debuged_remove(head, "toto");
883 xbt_test_add0("Free the dictionary (twice)");
884 xbt_dict_free(&head);
885 xbt_dict_free(&head);
889 count_check_get_key(head, 7);
890 xbt_test_add0("Change 123 to 'Changed 123'");
891 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
892 count_check_get_key(head, 7);
894 xbt_test_add0("Change 123 back to '123'");
895 xbt_dict_set(head, "123", strdup("123"), &free);
896 count_check_get_key(head, 7);
898 xbt_test_add0("Change 12a to 'Dummy 12a'");
899 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
900 count_check_get_key(head, 7);
902 xbt_test_add0("Change 12a to '12a'");
903 xbt_dict_set(head, "12a", strdup("12a"), &free);
904 count_check_get_key(head, 7);
906 xbt_test_add0("Traverse the resulting dictionary");
910 xbt_test_add0("Search 123");
911 data = xbt_dict_get(head, "123");
912 xbt_test_assert(data);
913 xbt_test_assert(!strcmp("123", data));
915 search_not_found(head, "Can't be found");
916 search_not_found(head, "123 Can't be found");
917 search_not_found(head, "12345678 NOT");
922 search(head, "123456");
923 search(head, "1234");
924 search(head, "123457");
926 xbt_test_add0("Traverse the resulting dictionary");
929 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
931 xbt_test_add0("Free the dictionary twice");
932 xbt_dict_free(&head);
933 xbt_dict_free(&head);
935 xbt_test_add0("Traverse the resulting dictionary");
939 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
943 xbt_test_add0("Remove non existing data");
945 debuged_remove(head, "Does not exist");
948 if (e.category != not_found_error)
949 xbt_test_exception(e);
954 xbt_dict_free(&head);
957 ("Remove each data manually (traversing the resulting dictionary each time)");
959 debuged_remove(head, "12a");
962 debuged_remove(head, "12b");
965 debuged_remove(head, "12");
968 debuged_remove(head, "123456");
972 debuged_remove(head, "12346");
975 if (e.category != not_found_error)
976 xbt_test_exception(e);
980 debuged_remove(head, "1234");
982 debuged_remove(head, "123457");
984 debuged_remove(head, "123");
987 debuged_remove(head, "12346");
990 if (e.category != not_found_error)
991 xbt_test_exception(e);
996 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
997 xbt_dict_free(&head);
999 xbt_dict_reset(head);
1003 xbt_test_add0("Free the dictionary twice");
1004 xbt_dict_free(&head);
1005 xbt_dict_free(&head);
1008 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
1012 xbt_test_add0("Store NULL under 'null'");
1013 xbt_dict_set(head, "null", NULL, NULL);
1014 search_ext(head, "null", NULL);
1016 xbt_test_add0("Check whether I see it while traversing...");
1018 xbt_dict_cursor_t cursor = NULL;
1022 xbt_dict_foreach(head, cursor, key, data) {
1023 if (!key || !data || strcmp(key, data)) {
1024 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
1026 xbt_test_log1("Seen: %s", PRINTF_STR(key));
1029 if (!strcmp(key, "null"))
1032 xbt_test_assert0(found,
1033 "the key 'null', associated to NULL is not found");
1035 xbt_dict_free(&head);
1038 #define NB_ELM 20000
1039 #define SIZEOFKEY 1024
1040 static int countelems(xbt_dict_t head)
1042 xbt_dict_cursor_t cursor;
1047 xbt_dict_foreach(head, cursor, key, data) {
1053 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
1055 xbt_dict_t head = NULL;
1060 srand((unsigned int) time(NULL));
1062 for (i = 0; i < 10; i++) {
1063 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
1064 xbt_test_log0("Fill the struct, count its elems and frees the structure");
1065 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
1067 head = xbt_dict_new();
1068 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1070 for (j = 0; j < 1000; j++) {
1072 key = xbt_malloc(SIZEOFKEY);
1075 for (k = 0; k < SIZEOFKEY - 1; k++)
1076 key[k] = rand() % ('z' - 'a') + 'a';
1078 /* printf("[%d %s]\n",j,key); */
1079 data = xbt_dict_get_or_null(head, key);
1080 } while (data != NULL);
1082 xbt_dict_set(head, key, key, &free);
1083 data = xbt_dict_get(head, key);
1084 xbt_test_assert2(!strcmp(key, data),
1085 "Retrieved value (%s) != Injected value (%s)", key,
1090 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
1092 xbt_dict_free(&head);
1093 xbt_dict_free(&head);
1097 head = xbt_dict_new();
1098 xbt_test_add1("Fill %d elements, with keys being the number of element",
1100 for (j = 0; j < NB_ELM; j++) {
1101 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
1103 key = xbt_malloc(10);
1105 sprintf(key, "%d", j);
1106 xbt_dict_set(head, key, key, &free);
1108 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
1110 xbt_test_add0("Count the elements (retrieving the key and data for each)");
1111 i = countelems(head);
1112 xbt_test_log1("There is %d elements", i);
1114 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
1115 key = xbt_malloc(10);
1116 for (i = 0; i < 20; i++) {
1117 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
1118 for (j = 0; j < NB_ELM; j++) {
1120 sprintf(key, "%d", j);
1121 data = xbt_dict_get(head, key);
1122 xbt_test_assert2(!strcmp(key, (char *) data),
1123 "with get, key=%s != data=%s", key, (char *) data);
1124 data = xbt_dict_get_ext(head, key, strlen(key));
1125 xbt_test_assert2(!strcmp(key, (char *) data),
1126 "with get_ext, key=%s != data=%s", key, (char *) data);
1131 xbt_test_add1("Remove my %d elements", NB_ELM);
1132 key = xbt_malloc(10);
1133 for (j = 0; j < NB_ELM; j++) {
1134 /* if (!(j%10000)) printf("."); fflush(stdout); */
1136 sprintf(key, "%d", j);
1137 xbt_dict_remove(head, key);
1142 xbt_test_add0("Free the structure (twice)");
1143 xbt_dict_free(&head);
1144 xbt_dict_free(&head);
1147 static void str_free(void *s)
1149 char *c = *(char **) s;
1153 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
1157 #define NB_ELM 100 /*00 */
1159 #define KEY_SIZE 512
1160 #define NB_TEST 20 /*20 */
1163 xbt_dict_t mdict = NULL;
1165 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
1170 xbt_test_add0("Generic multicache CRASH test");
1171 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1172 "depth of multicache=%d, key size=%d",
1173 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
1175 for (l = 0; l < DEPTH; l++) {
1176 key = xbt_malloc(KEY_SIZE);
1177 xbt_dynar_push(keys, &key);
1180 for (i = 0; i < NB_TEST; i++) {
1181 mdict = xbt_dict_new();
1182 VERB1("mdict=%p", mdict);
1184 printf("Test %d\n", i);
1185 /* else if (i%10) printf("."); else printf("%d",i/10); */
1187 for (j = 0; j < NB_ELM; j++) {
1191 for (l = 0; l < DEPTH; l++) {
1192 key = *(char **) xbt_dynar_get_ptr(keys, l);
1194 for (k = 0; k < KEY_SIZE - 1; k++)
1195 key[k] = rand() % ('z' - 'a') + 'a';
1200 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
1203 printf("in multitree %p.\n", mdict);
1205 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
1207 data = xbt_multidict_get(mdict, keys);
1209 xbt_test_assert2(data && !strcmp((char *) data, key),
1210 "Retrieved value (%s) does not match the given one (%s)\n",
1211 (char *) data, key);
1213 xbt_dict_free(&mdict);
1216 xbt_dynar_free(&keys);
1219 xbt_dict_dump(mdict,&xbt_dict_print); */
1221 xbt_dict_free(&mdict);
1222 xbt_dynar_free(&keys);
1225 #endif /* SIMGRID_TEST */