3 #include "silcruntime.h"
11 SilcTreeHeader header;
15 Foo foo[NUM], foo2[NUM], tmp, *entry;
17 static int compare(void *e1, void *e2, void *context)
19 Foo *f1 = e1, *f2 = e2;
20 SILC_LOG_DEBUG(("%p %d > %p %d", f1, f1->id, f2, f2->id));
22 return SILC_COMPARE_GREATER_THAN;
24 return SILC_COMPARE_LESS_THAN;
25 return SILC_COMPARE_EQUAL_TO;
28 int main(int argc, char **argv)
30 SilcBool success = FALSE;
35 if (argc > 1 && !strcmp(argv[1], "-d")) {
38 silc_log_debug_hexdump(TRUE);
39 silc_log_set_debug_string("*tree*");
42 for (i = 0; i < NUM; i++) {
47 for (i = 0; i < NUM; i++) {
48 foo2[i].id = (i + 1) % (NUM / 4);
49 foo2[i].od = (i + 10) % (NUM / 4);
53 SILC_LOG_DEBUG(("Create AVL tree"));
54 if (!silc_tree_init(tree, SILC_TREE_AVL, compare, NULL,
55 silc_offsetof(Foo, header), TRUE))
59 SILC_LOG_DEBUG(("Populate tree, %d entries", NUM));
60 for (i = 0; i < NUM; i++)
61 if (!silc_tree_add(tree, &foo[i]))
65 SILC_LOG_DEBUG(("Add duplicates"));
66 for (i = 0; i < NUM; i++)
67 if (!silc_tree_add(tree, &foo2[i]))
70 SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
71 if (silc_tree_count(tree) != NUM + NUM)
75 for (i = 0; i < NUM; i++) {
76 tmp.id = (silc_rand() % NUM) + 1;
77 SILC_LOG_DEBUG(("Find entry %d", tmp.id));
78 if ((entry = silc_tree_find(tree, &tmp)) == NULL)
80 SILC_LOG_DEBUG(("Found entry %p %d", entry, entry->id));
83 /* Find non-existing */
84 for (i = 0; i < 5; i++) {
85 tmp.id = (silc_rand() % NUM) + (i % 2 ? -NUM - 1 : NUM + 1);
86 SILC_LOG_DEBUG(("Find entry %d", tmp.id));
87 if (silc_tree_find(tree, &tmp))
91 /* Enumerate in order */
92 for (entry = silc_tree_enumerate(tree, NULL);
94 entry = silc_tree_enumerate(tree, entry)) {
95 SILC_LOG_DEBUG(("Enum entry %d, %p", entry->id, entry));
99 for (i = 0; i < NUM; i++) {
100 memset(&tmp, 0, sizeof(tmp));
102 SILC_LOG_DEBUG(("Delete entry %d", tmp.id));
103 if (!silc_tree_del(tree, &tmp))
107 /* Delete remaining duplicates in loop */
108 while ((entry = silc_tree_enumerate(tree, NULL))) {
109 if (!silc_tree_del(tree, entry))
113 SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
114 if (silc_tree_count(tree) != 0)
120 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
121 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
123 silc_runtime_uninit();