1 /*******************************/
2 /* GENERATED FILE, DO NOT EDIT */
3 /*******************************/
7 /*******************************/
8 /* GENERATED FILE, DO NOT EDIT */
9 /*******************************/
11 # 799 "/home/navarrop/Developments/simgrid/src/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), PRINTF_STR(key));
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);
39 static void debuged_add(xbt_dict_t head, const char *key)
41 debuged_add_ext(head, key, key);
44 static void fill(xbt_dict_t * head)
46 xbt_test_add0("Fill in the dictionnary");
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");
61 static void search_ext(xbt_dict_t head, const char *key, const char *data)
65 xbt_test_add1("Search %s", key);
66 found = xbt_dict_get(head, key);
67 xbt_test_log1("Found %s", (char *) found);
69 xbt_test_assert1(found,
70 "data do not match expectations: found NULL while searching for %s",
73 xbt_test_assert2(!strcmp((char *) data, found),
74 "data do not match expectations: found %s while searching for %s",
75 (char *) found, data);
78 static void search(xbt_dict_t head, const char *key)
80 search_ext(head, key, key);
83 static void debuged_remove(xbt_dict_t head, const char *key)
86 xbt_test_add1("Remove '%s'", PRINTF_STR(key));
87 xbt_dict_remove(head, key);
88 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
92 static void traverse(xbt_dict_t head)
94 xbt_dict_cursor_t cursor = NULL;
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),
104 xbt_test_log2("Seen #%d: %s", ++i, PRINTF_STR(key));
106 xbt_test_assert2(!data || !strcmp(key, data),
107 "Key(%s) != value(%s). Aborting", key, data);
111 static void search_not_found(xbt_dict_t head, const char *data)
116 xbt_test_add1("Search %s (expected not to be found)", data);
119 data = xbt_dict_get(head, data);
120 THROW1(unknown_error, 0, "Found something which shouldn't be there (%s)",
123 if (e.category != not_found_error)
124 xbt_test_exception(e);
128 xbt_test_assert0(ok, "Exception not raised");
131 static void count(xbt_dict_t dict, int length)
133 xbt_dict_cursor_t cursor;
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),
144 xbt_dict_foreach(dict, cursor, key, data)
147 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
152 static void count_check_get_key(xbt_dict_t dict, int length)
154 xbt_dict_cursor_t cursor;
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),
165 xbt_dict_foreach(dict, cursor, key, data) {
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);
172 xbt_test_assert2(effective == length, "Effective length(%d) != %d.",
178 xbt_dict_t head = NULL;
182 XBT_TEST_UNIT("basic", test_dict_basic,"Basic usage: change, retrieve, traverse")
184 xbt_test_add0("Traversal the null dictionary");
187 xbt_test_add0("Traversal and search the empty dictionary");
188 head = xbt_dict_new();
191 debuged_remove(head, "12346");
193 if (e.category != not_found_error)
194 xbt_test_exception(e);
197 xbt_dict_free(&head);
199 xbt_test_add0("Traverse the full dictionary");
201 count_check_get_key(head, 7);
203 debuged_add_ext(head, "toto", "tutu");
204 search_ext(head, "toto", "tutu");
205 debuged_remove(head, "toto");
210 xbt_test_add0("Free the dictionary (twice)");
211 xbt_dict_free(&head);
212 xbt_dict_free(&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);
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);
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);
229 xbt_test_add0("Change 12a to '12a'");
230 xbt_dict_set(head, "12a", strdup("12a"), &free);
231 count_check_get_key(head, 7);
233 xbt_test_add0("Traverse the resulting dictionary");
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));
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");
249 search(head, "123456");
250 search(head, "1234");
251 search(head, "123457");
253 xbt_test_add0("Traverse the resulting dictionary");
256 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
258 xbt_test_add0("Free the dictionary twice");
259 xbt_dict_free(&head);
260 xbt_dict_free(&head);
262 xbt_test_add0("Traverse the resulting dictionary");
266 XBT_TEST_UNIT("remove", test_dict_remove, "Removing some values")
270 xbt_test_add0("Remove non existing data");
272 debuged_remove(head, "Does not exist");
275 if (e.category != not_found_error)
276 xbt_test_exception(e);
281 xbt_dict_free(&head);
284 ("Remove each data manually (traversing the resulting dictionary each time)");
286 debuged_remove(head, "12a");
289 debuged_remove(head, "12b");
292 debuged_remove(head, "12");
295 debuged_remove(head, "123456");
299 debuged_remove(head, "12346");
302 if (e.category != not_found_error)
303 xbt_test_exception(e);
307 debuged_remove(head, "1234");
309 debuged_remove(head, "123457");
311 debuged_remove(head, "123");
314 debuged_remove(head, "12346");
317 if (e.category != not_found_error)
318 xbt_test_exception(e);
323 xbt_test_add0("Free dict, create new fresh one, and then reset the dict");
324 xbt_dict_free(&head);
326 xbt_dict_reset(head);
330 xbt_test_add0("Free the dictionary twice");
331 xbt_dict_free(&head);
332 xbt_dict_free(&head);
335 XBT_TEST_UNIT("nulldata", test_dict_nulldata, "NULL data management")
339 xbt_test_add0("Store NULL under 'null'");
340 xbt_dict_set(head, "null", NULL, NULL);
341 search_ext(head, "null", NULL);
343 xbt_test_add0("Check whether I see it while traversing...");
345 xbt_dict_cursor_t cursor = NULL;
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));
353 xbt_test_log1("Seen: %s", PRINTF_STR(key));
356 if (!strcmp(key, "null"))
359 xbt_test_assert0(found,
360 "the key 'null', associated to NULL is not found");
362 xbt_dict_free(&head);
365 static void debuged_addi(xbt_dict_t head, uintptr_t key, uintptr_t data) {
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 uintptr_t 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 /*******************************/