1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
20 static void print_str(void *str) {
21 printf("%s",(char*)PRINTF_STR(str));
24 static void debuged_add_ext(xbt_dict_t head,const char*key,const char*data_to_fill) {
25 char *data=xbt_strdup(data_to_fill);
27 xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
29 xbt_dict_set(head,key,data,&free);
30 if (XBT_LOG_ISENABLED(xbt_dict,xbt_log_priority_debug)) {
31 xbt_dict_dump(head,(void (*)(void*))&printf);
35 static void debuged_add(xbt_dict_t head,const char*key) {
36 debuged_add_ext(head,key,key);
39 static void fill(xbt_dict_t *head) {
40 xbt_test_add0("Fill in the dictionnary");
42 *head = xbt_dict_new();
43 debuged_add(*head,"12");
44 debuged_add(*head,"12a");
45 debuged_add(*head,"12b");
46 debuged_add(*head,"123");
47 debuged_add(*head,"123456");
48 /* Child becomes child of what to add */
49 debuged_add(*head,"1234");
50 /* Need of common ancestor */
51 debuged_add(*head,"123457");
55 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
58 xbt_test_add1("Search %s",key);
59 found=xbt_dict_get(head,key);
60 xbt_test_log1("Found %s",(char *)found);
62 xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
64 xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
67 static void search(xbt_dict_t head,const char*key) {
68 search_ext(head,key,key);
71 static void debuged_remove(xbt_dict_t head,const char*key) {
73 xbt_test_add1("Remove '%s'",PRINTF_STR(key));
74 xbt_dict_remove(head,key);
75 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
79 static void traverse(xbt_dict_t head) {
80 xbt_dict_cursor_t cursor=NULL;
85 xbt_dict_foreach(head,cursor,key,data) {
86 if (!key || !data || strcmp(key,data)) {
87 xbt_test_log3("Seen #%d: %s->%s",++i,PRINTF_STR(key),PRINTF_STR(data));
89 xbt_test_log2("Seen #%d: %s",++i,PRINTF_STR(key));
91 xbt_test_assert2(!data || !strcmp(key,data),
92 "Key(%s) != value(%s). Abording",key,data);
96 static void search_not_found(xbt_dict_t head, const char *data) {
100 xbt_test_add1("Search %s (expected not to be found)",data);
103 data = xbt_dict_get(head, data);
104 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
106 if (e.category != not_found_error)
107 xbt_test_exception(e);
111 xbt_test_assert0(ok,"Exception not raised");
114 static void count(xbt_dict_t dict, int length) {
115 xbt_dict_cursor_t cursor;
121 xbt_test_add1("Count elements (expecting %d)", length);
122 xbt_test_assert2(xbt_dict_length(dict) == length, "Announced length(%d) != %d.", xbt_dict_length(dict), length);
124 xbt_dict_foreach(dict,cursor,key,data) {
127 xbt_test_assert2(effective == length, "Effective length(%d) != %d.", effective, length);
131 xbt_dict_t head=NULL;
135 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
136 xbt_test_add0("Traversal the null dictionnary");
139 xbt_test_add0("Traversal and search the empty dictionnary");
140 head = xbt_dict_new();
143 debuged_remove(head,"12346");
145 if (e.category != not_found_error)
146 xbt_test_exception(e);
149 xbt_dict_free(&head);
151 xbt_test_add0("Traverse the full dictionnary");
155 debuged_add_ext(head,"toto","tutu");
156 search_ext(head,"toto","tutu");
157 debuged_remove(head,"toto");
162 xbt_test_add0("Free the dictionnary (twice)");
163 xbt_dict_free(&head);
164 xbt_dict_free(&head);
169 xbt_test_add0("Change 123 to 'Changed 123'");
170 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
173 xbt_test_add0("Change 123 back to '123'");
174 xbt_dict_set(head,"123",strdup("123"),&free);
177 xbt_test_add0("Change 12a to 'Dummy 12a'");
178 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
181 xbt_test_add0("Change 12a to '12a'");
182 xbt_dict_set(head,"12a",strdup("12a"),&free);
185 xbt_test_add0("Traverse the resulting dictionnary");
189 xbt_test_add0("Search 123");
190 data = xbt_dict_get(head,"123");
191 xbt_test_assert(data);
192 xbt_test_assert(!strcmp("123",data));
194 search_not_found(head,"Can't be found");
195 search_not_found(head,"123 Can't be found");
196 search_not_found(head,"12345678 NOT");
201 search(head,"123456");
203 search(head,"123457");
205 xbt_test_add0("Traverse the resulting dictionnary");
208 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
210 xbt_test_add0("Free the dictionnary twice");
211 xbt_dict_free(&head);
212 xbt_dict_free(&head);
214 xbt_test_add0("Traverse the resulting dictionnary");
218 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
221 xbt_test_add0("Remove non existing data");
223 debuged_remove(head,"Does not exist");
225 if (e.category != not_found_error)
226 xbt_test_exception(e);
231 xbt_dict_free(&head);
233 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
235 debuged_remove(head,"12a"); traverse(head);
237 debuged_remove(head,"12b"); traverse(head);
239 debuged_remove(head,"12"); traverse(head);
241 debuged_remove(head,"123456"); traverse(head);
244 debuged_remove(head,"12346");
246 if (e.category != not_found_error)
247 xbt_test_exception(e);
251 debuged_remove(head,"1234"); traverse(head);
252 debuged_remove(head,"123457"); traverse(head);
253 debuged_remove(head,"123"); traverse(head);
255 debuged_remove(head,"12346");
257 if (e.category != not_found_error)
258 xbt_test_exception(e);
262 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
263 xbt_dict_free(&head);
265 xbt_dict_reset(head);
269 xbt_test_add0("Free the dictionnary twice");
270 xbt_dict_free(&head);
271 xbt_dict_free(&head);
274 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
277 xbt_test_add0("Store NULL under 'null'");
278 xbt_dict_set(head,"null",NULL,NULL);
279 search_ext(head,"null",NULL);
281 xbt_test_add0("Check whether I see it while traversing...");
283 xbt_dict_cursor_t cursor=NULL;
287 xbt_dict_foreach(head,cursor,key,data) {
288 if (!key || !data || strcmp(key,data)) {
289 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
291 xbt_test_log1("Seen: %s",PRINTF_STR(key));
294 if (!strcmp(key,"null"))
297 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
299 xbt_dict_free(&head);
303 #define SIZEOFKEY 1024
304 static int countelems(xbt_dict_t head) {
305 xbt_dict_cursor_t cursor;
310 xbt_dict_foreach(head,cursor,key,data) {
316 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
317 xbt_dict_t head=NULL;
322 srand((unsigned int)time(NULL));
325 xbt_test_add2("CRASH test number %d (%d to go)",i+1,10-i-1);
326 xbt_test_log0("Fill the struct, count its elems and frees the structure");
327 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
329 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
331 for (j=0;j<1000;j++) {
333 key=xbt_malloc(SIZEOFKEY);
336 for (k=0;k<SIZEOFKEY-1;k++)
337 key[k]=rand() % ('z' - 'a') + 'a';
339 /* printf("[%d %s]\n",j,key); */
340 data = xbt_dict_get_or_null(head,key);
341 } while (data != NULL);
343 xbt_dict_set(head,key,key,&free);
344 data = xbt_dict_get(head,key);
345 xbt_test_assert2(!strcmp(key,data), "Retrieved value (%s) != Injected value (%s)",key,data);
349 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
351 xbt_dict_free(&head);
352 xbt_dict_free(&head);
357 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
358 for (j=0;j<NB_ELM;j++) {
359 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
361 key = xbt_malloc(10);
364 xbt_dict_set(head,key,key,&free);
366 /*xbt_dict_dump(head,(void (*)(void*))&printf);*/
368 xbt_test_add0("Count the elements (retrieving the key and data for each)");
369 i = countelems(head);
370 xbt_test_log1("There is %d elements",i);
372 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
375 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
376 for (j=0;j<NB_ELM;j++) {
379 data = xbt_dict_get(head,key);
380 xbt_test_assert2(!strcmp(key,(char*)data),
381 "with get, key=%s != data=%s",key,(char*)data);
382 data = xbt_dict_get_ext(head,key,strlen(key));
383 xbt_test_assert2(!strcmp(key,(char*)data),
384 "with get_ext, key=%s != data=%s",key,(char*)data);
389 xbt_test_add1("Remove my %d elements",NB_ELM);
391 for (j=0;j<NB_ELM;j++) {
392 /* if (!(j%10000)) printf("."); fflush(stdout); */
395 xbt_dict_remove(head,key);
400 xbt_test_add0("Free the structure (twice)");
401 xbt_dict_free(&head);
402 xbt_dict_free(&head);
405 static void str_free(void *s) {
410 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
413 #define NB_ELM 100 /*00*/
416 #define NB_TEST 20 /*20*/
419 xbt_dict_t mdict = NULL;
421 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
426 xbt_test_add0("Generic multicache CRASH test");
427 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
428 "depth of multicache=%d, key size=%d",
429 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
431 for (l=0 ; l<DEPTH ; l++) {
432 key=xbt_malloc(KEY_SIZE);
433 xbt_dynar_push(keys,&key);
436 for (i=0;i<NB_TEST;i++) {
437 mdict = xbt_dict_new();
438 VERB1("mdict=%p",mdict);
440 printf("Test %d\n",i);
441 /* else if (i%10) printf("."); else printf("%d",i/10);*/
443 for (j=0;j<NB_ELM;j++) {
444 if (verbose>0) printf (" Add {");
446 for (l=0 ; l<DEPTH ; l++) {
447 key=*(char**)xbt_dynar_get_ptr(keys,l);
449 for (k=0;k<KEY_SIZE-1;k++)
450 key[k]=rand() % ('z' - 'a') + 'a';
454 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
456 if (verbose>0) printf("in multitree %p.\n",mdict);
458 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
460 data = xbt_multidict_get(mdict,keys);
462 xbt_test_assert2(data && !strcmp((char*)data,key),
463 "Retrieved value (%s) does not match the entrered one (%s)\n",
466 xbt_dict_free(&mdict);
469 xbt_dynar_free(&keys);
472 xbt_dict_dump(mdict,&xbt_dict_print);*/
474 xbt_dict_free(&mdict);
475 xbt_dynar_free(&keys);
478 /*******************************/
479 /* GENERATED FILE, DO NOT EDIT */
480 /*******************************/