Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make GTNETS compatible with new parsing and variable name convention.
[simgrid.git] / src / dict_unit.c
1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
4
5 #include <stdio.h>
6 #include "xbt.h"
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
10
11 # 604 "xbt/dict.c" 
12 #include "xbt.h"
13 #include "xbt/ex.h"
14 #include "portable.h"
15
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
18
19
20 static void print_str(void *str) {
21   printf("%s",(char*)PRINTF_STR(str));
22 }
23
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);
26
27   xbt_test_log2("Add %s under %s",PRINTF_STR(data_to_fill),PRINTF_STR(key));
28
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);
32     fflush(stdout);
33   }
34 }
35 static void debuged_add(xbt_dict_t head,const char*key) {
36    debuged_add_ext(head,key,key);
37 }
38
39 static void fill(xbt_dict_t *head) {
40   xbt_test_add0("Fill in the dictionnary");
41
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");
52 }
53
54
55 static void search_ext(xbt_dict_t head,const char*key, const char *data) {
56   void *found;
57   
58   xbt_test_add1("Search %s",key);
59   found=xbt_dict_get(head,key);
60   xbt_test_log1("Found %s",(char *)found);
61   if (data)
62     xbt_test_assert1(found,"data do not match expectations: found NULL while searching for %s",data);
63   if (found)
64     xbt_test_assert2(!strcmp((char*)data,found),"data do not match expectations: found %s while searching for %s", (char*)found, data);
65 }
66
67 static void search(xbt_dict_t head,const char*key) {
68   search_ext(head,key,key);
69 }
70
71 static void debuged_remove(xbt_dict_t head,const char*key) {
72
73   xbt_test_add1("Remove '%s'",PRINTF_STR(key));
74   xbt_dict_remove(head,key);
75   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
76 }
77
78
79 static void traverse(xbt_dict_t head) {
80   xbt_dict_cursor_t cursor=NULL;
81   char *key;
82   char *data;
83   int i = 0;
84
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));
88     } else {
89        xbt_test_log2("Seen #%d:  %s",++i,PRINTF_STR(key));
90     }
91     xbt_test_assert2(!data || !strcmp(key,data),
92                      "Key(%s) != value(%s). Abording",key,data);
93   }
94 }
95
96 static void search_not_found(xbt_dict_t head, const char *data) {
97   int ok=0;
98   xbt_ex_t e;
99
100   xbt_test_add1("Search %s (expected not to be found)",data);
101
102   TRY {    
103     data = xbt_dict_get(head, data);
104     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
105   } CATCH(e) {
106     if (e.category != not_found_error) 
107       xbt_test_exception(e);
108     xbt_ex_free(e);
109     ok=1;
110   }
111   xbt_test_assert0(ok,"Exception not raised");
112 }
113
114 static void count(xbt_dict_t dict, int length) {
115   xbt_dict_cursor_t cursor;
116   char *key;
117   void *data;
118   int effective = 0;
119
120
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);
123    
124   xbt_dict_foreach(dict,cursor,key,data) {
125     effective++;
126   }
127   xbt_test_assert2(effective == length, "Effective length(%d) != %d.", effective, length);
128 }
129
130 xbt_ex_t e;
131 xbt_dict_t head=NULL;
132 char *data;
133
134
135 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
136   xbt_test_add0("Traversal the null dictionnary");
137   traverse(head);
138
139   xbt_test_add0("Traversal and search the empty dictionnary");
140   head = xbt_dict_new();
141   traverse(head);
142   TRY {
143     debuged_remove(head,"12346");
144   } CATCH(e) {
145     if (e.category != not_found_error) 
146       xbt_test_exception(e);
147     xbt_ex_free(e);
148   }
149   xbt_dict_free(&head);
150
151   xbt_test_add0("Traverse the full dictionnary");
152   fill(&head);
153   count(head, 7);
154    
155   debuged_add_ext(head,"toto","tutu");
156   search_ext(head,"toto","tutu");
157   debuged_remove(head,"toto");
158
159   search(head,"12a");
160   traverse(head);
161
162   xbt_test_add0("Free the dictionnary (twice)");
163   xbt_dict_free(&head);
164   xbt_dict_free(&head);
165
166   /* CHANGING */
167   fill(&head);
168   count(head, 7);
169   xbt_test_add0("Change 123 to 'Changed 123'");
170   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
171   count(head, 7);
172
173   xbt_test_add0("Change 123 back to '123'");
174   xbt_dict_set(head,"123",strdup("123"),&free);
175   count(head, 7);
176
177   xbt_test_add0("Change 12a to 'Dummy 12a'");
178   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
179   count(head, 7);
180
181   xbt_test_add0("Change 12a to '12a'");
182   xbt_dict_set(head,"12a",strdup("12a"),&free);
183   count(head, 7);
184
185   xbt_test_add0("Traverse the resulting dictionnary");
186   traverse(head);
187   
188   /* RETRIEVE */
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));
193
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");
197
198   search(head,"12a");
199   search(head,"12b");
200   search(head,"12");
201   search(head,"123456");
202   search(head,"1234");
203   search(head,"123457");
204
205   xbt_test_add0("Traverse the resulting dictionnary");
206   traverse(head);
207
208   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
209
210   xbt_test_add0("Free the dictionnary twice");
211   xbt_dict_free(&head);
212   xbt_dict_free(&head);
213
214   xbt_test_add0("Traverse the resulting dictionnary");
215   traverse(head);
216 }
217
218 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
219   fill(&head);
220   count(head, 7);
221   xbt_test_add0("Remove non existing data");
222   TRY {
223     debuged_remove(head,"Does not exist");
224   } CATCH(e) {
225     if (e.category != not_found_error) 
226       xbt_test_exception(e);
227     xbt_ex_free(e);
228   }
229   traverse(head);
230
231   xbt_dict_free(&head);
232
233   xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
234   fill(&head);
235   debuged_remove(head,"12a");    traverse(head);
236   count(head, 6);
237   debuged_remove(head,"12b");    traverse(head);
238   count(head, 5);
239   debuged_remove(head,"12");     traverse(head);
240   count(head, 4);
241   debuged_remove(head,"123456"); traverse(head);
242   count(head, 3);
243   TRY {
244     debuged_remove(head,"12346");
245   } CATCH(e) {
246     if (e.category != not_found_error) 
247       xbt_test_exception(e);
248     xbt_ex_free(e);         
249     traverse(head);
250   } 
251   debuged_remove(head,"1234");   traverse(head);
252   debuged_remove(head,"123457"); traverse(head);
253   debuged_remove(head,"123");    traverse(head);
254   TRY {
255     debuged_remove(head,"12346");
256   } CATCH(e) {
257     if (e.category != not_found_error) 
258       xbt_test_exception(e);
259     xbt_ex_free(e);
260   }                              traverse(head);
261   
262   xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
263   xbt_dict_free(&head);
264   fill(&head);
265   xbt_dict_reset(head);
266   count(head, 0);
267   traverse(head);
268
269   xbt_test_add0("Free the dictionnary twice");
270   xbt_dict_free(&head);
271   xbt_dict_free(&head);      
272 }
273
274 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
275   fill(&head);
276
277   xbt_test_add0("Store NULL under 'null'");
278   xbt_dict_set(head,"null",NULL,NULL);
279   search_ext(head,"null",NULL);
280
281   xbt_test_add0("Check whether I see it while traversing...");
282   {
283     xbt_dict_cursor_t cursor=NULL;
284     char *key;
285     int found=0;
286
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));
290       } else {
291          xbt_test_log1("Seen:  %s",PRINTF_STR(key));
292       }
293      
294       if (!strcmp(key,"null"))
295         found = 1;
296     }
297     xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
298   }
299   xbt_dict_free(&head);
300 }
301
302 #define NB_ELM 20000
303 #define SIZEOFKEY 1024
304 static int countelems(xbt_dict_t head) {
305   xbt_dict_cursor_t cursor;
306   char *key;
307   void *data;
308   int res = 0;
309
310   xbt_dict_foreach(head,cursor,key,data) {
311     res++;
312   }
313   return res;
314 }
315    
316 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
317   xbt_dict_t head=NULL;
318   int i,j,k, nb;
319   char *key;
320   void *data;
321
322   srand((unsigned int)time(NULL));
323
324   for (i=0;i<10;i++) {
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);
328     head=xbt_dict_new();
329     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
330     nb=0;
331     for (j=0;j<1000;j++) {
332       char *data = NULL;
333       key=xbt_malloc(SIZEOFKEY);
334
335       do {          
336          for (k=0;k<SIZEOFKEY-1;k++)
337            key[k]=rand() % ('z' - 'a') + 'a';
338          key[k]='\0';
339          /*      printf("[%d %s]\n",j,key); */
340          data = xbt_dict_get_or_null(head,key);
341       } while (data != NULL);
342         
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);
346        
347       count(head,j+1);
348     }
349     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
350     traverse(head);
351     xbt_dict_free(&head);
352     xbt_dict_free(&head);
353   }
354
355
356   head=xbt_dict_new();
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); } */
360
361     key = xbt_malloc(10);
362     
363     sprintf(key,"%d",j);
364     xbt_dict_set(head,key,key,&free);
365   }
366    /*xbt_dict_dump(head,(void (*)(void*))&printf);*/
367    
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);
371
372   xbt_test_add1("Search my %d elements 20 times",NB_ELM);
373   key=xbt_malloc(10);
374   for (i=0;i<20;i++) {
375     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
376     for (j=0;j<NB_ELM;j++) {
377       
378       sprintf(key,"%d",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);
385     }
386   }
387   free(key);
388
389   xbt_test_add1("Remove my %d elements",NB_ELM);
390   key=xbt_malloc(10);
391   for (j=0;j<NB_ELM;j++) {
392     /* if (!(j%10000)) printf("."); fflush(stdout); */
393     
394     sprintf(key,"%d",j);
395     xbt_dict_remove(head,key);
396   }
397   free(key);
398
399   
400   xbt_test_add0("Free the structure (twice)");
401   xbt_dict_free(&head);
402   xbt_dict_free(&head);
403 }
404
405 static void str_free(void *s) {
406   char *c=*(char**)s;
407   free(c);
408 }
409
410 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
411
412 #undef NB_ELM
413 #define NB_ELM 100 /*00*/
414 #define DEPTH 5
415 #define KEY_SIZE 512
416 #define NB_TEST 20 /*20*/
417 int verbose=0;
418
419   xbt_dict_t mdict = NULL;
420   int i,j,k,l;
421   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
422   void *data;
423   char *key;
424
425
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);
430
431   for (l=0 ; l<DEPTH ; l++) {
432     key=xbt_malloc(KEY_SIZE);
433     xbt_dynar_push(keys,&key);
434   }     
435
436   for (i=0;i<NB_TEST;i++) {
437     mdict = xbt_dict_new();
438     VERB1("mdict=%p",mdict);
439     if (verbose>0)
440       printf("Test %d\n",i);
441     /* else if (i%10) printf("."); else printf("%d",i/10);*/
442     
443     for (j=0;j<NB_ELM;j++) {
444       if (verbose>0) printf ("  Add {");
445       
446       for (l=0 ; l<DEPTH ; l++) {
447         key=*(char**)xbt_dynar_get_ptr(keys,l);
448         
449         for (k=0;k<KEY_SIZE-1;k++) 
450           key[k]=rand() % ('z' - 'a') + 'a';
451           
452         key[k]='\0';
453         
454         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
455       }
456       if (verbose>0) printf("in multitree %p.\n",mdict);
457                                                         
458       xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
459
460       data = xbt_multidict_get(mdict,keys);
461
462       xbt_test_assert2(data && !strcmp((char*)data,key),
463                        "Retrieved value (%s) does not match the entrered one (%s)\n",
464                        (char*)data,key);
465     }
466     xbt_dict_free(&mdict);
467   }
468   
469   xbt_dynar_free(&keys);
470
471 /*  if (verbose>0)
472     xbt_dict_dump(mdict,&xbt_dict_print);*/
473     
474   xbt_dict_free(&mdict);
475   xbt_dynar_free(&keys);
476
477 }
478 /*******************************/
479 /* GENERATED FILE, DO NOT EDIT */
480 /*******************************/
481