9 #define OUT(fmt, ...) do { \
10 fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
17 static void test_basics()
25 const char *vals
[] = {
26 "zero", "one", "two", "three", "four", "five", "six",
27 "seven", "eight", "nine", "ten", "eleven", "twelve"
30 avl_init(&t
, avl_strcmp
, false, NULL
);
33 for (i
=0; i
<ARRAY_SIZE(vals
); i
++) {
34 struct node
*n
= malloc(sizeof(struct node
));
37 int r
= avl_insert(&t
, &n
->avl
);
38 fprintf(stdout
, "%d=%s ", r
, (char *)n
->avl
.key
);
40 fprintf(stdout
, "\n");
42 OUT("insert duplicate: ");
43 for (i
=0; i
<ARRAY_SIZE(vals
); i
++) {
44 struct node
*n
= malloc(sizeof(struct node
));
47 int r
= avl_insert(&t
, &n
->avl
);
48 fprintf(stdout
, "%d=%s ", r
, (char *)n
->avl
.key
);
53 fprintf(stdout
, "\n");
55 first
= avl_first_element(&t
, first
, avl
);
56 last
= avl_last_element(&t
, last
, avl
);
57 OUT("first=%s last=%s\n", (char*)first
->avl
.key
, (char*)last
->avl
.key
);
59 OUT("for each element: ");
60 avl_for_each_element(&t
, elem
, avl
) {
61 fprintf(stdout
, "%s ", (char*)elem
->avl
.key
);
63 fprintf(stdout
, "\n");
65 OUT("delete 'one' element\n");
66 elem
= avl_find_element(&t
, "one", elem
, avl
);
67 avl_delete(&t
, &elem
->avl
);
70 OUT("for each element reverse: ");
71 avl_for_each_element_reverse(&t
, elem
, avl
) {
72 fprintf(stdout
, "%s ", (char*)elem
->avl
.key
);
74 fprintf(stdout
, "\n");
76 OUT("delete all elements\n");
77 avl_for_each_element_safe(&t
, elem
, avl
, temp
) {
78 avl_delete(&t
, &elem
->avl
);