1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
11 #line 810 "xbt/dict.c"
16 XBT_LOG_EXTERNAL_CATEGORY(xbt_dict);
17 XBT_LOG_DEFAULT_CATEGORY(xbt_dict);
19 static void print_str(void *str)
21 printf("%s", (char *) PRINTF_STR(str));
24 static void debuged_add_ext(xbt_dict_t head, const char *key,
25 const char *data_to_fill)
27 char *data = xbt_strdup(data_to_fill);
29 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
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);
38 static void debuged_add(xbt_dict_t head, const char *key)
40 debuged_add_ext(head, key, key);
43 static void fill(xbt_dict_t * head)
45 xbt_test_add0("Fill in the dictionnary");
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");
60 static void search_ext(xbt_dict_t head, const char *key, const char *data)
64 xbt_test_add1("Search %s", key);
65 found = xbt_dict_get(head, key);
66 xbt_test_log1("Found %s", (char *) found);
68 xbt_test_assert1(found,
69 "data do not match expectations: found NULL while searching for %s",
72 xbt_test_assert2(!strcmp((char *) data, found),
73 "data do not match expectations: found %s while searching for %s",
74 (char *) found, data);
77 static void search(xbt_dict_t head, const char *key)
79 search_ext(head, key, key);
82 static void debuged_remove(xbt_dict_t head, const char *key)
85 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
86 xbt_dict_remove(head, key);
87 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
91 static void traverse(xbt_dict_t head)
93 xbt_dict_cursor_t cursor = NULL;
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),
103 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
105 xbt_test_assert2(!data || !strcmp(key, data),
106 "Key(%s) != value(%s). Aborting", key, data);
110 static void search_not_found(xbt_dict_t head, const char *data)
115 xbt_test_add1("Search %s (expected not to be found)", data);
118 data = xbt_dict_get(head, data);
119 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
122 if (e.category != not_found_error)
123 xbt_test_exception(e);
127 xbt_test_assert0(ok, "Exception not raised");
130 static void count(xbt_dict_t dict, int length)
132 xbt_dict_cursor_t cursor;
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),
143 xbt_dict_foreach(dict, cursor, key, data)
146 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
151 static void count_check_get_key(xbt_dict_t dict, int length)
153 xbt_dict_cursor_t cursor;
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),
164 xbt_dict_foreach(dict, cursor, key, data) {
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);
171 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
177 xbt_dict_t head = NULL;
181 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
183 xbt_test_add0("Traversal the null dictionary");
186 xbt_test_add0("Traversal and search the empty dictionary");
187 head = xbt_dict_new();
190 debuged_remove(head, "12346");
192 if (e.category != not_found_error)
193 xbt_test_exception(e);
196 xbt_dict_free(&head);
198 xbt_test_add0("Traverse the full dictionary");
200 count_check_get_key(head, 7);
202 debuged_add_ext(head, "toto", "tutu");
203 search_ext(head, "toto", "tutu");
204 debuged_remove(head, "toto");
209 xbt_test_add0("Free the dictionary (twice)");
210 xbt_dict_free(&head);
211 xbt_dict_free(&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);
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);
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);
228 xbt_test_add0("Change 12a to '12a'");
229 xbt_dict_set(head, "12a", strdup("12a"), &free);
230 count_check_get_key(head, 7);
232 xbt_test_add0("Traverse the resulting dictionary");
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));
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");
248 search(head, "123456");
249 search(head, "1234");
250 search(head, "123457");
252 xbt_test_add0("Traverse the resulting dictionary");
255 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
257 xbt_test_add0("Free the dictionary twice");
258 xbt_dict_free(&head);
259 xbt_dict_free(&head);
261 xbt_test_add0("Traverse the resulting dictionary");
265 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
269 xbt_test_add0("Remove non existing data");
271 debuged_remove(head, "Does not exist");
274 if (e.category != not_found_error)
275 xbt_test_exception(e);
280 xbt_dict_free(&head);
283 ("Remove each data manually (traversing the resulting dictionary each time)");
285 debuged_remove(head, "12a");
288 debuged_remove(head, "12b");
291 debuged_remove(head, "12");
294 debuged_remove(head, "123456");
298 debuged_remove(head, "12346");
301 if (e.category != not_found_error)
302 xbt_test_exception(e);
306 debuged_remove(head, "1234");
308 debuged_remove(head, "123457");
310 debuged_remove(head, "123");
313 debuged_remove(head, "12346");
316 if (e.category != not_found_error)
317 xbt_test_exception(e);
322 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
323 xbt_dict_free(&head);
325 xbt_dict_reset(head);
329 xbt_test_add0("Free the dictionary twice");
330 xbt_dict_free(&head);
331 xbt_dict_free(&head);
334 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
338 xbt_test_add0("Store NULL under 'null'");
339 xbt_dict_set(head, "null", NULL, NULL);
340 search_ext(head, "null", NULL);
342 xbt_test_add0("Check whether I see it while traversing...");
344 xbt_dict_cursor_t cursor = NULL;
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));
352 xbt_test_log1("Seen: %s", PRINTF_STR(key));
355 if (!strcmp(key, "null"))
358 xbt_test_assert0(found,
359 "the key 'null', associated to NULL is not found");
361 xbt_dict_free(&head);
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);
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);
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);
378 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
380 xbt_test_add0("Fill in the dictionnary");
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);
388 debuged_addi(head, 12, 15);
389 debuged_addi(head, 15, 2000);
390 debuged_addi(head, 15, 3000);
392 debuged_addi(head, 0, 1000);
393 debuged_addi(head, 0, 2000);
394 debuged_addi(head, 0, 3000);
396 debuged_addi(head, 12, 0);
397 debuged_addi(head, 13, 0);
398 debuged_addi(head, 12, 0);
399 debuged_addi(head, 0, 0);
401 xbt_dict_free(&head);
405 #define SIZEOFKEY 1024
406 static int countelems(xbt_dict_t head)
408 xbt_dict_cursor_t cursor;
413 xbt_dict_foreach(head, cursor, key, data) {
419 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
421 xbt_dict_t head = NULL;
426 srand((unsigned int) time(NULL));
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.",
433 head = xbt_dict_new();
434 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
436 for (j = 0; j < 1000; j++) {
438 key = xbt_malloc(SIZEOFKEY);
441 for (k = 0; k < SIZEOFKEY - 1; k++)
442 key[k] = rand() % ('z' - 'a') + 'a';
444 /* printf("[%d %s]\n",j,key); */
445 data = xbt_dict_get_or_null(head, key);
446 } while (data != NULL);
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,
456 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
458 xbt_dict_free(&head);
459 xbt_dict_free(&head);
463 head = xbt_dict_new();
464 xbt_test_add1("Fill %d elements, with keys being the number of element",
466 for (j = 0; j < NB_ELM; j++) {
467 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
469 key = xbt_malloc(10);
471 sprintf(key, "%d", j);
472 xbt_dict_set(head, key, key, &free);
474 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
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);
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++) {
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);
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); */
502 sprintf(key, "%d", j);
503 xbt_dict_remove(head, key);
508 xbt_test_add0("Free the structure (twice)");
509 xbt_dict_free(&head);
510 xbt_dict_free(&head);
513 static void str_free(void *s)
515 char *c = *(char **) s;
519 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
523 #define NB_ELM 100 /*00 */
526 #define NB_TEST 20 /*20 */
529 xbt_dict_t mdict = NULL;
531 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
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);
541 for (l = 0; l < DEPTH; l++) {
542 key = xbt_malloc(KEY_SIZE);
543 xbt_dynar_push(keys, &key);
546 for (i = 0; i < NB_TEST; i++) {
547 mdict = xbt_dict_new();
548 VERB1("mdict=%p", mdict);
550 printf("Test %d\n", i);
551 /* else if (i%10) printf("."); else printf("%d",i/10); */
553 for (j = 0; j < NB_ELM; j++) {
557 for (l = 0; l < DEPTH; l++) {
558 key = *(char **) xbt_dynar_get_ptr(keys, l);
560 for (k = 0; k < KEY_SIZE - 1; k++)
561 key[k] = rand() % ('z' - 'a') + 'a';
566 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
569 printf("in multitree %p.\n", mdict);
571 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
573 data = xbt_multidict_get(mdict, keys);
575 xbt_test_assert2(data && !strcmp((char *) data, key),
576 "Retrieved value (%s) does not match the given one (%s)\n",
579 xbt_dict_free(&mdict);
582 xbt_dynar_free(&keys);
585 xbt_dict_dump(mdict,&xbt_dict_print); */
587 xbt_dict_free(&mdict);
588 xbt_dynar_free(&keys);
591 /*******************************/
592 /* GENERATED FILE, DO NOT EDIT */
593 /*******************************/