xen/xenbus: Add 'will_handle' callback support in xenbus_watch_path()
[linux/fpc-iii.git] / lib / rbtree_test.c
blobafedd37705620b5ae5f8416d1f25b1eb12ed709e
1 #include <linux/module.h>
2 #include <linux/moduleparam.h>
3 #include <linux/rbtree_augmented.h>
4 #include <linux/random.h>
5 #include <linux/slab.h>
6 #include <asm/timex.h>
8 #define __param(type, name, init, msg) \
9 static type name = init; \
10 module_param(name, type, 0444); \
11 MODULE_PARM_DESC(name, msg);
13 __param(int, nnodes, 100, "Number of nodes in the rb-tree");
14 __param(int, perf_loops, 1000, "Number of iterations modifying the rb-tree");
15 __param(int, check_loops, 100, "Number of iterations modifying and verifying the rb-tree");
17 struct test_node {
18 u32 key;
19 struct rb_node rb;
21 /* following fields used for testing augmented rbtree functionality */
22 u32 val;
23 u32 augmented;
26 static struct rb_root root = RB_ROOT;
27 static struct test_node *nodes = NULL;
29 static struct rnd_state rnd;
31 static void insert(struct test_node *node, struct rb_root *root)
33 struct rb_node **new = &root->rb_node, *parent = NULL;
34 u32 key = node->key;
36 while (*new) {
37 parent = *new;
38 if (key < rb_entry(parent, struct test_node, rb)->key)
39 new = &parent->rb_left;
40 else
41 new = &parent->rb_right;
44 rb_link_node(&node->rb, parent, new);
45 rb_insert_color(&node->rb, root);
48 static inline void erase(struct test_node *node, struct rb_root *root)
50 rb_erase(&node->rb, root);
53 static inline u32 augment_recompute(struct test_node *node)
55 u32 max = node->val, child_augmented;
56 if (node->rb.rb_left) {
57 child_augmented = rb_entry(node->rb.rb_left, struct test_node,
58 rb)->augmented;
59 if (max < child_augmented)
60 max = child_augmented;
62 if (node->rb.rb_right) {
63 child_augmented = rb_entry(node->rb.rb_right, struct test_node,
64 rb)->augmented;
65 if (max < child_augmented)
66 max = child_augmented;
68 return max;
71 RB_DECLARE_CALLBACKS(static, augment_callbacks, struct test_node, rb,
72 u32, augmented, augment_recompute)
74 static void insert_augmented(struct test_node *node, struct rb_root *root)
76 struct rb_node **new = &root->rb_node, *rb_parent = NULL;
77 u32 key = node->key;
78 u32 val = node->val;
79 struct test_node *parent;
81 while (*new) {
82 rb_parent = *new;
83 parent = rb_entry(rb_parent, struct test_node, rb);
84 if (parent->augmented < val)
85 parent->augmented = val;
86 if (key < parent->key)
87 new = &parent->rb.rb_left;
88 else
89 new = &parent->rb.rb_right;
92 node->augmented = val;
93 rb_link_node(&node->rb, rb_parent, new);
94 rb_insert_augmented(&node->rb, root, &augment_callbacks);
97 static void erase_augmented(struct test_node *node, struct rb_root *root)
99 rb_erase_augmented(&node->rb, root, &augment_callbacks);
102 static void init(void)
104 int i;
105 for (i = 0; i < nnodes; i++) {
106 nodes[i].key = prandom_u32_state(&rnd);
107 nodes[i].val = prandom_u32_state(&rnd);
111 static bool is_red(struct rb_node *rb)
113 return !(rb->__rb_parent_color & 1);
116 static int black_path_count(struct rb_node *rb)
118 int count;
119 for (count = 0; rb; rb = rb_parent(rb))
120 count += !is_red(rb);
121 return count;
124 static void check_postorder_foreach(int nr_nodes)
126 struct test_node *cur, *n;
127 int count = 0;
128 rbtree_postorder_for_each_entry_safe(cur, n, &root, rb)
129 count++;
131 WARN_ON_ONCE(count != nr_nodes);
134 static void check_postorder(int nr_nodes)
136 struct rb_node *rb;
137 int count = 0;
138 for (rb = rb_first_postorder(&root); rb; rb = rb_next_postorder(rb))
139 count++;
141 WARN_ON_ONCE(count != nr_nodes);
144 static void check(int nr_nodes)
146 struct rb_node *rb;
147 int count = 0, blacks = 0;
148 u32 prev_key = 0;
150 for (rb = rb_first(&root); rb; rb = rb_next(rb)) {
151 struct test_node *node = rb_entry(rb, struct test_node, rb);
152 WARN_ON_ONCE(node->key < prev_key);
153 WARN_ON_ONCE(is_red(rb) &&
154 (!rb_parent(rb) || is_red(rb_parent(rb))));
155 if (!count)
156 blacks = black_path_count(rb);
157 else
158 WARN_ON_ONCE((!rb->rb_left || !rb->rb_right) &&
159 blacks != black_path_count(rb));
160 prev_key = node->key;
161 count++;
164 WARN_ON_ONCE(count != nr_nodes);
165 WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1);
167 check_postorder(nr_nodes);
168 check_postorder_foreach(nr_nodes);
171 static void check_augmented(int nr_nodes)
173 struct rb_node *rb;
175 check(nr_nodes);
176 for (rb = rb_first(&root); rb; rb = rb_next(rb)) {
177 struct test_node *node = rb_entry(rb, struct test_node, rb);
178 WARN_ON_ONCE(node->augmented != augment_recompute(node));
182 static int __init rbtree_test_init(void)
184 int i, j;
185 cycles_t time1, time2, time;
187 nodes = kmalloc(nnodes * sizeof(*nodes), GFP_KERNEL);
188 if (!nodes)
189 return -ENOMEM;
191 printk(KERN_ALERT "rbtree testing");
193 prandom_seed_state(&rnd, 3141592653589793238ULL);
194 init();
196 time1 = get_cycles();
198 for (i = 0; i < perf_loops; i++) {
199 for (j = 0; j < nnodes; j++)
200 insert(nodes + j, &root);
201 for (j = 0; j < nnodes; j++)
202 erase(nodes + j, &root);
205 time2 = get_cycles();
206 time = time2 - time1;
208 time = div_u64(time, perf_loops);
209 printk(" -> %llu cycles\n", (unsigned long long)time);
211 for (i = 0; i < check_loops; i++) {
212 init();
213 for (j = 0; j < nnodes; j++) {
214 check(j);
215 insert(nodes + j, &root);
217 for (j = 0; j < nnodes; j++) {
218 check(nnodes - j);
219 erase(nodes + j, &root);
221 check(0);
224 printk(KERN_ALERT "augmented rbtree testing");
226 init();
228 time1 = get_cycles();
230 for (i = 0; i < perf_loops; i++) {
231 for (j = 0; j < nnodes; j++)
232 insert_augmented(nodes + j, &root);
233 for (j = 0; j < nnodes; j++)
234 erase_augmented(nodes + j, &root);
237 time2 = get_cycles();
238 time = time2 - time1;
240 time = div_u64(time, perf_loops);
241 printk(" -> %llu cycles\n", (unsigned long long)time);
243 for (i = 0; i < check_loops; i++) {
244 init();
245 for (j = 0; j < nnodes; j++) {
246 check_augmented(j);
247 insert_augmented(nodes + j, &root);
249 for (j = 0; j < nnodes; j++) {
250 check_augmented(nnodes - j);
251 erase_augmented(nodes + j, &root);
253 check_augmented(0);
256 kfree(nodes);
258 return -EAGAIN; /* Fail will directly unload the module */
261 static void __exit rbtree_test_exit(void)
263 printk(KERN_ALERT "test exit\n");
266 module_init(rbtree_test_init)
267 module_exit(rbtree_test_exit)
269 MODULE_LICENSE("GPL");
270 MODULE_AUTHOR("Michel Lespinasse");
271 MODULE_DESCRIPTION("Red Black Tree test");