1 #include <lunaix/ds/btrie.h>
2 #include <testing/basic.h>
3 #include <lunaix/spike.h>
4 #include <lunaix/compiler.h>
12 struct btrie_node *root;
13 btrie_init(&tree, ilog2(WIDTH));
15 expect_long(btrie_map(&tree, 0, 100, (void*)1), 0);
16 expect_long(btrie_map(&tree, 0, 100, (void*)2), 1);
17 expect_long(btrie_map(&tree, 0, 100, (void*)3), 2);
18 expect_long(btrie_map(&tree, 0, 100, (void*)4), 3);
20 root = tree.btrie_root;
21 expect_notnull(root->children);
22 expect_int(root->children_cnt, 4);
24 for (int i = 0; i < 4; i++)
26 expect_notnull(root->children[i]);
27 expect_int(root->children[i]->index, i);
28 expect_ulong((ptr_t)root->children[i]->data, i + 1);
31 for (int i = 4; i < WIDTH; i++)
33 expect_null(root->children[i]);
43 struct btrie_node *root;
44 btrie_init(&tree, ilog2(WIDTH));
46 expect_long(btrie_map(&tree, 7, 100, (void*)1), 7);
47 expect_long(btrie_map(&tree, 7, 100, (void*)2), 8);
48 expect_long(btrie_map(&tree, 7, 100, (void*)3), 9);
49 expect_long(btrie_map(&tree, 7, 100, (void*)4), 10);
51 root = tree.btrie_root;
52 expect_notnull(root->children);
53 expect_int(root->children_cnt, 2);
55 expect_notnull(root->children[7]);
56 expect_int(root->children[7]->index, 7);
57 expect_ulong((ptr_t)root->children[7]->data, 1);
59 expect_notnull(root->children[1]);
60 root = root->children[1];
62 for (int i = 0; i < 3; i++)
64 expect_notnull(root->children[i]);
65 expect_int(root->children[i]->index, i);
66 expect_ulong((ptr_t)root->children[i]->data, i + 2);
76 struct btrie_node *root;
77 btrie_init(&tree, ilog2(WIDTH));
79 expect_long(btrie_map(&tree, 4, 7, (void*)1), 4);
80 expect_long(btrie_map(&tree, 4, 7, (void*)2), 5);
81 expect_long(btrie_map(&tree, 4, 7, (void*)3), 6);
82 expect_long(btrie_map(&tree, 4, 7, (void*)4), -1);
84 root = tree.btrie_root;
85 expect_notnull(root->children);
86 expect_int(root->children_cnt, 3);
88 for (int i = 0; i < 4; ++i)
90 expect_null(root->children[i]);
93 for (int i = 4, j = 1; i < WIDTH - 1; ++i, ++j)
95 expect_notnull(root->children[i]);
96 expect_int(root->children[i]->index, i);
97 expect_ulong((ptr_t)root->children[i]->data, j);
100 btrie_release(&tree);
103 static void no_inline
104 __alloc_narrow_partial()
107 struct btrie_node *root;
108 btrie_init(&tree, ilog2(WIDTH));
110 expect_long(btrie_map(&tree, 15, 17, (void*)1), 15);
111 expect_long(btrie_map(&tree, 15, 17, (void*)2), 16);
112 expect_long(btrie_map(&tree, 15, 17, (void*)3), -1);
114 root = tree.btrie_root;
115 expect_notnull(root->children);
116 expect_int(root->children_cnt, 2);
118 // check left subtree
120 root = root->children[1];
121 expect_notnull(root);
122 expect_int(root->children_cnt, 1);
125 for (; i < WIDTH - 1; ++i)
127 expect_null(root->children[i]);
131 expect_notnull(root->children[i]);
132 expect_int(root->children[i]->index, i);
133 expect_ulong((ptr_t)root->children[i]->data, 1);
135 // check right subtree
137 root = tree.btrie_root;
138 root = root->children[2];
139 expect_notnull(root);
140 expect_int(root->children_cnt, 1);
143 expect_notnull(root->children[i]);
144 expect_int(root->children[i]->index, i);
145 expect_ulong((ptr_t)root->children[i]->data, 2);
147 for (i++; i < WIDTH; ++i)
149 expect_null(root->children[i]);
152 btrie_release(&tree);
155 static void no_inline
160 btrie_init(&tree, ilog2(WIDTH));
162 for (size_t i = 0; i < 1000; i++)
164 if (btrie_map(&tree, 0, 1001, (void*)i+1) == -1UL)
170 expect_int(mis_alloc, 0);
171 btrie_release(&tree);
174 static void no_inline
178 struct btrie_node *root;
179 btrie_init(&tree, ilog2(WIDTH));
181 expect_long(btrie_map(&tree, 4, 7, (void*)1), 4);
182 expect_long(btrie_map(&tree, 4, 7, (void*)2), 5);
183 expect_long(btrie_map(&tree, 4, 7, (void*)3), 6);
185 expect_ulong(__ptr(btrie_get(&tree, 6)), 3);
186 expect_ulong(__ptr(btrie_get(&tree, 5)), 2);
187 expect_ulong(__ptr(btrie_get(&tree, 4)), 1);
189 btrie_release(&tree);
193 run_test(int argc, const char* argv[])
195 testcase("simple_alloc", __alloc_simple());
196 testcase("simple_edge", __alloc_edge());
197 testcase("simple_narrow", __alloc_narrow());
198 testcase("narrow_partial", __alloc_narrow_partial());
199 testcase("alloc_dense", __alloc_dense());
200 testcase("alloc_get", __alloc_retrive());