Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
declare portable snprintf and vsnprintf functions for Visual C++ 7
[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 # 489 "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
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);
88   }
89 }
90
91 static void search_not_found(xbt_dict_t head, const char *data) {
92   int ok=0;
93   xbt_ex_t e;
94
95   xbt_test_add1("Search %s (expected not to be found)",data);
96
97   TRY {    
98     data = xbt_dict_get(head, data);
99     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
100   } CATCH(e) {
101     if (e.category != not_found_error) 
102       xbt_test_exception(e);
103     xbt_ex_free(e);
104     ok=1;
105   }
106   xbt_test_assert0(ok,"Exception not raised");
107 }
108
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);
112 }
113
114 xbt_ex_t e;
115 xbt_dict_t head=NULL;
116 char *data;
117
118
119 XBT_TEST_UNIT("basic",test_dict_basic,"Basic usage: change, retrieve, traverse"){
120   xbt_test_add0("Traversal the null dictionnary");
121   traverse(head);
122
123   xbt_test_add0("Traversal and search the empty dictionnary");
124   head = xbt_dict_new();
125   traverse(head);
126   TRY {
127     debuged_remove(head,"12346");
128   } CATCH(e) {
129     if (e.category != not_found_error) 
130       xbt_test_exception(e);
131     xbt_ex_free(e);
132   }
133   xbt_dict_free(&head);
134
135   xbt_test_add0("Traverse the full dictionnary");
136   fill(&head);
137   count(head, 7);
138    
139   debuged_add_ext(head,"toto","tutu");
140   search_ext(head,"toto","tutu");
141   debuged_remove(head,"toto");
142
143   search(head,"12a");
144   traverse(head);
145
146   xbt_test_add0("Free the dictionnary (twice)");
147   xbt_dict_free(&head);
148   xbt_dict_free(&head);
149
150   /* CHANGING */
151   fill(&head);
152   count(head, 7);
153   xbt_test_add0("Change 123 to 'Changed 123'");
154   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
155   count(head, 7);
156
157   xbt_test_add0("Change 123 back to '123'");
158   xbt_dict_set(head,"123",strdup("123"),&free);
159   count(head, 7);
160
161   xbt_test_add0("Change 12a to 'Dummy 12a'");
162   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
163   count(head, 7);
164
165   xbt_test_add0("Change 12a to '12a'");
166   xbt_dict_set(head,"12a",strdup("12a"),&free);
167   count(head, 7);
168
169   xbt_test_add0("Traverse the resulting dictionnary");
170   traverse(head);
171   
172   /* RETRIEVE */
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));
177
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");
181
182   search(head,"12a");
183   search(head,"12b");
184   search(head,"12");
185   search(head,"123456");
186   search(head,"1234");
187   search(head,"123457");
188
189   xbt_test_add0("Traverse the resulting dictionnary");
190   traverse(head);
191
192   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
193
194   xbt_test_add0("Free the dictionnary twice");
195   xbt_dict_free(&head);
196   xbt_dict_free(&head);
197
198   xbt_test_add0("Traverse the resulting dictionnary");
199   traverse(head);
200 }
201
202 XBT_TEST_UNIT("remove",test_dict_remove,"Removing some values"){
203   fill(&head);
204   count(head, 7);
205   xbt_test_add0("Remove non existing data");
206   TRY {
207     debuged_remove(head,"Does not exist");
208   } CATCH(e) {
209     if (e.category != not_found_error) 
210       xbt_test_exception(e);
211     xbt_ex_free(e);
212   }
213   traverse(head);
214
215   xbt_dict_free(&head);
216
217   xbt_test_add0("Remove each data manually (traversing the resulting dictionnary each time)");
218   fill(&head);
219   debuged_remove(head,"12a");    traverse(head);
220   count(head, 6);
221   debuged_remove(head,"12b");    traverse(head);
222   count(head, 5);
223   debuged_remove(head,"12");     traverse(head);
224   count(head, 4);
225   debuged_remove(head,"123456"); traverse(head);
226   count(head, 3);
227   TRY {
228     debuged_remove(head,"12346");
229   } CATCH(e) {
230     if (e.category != not_found_error) 
231       xbt_test_exception(e);
232     xbt_ex_free(e);         
233     traverse(head);
234   } 
235   debuged_remove(head,"1234");   traverse(head);
236   debuged_remove(head,"123457"); traverse(head);
237   debuged_remove(head,"123");    traverse(head);
238   TRY {
239     debuged_remove(head,"12346");
240   } CATCH(e) {
241     if (e.category != not_found_error) 
242       xbt_test_exception(e);
243     xbt_ex_free(e);
244   }                              traverse(head);
245   
246   xbt_test_add0("Remove all values");
247   xbt_dict_free(&head);
248   fill(&head);
249   xbt_dict_reset(head);
250   count(head, 0);
251   traverse(head);
252
253   xbt_test_add0("Free the dictionnary twice");
254   xbt_dict_free(&head);
255   xbt_dict_free(&head);      
256 }
257
258 XBT_TEST_UNIT("nulldata",test_dict_nulldata,"NULL data management"){
259   fill(&head);
260
261   xbt_test_add0("Store NULL under 'null'");
262   xbt_dict_set(head,"null",NULL,NULL);
263   search_ext(head,"null",NULL);
264
265   xbt_test_add0("Check whether I see it while traversing...");
266   {
267     xbt_dict_cursor_t cursor=NULL;
268     char *key;
269     int found=0;
270
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"))
274         found = 1;
275     }
276     xbt_test_assert0(found,"the key 'null', associated to NULL is not found");
277   }
278   xbt_dict_free(&head);
279 }
280
281 #define NB_ELM 20000
282 #define SIZEOFKEY 1024
283 static int countelems(xbt_dict_t head) {
284   xbt_dict_cursor_t cursor;
285   char *key;
286   void *data;
287   int res = 0;
288
289   xbt_dict_foreach(head,cursor,key,data) {
290     res++;
291   }
292   return res;
293 }
294
295 XBT_TEST_UNIT("crash",test_dict_crash,"Crash test"){
296   xbt_dict_t head=NULL;
297   int i,j,k, nb;
298   char *key;
299   void *data;
300
301   srand((unsigned int)time(NULL));
302
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);
306
307   for (i=0;i<10;i++) {
308     head=xbt_dict_new();
309     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
310     nb=0;
311     for (j=0;j<1000;j++) {
312       key=xbt_malloc(SIZEOFKEY);
313
314       for (k=0;k<SIZEOFKEY-1;k++)
315         key[k]=rand() % ('z' - 'a') + 'a';
316       key[k]='\0';
317       /*      printf("[%d %s]\n",j,key); */
318       xbt_dict_set(head,key,key,&free);
319     }
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);
323     traverse(head);
324     xbt_dict_free(&head);
325     xbt_dict_free(&head);
326   }
327
328
329   head=xbt_dict_new();
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); } */
333
334     key = xbt_malloc(10);
335     
336     sprintf(key,"%d",j);
337     xbt_dict_set(head,key,key,&free);
338   }
339
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);
343
344   xbt_test_add1("Search my %d elements 20 times",NB_ELM);
345   key=xbt_malloc(10);
346   for (i=0;i<20;i++) {
347     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
348     for (j=0;j<NB_ELM;j++) {
349       
350       sprintf(key,"%d",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);
354     }
355   }
356   free(key);
357
358   xbt_test_add1("Remove my %d elements",NB_ELM);
359   key=xbt_malloc(10);
360   for (j=0;j<NB_ELM;j++) {
361     /* if (!(j%10000)) printf("."); fflush(stdout); */
362     
363     sprintf(key,"%d",j);
364     xbt_dict_remove(head,key);
365   }
366   free(key);
367
368   
369   xbt_test_add0("Free the structure (twice)");
370   xbt_dict_free(&head);
371   xbt_dict_free(&head);
372 }
373
374 static void str_free(void *s) {
375   char *c=*(char**)s;
376   free(c);
377 }
378
379 XBT_TEST_UNIT("multicrash",test_dict_multicrash,"Multi-dict crash test"){
380
381 #undef NB_ELM
382 #define NB_ELM 100 /*00*/
383 #define DEPTH 5
384 #define KEY_SIZE 512
385 #define NB_TEST 20 /*20*/
386 int verbose=0;
387
388   xbt_dict_t mdict = NULL;
389   int i,j,k,l;
390   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
391   void *data;
392   char *key;
393
394
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);
399
400   for (l=0 ; l<DEPTH ; l++) {
401     key=xbt_malloc(KEY_SIZE);
402     xbt_dynar_push(keys,&key);
403   }     
404
405   for (i=0;i<NB_TEST;i++) {
406     mdict = xbt_dict_new();
407     VERB1("mdict=%p",mdict);
408     if (verbose>0)
409       printf("Test %d\n",i);
410     /* else if (i%10) printf("."); else printf("%d",i/10);*/
411     
412     for (j=0;j<NB_ELM;j++) {
413       if (verbose>0) printf ("  Add {");
414       
415       for (l=0 ; l<DEPTH ; l++) {
416         key=*(char**)xbt_dynar_get_ptr(keys,l);
417         
418         for (k=0;k<KEY_SIZE-1;k++) 
419           key[k]=rand() % ('z' - 'a') + 'a';
420           
421         key[k]='\0';
422         
423         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
424       }
425       if (verbose>0) printf("in multitree %p.\n",mdict);
426                                                         
427       xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
428
429       data = xbt_multidict_get(mdict,keys);
430
431       xbt_test_assert2(data && !strcmp((char*)data,key),
432                        "Retrieved value (%s) does not match the entrered one (%s)\n",
433                        (char*)data,key);
434     }
435     xbt_dict_free(&mdict);
436   }
437   
438   xbt_dynar_free(&keys);
439
440 /*  if (verbose>0)
441     xbt_dict_dump(mdict,&xbt_dict_print);*/
442     
443   xbt_dict_free(&mdict);
444   xbt_dynar_free(&keys);
445
446 }
447 /*******************************/
448 /* GENERATED FILE, DO NOT EDIT */
449 /*******************************/
450