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_NEW_DEFAULT_SUBCATEGORY(xbt_dict, xbt,
18 "Dictionaries provide the same functionalities than hash tables");
21 static void print_str(void *str)
23 printf("%s", (char *) PRINTF_STR(str));
26 static void debuged_add_ext(xbt_dict_t head, const char *key,
27 const char *data_to_fill)
29 char *data = xbt_strdup(data_to_fill);
31 xbt_test_log2("Add %s under %s", PRINTF_STR(data_to_fill), PRINTF_STR(key));
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, "Found something which shouldn't be there (%s)",
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;
161 xbt_test_add1("Count elements (expecting %d), and test the getkey function", length);
162 xbt_test_assert2(xbt_dict_length(dict) == length,
163 "Announced length(%d) != %d.", xbt_dict_length(dict),
166 xbt_dict_foreach(dict, cursor, key, data) {
168 key2 = xbt_dict_get_key(dict,data);
169 xbt_assert2(!strcmp(key,key2),
170 "The data was registered under %s instead of %s as expected",key2,key);
173 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
179 xbt_dict_t head = NULL;
183 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
185 xbt_test_add0("Traversal the null dictionary");
188 xbt_test_add0("Traversal and search the empty dictionary");
189 head = xbt_dict_new();
192 debuged_remove(head, "12346");
194 if (e.category != not_found_error)
195 xbt_test_exception(e);
198 xbt_dict_free(&head);
200 xbt_test_add0("Traverse the full dictionary");
202 count_check_get_key(head, 7);
204 debuged_add_ext(head, "toto", "tutu");
205 search_ext(head, "toto", "tutu");
206 debuged_remove(head, "toto");
211 xbt_test_add0("Free the dictionary (twice)");
212 xbt_dict_free(&head);
213 xbt_dict_free(&head);
217 count_check_get_key(head, 7);
218 xbt_test_add0("Change 123 to 'Changed 123'");
219 xbt_dict_set(head, "123", strdup("Changed 123"), &free);
220 count_check_get_key(head, 7);
222 xbt_test_add0("Change 123 back to '123'");
223 xbt_dict_set(head, "123", strdup("123"), &free);
224 count_check_get_key(head, 7);
226 xbt_test_add0("Change 12a to 'Dummy 12a'");
227 xbt_dict_set(head, "12a", strdup("Dummy 12a"), &free);
228 count_check_get_key(head, 7);
230 xbt_test_add0("Change 12a to '12a'");
231 xbt_dict_set(head, "12a", strdup("12a"), &free);
232 count_check_get_key(head, 7);
234 xbt_test_add0("Traverse the resulting dictionary");
238 xbt_test_add0("Search 123");
239 data = xbt_dict_get(head, "123");
240 xbt_test_assert(data);
241 xbt_test_assert(!strcmp("123", data));
243 search_not_found(head, "Can't be found");
244 search_not_found(head, "123 Can't be found");
245 search_not_found(head, "12345678 NOT");
250 search(head, "123456");
251 search(head, "1234");
252 search(head, "123457");
254 xbt_test_add0("Traverse the resulting dictionary");
257 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
259 xbt_test_add0("Free the dictionary twice");
260 xbt_dict_free(&head);
261 xbt_dict_free(&head);
263 xbt_test_add0("Traverse the resulting dictionary");
267 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
271 xbt_test_add0("Remove non existing data");
273 debuged_remove(head, "Does not exist");
276 if (e.category != not_found_error)
277 xbt_test_exception(e);
282 xbt_dict_free(&head);
285 ("Remove each data manually (traversing the resulting dictionary each time)");
287 debuged_remove(head, "12a");
290 debuged_remove(head, "12b");
293 debuged_remove(head, "12");
296 debuged_remove(head, "123456");
300 debuged_remove(head, "12346");
303 if (e.category != not_found_error)
304 xbt_test_exception(e);
308 debuged_remove(head, "1234");
310 debuged_remove(head, "123457");
312 debuged_remove(head, "123");
315 debuged_remove(head, "12346");
318 if (e.category != not_found_error)
319 xbt_test_exception(e);
324 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
325 xbt_dict_free(&head);
327 xbt_dict_reset(head);
331 xbt_test_add0("Free the dictionary twice");
332 xbt_dict_free(&head);
333 xbt_dict_free(&head);
336 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
340 xbt_test_add0("Store NULL under 'null'");
341 xbt_dict_set(head, "null", NULL, NULL);
342 search_ext(head, "null", NULL);
344 xbt_test_add0("Check whether I see it while traversing...");
346 xbt_dict_cursor_t cursor = NULL;
350 xbt_dict_foreach(head, cursor, key, data) {
351 if (!key || !data || strcmp(key, data)) {
352 xbt_test_log2("Seen: %s->%s", PRINTF_STR(key), PRINTF_STR(data));
354 xbt_test_log1("Seen: %s", PRINTF_STR(key));
357 if (!strcmp(key, "null"))
360 xbt_test_assert0(found,
361 "the key 'null', associated to NULL is not found");
363 xbt_dict_free(&head);
366 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
367 uintptr_t stored_data = 0;
368 xbt_test_log2("Add %zu under %zu", data, key);
370 xbt_dicti_set(head, key, data);
371 if (XBT_LOG_ISENABLED(xbt_dict, xbt_log_priority_debug)) {
372 xbt_dict_dump(head, (void (*)(void *)) &printf);
375 stored_data = xbt_dicti_get(head, key);
376 xbt_test_assert3(stored_data==data,
377 "Retrieved data (%zu) is not what I just stored (%zu) under key %zu",stored_data,data,key);
380 XBT_TEST_UNIT("dicti", test_dict_scalar, "Scalar data and key management")
382 xbt_test_add0("Fill in the dictionnary");
384 head = xbt_dict_new();
385 debuged_addi(head, 12, 12);
386 debuged_addi(head, 13, 13);
387 debuged_addi(head, 14, 14);
388 debuged_addi(head, 15, 15);
390 debuged_addi(head, 12, 15);
391 debuged_addi(head, 15, 2000);
392 debuged_addi(head, 15, 3000);
394 debuged_addi(head, 0, 1000);
395 debuged_addi(head, 0, 2000);
396 debuged_addi(head, 0, 3000);
398 debuged_addi(head, 12, 0);
399 debuged_addi(head, 13, 0);
400 debuged_addi(head, 12, 0);
401 debuged_addi(head, 0, 0);
403 xbt_dict_free(&head);
407 #define SIZEOFKEY 1024
408 static int countelems(xbt_dict_t head)
410 xbt_dict_cursor_t cursor;
415 xbt_dict_foreach(head, cursor, key, data) {
421 XBT_TEST_UNIT("crash", test_dict_crash, "Crash test")
423 xbt_dict_t head = NULL;
428 srand((unsigned int) time(NULL));
430 for (i = 0; i < 10; i++) {
431 xbt_test_add2("CRASH test number %d (%d to go)", i + 1, 10 - i - 1);
432 xbt_test_log0("Fill the struct, count its elems and frees the structure");
433 xbt_test_log1("using 1000 elements with %d chars long randomized keys.",
435 head = xbt_dict_new();
436 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
438 for (j = 0; j < 1000; j++) {
440 key = xbt_malloc(SIZEOFKEY);
443 for (k = 0; k < SIZEOFKEY - 1; k++)
444 key[k] = rand() % ('z' - 'a') + 'a';
446 /* printf("[%d %s]\n",j,key); */
447 data = xbt_dict_get_or_null(head, key);
448 } while (data != NULL);
450 xbt_dict_set(head, key, key, &free);
451 data = xbt_dict_get(head, key);
452 xbt_test_assert2(!strcmp(key, data),
453 "Retrieved value (%s) != Injected value (%s)", key,
458 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
460 xbt_dict_free(&head);
461 xbt_dict_free(&head);
465 head = xbt_dict_new();
466 xbt_test_add1("Fill %d elements, with keys being the number of element",
468 for (j = 0; j < NB_ELM; j++) {
469 /* if (!(j%1000)) { printf("."); fflush(stdout); } */
471 key = xbt_malloc(10);
473 sprintf(key, "%d", j);
474 xbt_dict_set(head, key, key, &free);
476 /*xbt_dict_dump(head,(void (*)(void*))&printf); */
478 xbt_test_add0("Count the elements (retrieving the key and data for each)");
479 i = countelems(head);
480 xbt_test_log1("There is %d elements", i);
482 xbt_test_add1("Search my %d elements 20 times", NB_ELM);
483 key = xbt_malloc(10);
484 for (i = 0; i < 20; i++) {
485 /* if (i%10) printf("."); else printf("%d",i/10); fflush(stdout); */
486 for (j = 0; j < NB_ELM; j++) {
488 sprintf(key, "%d", j);
489 data = xbt_dict_get(head, key);
490 xbt_test_assert2(!strcmp(key, (char *) data),
491 "with get, key=%s != data=%s", key, (char *) data);
492 data = xbt_dict_get_ext(head, key, strlen(key));
493 xbt_test_assert2(!strcmp(key, (char *) data),
494 "with get_ext, key=%s != data=%s", key, (char *) data);
499 xbt_test_add1("Remove my %d elements", NB_ELM);
500 key = xbt_malloc(10);
501 for (j = 0; j < NB_ELM; j++) {
502 /* if (!(j%10000)) printf("."); fflush(stdout); */
504 sprintf(key, "%d", j);
505 xbt_dict_remove(head, key);
510 xbt_test_add0("Free the structure (twice)");
511 xbt_dict_free(&head);
512 xbt_dict_free(&head);
515 static void str_free(void *s)
517 char *c = *(char **) s;
521 XBT_TEST_UNIT("multicrash", test_dict_multicrash, "Multi-dict crash test")
525 #define NB_ELM 100 /*00 */
528 #define NB_TEST 20 /*20 */
531 xbt_dict_t mdict = NULL;
533 xbt_dynar_t keys = xbt_dynar_new(sizeof(char *), str_free);
538 xbt_test_add0("Generic multicache CRASH test");
539 xbt_test_log4(" Fill the struct and frees it %d times, using %d elements, "
540 "depth of multicache=%d, key size=%d",
541 NB_TEST, NB_ELM, DEPTH, KEY_SIZE);
543 for (l = 0; l < DEPTH; l++) {
544 key = xbt_malloc(KEY_SIZE);
545 xbt_dynar_push(keys, &key);
548 for (i = 0; i < NB_TEST; i++) {
549 mdict = xbt_dict_new();
550 VERB1("mdict=%p", mdict);
552 printf("Test %d\n", i);
553 /* else if (i%10) printf("."); else printf("%d",i/10); */
555 for (j = 0; j < NB_ELM; j++) {
559 for (l = 0; l < DEPTH; l++) {
560 key = *(char **) xbt_dynar_get_ptr(keys, l);
562 for (k = 0; k < KEY_SIZE - 1; k++)
563 key[k] = rand() % ('z' - 'a') + 'a';
568 printf("%p=%s %s ", key, key, (l < DEPTH - 1 ? ";" : "}"));
571 printf("in multitree %p.\n", mdict);
573 xbt_multidict_set(mdict, keys, xbt_strdup(key), free);
575 data = xbt_multidict_get(mdict, keys);
577 xbt_test_assert2(data && !strcmp((char *) data, key),
578 "Retrieved value (%s) does not match the given one (%s)\n",
581 xbt_dict_free(&mdict);
584 xbt_dynar_free(&keys);
587 xbt_dict_dump(mdict,&xbt_dict_print); */
589 xbt_dict_free(&mdict);
590 xbt_dynar_free(&keys);
593 /*******************************/
594 /* GENERATED FILE, DO NOT EDIT */
595 /*******************************/