3 /* dict - a generic dictionnary, 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 functionnalities 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 dictionnary with a default hashtable size.
40 xbt_dict_t xbt_dict_new(void) {
43 if (dict_mallocator == NULL) {
45 dict_mallocator = xbt_mallocator_new(256,
46 dict_mallocator_new_f,
47 dict_mallocator_free_f,
48 dict_mallocator_reset_f);
49 dict_elm_mallocator = xbt_mallocator_new(256,
50 dict_elm_mallocator_new_f,
51 dict_elm_mallocator_free_f,
52 dict_elm_mallocator_reset_f);
55 dict = xbt_mallocator_get(dict_mallocator);
56 dict->table_size = 127;
57 dict->table = xbt_new0(xbt_dictelm_t, dict->table_size+1);
66 * \param dict the dictionnary to be freed
68 * Frees a dictionary with all the data
70 void xbt_dict_free(xbt_dict_t *dict) {
72 xbt_dictelm_t current, previous;
76 // if ( *dict ) xbt_dict_dump_sizes(*dict);
78 if (dict != NULL && *dict != NULL) {
79 table_size = (*dict)->table_size;
80 table = (*dict)->table;
81 for (i = 0; (*dict)->count && i < table_size; i++) {
83 while (current != NULL) {
85 current = current->next;
86 xbt_dictelm_free(previous);
91 xbt_mallocator_release(dict_mallocator, *dict);
97 * Returns the amount of elements in the dict
99 unsigned int xbt_dict_size(xbt_dict_t dict) {
104 * Returns the hash code of a string.
106 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len) {
109 #ifdef DJB2_HASH_FUNCTION
110 /* fast implementation of djb2 algorithm */
112 register unsigned int hash = 5381;
116 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
118 # elif defined(FNV_HASH_FUNCTION)
119 register unsigned int hash = 0x811c9dc5;
120 unsigned char *bp = (unsigned char *)str; /* start of buffer */
121 unsigned char *be = bp + str_len; /* beyond end of buffer */
124 /* multiply by the 32 bit FNV magic prime mod 2^32 */
125 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
127 /* xor the bottom with the current octet */
128 hash ^= (unsigned int)*bp++;
132 register unsigned int hash = 0;
135 hash += (*str) * (*str);
143 static XBT_INLINE unsigned int xbt_dict_hash(const char *str) {
144 #ifdef DJB2_HASH_FUNCTION
145 /* fast implementation of djb2 algorithm */
147 register unsigned int hash = 5381;
149 while ( (c = *str++) ) {
150 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
153 # elif defined(FNV_HASH_FUNCTION)
154 register unsigned int hash = 0x811c9dc5;
157 /* multiply by the 32 bit FNV magic prime mod 2^32 */
158 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
160 /* xor the bottom with the current octet */
161 hash ^= (unsigned int)*str++;
165 register unsigned int hash = 0;
168 hash += (*str) * (*str);
175 /* Expend the size of the dict */
176 static void xbt_dict_rehash(xbt_dict_t dict) {
177 const int oldsize = dict->table_size + 1;
178 register int newsize = oldsize * 2;
180 register xbt_dictelm_t *currcell;
181 register xbt_dictelm_t *twincell;
182 register xbt_dictelm_t bucklet;
183 register xbt_dictelm_t *pprev;
185 currcell = (xbt_dictelm_t*) xbt_realloc((char*)dict->table, newsize * sizeof(xbt_dictelm_t));
186 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
187 dict->table_size = --newsize;
188 dict->table = currcell;
189 DEBUG2("REHASH (%d->%d)",oldsize,newsize);
191 for (i=0; i<oldsize; i++,currcell++) {
192 if (!*currcell) /* empty cell */
194 twincell = currcell+oldsize;
195 for (pprev = currcell, bucklet = *currcell;
198 /* Since we use "& size" instead of "%size" and since the size was doubled,
199 each bucklet of this cell must either :
200 - stay in cell i (ie, currcell)
201 - go to the cell i+oldsize (ie, twincell) */
202 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
203 *pprev = bucklet->next;
204 bucklet->next = *twincell;
210 pprev = &bucklet->next;
215 if (!*currcell) /* everything moved */
221 * \brief Add data to the dict (arbitrary key)
222 * \param dict the container
223 * \param key the key to set the new data
224 * \param key_len the size of the \a key
225 * \param data the data to add in the dict
226 * \param free_ctn function to call with (\a key as argument) when
227 * \a key is removed from the dictionnary
229 * Set the \a data in the structure under the \a key, which can be any kind
230 * of data, as long as its length is provided in \a key_len.
232 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
233 const char *key, int key_len,
234 void *data, void_f_pvoid_t free_ctn) {
236 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
238 xbt_dictelm_t current, previous = NULL;
241 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
242 current = dict->table[hash_code & dict->table_size];
243 while (current != NULL &&
244 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
246 current = current->next;
249 if (current == NULL) {
250 /* this key doesn't exist yet */
251 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
253 if (previous == NULL) {
254 dict->table[hash_code & dict->table_size] = current;
256 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
257 xbt_dict_rehash(dict);
259 previous->next = current;
264 DEBUG6("Replace %.*s by %.*s under key %.*s",
265 key_len,(char*)current->content,
268 /* there is already an element with the same key: overwrite it */
269 if (current->content != NULL && current->free_f != NULL) {
270 current->free_f(current->content);
272 current->content = data;
273 current->free_f = free_ctn;
278 * \brief Add data to the dict (null-terminated key)
280 * \param dict the dict
281 * \param key the key to set the new data
282 * \param data the data to add in the dict
283 * \param free_ctn function to call with (\a key as argument) when
284 * \a key is removed from the dictionnary
286 * set the \a data in the structure under the \a key, which is a
287 * null terminated string.
289 void xbt_dict_set(xbt_dict_t dict,
292 void_f_pvoid_t free_ctn) {
294 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
298 * \brief Retrieve data from the dict (arbitrary key)
300 * \param dict the dealer of data
301 * \param key the key to find data
302 * \param key_len the size of the \a key
303 * \return the data that we are looking for
305 * Search the given \a key. Throws not_found_error when not found.
307 void *xbt_dict_get_ext(xbt_dict_t dict,
308 const char *key, int key_len) {
311 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
312 xbt_dictelm_t current;
316 current = dict->table[hash_code & dict->table_size];
317 while (current != NULL &&
318 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
319 current = current->next;
323 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
325 return current->content;
328 * \brief like xbt_dict_get_ext(), but returning NULL when not found
330 void *xbt_dict_get_or_null_ext(xbt_dict_t dict,
331 const char *key, int key_len) {
332 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
333 xbt_dictelm_t current;
337 current = dict->table[hash_code & dict->table_size];
338 while (current != NULL &&
339 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
340 current = current->next;
346 return current->content;
350 * \brief Retrieve data from the dict (null-terminated key)
352 * \param dict the dealer of data
353 * \param key the key to find data
354 * \return the data that we are looking for
356 * Search the given \a key. Throws not_found_error when not found.
357 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
360 void *xbt_dict_get(xbt_dict_t dict,
363 unsigned int hash_code = xbt_dict_hash(key);
364 xbt_dictelm_t current;
368 current = dict->table[hash_code & dict->table_size];
369 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
370 current = current->next;
373 THROW1(not_found_error, 0, "key %s not found", key);
375 return current->content;
379 * \brief like xbt_dict_get(), but returning NULL when not found
381 void *xbt_dict_get_or_null(xbt_dict_t dict,
383 unsigned int hash_code = xbt_dict_hash(key);
384 xbt_dictelm_t current;
388 current = dict->table[hash_code & dict->table_size];
389 while (current != NULL &&
390 hash_code != current->hash_code && strcmp(key, current->key))
391 current = current->next;
396 return current->content;
401 * \brief Remove data from the dict (arbitrary key)
403 * \param dict the trash can
404 * \param key the key of the data to be removed
405 * \param key_len the size of the \a key
407 * Remove the entry associated with the given \a key (throws not_found)
409 void xbt_dict_remove_ext(xbt_dict_t dict,
414 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
415 xbt_dictelm_t current, previous = NULL;
419 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
420 current = dict->table[hash_code & dict->table_size];
421 while (current != NULL &&
422 (hash_code != current->hash_code || key_len != current->key_len || strncmp(key, current->key, key_len))) {
423 previous = current; /* save the previous node */
424 current = current->next;
428 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
430 if (previous != NULL) {
431 previous->next = current->next;
433 dict->table[hash_code & dict->table_size] = current->next;
436 if (!dict->table[hash_code & dict->table_size])
439 xbt_dictelm_free(current);
444 * \brief Remove data from the dict (null-terminated key)
446 * \param dict the dict
447 * \param key the key of the data to be removed
449 * Remove the entry associated with the given \a key
451 void xbt_dict_remove(xbt_dict_t dict, const char *key) {
452 xbt_dict_remove_ext(dict, key, strlen(key));
456 * \brief Remove all data from the dict
457 * \param dict the dict
459 void xbt_dict_reset(xbt_dict_t dict) {
462 xbt_dictelm_t current, previous = NULL;
466 if (dict->count == 0)
469 for (i = 0; i <= dict->table_size; i++) {
470 current = dict->table[i];
471 while (current != NULL) {
473 current = current->next;
474 xbt_dictelm_free(previous);
476 dict->table[i] = NULL;
484 * \brief Return the number of elements in the dict.
485 * \param dict a dictionary
487 int xbt_dict_length(xbt_dict_t dict) {
494 * \brief Outputs the content of the structure (debuging purpose)
496 * \param dict the exibitionist
497 * \param output a function to dump each data in the tree
499 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
500 * function to output the data. If NULL, data won't be displayed.
503 void xbt_dict_dump(xbt_dict_t dict,
504 void_f_pvoid_t output) {
506 xbt_dictelm_t element;
507 printf("Dict %p:\n", dict);
509 for (i = 0; i < dict->table_size; i++) {
510 element = dict->table[i];
513 while (element != NULL) {
514 printf(" %s -> ", element->key);
515 if (output != NULL) {
516 (*output)(element->content);
519 element = element->next;
529 xbt_dynar_t all_sizes = NULL;
530 /** @brief shows some debugging info about the bucklet repartition */
531 void xbt_dict_dump_sizes(xbt_dict_t dict) {
536 xbt_dictelm_t element;
537 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int),NULL);
539 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill,dict->table_size);
541 for (i = 0; i < dict->table_size; i++) {
542 element = dict->table[i];
545 while (element != NULL) {
547 element = element->next;
550 if (xbt_dynar_length(sizes) <= size) {
552 xbt_dynar_set(sizes,size,&prevsize);
555 xbt_dynar_get_cpy(sizes,size,&prevsize);
557 xbt_dynar_set(sizes,size,&prevsize);
561 all_sizes = xbt_dynar_new(sizeof(int), NULL);
563 xbt_dynar_foreach(sizes,count,size) {
564 /* Copy values of this one into all_sizes */
566 if (xbt_dynar_length(all_sizes) <= count) {
568 xbt_dynar_set(all_sizes,count,&prevcount);
570 xbt_dynar_get_cpy(all_sizes,count,&prevcount);
572 xbt_dynar_set(all_sizes,count,&prevcount);
575 /* Report current sizes */
580 printf("%delm x %u cells; ",count,size);
584 xbt_dynar_free(&sizes);
588 * Destroy the dict mallocators.
589 * This is an internal XBT function called by xbt_exit().
591 void xbt_dict_exit(void) {
592 if (dict_mallocator != NULL) {
593 xbt_mallocator_free(dict_mallocator);
594 dict_mallocator = NULL;
595 xbt_mallocator_free(dict_elm_mallocator);
596 dict_elm_mallocator = NULL;
603 printf("Overall stats:");
604 xbt_dynar_foreach(all_sizes,count,size) {
609 printf("%delm x %d cells; ",count,size);
613 printf("; %f elm per cell\n",avg/(double)total_count);
617 static void* dict_mallocator_new_f(void) {
618 return xbt_new(s_xbt_dict_t, 1);
621 static void dict_mallocator_free_f(void* dict) {
625 static void dict_mallocator_reset_f(void* dict) {
626 /* nothing to do because all fields are
627 * initialized in xbt_dict_new
634 #include "portable.h"
636 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
637 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
639 XBT_TEST_SUITE("dict","Dict data container");
641 static void print_str(void *str) {
642 printf("%s",(char*)PRINTF_STR(str));
645 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
646 char *data=xbt_strdup(data_to_fill);
648 xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
650 xbt_dict_set(head,key,data,&free);
651 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
652 xbt_dict_dump(head,(void (*)(void*))&printf);
656 static void debuged_add(xbt_dict_t head,const char*key) {
657 debuged_add_ext(head,key,key);
660 static void fill(xbt_dict_t *head) {
661 xbt_test_add0("Fill in the dictionnary");
663 *head = xbt_dict_new();
664 debuged_add(*head,"12");
665 debuged_add(*head,"12a");
666 debuged_add(*head,"12b");
667 debuged_add(*head,"123");
668 debuged_add(*head,"123456");
669 /* Child becomes child of what to add */
670 debuged_add(*head,"1234");
671 /* Need of common ancestor */
672 debuged_add(*head,"123457");
676 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
679 xbt_test_add1("Search %s",key);
680 found=xbt_dict_get(head,key);
681 xbt_test_log1("Found %s",(char *)found);
683 xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
685 xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
688 static void search(xbt_dict_t head,const char*key) {
689 search_ext(head,key,key);
692 static void debuged_remove(xbt_dict_t head,const char*key) {
694 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
695 xbt_dict_remove(head,key);
696 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
700 static void traverse(xbt_dict_t head) {
701 xbt_dict_cursor_t cursor=NULL;
706 xbt_dict_foreach(head,cursor,key,data) {
707 if (!key || !data || strcmp(key,data)) {
708 xbt_test_log3("Seen #%d: %s->%s",++i,PRINTF_STR(key),PRINTF_STR(data));
710 xbt_test_log2("Seen #%d: %s",++i,PRINTF_STR(key));
712 xbt_test_assert2(!data || !strcmp(key,data),
713 "Key(%s) != value(%s). Abording",key,data);
717 static void search_not_found(xbt_dict_t head, const char *data) {
721 xbt_test_add1("Search %s (expected not to be found)",data);
724 data = xbt_dict_get(head, data);
725 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
727 if (e.category != not_found_error)
728 xbt_test_exception(e);
732 xbt_test_assert0(ok,"Exception not raised");
735 static void count(xbt_dict_t dict, int length) {
736 xbt_dict_cursor_t cursor;
742 xbt_test_add1("Count elements (expecting %d)", length);
743 xbt_test_assert2(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
745 xbt_dict_foreach(dict,cursor,key,data) {
748 xbt_test_assert2(effective == length, "Effective length(%d) != %d.", effective, length);
752 xbt_dict_t head=NULL;
756 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
757 xbt_test_add0("Traversal the null dictionnary");
760 xbt_test_add0("Traversal and search the empty dictionnary");
761 head = xbt_dict_new();
764 debuged_remove(head,"12346");
766 if (e.category != not_found_error)
767 xbt_test_exception(e);
770 xbt_dict_free(&head);
772 xbt_test_add0("Traverse the full dictionnary");
776 debuged_add_ext(head,"toto","tutu");
777 search_ext(head,"toto","tutu");
778 debuged_remove(head,"toto");
783 xbt_test_add0("Free the dictionnary (twice)");
784 xbt_dict_free(&head);
785 xbt_dict_free(&head);
790 xbt_test_add0("Change 123 to 'Changed 123'");
791 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
794 xbt_test_add0("Change 123 back to '123'");
795 xbt_dict_set(head,"123",strdup("123"),&free);
798 xbt_test_add0("Change 12a to 'Dummy 12a'");
799 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
802 xbt_test_add0("Change 12a to '12a'");
803 xbt_dict_set(head,"12a",strdup("12a"),&free);
806 xbt_test_add0("Traverse the resulting dictionnary");
810 xbt_test_add0("Search 123");
811 data = xbt_dict_get(head,"123");
812 xbt_test_assert(data);
813 xbt_test_assert(!strcmp("123",data));
815 search_not_found(head,"Can't be found");
816 search_not_found(head,"123 Can't be found");
817 search_not_found(head,"12345678 NOT");
822 search(head,"123456");
824 search(head,"123457");
826 xbt_test_add0("Traverse the resulting dictionnary");
829 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
831 xbt_test_add0("Free the dictionnary twice");
832 xbt_dict_free(&head);
833 xbt_dict_free(&head);
835 xbt_test_add0("Traverse the resulting dictionnary");
839 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
842 xbt_test_add0("Remove non existing data");
844 debuged_remove(head,"Does not exist");
846 if (e.category != not_found_error)
847 xbt_test_exception(e);
852 xbt_dict_free(&head);
854 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
856 debuged_remove(head,"12a"); traverse(head);
858 debuged_remove(head,"12b"); traverse(head);
860 debuged_remove(head,"12"); traverse(head);
862 debuged_remove(head,"123456"); traverse(head);
865 debuged_remove(head,"12346");
867 if (e.category != not_found_error)
868 xbt_test_exception(e);
872 debuged_remove(head,"1234"); traverse(head);
873 debuged_remove(head,"123457"); traverse(head);
874 debuged_remove(head,"123"); traverse(head);
876 debuged_remove(head,"12346");
878 if (e.category != not_found_error)
879 xbt_test_exception(e);
883 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
884 xbt_dict_free(&head);
886 xbt_dict_reset(head);
890 xbt_test_add0("Free the dictionnary twice");
891 xbt_dict_free(&head);
892 xbt_dict_free(&head);
895 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
898 xbt_test_add0("Store NULL under 'null'");
899 xbt_dict_set(head,"null",NULL,NULL);
900 search_ext(head,"null",NULL);
902 xbt_test_add0("Check whether I see it while traversing...");
904 xbt_dict_cursor_t cursor=NULL;
908 xbt_dict_foreach(head,cursor,key,data) {
909 if (!key || !data || strcmp(key,data)) {
910 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
912 xbt_test_log1("Seen: %s",PRINTF_STR(key));
915 if (!strcmp(key,"null"))
918 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
920 xbt_dict_free(&head);
924 #define SIZEOFKEY 1024
925 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"){
938 xbt_dict_t head=NULL;
943 srand((unsigned int)time(NULL));
946 xbt_test_add2("CRASH test number %d (%d to go)",i+1,10-i-1);
947 xbt_test_log0("Fill the struct, count its elems and frees the structure");
948 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
950 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
952 for (j=0;j<1000;j++) {
954 key=xbt_malloc(SIZEOFKEY);
957 for (k=0;k<SIZEOFKEY-1;k++)
958 key[k]=rand() % ('z' - 'a') + 'a';
960 /* printf("[%d %s]\n",j,key); */
961 data = xbt_dict_get_or_null(head,key);
962 } while (data != NULL);
964 xbt_dict_set(head,key,key,&free);
965 data = xbt_dict_get(head,key);
966 xbt_test_assert2(!strcmp(key,data), "Retrieved value (%s) != Injected value (%s)",key,data);
970 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
972 xbt_dict_free(&head);
973 xbt_dict_free(&head);
978 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
979 for (j=0;j<NB_ELM;j++) {
980 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
982 key = xbt_malloc(10);
985 xbt_dict_set(head,key,key,&free);
987 /*xbt_dict_dump(head,(void (*)(void*))&printf);*/
989 xbt_test_add0("Count the elements (retrieving the key and data for each)");
990 i = countelems(head);
991 xbt_test_log1("There is %d elements",i);
993 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
996 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
997 for (j=0;j<NB_ELM;j++) {
1000 data = xbt_dict_get(head,key);
1001 xbt_test_assert2(!strcmp(key,(char*)data),
1002 "with get, key=%s != data=%s",key,(char*)data);
1003 data = xbt_dict_get_ext(head,key,strlen(key));
1004 xbt_test_assert2(!strcmp(key,(char*)data),
1005 "with get_ext, key=%s != data=%s",key,(char*)data);
1010 xbt_test_add1("Remove my %d elements",NB_ELM);
1012 for (j=0;j<NB_ELM;j++) {
1013 /* if (!(j%10000)) printf("."); fflush(stdout); */
1015 sprintf(key,"%d",j);
1016 xbt_dict_remove(head,key);
1021 xbt_test_add0("Free the structure (twice)");
1022 xbt_dict_free(&head);
1023 xbt_dict_free(&head);
1026 static void str_free(void *s) {
1031 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
1034 #define NB_ELM 100 /*00*/
1036 #define KEY_SIZE 512
1037 #define NB_TEST 20 /*20*/
1040 xbt_dict_t mdict = NULL;
1042 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
1047 xbt_test_add0("Generic multicache CRASH test");
1048 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1049 "depth of multicache=%d, key size=%d",
1050 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
1052 for (l=0 ; l<DEPTH ; l++) {
1053 key=xbt_malloc(KEY_SIZE);
1054 xbt_dynar_push(keys,&key);
1057 for (i=0;i<NB_TEST;i++) {
1058 mdict = xbt_dict_new();
1059 VERB1("mdict=%p",mdict);
1061 printf("Test %d\n",i);
1062 /* else if (i%10) printf("."); else printf("%d",i/10);*/
1064 for (j=0;j<NB_ELM;j++) {
1065 if (verbose>0) printf (" Add {");
1067 for (l=0 ; l<DEPTH ; l++) {
1068 key=*(char**)xbt_dynar_get_ptr(keys,l);
1070 for (k=0;k<KEY_SIZE-1;k++)
1071 key[k]=rand() % ('z' - 'a') + 'a';
1075 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
1077 if (verbose>0) printf("in multitree %p.\n",mdict);
1079 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
1081 data = xbt_multidict_get(mdict,keys);
1083 xbt_test_assert2(data && !strcmp((char*)data,key),
1084 "Retrieved value (%s) does not match the entrered one (%s)\n",
1087 xbt_dict_free(&mdict);
1090 xbt_dynar_free(&keys);
1093 xbt_dict_dump(mdict,&xbt_dict_print);*/
1095 xbt_dict_free(&mdict);
1096 xbt_dynar_free(&keys);
1099 #endif /* SIMGRID_TEST */