1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
6 /*******************************/
7 /* GENERATED FILE, DO NOT EDIT */
8 /*******************************/
15 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
16 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
19 static void print_str(void *str) {
20 printf("%s",(char*)PRINTF_STR(str));
23 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
24 char *data=xbt_strdup(data_to_fill);
26 xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
28 xbt_dict_set(head,key,data,&free);
29 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
30 xbt_dict_dump(head,(void (*)(void*))&printf);
34 static void debuged_add(xbt_dict_t head,const char*key) {
35 debuged_add_ext(head,key,key);
38 static void fill(xbt_dict_t *head) {
39 xbt_test_add0("Fill in the dictionnary");
41 *head = xbt_dict_new();
42 debuged_add(*head,"12");
43 debuged_add(*head,"12a");
44 debuged_add(*head,"12b");
45 debuged_add(*head,"123");
46 debuged_add(*head,"123456");
47 /* Child becomes child of what to add */
48 debuged_add(*head,"1234");
49 /* Need of common ancestor */
50 debuged_add(*head,"123457");
54 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
57 xbt_test_add1("Search %s",key);
58 found=xbt_dict_get(head,key);
59 xbt_test_log1("Found %s",(char *)found);
61 xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
63 xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
66 static void search(xbt_dict_t head,const char*key) {
67 search_ext(head,key,key);
70 static void debuged_remove(xbt_dict_t head,const char*key) {
72 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
73 xbt_dict_remove(head,key);
74 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
78 static void traverse(xbt_dict_t head) {
79 xbt_dict_cursor_t cursor=NULL;
83 xbt_dict_foreach(head,cursor,key,data) {
84 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
85 xbt_test_assert2(!data || !strcmp(key,data),
86 "Key(%s) != value(%s). Abording\n",key,data);
90 static void search_not_found(xbt_dict_t head, const char *data) {
94 xbt_test_add1("Search %s (expected not to be found)",data);
97 data = xbt_dict_get(head, data);
98 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
100 if (e.category != not_found_error)
101 xbt_test_exception(e);
105 xbt_test_assert0(ok,"Exception not raised");
108 static void count(xbt_dict_t dict, int length) {
109 xbt_test_add1("Count elements (expecting %d)", length);
110 xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
114 xbt_dict_t head=NULL;
118 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
119 xbt_test_add0("Traversal the null dictionnary");
122 xbt_test_add0("Traversal and search the empty dictionnary");
123 head = xbt_dict_new();
126 debuged_remove(head,"12346");
128 if (e.category != not_found_error)
129 xbt_test_exception(e);
132 xbt_dict_free(&head);
134 xbt_test_add0("Traverse the full dictionnary");
138 debuged_add_ext(head,"toto","tutu");
139 search_ext(head,"toto","tutu");
140 debuged_remove(head,"toto");
145 xbt_test_add0("Free the dictionnary (twice)");
146 xbt_dict_free(&head);
147 xbt_dict_free(&head);
152 xbt_test_add0("Change 123 to 'Changed 123'");
153 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
156 xbt_test_add0("Change 123 back to '123'");
157 xbt_dict_set(head,"123",strdup("123"),&free);
160 xbt_test_add0("Change 12a to 'Dummy 12a'");
161 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
164 xbt_test_add0("Change 12a to '12a'");
165 xbt_dict_set(head,"12a",strdup("12a"),&free);
168 xbt_test_add0("Traverse the resulting dictionnary");
172 xbt_test_add0("Search 123");
173 data = xbt_dict_get(head,"123");
174 xbt_test_assert(data);
175 xbt_test_assert(!strcmp("123",data));
177 search_not_found(head,"Can't be found");
178 search_not_found(head,"123 Can't be found");
179 search_not_found(head,"12345678 NOT");
184 search(head,"123456");
186 search(head,"123457");
188 xbt_test_add0("Traverse the resulting dictionnary");
191 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
193 xbt_test_add0("Free the dictionnary twice");
194 xbt_dict_free(&head);
195 xbt_dict_free(&head);
197 xbt_test_add0("Traverse the resulting dictionnary");
201 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
204 xbt_test_add0("Remove non existing data");
206 debuged_remove(head,"Does not exist");
208 if (e.category != not_found_error)
209 xbt_test_exception(e);
214 xbt_dict_free(&head);
216 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
218 debuged_remove(head,"12a"); traverse(head);
220 debuged_remove(head,"12b"); traverse(head);
222 debuged_remove(head,"12"); traverse(head);
224 debuged_remove(head,"123456"); traverse(head);
227 debuged_remove(head,"12346");
229 if (e.category != not_found_error)
230 xbt_test_exception(e);
234 debuged_remove(head,"1234"); traverse(head);
235 debuged_remove(head,"123457"); traverse(head);
236 debuged_remove(head,"123"); traverse(head);
238 debuged_remove(head,"12346");
240 if (e.category != not_found_error)
241 xbt_test_exception(e);
245 xbt_test_add0("Remove all values");
246 xbt_dict_free(&head);
248 xbt_dict_reset(head);
252 xbt_test_add0("Free the dictionnary twice");
253 xbt_dict_free(&head);
254 xbt_dict_free(&head);
257 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
260 xbt_test_add0("Store NULL under 'null'");
261 xbt_dict_set(head,"null",NULL,NULL);
262 search_ext(head,"null",NULL);
264 xbt_test_add0("Check whether I see it while traversing...");
266 xbt_dict_cursor_t cursor=NULL;
270 xbt_dict_foreach(head,cursor,key,data) {
271 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
272 if (!strcmp(key,"null"))
275 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
277 xbt_dict_free(&head);
281 #define SIZEOFKEY 1024
282 static int countelems(xbt_dict_t head) {
283 xbt_dict_cursor_t cursor;
288 xbt_dict_foreach(head,cursor,key,data) {
294 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
295 xbt_dict_t head=NULL;
300 srand((unsigned int)time(NULL));
302 xbt_test_add0("CRASH test");
303 xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
304 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
308 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
310 for (j=0;j<1000;j++) {
311 key=xbt_malloc(SIZEOFKEY);
313 for (k=0;k<SIZEOFKEY-1;k++)
314 key[k]=rand() % ('z' - 'a') + 'a';
316 /* printf("[%d %s]\n",j,key); */
317 xbt_dict_set(head,key,key,&free);
319 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
320 nb = countelems(head);
321 xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
323 xbt_dict_free(&head);
324 xbt_dict_free(&head);
329 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
330 for (j=0;j<NB_ELM;j++) {
331 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
333 key = xbt_malloc(10);
336 xbt_dict_set(head,key,key,&free);
339 xbt_test_add0("Count the elements (retrieving the key and data for each)");
340 i = countelems(head);
341 xbt_test_log1("There is %d elements",i);
343 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
346 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
347 for (j=0;j<NB_ELM;j++) {
350 data = xbt_dict_get(head,key);
351 xbt_test_assert2(!strcmp(key,(char*)data),
352 "key=%s != data=%s\n",key,(char*)data);
357 xbt_test_add1("Remove my %d elements",NB_ELM);
359 for (j=0;j<NB_ELM;j++) {
360 /* if (!(j%10000)) printf("."); fflush(stdout); */
363 xbt_dict_remove(head,key);
368 xbt_test_add0("Free the structure (twice)");
369 xbt_dict_free(&head);
370 xbt_dict_free(&head);
373 static void str_free(void *s) {
378 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
381 #define NB_ELM 100 /*00*/
384 #define NB_TEST 20 /*20*/
387 xbt_dict_t mdict = NULL;
389 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
394 xbt_test_add0("Generic multicache CRASH test");
395 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
396 "depth of multicache=%d, key size=%d",
397 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
399 for (l=0 ; l<DEPTH ; l++) {
400 key=xbt_malloc(KEY_SIZE);
401 xbt_dynar_push(keys,&key);
404 for (i=0;i<NB_TEST;i++) {
405 mdict = xbt_dict_new();
406 VERB1("mdict=%p",mdict);
408 printf("Test %d\n",i);
409 /* else if (i%10) printf("."); else printf("%d",i/10);*/
411 for (j=0;j<NB_ELM;j++) {
412 if (verbose>0) printf (" Add {");
414 for (l=0 ; l<DEPTH ; l++) {
415 key=*(char**)xbt_dynar_get_ptr(keys,l);
417 for (k=0;k<KEY_SIZE-1;k++)
418 key[k]=rand() % ('z' - 'a') + 'a';
422 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
424 if (verbose>0) printf("in multitree %p.\n",mdict);
426 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
428 data = xbt_multidict_get(mdict,keys);
430 xbt_test_assert2(data && !strcmp((char*)data,key),
431 "Retrieved value (%s) does not match the entrered one (%s)\n",
434 xbt_dict_free(&mdict);
437 xbt_dynar_free(&keys);
440 xbt_dict_dump(mdict,&xbt_dict_print);*/
442 xbt_dict_free(&mdict);
443 xbt_dynar_free(&keys);
446 /*******************************/
447 /* GENERATED FILE, DO NOT EDIT */
448 /*******************************/