Logo AND Algorithmique Numérique Distribuée

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