Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix so enable_compile_warnings works nice when enable_tracing is on
[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 # 799 "/home/navarrop/Developments/simgrid/src/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 {
22   printf("%s", (char *) PRINTF_STR(str));
23 }
24
25 static void debuged_add_ext(xbt_dict_t head, const char *key,
26                             const char *data_to_fill)
27 {
28   char *data = xbt_strdup(data_to_fill);
29
30   xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
31
32   xbt_dict_set(head, key, data, &free);
33   if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
34     xbt_dict_dump(head, (void (*)(void *)) &printf);
35     fflush(stdout);
36   }
37 }
38
39 static void debuged_add(xbt_dict_t head, const char *key)
40 {
41   debuged_add_ext(head, key, key);
42 }
43
44 static void fill(xbt_dict_t * head)
45 {
46   xbt_test_add0("Fill in the dictionnary");
47
48   *head = xbt_dict_new();
49   debuged_add(*head, "12");
50   debuged_add(*head, "12a");
51   debuged_add(*head, "12b");
52   debuged_add(*head, "123");
53   debuged_add(*head, "123456");
54   /* Child becomes child of what to add */
55   debuged_add(*head, "1234");
56   /* Need of common ancestor */
57   debuged_add(*head, "123457");
58 }
59
60
61 static void search_ext(xbt_dict_t head, const char *key, const char *data)
62 {
63   void *found;
64
65   xbt_test_add1("Search %s", key);
66   found = xbt_dict_get(head, key);
67   xbt_test_log1("Found %s", (char *) found);
68   if (data)
69     xbt_test_assert1(found,
70                      "data do not match expectations: found NULL while searching for %s",
71                      data);
72   if (found)
73     xbt_test_assert2(!strcmp((char *) data, found),
74                      "data do not match expectations: found %s while searching for %s",
75                      (char *) found, data);
76 }
77
78 static void search(xbt_dict_t head, const char *key)
79 {
80   search_ext(head, key, key);
81 }
82
83 static void debuged_remove(xbt_dict_t head, const char *key)
84 {
85
86   xbt_test_add1("Remove '%s'", PRINTF_STR(key));
87   xbt_dict_remove(head, key);
88   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
89 }
90
91
92 static void traverse(xbt_dict_t head)
93 {
94   xbt_dict_cursor_t cursor = NULL;
95   char *key;
96   char *data;
97   int i = 0;
98
99   xbt_dict_foreach(head, cursor, key, data) {
100     if (!key || !data || strcmp(key, data)) {
101       xbt_test_log3("Seen #%d:  %s->%s", ++i, PRINTF_STR(key),
102                     PRINTF_STR(data));
103     } else {
104       xbt_test_log2("Seen #%d:  %s", ++i, PRINTF_STR(key));
105     }
106     xbt_test_assert2(!data || !strcmp(key, data),
107                      "Key(%s) != value(%s). Aborting", key, data);
108   }
109 }
110
111 static void search_not_found(xbt_dict_t head, const char *data)
112 {
113   int ok = 0;
114   xbt_ex_t e;
115
116   xbt_test_add1("Search %s (expected not to be found)", data);
117
118   TRY {
119     data = xbt_dict_get(head, data);
120     THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
121            data);
122   } CATCH(e) {
123     if (e.category != not_found_error)
124       xbt_test_exception(e);
125     xbt_ex_free(e);
126     ok = 1;
127   }
128   xbt_test_assert0(ok, "Exception not raised");
129 }
130
131 static void count(xbt_dict_t dict, int length)
132 {
133   xbt_dict_cursor_t cursor;
134   char *key;
135   void *data;
136   int effective = 0;
137
138
139   xbt_test_add1("Count elements (expecting %d)", length);
140   xbt_test_assert2(xbt_dict_length(dict) == length,
141                    "Announced length(%d) != %d.", xbt_dict_length(dict),
142                    length);
143
144   xbt_dict_foreach(dict, cursor, key, data)
145     effective++;
146
147   xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
148                    effective, length);
149
150 }
151
152 static void count_check_get_key(xbt_dict_t dict, int length)
153 {
154   xbt_dict_cursor_t cursor;
155   char *key,*key2;
156   void *data;
157   int effective = 0;
158
159
160   xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
161   xbt_test_assert2(xbt_dict_length(dict) == length,
162                    "Announced length(%d) != %d.", xbt_dict_length(dict),
163                    length);
164
165   xbt_dict_foreach(dict, cursor, key, data) {
166     effective++;
167     key2 = xbt_dict_get_key(dict,data);
168     xbt_assert2(!strcmp(key,key2),
169           "The data was registered under %s instead of %s as expected",key2,key);
170   }
171
172   xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
173                    effective, length);
174
175 }
176
177 xbt_ex_t e;
178 xbt_dict_t head = NULL;
179 char *data;
180
181
182 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
183 {
184   xbt_test_add0("Traversal the null dictionary");
185   traverse(head);
186
187   xbt_test_add0("Traversal and search the empty dictionary");
188   head = xbt_dict_new();
189   traverse(head);
190   TRY {
191     debuged_remove(head, "12346");
192   } CATCH(e) {
193     if (e.category != not_found_error)
194       xbt_test_exception(e);
195     xbt_ex_free(e);
196   }
197   xbt_dict_free(&head);
198
199   xbt_test_add0("Traverse the full dictionary");
200   fill(&head);
201   count_check_get_key(head, 7);
202
203   debuged_add_ext(head, "toto", "tutu");
204   search_ext(head, "toto", "tutu");
205   debuged_remove(head, "toto");
206
207   search(head, "12a");
208   traverse(head);
209
210   xbt_test_add0("Free the dictionary (twice)");
211   xbt_dict_free(&head);
212   xbt_dict_free(&head);
213
214   /* CHANGING */
215   fill(&head);
216   count_check_get_key(head, 7);
217   xbt_test_add0("Change 123 to 'Changed 123'");
218   xbt_dict_set(head, "123", strdup("Changed 123"), &free);
219   count_check_get_key(head, 7);
220
221   xbt_test_add0("Change 123 back to '123'");
222   xbt_dict_set(head, "123", strdup("123"), &free);
223   count_check_get_key(head, 7);
224
225   xbt_test_add0("Change 12a to 'Dummy 12a'");
226   xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
227   count_check_get_key(head, 7);
228
229   xbt_test_add0("Change 12a to '12a'");
230   xbt_dict_set(head, "12a", strdup("12a"), &free);
231   count_check_get_key(head, 7);
232
233   xbt_test_add0("Traverse the resulting dictionary");
234   traverse(head);
235
236   /* RETRIEVE */
237   xbt_test_add0("Search 123");
238   data = xbt_dict_get(head, "123");
239   xbt_test_assert(data);
240   xbt_test_assert(!strcmp("123", data));
241
242   search_not_found(head, "Can't be found");
243   search_not_found(head, "123 Can't be found");
244   search_not_found(head, "12345678 NOT");
245
246   search(head, "12a");
247   search(head, "12b");
248   search(head, "12");
249   search(head, "123456");
250   search(head, "1234");
251   search(head, "123457");
252
253   xbt_test_add0("Traverse the resulting dictionary");
254   traverse(head);
255
256   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
257
258   xbt_test_add0("Free the dictionary twice");
259   xbt_dict_free(&head);
260   xbt_dict_free(&head);
261
262   xbt_test_add0("Traverse the resulting dictionary");
263   traverse(head);
264 }
265
266 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
267 {
268   fill(&head);
269   count(head, 7);
270   xbt_test_add0("Remove non existing data");
271   TRY {
272     debuged_remove(head, "Does not exist");
273   }
274   CATCH(e) {
275     if (e.category != not_found_error)
276       xbt_test_exception(e);
277     xbt_ex_free(e);
278   }
279   traverse(head);
280
281   xbt_dict_free(&head);
282
283   xbt_test_add0
284     ("Remove each data manually (traversing the resulting dictionary each time)");
285   fill(&head);
286   debuged_remove(head, "12a");
287   traverse(head);
288   count(head, 6);
289   debuged_remove(head, "12b");
290   traverse(head);
291   count(head, 5);
292   debuged_remove(head, "12");
293   traverse(head);
294   count(head, 4);
295   debuged_remove(head, "123456");
296   traverse(head);
297   count(head, 3);
298   TRY {
299     debuged_remove(head, "12346");
300   }
301   CATCH(e) {
302     if (e.category != not_found_error)
303       xbt_test_exception(e);
304     xbt_ex_free(e);
305     traverse(head);
306   }
307   debuged_remove(head, "1234");
308   traverse(head);
309   debuged_remove(head, "123457");
310   traverse(head);
311   debuged_remove(head, "123");
312   traverse(head);
313   TRY {
314     debuged_remove(head, "12346");
315   }
316   CATCH(e) {
317     if (e.category != not_found_error)
318       xbt_test_exception(e);
319     xbt_ex_free(e);
320   }
321   traverse(head);
322
323   xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
324   xbt_dict_free(&head);
325   fill(&head);
326   xbt_dict_reset(head);
327   count(head, 0);
328   traverse(head);
329
330   xbt_test_add0("Free the dictionary twice");
331   xbt_dict_free(&head);
332   xbt_dict_free(&head);
333 }
334
335 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
336 {
337   fill(&head);
338
339   xbt_test_add0("Store NULL under 'null'");
340   xbt_dict_set(head, "null", NULL, NULL);
341   search_ext(head, "null", NULL);
342
343   xbt_test_add0("Check whether I see it while traversing...");
344   {
345     xbt_dict_cursor_t cursor = NULL;
346     char *key;
347     int found = 0;
348
349     xbt_dict_foreach(head, cursor, key, data) {
350       if (!key || !data || strcmp(key, data)) {
351         xbt_test_log2("Seen:  %s->%s", PRINTF_STR(key), PRINTF_STR(data));
352       } else {
353         xbt_test_log1("Seen:  %s", PRINTF_STR(key));
354       }
355
356       if (!strcmp(key, "null"))
357         found = 1;
358     }
359     xbt_test_assert0(found,
360                      "the key 'null', associated to NULL is not found");
361   }
362   xbt_dict_free(&head);
363 }
364
365 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
366   xbt_test_log2("Add %zu under %zu", data, key);
367
368   xbt_dicti_set(head, key, data);
369   if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
370     xbt_dict_dump(head, (void (*)(void *)) &printf);
371     fflush(stdout);
372   }
373   uintptr_t stored_data = xbt_dicti_get(head, key);
374   xbt_test_assert3(stored_data==data,
375       "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
376 }
377
378 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
379 {
380   xbt_test_add0("Fill in the dictionnary");
381
382   head = xbt_dict_new();
383   debuged_addi(head, 12, 12);
384   debuged_addi(head, 13, 13);
385   debuged_addi(head, 14, 14);
386   debuged_addi(head, 15, 15);
387   /* Change values */
388   debuged_addi(head, 12, 15);
389   debuged_addi(head, 15, 2000);
390   debuged_addi(head, 15, 3000);
391   /* 0 as key */
392   debuged_addi(head, 0, 1000);
393   debuged_addi(head, 0, 2000);
394   debuged_addi(head, 0, 3000);
395   /* 0 as value */
396   debuged_addi(head, 12, 0);
397   debuged_addi(head, 13, 0);
398   debuged_addi(head, 12, 0);
399   debuged_addi(head, 0, 0);
400
401   xbt_dict_free(&head);
402 }
403
404 #define NB_ELM 20000
405 #define SIZEOFKEY 1024
406 static int countelems(xbt_dict_t head)
407 {
408   xbt_dict_cursor_t cursor;
409   char *key;
410   void *data;
411   int res = 0;
412
413   xbt_dict_foreach(head, cursor, key, data) {
414     res++;
415   }
416   return res;
417 }
418
419 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
420 {
421   xbt_dict_t head = NULL;
422   int i, j, k, nb;
423   char *key;
424   void *data;
425
426   srand((unsigned int) time(NULL));
427
428   for (i = 0; i < 10; i++) {
429     xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
430     xbt_test_log0("Fill the struct, count its elems and frees the structure");
431     xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
432                   SIZEOFKEY);
433     head = xbt_dict_new();
434     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
435     nb = 0;
436     for (j = 0; j < 1000; j++) {
437       char *data = NULL;
438       key = xbt_malloc(SIZEOFKEY);
439
440       do {
441         for (k = 0; k < SIZEOFKEY - 1; k++)
442           key[k] = rand() % ('z' - 'a') + 'a';
443         key[k] = '\0';
444         /*      printf("[%d %s]\n",j,key); */
445         data = xbt_dict_get_or_null(head, key);
446       } while (data != NULL);
447
448       xbt_dict_set(head, key, key, &free);
449       data = xbt_dict_get(head, key);
450       xbt_test_assert2(!strcmp(key, data),
451                        "Retrieved value (%s) != Injected value (%s)", key,
452                        data);
453
454       count(head, j + 1);
455     }
456     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
457     traverse(head);
458     xbt_dict_free(&head);
459     xbt_dict_free(&head);
460   }
461
462
463   head = xbt_dict_new();
464   xbt_test_add1("Fill %d elements, with keys being the number of element",
465                 NB_ELM);
466   for (j = 0; j < NB_ELM; j++) {
467     /* if (!(j%1000)) { printf("."); fflush(stdout); } */
468
469     key = xbt_malloc(10);
470
471     sprintf(key, "%d", j);
472     xbt_dict_set(head, key, key, &free);
473   }
474   /*xbt_dict_dump(head,(void (*)(void*))&printf); */
475
476   xbt_test_add0("Count the elements (retrieving the key and data for each)");
477   i = countelems(head);
478   xbt_test_log1("There is %d elements", i);
479
480   xbt_test_add1("Search my %d elements 20 times", NB_ELM);
481   key = xbt_malloc(10);
482   for (i = 0; i < 20; i++) {
483     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
484     for (j = 0; j < NB_ELM; j++) {
485
486       sprintf(key, "%d", j);
487       data = xbt_dict_get(head, key);
488       xbt_test_assert2(!strcmp(key, (char *) data),
489                        "with get, key=%s != data=%s", key, (char *) data);
490       data = xbt_dict_get_ext(head, key, strlen(key));
491       xbt_test_assert2(!strcmp(key, (char *) data),
492                        "with get_ext, key=%s != data=%s", key, (char *) data);
493     }
494   }
495   free(key);
496
497   xbt_test_add1("Remove my %d elements", NB_ELM);
498   key = xbt_malloc(10);
499   for (j = 0; j < NB_ELM; j++) {
500     /* if (!(j%10000)) printf("."); fflush(stdout); */
501
502     sprintf(key, "%d", j);
503     xbt_dict_remove(head, key);
504   }
505   free(key);
506
507
508   xbt_test_add0("Free the structure (twice)");
509   xbt_dict_free(&head);
510   xbt_dict_free(&head);
511 }
512
513 static void str_free(void *s)
514 {
515   char *c = *(char **) s;
516   free(c);
517 }
518
519 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
520 {
521
522 #undef NB_ELM
523 #define NB_ELM 100              /*00 */
524 #define DEPTH 5
525 #define KEY_SIZE 512
526 #define NB_TEST 20              /*20 */
527   int verbose = 0;
528
529   xbt_dict_t mdict = NULL;
530   int i, j, k, l;
531   xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
532   void *data;
533   char *key;
534
535
536   xbt_test_add0("Generic multicache CRASH test");
537   xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
538                 "depth of multicache=%d, key size=%d",
539                 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
540
541   for (l = 0; l < DEPTH; l++) {
542     key = xbt_malloc(KEY_SIZE);
543     xbt_dynar_push(keys, &key);
544   }
545
546   for (i = 0; i < NB_TEST; i++) {
547     mdict = xbt_dict_new();
548     VERB1("mdict=%p", mdict);
549     if (verbose > 0)
550       printf("Test %d\n", i);
551     /* else if (i%10) printf("."); else printf("%d",i/10); */
552
553     for (j = 0; j < NB_ELM; j++) {
554       if (verbose > 0)
555         printf("  Add {");
556
557       for (l = 0; l < DEPTH; l++) {
558         key = *(char **) xbt_dynar_get_ptr(keys, l);
559
560         for (k = 0; k < KEY_SIZE - 1; k++)
561           key[k] = rand() % ('z' - 'a') + 'a';
562
563         key[k] = '\0';
564
565         if (verbose > 0)
566           printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
567       }
568       if (verbose > 0)
569         printf("in multitree %p.\n", mdict);
570
571       xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
572
573       data = xbt_multidict_get(mdict, keys);
574
575       xbt_test_assert2(data && !strcmp((char *) data, key),
576                        "Retrieved value (%s) does not match the given one (%s)\n",
577                        (char *) data, key);
578     }
579     xbt_dict_free(&mdict);
580   }
581
582   xbt_dynar_free(&keys);
583
584   /*  if (verbose>0)
585      xbt_dict_dump(mdict,&xbt_dict_print); */
586
587   xbt_dict_free(&mdict);
588   xbt_dynar_free(&keys);
589
590 }
591 /*******************************/
592 /* GENERATED FILE, DO NOT EDIT */
593 /*******************************/
594