Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
creating a source file dedicated to handle the categories
[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
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         uintptr_t stored_data = 0;
367   xbt_test_log2("Add %zu under %zu", data, key);
368
369   xbt_dicti_set(head, key, data);
370   if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
371     xbt_dict_dump(head, (void (*)(void *)) &printf);
372     fflush(stdout);
373   }
374   stored_data = xbt_dicti_get(head, key);
375   xbt_test_assert3(stored_data==data,
376       "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
377 }
378
379 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
380 {
381   xbt_test_add0("Fill in the dictionnary");
382
383   head = xbt_dict_new();
384   debuged_addi(head, 12, 12);
385   debuged_addi(head, 13, 13);
386   debuged_addi(head, 14, 14);
387   debuged_addi(head, 15, 15);
388   /* Change values */
389   debuged_addi(head, 12, 15);
390   debuged_addi(head, 15, 2000);
391   debuged_addi(head, 15, 3000);
392   /* 0 as key */
393   debuged_addi(head, 0, 1000);
394   debuged_addi(head, 0, 2000);
395   debuged_addi(head, 0, 3000);
396   /* 0 as value */
397   debuged_addi(head, 12, 0);
398   debuged_addi(head, 13, 0);
399   debuged_addi(head, 12, 0);
400   debuged_addi(head, 0, 0);
401
402   xbt_dict_free(&head);
403 }
404
405 #define NB_ELM 20000
406 #define SIZEOFKEY 1024
407 static int countelems(xbt_dict_t head)
408 {
409   xbt_dict_cursor_t cursor;
410   char *key;
411   void *data;
412   int res = 0;
413
414   xbt_dict_foreach(head, cursor, key, data) {
415     res++;
416   }
417   return res;
418 }
419
420 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
421 {
422   xbt_dict_t head = NULL;
423   int i, j, k, nb;
424   char *key;
425   void *data;
426
427   srand((unsigned int) time(NULL));
428
429   for (i = 0; i < 10; i++) {
430     xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
431     xbt_test_log0("Fill the struct, count its elems and frees the structure");
432     xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
433                   SIZEOFKEY);
434     head = xbt_dict_new();
435     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
436     nb = 0;
437     for (j = 0; j < 1000; j++) {
438       char *data = NULL;
439       key = xbt_malloc(SIZEOFKEY);
440
441       do {
442         for (k = 0; k < SIZEOFKEY - 1; k++)
443           key[k] = rand() % ('z' - 'a') + 'a';
444         key[k] = '\0';
445         /*      printf("[%d %s]\n",j,key); */
446         data = xbt_dict_get_or_null(head, key);
447       } while (data != NULL);
448
449       xbt_dict_set(head, key, key, &free);
450       data = xbt_dict_get(head, key);
451       xbt_test_assert2(!strcmp(key, data),
452                        "Retrieved value (%s) != Injected value (%s)", key,
453                        data);
454
455       count(head, j + 1);
456     }
457     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
458     traverse(head);
459     xbt_dict_free(&head);
460     xbt_dict_free(&head);
461   }
462
463
464   head = xbt_dict_new();
465   xbt_test_add1("Fill %d elements, with keys being the number of element",
466                 NB_ELM);
467   for (j = 0; j < NB_ELM; j++) {
468     /* if (!(j%1000)) { printf("."); fflush(stdout); } */
469
470     key = xbt_malloc(10);
471
472     sprintf(key, "%d", j);
473     xbt_dict_set(head, key, key, &free);
474   }
475   /*xbt_dict_dump(head,(void (*)(void*))&printf); */
476
477   xbt_test_add0("Count the elements (retrieving the key and data for each)");
478   i = countelems(head);
479   xbt_test_log1("There is %d elements", i);
480
481   xbt_test_add1("Search my %d elements 20 times", NB_ELM);
482   key = xbt_malloc(10);
483   for (i = 0; i < 20; i++) {
484     /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
485     for (j = 0; j < NB_ELM; j++) {
486
487       sprintf(key, "%d", j);
488       data = xbt_dict_get(head, key);
489       xbt_test_assert2(!strcmp(key, (char *) data),
490                        "with get, key=%s != data=%s", key, (char *) data);
491       data = xbt_dict_get_ext(head, key, strlen(key));
492       xbt_test_assert2(!strcmp(key, (char *) data),
493                        "with get_ext, key=%s != data=%s", key, (char *) data);
494     }
495   }
496   free(key);
497
498   xbt_test_add1("Remove my %d elements", NB_ELM);
499   key = xbt_malloc(10);
500   for (j = 0; j < NB_ELM; j++) {
501     /* if (!(j%10000)) printf("."); fflush(stdout); */
502
503     sprintf(key, "%d", j);
504     xbt_dict_remove(head, key);
505   }
506   free(key);
507
508
509   xbt_test_add0("Free the structure (twice)");
510   xbt_dict_free(&head);
511   xbt_dict_free(&head);
512 }
513
514 static void str_free(void *s)
515 {
516   char *c = *(char **) s;
517   free(c);
518 }
519
520 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
521 {
522
523 #undef NB_ELM
524 #define NB_ELM 100              /*00 */
525 #define DEPTH 5
526 #define KEY_SIZE 512
527 #define NB_TEST 20              /*20 */
528   int verbose = 0;
529
530   xbt_dict_t mdict = NULL;
531   int i, j, k, l;
532   xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
533   void *data;
534   char *key;
535
536
537   xbt_test_add0("Generic multicache CRASH test");
538   xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
539                 "depth of multicache=%d, key size=%d",
540                 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
541
542   for (l = 0; l < DEPTH; l++) {
543     key = xbt_malloc(KEY_SIZE);
544     xbt_dynar_push(keys, &key);
545   }
546
547   for (i = 0; i < NB_TEST; i++) {
548     mdict = xbt_dict_new();
549     VERB1("mdict=%p", mdict);
550     if (verbose > 0)
551       printf("Test %d\n", i);
552     /* else if (i%10) printf("."); else printf("%d",i/10); */
553
554     for (j = 0; j < NB_ELM; j++) {
555       if (verbose > 0)
556         printf("  Add {");
557
558       for (l = 0; l < DEPTH; l++) {
559         key = *(char **) xbt_dynar_get_ptr(keys, l);
560
561         for (k = 0; k < KEY_SIZE - 1; k++)
562           key[k] = rand() % ('z' - 'a') + 'a';
563
564         key[k] = '\0';
565
566         if (verbose > 0)
567           printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
568       }
569       if (verbose > 0)
570         printf("in multitree %p.\n", mdict);
571
572       xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
573
574       data = xbt_multidict_get(mdict, keys);
575
576       xbt_test_assert2(data && !strcmp((char *) data, key),
577                        "Retrieved value (%s) does not match the given one (%s)\n",
578                        (char *) data, key);
579     }
580     xbt_dict_free(&mdict);
581   }
582
583   xbt_dynar_free(&keys);
584
585   /*  if (verbose>0)
586      xbt_dict_dump(mdict,&xbt_dict_print); */
587
588   xbt_dict_free(&mdict);
589   xbt_dynar_free(&keys);
590
591 }
592 /*******************************/
593 /* GENERATED FILE, DO NOT EDIT */
594 /*******************************/
595