Logo AND Algorithmique Numérique Distribuée

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