Added SILC Tree API, a generic binary search tree interface
[runtime.git] / lib / silcutil / tests / test_silctree.c
1 /* SilcTree tests */
2
3 #include "silcruntime.h"
4
5 SilcTree tree;
6
7 typedef struct {
8   int id;
9   int od;
10   char *foo;
11   SilcTreeHeader header;
12 } Foo;
13
14 #define NUM 199
15 Foo foo[NUM], foo2[NUM], tmp, *entry;
16
17 static int compare(void *e1, void *e2, void *context)
18 {
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;
22 }
23
24 int main(int argc, char **argv)
25 {
26   SilcBool success = FALSE;
27   int i;
28
29   silc_runtime_init();
30
31   if (argc > 1 && !strcmp(argv[1], "-d")) {
32     silc_log_debug(TRUE);
33     silc_log_quick(TRUE);
34     silc_log_debug_hexdump(TRUE);
35     silc_log_set_debug_string("*tree*");
36   }
37
38   for (i = 0; i < NUM; i++) {
39     foo[i].id = i + 1;
40     foo[i].od = i + 10;
41   }
42
43   for (i = 0; i < NUM; i++) {
44     foo2[i].id = (i + 1) % (NUM / 4);
45     foo2[i].od = (i + 10) % (NUM / 4);
46   }
47
48   /* AVL */
49   SILC_LOG_DEBUG(("Create AVL tree"));
50   if (!silc_tree_init(tree, SILC_TREE_AVL, compare, NULL,
51                       silc_offsetof(Foo, header), TRUE))
52     goto err;
53
54   /* Populate tree */
55   SILC_LOG_DEBUG(("Populate tree, %d entries", NUM));
56   for (i = 0; i < NUM; i++)
57     if (!silc_tree_add(tree, &foo[i]))
58       goto err;
59
60   /* Add duplicates */
61   SILC_LOG_DEBUG(("Add duplicates"));
62   for (i = 0; i < NUM; i++)
63     if (!silc_tree_add(tree, &foo2[i]))
64       goto err;
65
66   SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
67   if (silc_tree_count(tree) != NUM + NUM)
68     goto err;
69
70   /* Find random */
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)
75       goto err;
76     SILC_LOG_DEBUG(("Found entry %p %d", entry, entry->id));
77   }
78
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))
84       goto err;
85   }
86
87   /* Enumerate in order */
88   for (entry = silc_tree_enumerate(tree, NULL);
89        entry;
90        entry = silc_tree_enumerate(tree, entry)) {
91     SILC_LOG_DEBUG(("Enum entry %d, %p", entry->id, entry));
92   }
93
94   /* Delete all */
95   for (i = 0; i < NUM; i++) {
96     memset(&tmp, 0, sizeof(tmp));
97     tmp.id = i + 1;
98     SILC_LOG_DEBUG(("Delete entry %d", tmp.id));
99     if (!silc_tree_del(tree, &tmp))
100       goto err;
101   }
102
103   /* Delete remaining duplicates in loop */
104   while ((entry = silc_tree_enumerate(tree, NULL))) {
105     if (!silc_tree_del(tree, entry))
106       goto err;
107   }
108
109   SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
110   if (silc_tree_count(tree) != 0)
111     goto err;
112
113   success = TRUE;
114
115  err:
116   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
117   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
118
119   silc_runtime_uninit();
120
121   return !success;
122 }