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; i < table_size; i++) {
83 while (current != NULL) {
85 current = current->next;
86 xbt_dictelm_free(previous);
90 xbt_mallocator_release(dict_mallocator, *dict);
96 * Returns the hash code of a string.
98 static XBT_INLINE unsigned int xbt_dict_hash_ext(const char *str, int str_len) {
101 #ifdef DJB2_HASH_FUNCTION
102 /* fast implementation of djb2 algorithm */
104 register unsigned int hash = 5381;
108 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
110 # elif defined(FNV_HASH_FUNCTION)
111 register unsigned int hash = 0x811c9dc5;
112 unsigned char *bp = (unsigned char *)str; /* start of buffer */
113 unsigned char *be = bp + str_len; /* beyond end of buffer */
116 /* multiply by the 32 bit FNV magic prime mod 2^32 */
117 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
119 /* xor the bottom with the current octet */
120 hash ^= (unsigned int)*bp++;
124 register unsigned int hash = 0;
127 hash += (*str) * (*str);
135 static XBT_INLINE unsigned int xbt_dict_hash(const char *str) {
136 #ifdef DJB2_HASH_FUNCTION
137 /* fast implementation of djb2 algorithm */
139 register unsigned int hash = 5381;
141 while ( (c = *str++) ) {
142 hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
145 # elif defined(FNV_HASH_FUNCTION)
146 register unsigned int hash = 0x811c9dc5;
149 /* multiply by the 32 bit FNV magic prime mod 2^32 */
150 hash += (hash<<1) + (hash<<4) + (hash<<7) + (hash<<8) + (hash<<24);
152 /* xor the bottom with the current octet */
153 hash ^= (unsigned int)*str++;
157 register unsigned int hash = 0;
160 hash += (*str) * (*str);
167 /* Expend the size of the dict */
168 static void xbt_dict_rehash(xbt_dict_t dict) {
169 const int oldsize = dict->table_size + 1;
170 register int newsize = oldsize * 2;
172 register xbt_dictelm_t *currcell;
173 register xbt_dictelm_t *twincell;
174 register xbt_dictelm_t bucklet;
175 register xbt_dictelm_t *pprev;
177 currcell = (xbt_dictelm_t*) xbt_realloc((char*)dict->table, newsize * sizeof(xbt_dictelm_t));
178 memset(&currcell[oldsize], 0, oldsize * sizeof(xbt_dictelm_t)); /* zero second half */
179 dict->table_size = --newsize;
180 dict->table = currcell;
181 DEBUG2("REHASH (%d->%d)",oldsize,newsize);
183 for (i=0; i<oldsize; i++,currcell++) {
184 if (!*currcell) /* empty cell */
186 twincell = currcell+oldsize;
187 for (pprev = currcell, bucklet = *currcell;
190 /* Since we use "& size" instead of "%size" and since the size was doubled,
191 each bucklet of this cell must either :
192 - stay in cell i (ie, currcell)
193 - go to the cell i+oldsize (ie, twincell) */
194 if ((bucklet->hash_code & newsize) != i) { /* Move to b */
195 *pprev = bucklet->next;
196 bucklet->next = *twincell;
202 pprev = &bucklet->next;
207 if (!*currcell) /* everything moved */
213 * \brief Add data to the dict (arbitrary key)
214 * \param dict the container
215 * \param key the key to set the new data
216 * \param key_len the size of the \a key
217 * \param data the data to add in the dict
218 * \param free_ctn function to call with (\a key as argument) when
219 * \a key is removed from the dictionnary
221 * Set the \a data in the structure under the \a key, which can be any kind
222 * of data, as long as its length is provided in \a key_len.
224 XBT_INLINE void xbt_dict_set_ext(xbt_dict_t dict,
225 const char *key, int key_len,
226 void *data, void_f_pvoid_t free_ctn) {
228 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
230 xbt_dictelm_t current, previous = NULL;
233 DEBUG5("ADD %.*s hash = %d, size = %d, & = %d",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
234 current = dict->table[hash_code & dict->table_size];
235 while (current != NULL &&
236 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
238 current = current->next;
241 if (current == NULL) {
242 /* this key doesn't exist yet */
243 current = xbt_dictelm_new(key, key_len, hash_code, data, free_ctn);
245 if (previous == NULL) {
246 dict->table[hash_code & dict->table_size] = current;
248 if ((dict->fill * 100) / (dict->table_size + 1) > MAX_FILL_PERCENT)
249 xbt_dict_rehash(dict);
251 previous->next = current;
256 DEBUG6("Replace %.*s by %.*s under key %.*s",
257 key_len,(char*)current->content,
260 /* there is already an element with the same key: overwrite it */
261 if (current->content != NULL && current->free_f != NULL) {
262 current->free_f(current->content);
264 current->content = data;
265 current->free_f = free_ctn;
270 * \brief Add data to the dict (null-terminated key)
272 * \param dict the dict
273 * \param key the key to set the new data
274 * \param data the data to add in the dict
275 * \param free_ctn function to call with (\a key as argument) when
276 * \a key is removed from the dictionnary
278 * set the \a data in the structure under the \a key, which is a
279 * null terminated string.
281 void xbt_dict_set(xbt_dict_t dict,
284 void_f_pvoid_t free_ctn) {
286 xbt_dict_set_ext(dict, key, strlen(key), data, free_ctn);
290 * \brief Retrieve data from the dict (arbitrary key)
292 * \param dict the dealer of data
293 * \param key the key to find data
294 * \param key_len the size of the \a key
295 * \return the data that we are looking for
297 * Search the given \a key. Throws not_found_error when not found.
299 void *xbt_dict_get_ext(xbt_dict_t dict,
300 const char *key, int key_len) {
303 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
304 xbt_dictelm_t current;
308 current = dict->table[hash_code & dict->table_size];
309 while (current != NULL &&
310 (hash_code != current->hash_code || key_len != current->key_len || memcmp(key, current->key, key_len))) {
311 current = current->next;
315 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
317 return current->content;
321 * \brief Retrieve data from the dict (null-terminated key)
323 * \param dict the dealer of data
324 * \param key the key to find data
325 * \return the data that we are looking for
327 * Search the given \a key. Throws not_found_error when not found.
328 * Check xbt_dict_get_or_null() for a version returning NULL without exception when
331 void *xbt_dict_get(xbt_dict_t dict,
334 unsigned int hash_code = xbt_dict_hash(key);
335 xbt_dictelm_t current;
339 current = dict->table[hash_code & dict->table_size];
340 while (current != NULL && (hash_code != current->hash_code || strcmp(key, current->key)))
341 current = current->next;
344 THROW1(not_found_error, 0, "key %s not found", key);
346 return current->content;
350 * \brief like xbt_dict_get(), but returning NULL when not found
352 void *xbt_dict_get_or_null(xbt_dict_t dict,
354 unsigned int hash_code = xbt_dict_hash(key);
355 xbt_dictelm_t current;
359 current = dict->table[hash_code & dict->table_size];
360 while (current != NULL &&
361 hash_code != current->hash_code && strcmp(key, current->key))
362 current = current->next;
367 return current->content;
372 * \brief Remove data from the dict (arbitrary key)
374 * \param dict the trash can
375 * \param key the key of the data to be removed
376 * \param key_len the size of the \a key
378 * Remove the entry associated with the given \a key (throws not_found)
380 void xbt_dict_remove_ext(xbt_dict_t dict,
385 unsigned int hash_code = xbt_dict_hash_ext(key,key_len);
386 xbt_dictelm_t current, previous = NULL;
390 // fprintf(stderr,"RM %.*s hash = %d, size = %d, & = %d\n",key_len,key,hash_code, dict->table_size, hash_code & dict->table_size);
391 current = dict->table[hash_code & dict->table_size];
392 while (current != NULL &&
393 (hash_code != current->hash_code || key_len != current->key_len || strncmp(key, current->key, key_len))) {
394 previous = current; /* save the previous node */
395 current = current->next;
399 THROW2(not_found_error, 0, "key %.*s not found", key_len, key);
401 if (previous != NULL) {
402 previous->next = current->next;
404 dict->table[hash_code & dict->table_size] = current->next;
407 if (!dict->table[hash_code & dict->table_size])
410 xbt_dictelm_free(current);
415 * \brief Remove data from the dict (null-terminated key)
417 * \param dict the dict
418 * \param key the key of the data to be removed
420 * Remove the entry associated with the given \a key
422 void xbt_dict_remove(xbt_dict_t dict, const char *key) {
423 xbt_dict_remove_ext(dict, key, strlen(key));
427 * \brief Remove all data from the dict
428 * \param dict the dict
430 void xbt_dict_reset(xbt_dict_t dict) {
433 xbt_dictelm_t current, previous = NULL;
437 if (dict->count == 0)
440 for (i = 0; i <= dict->table_size; i++) {
441 current = dict->table[i];
442 while (current != NULL) {
444 current = current->next;
445 xbt_dictelm_free(previous);
447 dict->table[i] = NULL;
455 * \brief Return the number of elements in the dict.
456 * \param dict a dictionary
458 int xbt_dict_length(xbt_dict_t dict) {
465 * \brief Outputs the content of the structure (debuging purpose)
467 * \param dict the exibitionist
468 * \param output a function to dump each data in the tree
470 * Ouputs the content of the structure. (for debuging purpose). \a ouput is a
471 * function to output the data. If NULL, data won't be displayed.
474 void xbt_dict_dump(xbt_dict_t dict,
475 void_f_pvoid_t output) {
477 xbt_dictelm_t element;
478 printf("Dict %p:\n", dict);
480 for (i = 0; i < dict->table_size; i++) {
481 element = dict->table[i];
484 while (element != NULL) {
485 printf(" %s -> ", element->key);
486 if (output != NULL) {
487 (*output)(element->content);
490 element = element->next;
500 xbt_dynar_t all_sizes = NULL;
501 /** @brief shows some debugging info about the bucklet repartition */
502 void xbt_dict_dump_sizes(xbt_dict_t dict) {
507 xbt_dictelm_t element;
508 xbt_dynar_t sizes = xbt_dynar_new(sizeof(int),NULL);
510 printf("Dict %p: %d bucklets, %d used cells (of %d) ", dict, dict->count, dict->fill,dict->table_size);
512 for (i = 0; i < dict->table_size; i++) {
513 element = dict->table[i];
516 while (element != NULL) {
518 element = element->next;
521 if (xbt_dynar_length(sizes) <= size) {
523 xbt_dynar_set(sizes,size,&prevsize);
526 xbt_dynar_get_cpy(sizes,size,&prevsize);
528 xbt_dynar_set(sizes,size,&prevsize);
532 all_sizes = xbt_dynar_new(sizeof(int), NULL);
534 xbt_dynar_foreach(sizes,count,size) {
535 /* Copy values of this one into all_sizes */
537 if (xbt_dynar_length(all_sizes) <= count) {
539 xbt_dynar_set(all_sizes,count,&prevcount);
541 xbt_dynar_get_cpy(all_sizes,count,&prevcount);
543 xbt_dynar_set(all_sizes,count,&prevcount);
546 /* Report current sizes */
551 printf("%delm x %u cells; ",count,size);
555 xbt_dynar_free(&sizes);
559 * Destroy the dict mallocators.
560 * This is an internal XBT function called by xbt_exit().
562 void xbt_dict_exit(void) {
563 if (dict_mallocator != NULL) {
564 xbt_mallocator_free(dict_mallocator);
565 dict_mallocator = NULL;
566 xbt_mallocator_free(dict_elm_mallocator);
567 dict_elm_mallocator = NULL;
574 printf("Overall stats:");
575 xbt_dynar_foreach(all_sizes,count,size) {
580 printf("%delm x %d cells; ",count,size);
584 printf("; %f elm per cell\n",avg/(double)total_count);
588 static void* dict_mallocator_new_f(void) {
589 return xbt_new(s_xbt_dict_t, 1);
592 static void dict_mallocator_free_f(void* dict) {
596 static void dict_mallocator_reset_f(void* dict) {
597 /* nothing to do because all fields are
598 * initialized in xbt_dict_new
605 #include "portable.h"
607 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
608 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
610 XBT_TEST_SUITE("dict","Dict data container");
612 static void print_str(void *str) {
613 printf("%s",(char*)PRINTF_STR(str));
616 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
617 char *data=xbt_strdup(data_to_fill);
619 xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
621 xbt_dict_set(head,key,data,&free);
622 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
623 xbt_dict_dump(head,(void (*)(void*))&printf);
627 static void debuged_add(xbt_dict_t head,const char*key) {
628 debuged_add_ext(head,key,key);
631 static void fill(xbt_dict_t *head) {
632 xbt_test_add0("Fill in the dictionnary");
634 *head = xbt_dict_new();
635 debuged_add(*head,"12");
636 debuged_add(*head,"12a");
637 debuged_add(*head,"12b");
638 debuged_add(*head,"123");
639 debuged_add(*head,"123456");
640 /* Child becomes child of what to add */
641 debuged_add(*head,"1234");
642 /* Need of common ancestor */
643 debuged_add(*head,"123457");
647 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
650 xbt_test_add1("Search %s",key);
651 found=xbt_dict_get(head,key);
652 xbt_test_log1("Found %s",(char *)found);
654 xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
656 xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
659 static void search(xbt_dict_t head,const char*key) {
660 search_ext(head,key,key);
663 static void debuged_remove(xbt_dict_t head,const char*key) {
665 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
666 xbt_dict_remove(head,key);
667 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
671 static void traverse(xbt_dict_t head) {
672 xbt_dict_cursor_t cursor=NULL;
677 xbt_dict_foreach(head,cursor,key,data) {
678 if (!key || !data || strcmp(key,data)) {
679 xbt_test_log3("Seen #%d: %s->%s",++i,PRINTF_STR(key),PRINTF_STR(data));
681 xbt_test_log2("Seen #%d: %s",++i,PRINTF_STR(key));
683 xbt_test_assert2(!data || !strcmp(key,data),
684 "Key(%s) != value(%s). Abording",key,data);
688 static void search_not_found(xbt_dict_t head, const char *data) {
692 xbt_test_add1("Search %s (expected not to be found)",data);
695 data = xbt_dict_get(head, data);
696 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
698 if (e.category != not_found_error)
699 xbt_test_exception(e);
703 xbt_test_assert0(ok,"Exception not raised");
706 static void count(xbt_dict_t dict, int length) {
707 xbt_dict_cursor_t cursor;
713 xbt_test_add1("Count elements (expecting %d)", length);
714 xbt_test_assert2(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
716 xbt_dict_foreach(dict,cursor,key,data) {
719 xbt_test_assert2(effective == length, "Effective length(%d) != %d.", effective, length);
723 xbt_dict_t head=NULL;
727 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
728 xbt_test_add0("Traversal the null dictionnary");
731 xbt_test_add0("Traversal and search the empty dictionnary");
732 head = xbt_dict_new();
735 debuged_remove(head,"12346");
737 if (e.category != not_found_error)
738 xbt_test_exception(e);
741 xbt_dict_free(&head);
743 xbt_test_add0("Traverse the full dictionnary");
747 debuged_add_ext(head,"toto","tutu");
748 search_ext(head,"toto","tutu");
749 debuged_remove(head,"toto");
754 xbt_test_add0("Free the dictionnary (twice)");
755 xbt_dict_free(&head);
756 xbt_dict_free(&head);
761 xbt_test_add0("Change 123 to 'Changed 123'");
762 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
765 xbt_test_add0("Change 123 back to '123'");
766 xbt_dict_set(head,"123",strdup("123"),&free);
769 xbt_test_add0("Change 12a to 'Dummy 12a'");
770 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
773 xbt_test_add0("Change 12a to '12a'");
774 xbt_dict_set(head,"12a",strdup("12a"),&free);
777 xbt_test_add0("Traverse the resulting dictionnary");
781 xbt_test_add0("Search 123");
782 data = xbt_dict_get(head,"123");
783 xbt_test_assert(data);
784 xbt_test_assert(!strcmp("123",data));
786 search_not_found(head,"Can't be found");
787 search_not_found(head,"123 Can't be found");
788 search_not_found(head,"12345678 NOT");
793 search(head,"123456");
795 search(head,"123457");
797 xbt_test_add0("Traverse the resulting dictionnary");
800 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
802 xbt_test_add0("Free the dictionnary twice");
803 xbt_dict_free(&head);
804 xbt_dict_free(&head);
806 xbt_test_add0("Traverse the resulting dictionnary");
810 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
813 xbt_test_add0("Remove non existing data");
815 debuged_remove(head,"Does not exist");
817 if (e.category != not_found_error)
818 xbt_test_exception(e);
823 xbt_dict_free(&head);
825 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
827 debuged_remove(head,"12a"); traverse(head);
829 debuged_remove(head,"12b"); traverse(head);
831 debuged_remove(head,"12"); traverse(head);
833 debuged_remove(head,"123456"); traverse(head);
836 debuged_remove(head,"12346");
838 if (e.category != not_found_error)
839 xbt_test_exception(e);
843 debuged_remove(head,"1234"); traverse(head);
844 debuged_remove(head,"123457"); traverse(head);
845 debuged_remove(head,"123"); traverse(head);
847 debuged_remove(head,"12346");
849 if (e.category != not_found_error)
850 xbt_test_exception(e);
854 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
855 xbt_dict_free(&head);
857 xbt_dict_reset(head);
861 xbt_test_add0("Free the dictionnary twice");
862 xbt_dict_free(&head);
863 xbt_dict_free(&head);
866 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
869 xbt_test_add0("Store NULL under 'null'");
870 xbt_dict_set(head,"null",NULL,NULL);
871 search_ext(head,"null",NULL);
873 xbt_test_add0("Check whether I see it while traversing...");
875 xbt_dict_cursor_t cursor=NULL;
879 xbt_dict_foreach(head,cursor,key,data) {
880 if (!key || !data || strcmp(key,data)) {
881 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
883 xbt_test_log1("Seen: %s",PRINTF_STR(key));
886 if (!strcmp(key,"null"))
889 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
891 xbt_dict_free(&head);
895 #define SIZEOFKEY 1024
896 static int countelems(xbt_dict_t head) {
897 xbt_dict_cursor_t cursor;
902 xbt_dict_foreach(head,cursor,key,data) {
908 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
909 xbt_dict_t head=NULL;
914 srand((unsigned int)time(NULL));
917 xbt_test_add2("CRASH test number %d (%d to go)",i+1,10-i-1);
918 xbt_test_log0("Fill the struct, count its elems and frees the structure");
919 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
921 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
923 for (j=0;j<1000;j++) {
925 key=xbt_malloc(SIZEOFKEY);
928 for (k=0;k<SIZEOFKEY-1;k++)
929 key[k]=rand() % ('z' - 'a') + 'a';
931 /* printf("[%d %s]\n",j,key); */
932 data = xbt_dict_get_or_null(head,key);
933 } while (data != NULL);
935 xbt_dict_set(head,key,key,&free);
936 data = xbt_dict_get(head,key);
937 xbt_test_assert2(!strcmp(key,data), "Retrieved value (%s) != Injected value (%s)",key,data);
941 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
943 xbt_dict_free(&head);
944 xbt_dict_free(&head);
949 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
950 for (j=0;j<NB_ELM;j++) {
951 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
953 key = xbt_malloc(10);
956 xbt_dict_set(head,key,key,&free);
958 /*xbt_dict_dump(head,(void (*)(void*))&printf);*/
960 xbt_test_add0("Count the elements (retrieving the key and data for each)");
961 i = countelems(head);
962 xbt_test_log1("There is %d elements",i);
964 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
967 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
968 for (j=0;j<NB_ELM;j++) {
971 data = xbt_dict_get(head,key);
972 xbt_test_assert2(!strcmp(key,(char*)data),
973 "with get, key=%s != data=%s",key,(char*)data);
974 data = xbt_dict_get_ext(head,key,strlen(key));
975 xbt_test_assert2(!strcmp(key,(char*)data),
976 "with get_ext, key=%s != data=%s",key,(char*)data);
981 xbt_test_add1("Remove my %d elements",NB_ELM);
983 for (j=0;j<NB_ELM;j++) {
984 /* if (!(j%10000)) printf("."); fflush(stdout); */
987 xbt_dict_remove(head,key);
992 xbt_test_add0("Free the structure (twice)");
993 xbt_dict_free(&head);
994 xbt_dict_free(&head);
997 static void str_free(void *s) {
1002 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
1005 #define NB_ELM 100 /*00*/
1007 #define KEY_SIZE 512
1008 #define NB_TEST 20 /*20*/
1011 xbt_dict_t mdict = NULL;
1013 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
1018 xbt_test_add0("Generic multicache CRASH test");
1019 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
1020 "depth of multicache=%d, key size=%d",
1021 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
1023 for (l=0 ; l<DEPTH ; l++) {
1024 key=xbt_malloc(KEY_SIZE);
1025 xbt_dynar_push(keys,&key);
1028 for (i=0;i<NB_TEST;i++) {
1029 mdict = xbt_dict_new();
1030 VERB1("mdict=%p",mdict);
1032 printf("Test %d\n",i);
1033 /* else if (i%10) printf("."); else printf("%d",i/10);*/
1035 for (j=0;j<NB_ELM;j++) {
1036 if (verbose>0) printf (" Add {");
1038 for (l=0 ; l<DEPTH ; l++) {
1039 key=*(char**)xbt_dynar_get_ptr(keys,l);
1041 for (k=0;k<KEY_SIZE-1;k++)
1042 key[k]=rand() % ('z' - 'a') + 'a';
1046 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
1048 if (verbose>0) printf("in multitree %p.\n",mdict);
1050 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
1052 data = xbt_multidict_get(mdict,keys);
1054 xbt_test_assert2(data && !strcmp((char*)data,key),
1055 "Retrieved value (%s) does not match the entrered one (%s)\n",
1058 xbt_dict_free(&mdict);
1061 xbt_dynar_free(&keys);
1064 xbt_dict_dump(mdict,&xbt_dict_print);*/
1066 xbt_dict_free(&mdict);
1067 xbt_dynar_free(&keys);
1070 #endif /* SIMGRID_TEST */