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 hash code of a string.
99 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len) {
102 #ifdef DJB2_HASH_FUNCTION
103 /* fast implementation of djb2 algorithm */
105 register unsigned int hash = 5381;
109 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
111 # elif defined(FNV_HASH_FUNCTION)
112 register unsigned int hash = 0x811c9dc5;
113 unsigned char *bp = (unsigned char *)str; /* start of buffer */
114 unsigned char *be = bp + str_len; /* beyond end of buffer */
117 /* multiply by the 32 bit FNV magic prime mod 2^32 */
118 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
120 /* xor the bottom with the current octet */
121 hash ^= (unsigned int)*bp++;
125 register unsigned int hash = 0;
128 hash += (*str) * (*str);
136 static XBT_INLINE unsigned int xbt_dict_hash(const char *str) {
137 #ifdef DJB2_HASH_FUNCTION
138 /* fast implementation of djb2 algorithm */
140 register unsigned int hash = 5381;
142 while ( (c = *str++) ) {
143 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
146 # elif defined(FNV_HASH_FUNCTION)
147 register unsigned int hash = 0x811c9dc5;
150 /* multiply by the 32 bit FNV magic prime mod 2^32 */
151 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
153 /* xor the bottom with the current octet */
154 hash ^= (unsigned int)*str++;
158 register unsigned int hash = 0;
161 hash += (*str) * (*str);
168 /* Expend the size of the dict */
169 static void xbt_dict_rehash(xbt_dict_t dict) {
170 const int oldsize = dict->table_size + 1;
171 register int newsize = oldsize * 2;
173 register xbt_dictelm_t *currcell;
174 register xbt_dictelm_t *twincell;
175 register xbt_dictelm_t bucklet;
176 register xbt_dictelm_t *pprev;
178 currcell = (xbt_dictelm_t*) xbt_realloc((char*)dict->table, newsize * sizeof(xbt_dictelm_t));
179 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
180 dict->table_size = --newsize;
181 dict->table = currcell;
182 DEBUG2("REHASH (%d->%d)",oldsize,newsize);
184 for (i=0; i<oldsize; i++,currcell++) {
185 if (!*currcell) /* empty cell */
187 twincell = currcell+oldsize;
188 for (pprev = currcell, bucklet = *currcell;
191 /* Since we use "& size" instead of "%size" and since the size was doubled,
192 each bucklet of this cell must either :
193 - stay in cell i (ie, currcell)
194 - go to the cell i+oldsize (ie, twincell) */
195 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
196 *pprev = bucklet->next;
197 bucklet->next = *twincell;
203 pprev = &bucklet->next;
208 if (!*currcell) /* everything moved */
214 * \brief Add data to the dict (arbitrary key)
215 * \param dict the container
216 * \param key the key to set the new data
217 * \param key_len the size of the \a key
218 * \param data the data to add in the dict
219 * \param free_ctn function to call with (\a key as argument) when
220 * \a key is removed from the dictionnary
222 * Set the \a data in the structure under the \a key, which can be any kind
223 * of data, as long as its length is provided in \a key_len.
225 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
226 const char *key, int key_len,
227 void *data, void_f_pvoid_t free_ctn) {
229 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
231 xbt_dictelm_t current, previous = NULL;
234 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
235 current = dict->table[hash_code & dict->table_size];
236 while (current != NULL &&
237 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
239 current = current->next;
242 if (current == NULL) {
243 /* this key doesn't exist yet */
244 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
246 if (previous == NULL) {
247 dict->table[hash_code & dict->table_size] = current;
249 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
250 xbt_dict_rehash(dict);
252 previous->next = current;
257 DEBUG6("Replace %.*s by %.*s under key %.*s",
258 key_len,(char*)current->content,
261 /* there is already an element with the same key: overwrite it */
262 if (current->content != NULL && current->free_f != NULL) {
263 current->free_f(current->content);
265 current->content = data;
266 current->free_f = free_ctn;
271 * \brief Add data to the dict (null-terminated key)
273 * \param dict the dict
274 * \param key the key to set the new data
275 * \param data the data to add in the dict
276 * \param free_ctn function to call with (\a key as argument) when
277 * \a key is removed from the dictionnary
279 * set the \a data in the structure under the \a key, which is a
280 * null terminated string.
282 void xbt_dict_set(xbt_dict_t dict,
285 void_f_pvoid_t free_ctn) {
287 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
291 * \brief Retrieve data from the dict (arbitrary key)
293 * \param dict the dealer of data
294 * \param key the key to find data
295 * \param key_len the size of the \a key
296 * \return the data that we are looking for
298 * Search the given \a key. Throws not_found_error when not found.
300 void *xbt_dict_get_ext(xbt_dict_t dict,
301 const char *key, int key_len) {
304 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
305 xbt_dictelm_t current;
309 current = dict->table[hash_code & dict->table_size];
310 while (current != NULL &&
311 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
312 current = current->next;
316 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
318 return current->content;
322 * \brief Retrieve data from the dict (null-terminated key)
324 * \param dict the dealer of data
325 * \param key the key to find data
326 * \return the data that we are looking for
328 * Search the given \a key. Throws not_found_error when not found.
329 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
332 void *xbt_dict_get(xbt_dict_t dict,
335 unsigned int hash_code = xbt_dict_hash(key);
336 xbt_dictelm_t current;
340 current = dict->table[hash_code & dict->table_size];
341 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
342 current = current->next;
345 THROW1(not_found_error, 0, "key %s not found", key);
347 return current->content;
351 * \brief like xbt_dict_get(), but returning NULL when not found
353 void *xbt_dict_get_or_null(xbt_dict_t dict,
355 unsigned int hash_code = xbt_dict_hash(key);
356 xbt_dictelm_t current;
360 current = dict->table[hash_code & dict->table_size];
361 while (current != NULL &&
362 hash_code != current->hash_code && strcmp(key, current->key))
363 current = current->next;
368 return current->content;
373 * \brief Remove data from the dict (arbitrary key)
375 * \param dict the trash can
376 * \param key the key of the data to be removed
377 * \param key_len the size of the \a key
379 * Remove the entry associated with the given \a key (throws not_found)
381 void xbt_dict_remove_ext(xbt_dict_t dict,
386 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
387 xbt_dictelm_t current, previous = NULL;
391 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
392 current = dict->table[hash_code & dict->table_size];
393 while (current != NULL &&
394 (hash_code != current->hash_code || key_len != current->key_len || strncmp(key, current->key, key_len))) {
395 previous = current; /* save the previous node */
396 current = current->next;
400 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
402 if (previous != NULL) {
403 previous->next = current->next;
405 dict->table[hash_code & dict->table_size] = current->next;
408 if (!dict->table[hash_code & dict->table_size])
411 xbt_dictelm_free(current);
416 * \brief Remove data from the dict (null-terminated key)
418 * \param dict the dict
419 * \param key the key of the data to be removed
421 * Remove the entry associated with the given \a key
423 void xbt_dict_remove(xbt_dict_t dict, const char *key) {
424 xbt_dict_remove_ext(dict, key, strlen(key));
428 * \brief Remove all data from the dict
429 * \param dict the dict
431 void xbt_dict_reset(xbt_dict_t dict) {
434 xbt_dictelm_t current, previous = NULL;
438 if (dict->count == 0)
441 for (i = 0; i <= dict->table_size; i++) {
442 current = dict->table[i];
443 while (current != NULL) {
445 current = current->next;
446 xbt_dictelm_free(previous);
448 dict->table[i] = NULL;
456 * \brief Return the number of elements in the dict.
457 * \param dict a dictionary
459 int xbt_dict_length(xbt_dict_t dict) {
466 * \brief Outputs the content of the structure (debuging purpose)
468 * \param dict the exibitionist
469 * \param output a function to dump each data in the tree
471 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
472 * function to output the data. If NULL, data won't be displayed.
475 void xbt_dict_dump(xbt_dict_t dict,
476 void_f_pvoid_t output) {
478 xbt_dictelm_t element;
479 printf("Dict %p:\n", dict);
481 for (i = 0; i < dict->table_size; i++) {
482 element = dict->table[i];
485 while (element != NULL) {
486 printf(" %s -> ", element->key);
487 if (output != NULL) {
488 (*output)(element->content);
491 element = element->next;
501 xbt_dynar_t all_sizes = NULL;
502 /** @brief shows some debugging info about the bucklet repartition */
503 void xbt_dict_dump_sizes(xbt_dict_t dict) {
508 xbt_dictelm_t element;
509 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int),NULL);
511 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill,dict->table_size);
513 for (i = 0; i < dict->table_size; i++) {
514 element = dict->table[i];
517 while (element != NULL) {
519 element = element->next;
522 if (xbt_dynar_length(sizes) <= size) {
524 xbt_dynar_set(sizes,size,&prevsize);
527 xbt_dynar_get_cpy(sizes,size,&prevsize);
529 xbt_dynar_set(sizes,size,&prevsize);
533 all_sizes = xbt_dynar_new(sizeof(int), NULL);
535 xbt_dynar_foreach(sizes,count,size) {
536 /* Copy values of this one into all_sizes */
538 if (xbt_dynar_length(all_sizes) <= count) {
540 xbt_dynar_set(all_sizes,count,&prevcount);
542 xbt_dynar_get_cpy(all_sizes,count,&prevcount);
544 xbt_dynar_set(all_sizes,count,&prevcount);
547 /* Report current sizes */
552 printf("%delm x %u cells; ",count,size);
556 xbt_dynar_free(&sizes);
560 * Destroy the dict mallocators.
561 * This is an internal XBT function called by xbt_exit().
563 void xbt_dict_exit(void) {
564 if (dict_mallocator != NULL) {
565 xbt_mallocator_free(dict_mallocator);
566 dict_mallocator = NULL;
567 xbt_mallocator_free(dict_elm_mallocator);
568 dict_elm_mallocator = NULL;
575 printf("Overall stats:");
576 xbt_dynar_foreach(all_sizes,count,size) {
581 printf("%delm x %d cells; ",count,size);
585 printf("; %f elm per cell\n",avg/(double)total_count);
589 static void* dict_mallocator_new_f(void) {
590 return xbt_new(s_xbt_dict_t, 1);
593 static void dict_mallocator_free_f(void* dict) {
597 static void dict_mallocator_reset_f(void* dict) {
598 /* nothing to do because all fields are
599 * initialized in xbt_dict_new
606 #include "portable.h"
608 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
609 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
611 XBT_TEST_SUITE("dict","Dict data container");
613 static void print_str(void *str) {
614 printf("%s",(char*)PRINTF_STR(str));
617 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
618 char *data=xbt_strdup(data_to_fill);
620 xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
622 xbt_dict_set(head,key,data,&free);
623 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
624 xbt_dict_dump(head,(void (*)(void*))&printf);
628 static void debuged_add(xbt_dict_t head,const char*key) {
629 debuged_add_ext(head,key,key);
632 static void fill(xbt_dict_t *head) {
633 xbt_test_add0("Fill in the dictionnary");
635 *head = xbt_dict_new();
636 debuged_add(*head,"12");
637 debuged_add(*head,"12a");
638 debuged_add(*head,"12b");
639 debuged_add(*head,"123");
640 debuged_add(*head,"123456");
641 /* Child becomes child of what to add */
642 debuged_add(*head,"1234");
643 /* Need of common ancestor */
644 debuged_add(*head,"123457");
648 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
651 xbt_test_add1("Search %s",key);
652 found=xbt_dict_get(head,key);
653 xbt_test_log1("Found %s",(char *)found);
655 xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
657 xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
660 static void search(xbt_dict_t head,const char*key) {
661 search_ext(head,key,key);
664 static void debuged_remove(xbt_dict_t head,const char*key) {
666 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
667 xbt_dict_remove(head,key);
668 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
672 static void traverse(xbt_dict_t head) {
673 xbt_dict_cursor_t cursor=NULL;
678 xbt_dict_foreach(head,cursor,key,data) {
679 if (!key || !data || strcmp(key,data)) {
680 xbt_test_log3("Seen #%d: %s->%s",++i,PRINTF_STR(key),PRINTF_STR(data));
682 xbt_test_log2("Seen #%d: %s",++i,PRINTF_STR(key));
684 xbt_test_assert2(!data || !strcmp(key,data),
685 "Key(%s) != value(%s). Abording",key,data);
689 static void search_not_found(xbt_dict_t head, const char *data) {
693 xbt_test_add1("Search %s (expected not to be found)",data);
696 data = xbt_dict_get(head, data);
697 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
699 if (e.category != not_found_error)
700 xbt_test_exception(e);
704 xbt_test_assert0(ok,"Exception not raised");
707 static void count(xbt_dict_t dict, int length) {
708 xbt_dict_cursor_t cursor;
714 xbt_test_add1("Count elements (expecting %d)", length);
715 xbt_test_assert2(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
717 xbt_dict_foreach(dict,cursor,key,data) {
720 xbt_test_assert2(effective == length, "Effective length(%d) != %d.", effective, length);
724 xbt_dict_t head=NULL;
728 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
729 xbt_test_add0("Traversal the null dictionnary");
732 xbt_test_add0("Traversal and search the empty dictionnary");
733 head = xbt_dict_new();
736 debuged_remove(head,"12346");
738 if (e.category != not_found_error)
739 xbt_test_exception(e);
742 xbt_dict_free(&head);
744 xbt_test_add0("Traverse the full dictionnary");
748 debuged_add_ext(head,"toto","tutu");
749 search_ext(head,"toto","tutu");
750 debuged_remove(head,"toto");
755 xbt_test_add0("Free the dictionnary (twice)");
756 xbt_dict_free(&head);
757 xbt_dict_free(&head);
762 xbt_test_add0("Change 123 to 'Changed 123'");
763 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
766 xbt_test_add0("Change 123 back to '123'");
767 xbt_dict_set(head,"123",strdup("123"),&free);
770 xbt_test_add0("Change 12a to 'Dummy 12a'");
771 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
774 xbt_test_add0("Change 12a to '12a'");
775 xbt_dict_set(head,"12a",strdup("12a"),&free);
778 xbt_test_add0("Traverse the resulting dictionnary");
782 xbt_test_add0("Search 123");
783 data = xbt_dict_get(head,"123");
784 xbt_test_assert(data);
785 xbt_test_assert(!strcmp("123",data));
787 search_not_found(head,"Can't be found");
788 search_not_found(head,"123 Can't be found");
789 search_not_found(head,"12345678 NOT");
794 search(head,"123456");
796 search(head,"123457");
798 xbt_test_add0("Traverse the resulting dictionnary");
801 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
803 xbt_test_add0("Free the dictionnary twice");
804 xbt_dict_free(&head);
805 xbt_dict_free(&head);
807 xbt_test_add0("Traverse the resulting dictionnary");
811 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
814 xbt_test_add0("Remove non existing data");
816 debuged_remove(head,"Does not exist");
818 if (e.category != not_found_error)
819 xbt_test_exception(e);
824 xbt_dict_free(&head);
826 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
828 debuged_remove(head,"12a"); traverse(head);
830 debuged_remove(head,"12b"); traverse(head);
832 debuged_remove(head,"12"); traverse(head);
834 debuged_remove(head,"123456"); traverse(head);
837 debuged_remove(head,"12346");
839 if (e.category != not_found_error)
840 xbt_test_exception(e);
844 debuged_remove(head,"1234"); traverse(head);
845 debuged_remove(head,"123457"); traverse(head);
846 debuged_remove(head,"123"); traverse(head);
848 debuged_remove(head,"12346");
850 if (e.category != not_found_error)
851 xbt_test_exception(e);
855 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
856 xbt_dict_free(&head);
858 xbt_dict_reset(head);
862 xbt_test_add0("Free the dictionnary twice");
863 xbt_dict_free(&head);
864 xbt_dict_free(&head);
867 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
870 xbt_test_add0("Store NULL under 'null'");
871 xbt_dict_set(head,"null",NULL,NULL);
872 search_ext(head,"null",NULL);
874 xbt_test_add0("Check whether I see it while traversing...");
876 xbt_dict_cursor_t cursor=NULL;
880 xbt_dict_foreach(head,cursor,key,data) {
881 if (!key || !data || strcmp(key,data)) {
882 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
884 xbt_test_log1("Seen: %s",PRINTF_STR(key));
887 if (!strcmp(key,"null"))
890 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
892 xbt_dict_free(&head);
896 #define SIZEOFKEY 1024
897 static int countelems(xbt_dict_t head) {
898 xbt_dict_cursor_t cursor;
903 xbt_dict_foreach(head,cursor,key,data) {
909 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
910 xbt_dict_t head=NULL;
915 srand((unsigned int)time(NULL));
918 xbt_test_add2("CRASH test number %d (%d to go)",i+1,10-i-1);
919 xbt_test_log0("Fill the struct, count its elems and frees the structure");
920 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
922 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
924 for (j=0;j<1000;j++) {
926 key=xbt_malloc(SIZEOFKEY);
929 for (k=0;k<SIZEOFKEY-1;k++)
930 key[k]=rand() % ('z' - 'a') + 'a';
932 /* printf("[%d %s]\n",j,key); */
933 data = xbt_dict_get_or_null(head,key);
934 } while (data != NULL);
936 xbt_dict_set(head,key,key,&free);
937 data = xbt_dict_get(head,key);
938 xbt_test_assert2(!strcmp(key,data), "Retrieved value (%s) != Injected value (%s)",key,data);
942 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
944 xbt_dict_free(&head);
945 xbt_dict_free(&head);
950 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
951 for (j=0;j<NB_ELM;j++) {
952 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
954 key = xbt_malloc(10);
957 xbt_dict_set(head,key,key,&free);
959 /*xbt_dict_dump(head,(void (*)(void*))&printf);*/
961 xbt_test_add0("Count the elements (retrieving the key and data for each)");
962 i = countelems(head);
963 xbt_test_log1("There is %d elements",i);
965 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
968 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
969 for (j=0;j<NB_ELM;j++) {
972 data = xbt_dict_get(head,key);
973 xbt_test_assert2(!strcmp(key,(char*)data),
974 "with get, key=%s != data=%s",key,(char*)data);
975 data = xbt_dict_get_ext(head,key,strlen(key));
976 xbt_test_assert2(!strcmp(key,(char*)data),
977 "with get_ext, key=%s != data=%s",key,(char*)data);
982 xbt_test_add1("Remove my %d elements",NB_ELM);
984 for (j=0;j<NB_ELM;j++) {
985 /* if (!(j%10000)) printf("."); fflush(stdout); */
988 xbt_dict_remove(head,key);
993 xbt_test_add0("Free the structure (twice)");
994 xbt_dict_free(&head);
995 xbt_dict_free(&head);
998 static void str_free(void *s) {
1003 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
1006 #define NB_ELM 100 /*00*/
1008 #define KEY_SIZE 512
1009 #define NB_TEST 20 /*20*/
1012 xbt_dict_t mdict = NULL;
1014 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
1019 xbt_test_add0("Generic multicache CRASH test");
1020 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1021 "depth of multicache=%d, key size=%d",
1022 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
1024 for (l=0 ; l<DEPTH ; l++) {
1025 key=xbt_malloc(KEY_SIZE);
1026 xbt_dynar_push(keys,&key);
1029 for (i=0;i<NB_TEST;i++) {
1030 mdict = xbt_dict_new();
1031 VERB1("mdict=%p",mdict);
1033 printf("Test %d\n",i);
1034 /* else if (i%10) printf("."); else printf("%d",i/10);*/
1036 for (j=0;j<NB_ELM;j++) {
1037 if (verbose>0) printf (" Add {");
1039 for (l=0 ; l<DEPTH ; l++) {
1040 key=*(char**)xbt_dynar_get_ptr(keys,l);
1042 for (k=0;k<KEY_SIZE-1;k++)
1043 key[k]=rand() % ('z' - 'a') + 'a';
1047 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
1049 if (verbose>0) printf("in multitree %p.\n",mdict);
1051 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
1053 data = xbt_multidict_get(mdict,keys);
1055 xbt_test_assert2(data && !strcmp((char*)data,key),
1056 "Retrieved value (%s) does not match the entrered one (%s)\n",
1059 xbt_dict_free(&mdict);
1062 xbt_dynar_free(&keys);
1065 xbt_dict_dump(mdict,&xbt_dict_print);*/
1067 xbt_dict_free(&mdict);
1068 xbt_dynar_free(&keys);
1071 #endif /* SIMGRID_TEST */