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));
21 return f1->id - f2->id;
24 int main(int argc, char **argv)
26 SilcBool success = FALSE;
31 if (argc > 1 && !strcmp(argv[1], "-d")) {
34 silc_log_debug_hexdump(TRUE);
35 silc_log_set_debug_string("*tree*");
38 for (i = 0; i < NUM; i++) {
43 for (i = 0; i < NUM; i++) {
44 foo2[i].id = (i + 1) % (NUM / 4);
45 foo2[i].od = (i + 10) % (NUM / 4);
49 SILC_LOG_DEBUG(("Create AVL tree"));
50 if (!silc_tree_init(tree, SILC_TREE_AVL, compare, NULL,
51 silc_offsetof(Foo, header), TRUE))
55 SILC_LOG_DEBUG(("Populate tree, %d entries", NUM));
56 for (i = 0; i < NUM; i++)
57 if (!silc_tree_add(tree, &foo[i]))
61 SILC_LOG_DEBUG(("Add duplicates"));
62 for (i = 0; i < NUM; i++)
63 if (!silc_tree_add(tree, &foo2[i]))
66 SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
67 if (silc_tree_count(tree) != NUM + NUM)
71 for (i = 0; i < NUM; i++) {
72 tmp.id = (silc_rand() % NUM) + 1;
73 SILC_LOG_DEBUG(("Find entry %d", tmp.id));
74 if ((entry = silc_tree_find(tree, &tmp)) == NULL)
76 SILC_LOG_DEBUG(("Found entry %p %d", entry, entry->id));
79 /* Find non-existing */
80 for (i = 0; i < 5; i++) {
81 tmp.id = (silc_rand() % NUM) + (i % 2 ? -NUM - 1 : NUM + 1);
82 SILC_LOG_DEBUG(("Find entry %d", tmp.id));
83 if (silc_tree_find(tree, &tmp))
87 /* Enumerate in order */
88 for (entry = silc_tree_enumerate(tree, NULL);
90 entry = silc_tree_enumerate(tree, entry)) {
91 SILC_LOG_DEBUG(("Enum entry %d, %p", entry->id, entry));
95 for (i = 0; i < NUM; i++) {
96 memset(&tmp, 0, sizeof(tmp));
98 SILC_LOG_DEBUG(("Delete entry %d", tmp.id));
99 if (!silc_tree_del(tree, &tmp))
103 /* Delete remaining duplicates in loop */
104 while ((entry = silc_tree_enumerate(tree, NULL))) {
105 if (!silc_tree_del(tree, entry))
109 SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
110 if (silc_tree_count(tree) != 0)
116 SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
117 fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
119 silc_runtime_uninit();