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;
84 xbt_dict_foreach(head,cursor,key,data) {
85 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
86 xbt_test_assert2(!data || !strcmp(key,data),
87 "Key(%s) != value(%s). Abording\n",key,data);
91 static void search_not_found(xbt_dict_t head, const char *data) {
95 xbt_test_add1("Search %s (expected not to be found)",data);
98 data = xbt_dict_get(head, data);
99 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
101 if (e.category != not_found_error)
102 xbt_test_exception(e);
106 xbt_test_assert0(ok,"Exception not raised");
109 static void count(xbt_dict_t dict, int length) {
110 xbt_test_add1("Count elements (expecting %d)", length);
111 xbt_test_assert2(xbt_dict_length(dict) == length, "Length(%d) != %d.", xbt_dict_length(dict), length);
115 xbt_dict_t head=NULL;
119 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
120 xbt_test_add0("Traversal the null dictionnary");
123 xbt_test_add0("Traversal and search the empty dictionnary");
124 head = xbt_dict_new();
127 debuged_remove(head,"12346");
129 if (e.category != not_found_error)
130 xbt_test_exception(e);
133 xbt_dict_free(&head);
135 xbt_test_add0("Traverse the full dictionnary");
139 debuged_add_ext(head,"toto","tutu");
140 search_ext(head,"toto","tutu");
141 debuged_remove(head,"toto");
146 xbt_test_add0("Free the dictionnary (twice)");
147 xbt_dict_free(&head);
148 xbt_dict_free(&head);
153 xbt_test_add0("Change 123 to 'Changed 123'");
154 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
157 xbt_test_add0("Change 123 back to '123'");
158 xbt_dict_set(head,"123",strdup("123"),&free);
161 xbt_test_add0("Change 12a to 'Dummy 12a'");
162 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
165 xbt_test_add0("Change 12a to '12a'");
166 xbt_dict_set(head,"12a",strdup("12a"),&free);
169 xbt_test_add0("Traverse the resulting dictionnary");
173 xbt_test_add0("Search 123");
174 data = xbt_dict_get(head,"123");
175 xbt_test_assert(data);
176 xbt_test_assert(!strcmp("123",data));
178 search_not_found(head,"Can't be found");
179 search_not_found(head,"123 Can't be found");
180 search_not_found(head,"12345678 NOT");
185 search(head,"123456");
187 search(head,"123457");
189 xbt_test_add0("Traverse the resulting dictionnary");
192 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
194 xbt_test_add0("Free the dictionnary twice");
195 xbt_dict_free(&head);
196 xbt_dict_free(&head);
198 xbt_test_add0("Traverse the resulting dictionnary");
202 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
205 xbt_test_add0("Remove non existing data");
207 debuged_remove(head,"Does not exist");
209 if (e.category != not_found_error)
210 xbt_test_exception(e);
215 xbt_dict_free(&head);
217 xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
219 debuged_remove(head,"12a"); traverse(head);
221 debuged_remove(head,"12b"); traverse(head);
223 debuged_remove(head,"12"); traverse(head);
225 debuged_remove(head,"123456"); traverse(head);
228 debuged_remove(head,"12346");
230 if (e.category != not_found_error)
231 xbt_test_exception(e);
235 debuged_remove(head,"1234"); traverse(head);
236 debuged_remove(head,"123457"); traverse(head);
237 debuged_remove(head,"123"); traverse(head);
239 debuged_remove(head,"12346");
241 if (e.category != not_found_error)
242 xbt_test_exception(e);
246 xbt_test_add0("Remove all values");
247 xbt_dict_free(&head);
249 xbt_dict_reset(head);
253 xbt_test_add0("Free the dictionnary twice");
254 xbt_dict_free(&head);
255 xbt_dict_free(&head);
258 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
261 xbt_test_add0("Store NULL under 'null'");
262 xbt_dict_set(head,"null",NULL,NULL);
263 search_ext(head,"null",NULL);
265 xbt_test_add0("Check whether I see it while traversing...");
267 xbt_dict_cursor_t cursor=NULL;
271 xbt_dict_foreach(head,cursor,key,data) {
272 xbt_test_log2("Seen: %s->%s",PRINTF_STR(key),PRINTF_STR(data));
273 if (!strcmp(key,"null"))
276 xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
278 xbt_dict_free(&head);
282 #define SIZEOFKEY 1024
283 static int countelems(xbt_dict_t head) {
284 xbt_dict_cursor_t cursor;
289 xbt_dict_foreach(head,cursor,key,data) {
295 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
296 xbt_dict_t head=NULL;
301 srand((unsigned int)time(NULL));
303 xbt_test_add0("CRASH test");
304 xbt_test_log0("Fill the struct, count its elems and frees the structure (x10)");
305 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",SIZEOFKEY);
309 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
311 for (j=0;j<1000;j++) {
312 key=xbt_malloc(SIZEOFKEY);
314 for (k=0;k<SIZEOFKEY-1;k++)
315 key[k]=rand() % ('z' - 'a') + 'a';
317 /* printf("[%d %s]\n",j,key); */
318 xbt_dict_set(head,key,key,&free);
320 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
321 nb = countelems(head);
322 xbt_test_assert1(nb == 1000,"found %d elements instead of 1000",nb);
324 xbt_dict_free(&head);
325 xbt_dict_free(&head);
330 xbt_test_add1("Fill %d elements, with keys being the number of element",NB_ELM);
331 for (j=0;j<NB_ELM;j++) {
332 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
334 key = xbt_malloc(10);
337 xbt_dict_set(head,key,key,&free);
340 xbt_test_add0("Count the elements (retrieving the key and data for each)");
341 i = countelems(head);
342 xbt_test_log1("There is %d elements",i);
344 xbt_test_add1("Search my %d elements 20 times",NB_ELM);
347 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
348 for (j=0;j<NB_ELM;j++) {
351 data = xbt_dict_get(head,key);
352 xbt_test_assert2(!strcmp(key,(char*)data),
353 "key=%s != data=%s\n",key,(char*)data);
358 xbt_test_add1("Remove my %d elements",NB_ELM);
360 for (j=0;j<NB_ELM;j++) {
361 /* if (!(j%10000)) printf("."); fflush(stdout); */
364 xbt_dict_remove(head,key);
369 xbt_test_add0("Free the structure (twice)");
370 xbt_dict_free(&head);
371 xbt_dict_free(&head);
374 static void str_free(void *s) {
379 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
382 #define NB_ELM 100 /*00*/
385 #define NB_TEST 20 /*20*/
388 xbt_dict_t mdict = NULL;
390 xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
395 xbt_test_add0("Generic multicache CRASH test");
396 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
397 "depth of multicache=%d, key size=%d",
398 NB_TEST,NB_ELM,DEPTH,KEY_SIZE);
400 for (l=0 ; l<DEPTH ; l++) {
401 key=xbt_malloc(KEY_SIZE);
402 xbt_dynar_push(keys,&key);
405 for (i=0;i<NB_TEST;i++) {
406 mdict = xbt_dict_new();
407 VERB1("mdict=%p",mdict);
409 printf("Test %d\n",i);
410 /* else if (i%10) printf("."); else printf("%d",i/10);*/
412 for (j=0;j<NB_ELM;j++) {
413 if (verbose>0) printf (" Add {");
415 for (l=0 ; l<DEPTH ; l++) {
416 key=*(char**)xbt_dynar_get_ptr(keys,l);
418 for (k=0;k<KEY_SIZE-1;k++)
419 key[k]=rand() % ('z' - 'a') + 'a';
423 if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
425 if (verbose>0) printf("in multitree %p.\n",mdict);
427 xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
429 data = xbt_multidict_get(mdict,keys);
431 xbt_test_assert2(data && !strcmp((char*)data,key),
432 "Retrieved value (%s) does not match the entrered one (%s)\n",
435 xbt_dict_free(&mdict);
438 xbt_dynar_free(&keys);
441 xbt_dict_dump(mdict,&xbt_dict_print);*/
443 xbt_dict_free(&mdict);
444 xbt_dynar_free(&keys);
447 /*******************************/
448 /* GENERATED FILE, DO NOT EDIT */
449 /*******************************/