Added protected memory allocation to stacktrace
[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   if (f1->id > f2->id)
22     return SILC_COMPARE_GREATER_THAN;
23   if (f1->id < f2->id)
24     return SILC_COMPARE_LESS_THAN;
25   return SILC_COMPARE_EQUAL_TO;
26 }
27
28 int main(int argc, char **argv)
29 {
30   SilcBool success = FALSE;
31   int i;
32
33   silc_runtime_init();
34
35   if (argc > 1 && !strcmp(argv[1], "-d")) {
36     silc_log_debug(TRUE);
37     silc_log_quick(TRUE);
38     silc_log_debug_hexdump(TRUE);
39     silc_log_set_debug_string("*tree*");
40   }
41
42   for (i = 0; i < NUM; i++) {
43     foo[i].id = i + 1;
44     foo[i].od = i + 10;
45   }
46
47   for (i = 0; i < NUM; i++) {
48     foo2[i].id = (i + 1) % (NUM / 4);
49     foo2[i].od = (i + 10) % (NUM / 4);
50   }
51
52   /* AVL */
53   SILC_LOG_DEBUG(("Create AVL tree"));
54   if (!silc_tree_init(tree, SILC_TREE_AVL, compare, NULL,
55                       silc_offsetof(Foo, header), TRUE))
56     goto err;
57
58   /* Populate tree */
59   SILC_LOG_DEBUG(("Populate tree, %d entries", NUM));
60   for (i = 0; i < NUM; i++)
61     if (!silc_tree_add(tree, &foo[i]))
62       goto err;
63
64   /* Add duplicates */
65   SILC_LOG_DEBUG(("Add duplicates"));
66   for (i = 0; i < NUM; i++)
67     if (!silc_tree_add(tree, &foo2[i]))
68       goto err;
69
70   SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
71   if (silc_tree_count(tree) != NUM + NUM)
72     goto err;
73
74   /* Find random */
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)
79       goto err;
80     SILC_LOG_DEBUG(("Found entry %p %d", entry, entry->id));
81   }
82
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))
88       goto err;
89   }
90
91   /* Enumerate in order */
92   for (entry = silc_tree_enumerate(tree, NULL);
93        entry;
94        entry = silc_tree_enumerate(tree, entry)) {
95     SILC_LOG_DEBUG(("Enum entry %d, %p", entry->id, entry));
96   }
97
98   /* Delete all */
99   for (i = 0; i < NUM; i++) {
100     memset(&tmp, 0, sizeof(tmp));
101     tmp.id = i + 1;
102     SILC_LOG_DEBUG(("Delete entry %d", tmp.id));
103     if (!silc_tree_del(tree, &tmp))
104       goto err;
105   }
106
107   /* Delete remaining duplicates in loop */
108   while ((entry = silc_tree_enumerate(tree, NULL))) {
109     if (!silc_tree_del(tree, entry))
110       goto err;
111   }
112
113   SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
114   if (silc_tree_count(tree) != 0)
115     goto err;
116
117   success = TRUE;
118
119  err:
120   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
121   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
122
123   silc_runtime_uninit();
124
125   return !success;
126 }