Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Some log default categories are already defined.
[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 #line 810 "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 static void print_str(void *str)
20 {
21   printf("%s", (char *) PRINTF_STR(str));
22 }
23
24 static void debuged_add_ext(xbt_dict_t head, const char *key,
25                             const char *data_to_fill)
26 {
27   char *data = xbt_strdup(data_to_fill);
28
29   xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
30
31   xbt_dict_set(head, key, data, &free);
32   if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
33     xbt_dict_dump(head, (void (*)(void *)) &printf);
34     fflush(stdout);
35   }
36 }
37
38 static void debuged_add(xbt_dict_t head, const char *key)
39 {
40   debuged_add_ext(head, key, key);
41 }
42
43 static void fill(xbt_dict_t * head)
44 {
45   xbt_test_add0("Fill in the dictionnary");
46
47   *head = xbt_dict_new();
48   debuged_add(*head, "12");
49   debuged_add(*head, "12a");
50   debuged_add(*head, "12b");
51   debuged_add(*head, "123");
52   debuged_add(*head, "123456");
53   /* Child becomes child of what to add */
54   debuged_add(*head, "1234");
55   /* Need of common ancestor */
56   debuged_add(*head, "123457");
57 }
58
59
60 static void search_ext(xbt_dict_t head, const char *key, const char *data)
61 {
62   void *found;
63
64   xbt_test_add1("Search %s", key);
65   found = xbt_dict_get(head, key);
66   xbt_test_log1("Found %s", (char *) found);
67   if (data)
68     xbt_test_assert1(found,
69                      "data do not match expectations: found NULL while searching for %s",
70                      data);
71   if (found)
72     xbt_test_assert2(!strcmp((char *) data, found),
73                      "data do not match expectations: found %s while searching for %s",
74                      (char *) found, data);
75 }
76
77 static void search(xbt_dict_t head, const char *key)
78 {
79   search_ext(head, key, key);
80 }
81
82 static void debuged_remove(xbt_dict_t head, const char *key)
83 {
84
85   xbt_test_add1("Remove '%s'", PRINTF_STR(key));
86   xbt_dict_remove(head, key);
87   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
88 }
89
90
91 static void traverse(xbt_dict_t head)
92 {
93   xbt_dict_cursor_t cursor = NULL;
94   char *key;
95   char *data;
96   int i = 0;
97
98   xbt_dict_foreach(head, cursor, key, data) {
99     if (!key || !data || strcmp(key, data)) {
100       xbt_test_log3("Seen #%d:  %s->%s", ++i, PRINTF_STR(key),
101                     PRINTF_STR(data));
102     } else {
103       xbt_test_log2("Seen #%d:  %s", ++i, PRINTF_STR(key));
104     }
105     xbt_test_assert2(!data || !strcmp(key, data),
106                      "Key(%s) != value(%s). Aborting", key, data);
107   }
108 }
109
110 static void search_not_found(xbt_dict_t head, const char *data)
111 {
112   int ok = 0;
113   xbt_ex_t e;
114
115   xbt_test_add1("Search %s (expected not to be found)", data);
116
117   TRY {
118     data = xbt_dict_get(head, data);
119     THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
120            data);
121   } CATCH(e) {
122     if (e.category != not_found_error)
123       xbt_test_exception(e);
124     xbt_ex_free(e);
125     ok = 1;
126   }
127   xbt_test_assert0(ok, "Exception not raised");
128 }
129
130 static void count(xbt_dict_t dict, int length)
131 {
132   xbt_dict_cursor_t cursor;
133   char *key;
134   void *data;
135   int effective = 0;
136
137
138   xbt_test_add1("Count elements (expecting %d)", length);
139   xbt_test_assert2(xbt_dict_length(dict) == length,
140                    "Announced length(%d) != %d.", xbt_dict_length(dict),
141                    length);
142
143   xbt_dict_foreach(dict, cursor, key, data)
144     effective++;
145
146   xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
147                    effective, length);
148
149 }
150
151 static void count_check_get_key(xbt_dict_t dict, int length)
152 {
153   xbt_dict_cursor_t cursor;
154   char *key,*key2;
155   void *data;
156   int effective = 0;
157
158
159   xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
160   xbt_test_assert2(xbt_dict_length(dict) == length,
161                    "Announced length(%d) != %d.", xbt_dict_length(dict),
162                    length);
163
164   xbt_dict_foreach(dict, cursor, key, data) {
165     effective++;
166     key2 = xbt_dict_get_key(dict,data);
167     xbt_assert2(!strcmp(key,key2),
168           "The data was registered under %s instead of %s as expected",key2,key);
169   }
170
171   xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
172                    effective, length);
173
174 }
175
176 xbt_ex_t e;
177 xbt_dict_t head = NULL;
178 char *data;
179
180
181 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
182 {
183   xbt_test_add0("Traversal the null dictionary");
184   traverse(head);
185
186   xbt_test_add0("Traversal and search the empty dictionary");
187   head = xbt_dict_new();
188   traverse(head);
189   TRY {
190     debuged_remove(head, "12346");
191   } CATCH(e) {
192     if (e.category != not_found_error)
193       xbt_test_exception(e);
194     xbt_ex_free(e);
195   }
196   xbt_dict_free(&head);
197
198   xbt_test_add0("Traverse the full dictionary");
199   fill(&head);
200   count_check_get_key(head, 7);
201
202   debuged_add_ext(head, "toto", "tutu");
203   search_ext(head, "toto", "tutu");
204   debuged_remove(head, "toto");
205
206   search(head, "12a");
207   traverse(head);
208
209   xbt_test_add0("Free the dictionary (twice)");
210   xbt_dict_free(&head);
211   xbt_dict_free(&head);
212
213   /* CHANGING */
214   fill(&head);
215   count_check_get_key(head, 7);
216   xbt_test_add0("Change 123 to 'Changed 123'");
217   xbt_dict_set(head, "123", strdup("Changed 123"), &free);
218   count_check_get_key(head, 7);
219
220   xbt_test_add0("Change 123 back to '123'");
221   xbt_dict_set(head, "123", strdup("123"), &free);
222   count_check_get_key(head, 7);
223
224   xbt_test_add0("Change 12a to 'Dummy 12a'");
225   xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
226   count_check_get_key(head, 7);
227
228   xbt_test_add0("Change 12a to '12a'");
229   xbt_dict_set(head, "12a", strdup("12a"), &free);
230   count_check_get_key(head, 7);
231
232   xbt_test_add0("Traverse the resulting dictionary");
233   traverse(head);
234
235   /* RETRIEVE */
236   xbt_test_add0("Search 123");
237   data = xbt_dict_get(head, "123");
238   xbt_test_assert(data);
239   xbt_test_assert(!strcmp("123", data));
240
241   search_not_found(head, "Can't be found");
242   search_not_found(head, "123 Can't be found");
243   search_not_found(head, "12345678 NOT");
244
245   search(head, "12a");
246   search(head, "12b");
247   search(head, "12");
248   search(head, "123456");
249   search(head, "1234");
250   search(head, "123457");
251
252   xbt_test_add0("Traverse the resulting dictionary");
253   traverse(head);
254
255   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
256
257   xbt_test_add0("Free the dictionary twice");
258   xbt_dict_free(&head);
259   xbt_dict_free(&head);
260
261   xbt_test_add0("Traverse the resulting dictionary");
262   traverse(head);
263 }
264
265 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
266 {
267   fill(&head);
268   count(head, 7);
269   xbt_test_add0("Remove non existing data");
270   TRY {
271     debuged_remove(head, "Does not exist");
272   }
273   CATCH(e) {
274     if (e.category != not_found_error)
275       xbt_test_exception(e);
276     xbt_ex_free(e);
277   }
278   traverse(head);
279
280   xbt_dict_free(&head);
281
282   xbt_test_add0
283     ("Remove each data manually (traversing the resulting dictionary each time)");
284   fill(&head);
285   debuged_remove(head, "12a");
286   traverse(head);
287   count(head, 6);
288   debuged_remove(head, "12b");
289   traverse(head);
290   count(head, 5);
291   debuged_remove(head, "12");
292   traverse(head);
293   count(head, 4);
294   debuged_remove(head, "123456");
295   traverse(head);
296   count(head, 3);
297   TRY {
298     debuged_remove(head, "12346");
299   }
300   CATCH(e) {
301     if (e.category != not_found_error)
302       xbt_test_exception(e);
303     xbt_ex_free(e);
304     traverse(head);
305   }
306   debuged_remove(head, "1234");
307   traverse(head);
308   debuged_remove(head, "123457");
309   traverse(head);
310   debuged_remove(head, "123");
311   traverse(head);
312   TRY {
313     debuged_remove(head, "12346");
314   }
315   CATCH(e) {
316     if (e.category != not_found_error)
317       xbt_test_exception(e);
318     xbt_ex_free(e);
319   }
320   traverse(head);
321
322   xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
323   xbt_dict_free(&head);
324   fill(&head);
325   xbt_dict_reset(head);
326   count(head, 0);
327   traverse(head);
328
329   xbt_test_add0("Free the dictionary twice");
330   xbt_dict_free(&head);
331   xbt_dict_free(&head);
332 }
333
334 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
335 {
336   fill(&head);
337
338   xbt_test_add0("Store NULL under 'null'");
339   xbt_dict_set(head, "null", NULL, NULL);
340   search_ext(head, "null", NULL);
341
342   xbt_test_add0("Check whether I see it while traversing...");
343   {
344     xbt_dict_cursor_t cursor = NULL;
345     char *key;
346     int found = 0;
347
348     xbt_dict_foreach(head, cursor, key, data) {
349       if (!key || !data || strcmp(key, data)) {
350         xbt_test_log2("Seen:  %s->%s", PRINTF_STR(key), PRINTF_STR(data));
351       } else {
352         xbt_test_log1("Seen:  %s", PRINTF_STR(key));
353       }
354
355       if (!strcmp(key, "null"))
356         found = 1;
357     }
358     xbt_test_assert0(found,
359                      "the key 'null', associated to NULL is not found");
360   }
361   xbt_dict_free(&head);
362 }
363
364 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
365         uintptr_t stored_data = 0;
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   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