1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
11 #line 835 "xbt/dict.c"
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
20 static void print_str(void *str)
22 printf("%s", (char *) PRINTF_STR(str));
25 static void debuged_add_ext(xbt_dict_t head, const char *key,
26 const char *data_to_fill)
28 char *data = xbt_strdup(data_to_fill);
30 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill),
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);
40 static void debuged_add(xbt_dict_t head, const char *key)
42 debuged_add_ext(head, key, key);
45 static void fill(xbt_dict_t * head)
47 xbt_test_add0("Fill in the dictionnary");
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");
62 static void search_ext(xbt_dict_t head, const char *key, const char *data)
66 xbt_test_add1("Search %s", key);
67 found = xbt_dict_get(head, key);
68 xbt_test_log1("Found %s", (char *) found);
70 xbt_test_assert1(found,
71 "data do not match expectations: found NULL while searching for %s",
74 xbt_test_assert2(!strcmp((char *) data, found),
75 "data do not match expectations: found %s while searching for %s",
76 (char *) found, data);
79 static void search(xbt_dict_t head, const char *key)
81 search_ext(head, key, key);
84 static void debuged_remove(xbt_dict_t head, const char *key)
87 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
88 xbt_dict_remove(head, key);
89 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
93 static void traverse(xbt_dict_t head)
95 xbt_dict_cursor_t cursor = NULL;
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),
105 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
107 xbt_test_assert2(!data || !strcmp(key, data),
108 "Key(%s) != value(%s). Aborting", key, data);
112 static void search_not_found(xbt_dict_t head, const char *data)
117 xbt_test_add1("Search %s (expected not to be found)", data);
120 data = xbt_dict_get(head, data);
121 THROW1(unknown_error, 0,
122 "Found something which shouldn't be there (%s)", data);
124 if (e.category != not_found_error)
125 xbt_test_exception(e);
129 xbt_test_assert0(ok, "Exception not raised");
132 static void count(xbt_dict_t dict, int length)
134 xbt_dict_cursor_t cursor;
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),
145 xbt_dict_foreach(dict, cursor, key, data)
148 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
153 static void count_check_get_key(xbt_dict_t dict, int length)
155 xbt_dict_cursor_t cursor;
162 ("Count elements (expecting %d), and test the getkey function",
164 xbt_test_assert2(xbt_dict_length(dict) == length,
165 "Announced length(%d) != %d.", xbt_dict_length(dict),
168 xbt_dict_foreach(dict, cursor, key, data) {
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",
176 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
182 xbt_dict_t head = NULL;
186 XBT_TEST_UNIT("basic", test_dict_basic, "Basic usage: change, retrieve, traverse")
188 xbt_test_add0("Traversal the null dictionary");
191 xbt_test_add0("Traversal and search the empty dictionary");
192 head = xbt_dict_new();
195 debuged_remove(head, "12346");
197 if (e.category != not_found_error)
198 xbt_test_exception(e);
201 xbt_dict_free(&head);
203 xbt_test_add0("Traverse the full dictionary");
205 count_check_get_key(head, 7);
207 debuged_add_ext(head, "toto", "tutu");
208 search_ext(head, "toto", "tutu");
209 debuged_remove(head, "toto");
214 xbt_test_add0("Free the dictionary (twice)");
215 xbt_dict_free(&head);
216 xbt_dict_free(&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);
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);
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);
233 xbt_test_add0("Change 12a to '12a'");
234 xbt_dict_set(head, "12a", strdup("12a"), &free);
235 count_check_get_key(head, 7);
237 xbt_test_add0("Traverse the resulting dictionary");
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));
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");
253 search(head, "123456");
254 search(head, "1234");
255 search(head, "123457");
257 xbt_test_add0("Traverse the resulting dictionary");
260 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
262 xbt_test_add0("Free the dictionary twice");
263 xbt_dict_free(&head);
264 xbt_dict_free(&head);
266 xbt_test_add0("Traverse the resulting dictionary");
270 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
274 xbt_test_add0("Remove non existing data");
276 debuged_remove(head, "Does not exist");
279 if (e.category != not_found_error)
280 xbt_test_exception(e);
285 xbt_dict_free(&head);
288 ("Remove each data manually (traversing the resulting dictionary each time)");
290 debuged_remove(head, "12a");
293 debuged_remove(head, "12b");
296 debuged_remove(head, "12");
299 debuged_remove(head, "123456");
303 debuged_remove(head, "12346");
306 if (e.category != not_found_error)
307 xbt_test_exception(e);
311 debuged_remove(head, "1234");
313 debuged_remove(head, "123457");
315 debuged_remove(head, "123");
318 debuged_remove(head, "12346");
321 if (e.category != not_found_error)
322 xbt_test_exception(e);
328 ("Free dict, create new fresh one, and then reset the dict");
329 xbt_dict_free(&head);
331 xbt_dict_reset(head);
335 xbt_test_add0("Free the dictionary twice");
336 xbt_dict_free(&head);
337 xbt_dict_free(&head);
340 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
344 xbt_test_add0("Store NULL under 'null'");
345 xbt_dict_set(head, "null", NULL, NULL);
346 search_ext(head, "null", NULL);
348 xbt_test_add0("Check whether I see it while traversing...");
350 xbt_dict_cursor_t cursor = NULL;
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));
358 xbt_test_log1("Seen: %s", PRINTF_STR(key));
361 if (!strcmp(key, "null"))
364 xbt_test_assert0(found,
365 "the key 'null', associated to NULL is not found");
367 xbt_dict_free(&head);
370 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data)
372 uintptr_t stored_data = 0;
373 xbt_test_log2("Add %zu under %zu", data, key);
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);
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);
386 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
388 xbt_test_add0("Fill in the dictionnary");
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);
396 debuged_addi(head, 12, 15);
397 debuged_addi(head, 15, 2000);
398 debuged_addi(head, 15, 3000);
400 debuged_addi(head, 0, 1000);
401 debuged_addi(head, 0, 2000);
402 debuged_addi(head, 0, 3000);
404 debuged_addi(head, 12, 0);
405 debuged_addi(head, 13, 0);
406 debuged_addi(head, 12, 0);
407 debuged_addi(head, 0, 0);
409 xbt_dict_free(&head);
413 #define SIZEOFKEY 1024
414 static int countelems(xbt_dict_t head)
416 xbt_dict_cursor_t cursor;
421 xbt_dict_foreach(head, cursor, key, data) {
427 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
429 xbt_dict_t head = NULL;
434 srand((unsigned int) time(NULL));
436 for (i = 0; i < 10; i++) {
437 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
439 ("Fill the struct, count its elems and frees the structure");
441 ("using 1000 elements with %d chars long randomized keys.",
443 head = xbt_dict_new();
444 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
446 for (j = 0; j < 1000; j++) {
448 key = xbt_malloc(SIZEOFKEY);
451 for (k = 0; k < SIZEOFKEY - 1; k++)
452 key[k] = rand() % ('z' - 'a') + 'a';
454 /* printf("[%d %s]\n",j,key); */
455 data = xbt_dict_get_or_null(head, key);
456 } while (data != NULL);
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,
466 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
468 xbt_dict_free(&head);
469 xbt_dict_free(&head);
473 head = xbt_dict_new();
474 xbt_test_add1("Fill %d elements, with keys being the number of element",
476 for (j = 0; j < NB_ELM; j++) {
477 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
479 key = xbt_malloc(10);
481 sprintf(key, "%d", j);
482 xbt_dict_set(head, key, key, &free);
484 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
487 ("Count the elements (retrieving the key and data for each)");
488 i = countelems(head);
489 xbt_test_log1("There is %d elements", i);
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++) {
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,
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); */
514 sprintf(key, "%d", j);
515 xbt_dict_remove(head, key);
520 xbt_test_add0("Free the structure (twice)");
521 xbt_dict_free(&head);
522 xbt_dict_free(&head);
525 static void str_free(void *s)
527 char *c = *(char **) s;
531 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
535 #define NB_ELM 100 /*00 */
538 #define NB_TEST 20 /*20 */
541 xbt_dict_t mdict = NULL;
543 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
548 xbt_test_add0("Generic multicache CRASH test");
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,
554 for (l = 0; l < DEPTH; l++) {
555 key = xbt_malloc(KEY_SIZE);
556 xbt_dynar_push(keys, &key);
559 for (i = 0; i < NB_TEST; i++) {
560 mdict = xbt_dict_new();
561 VERB1("mdict=%p", mdict);
563 printf("Test %d\n", i);
564 /* else if (i%10) printf("."); else printf("%d",i/10); */
566 for (j = 0; j < NB_ELM; j++) {
570 for (l = 0; l < DEPTH; l++) {
571 key = *(char **) xbt_dynar_get_ptr(keys, l);
573 for (k = 0; k < KEY_SIZE - 1; k++)
574 key[k] = rand() % ('z' - 'a') + 'a';
579 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
582 printf("in multitree %p.\n", mdict);
584 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
586 data = xbt_multidict_get(mdict, keys);
588 xbt_test_assert2(data && !strcmp((char *) data, key),
589 "Retrieved value (%s) does not match the given one (%s)\n",
592 xbt_dict_free(&mdict);
595 xbt_dynar_free(&keys);
598 xbt_dict_dump(mdict,&xbt_dict_print); */
600 xbt_dict_free(&mdict);
601 xbt_dynar_free(&keys);
604 /*******************************/
605 /* GENERATED FILE, DO NOT EDIT */
606 /*******************************/