2 * Test of red-black trees
4 * (c) 2002, Robert Spalek <robert@ucw.cz>
8 #include <ucw/getopt.h>
9 #include <ucw/fastbuf.h>
19 static void my_dump_key(struct fastbuf *fb, struct my1_node *n)
22 sprintf(tmp, "key=%d ", n->key);
26 static void my_dump_data(struct fastbuf *fb, struct my1_node *n)
29 sprintf(tmp, "x=%d ", n->x);
33 #define TREE_NODE struct my1_node
34 #define TREE_PREFIX(x) my_##x
35 #define TREE_KEY_ATOMIC key
36 #define TREE_WANT_CLEANUP
37 #define TREE_WANT_LOOKUP
38 #define TREE_WANT_DELETE
39 #define TREE_WANT_ITERATOR
40 #define TREE_WANT_DUMP
41 #define TREE_CONSERVE_SPACE
42 #define TREE_AUTO_POOL 4096
45 static void my_check_order(struct fastbuf *fb, struct my_tree *t)
47 int last_key = 0x80000000;
48 TREE_FOR_ALL(my, t, n)
50 ASSERT(n->key >= last_key);
55 sprintf(tmp, "%d -> %d\n", n->key, n->x);
69 static void my2_dump_key(struct fastbuf *fb, struct my2_node *n)
76 static void my2_dump_data(struct fastbuf *fb UNUSED, struct my2_node *n UNUSED)
80 #define TREE_NODE struct my2_node
81 #define TREE_PREFIX(x) my2_##x
82 #define TREE_KEY_ENDSTRING key
84 #define TREE_WANT_CLEANUP
86 #define TREE_WANT_SEARCH
87 #define TREE_WANT_REMOVE
88 #define TREE_WANT_FIND_NEXT
89 #define TREE_WANT_ITERATOR
90 #define TREE_WANT_DUMP
91 #define TREE_CONSERVE_SPACE
92 #define TREE_AUTO_POOL 4096
95 static void random_string(char *txt, uint max_len)
97 uint len = random() % max_len;
100 txt[j] = random() % 96 + 32;
104 static char *options = CF_SHORT_OPTS "vn:a";
106 static char *help = "\
107 Usage: test1.bin <options>\n\
110 "-v\tSet verbose mode\n\
111 -n num\tNumber of inserted nodes\n\
112 -a\tProbe some ASSERTs\n\
123 main(int argc, char **argv)
125 int verbose = 0, number = 1000, asserts = 0;
127 struct fastbuf *fb, *dump_fb;
133 while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0)
140 number = atoi(optarg);
151 fb = bfdopen(1, 4096);
158 for (i=0; i<number; i++)
159 my_lookup(&t, random() % 1000000)->x = i;
160 my_dump(dump_fb, &t);
161 my_check_order(dump_fb, &t);
169 bputs(fb, "Load test passed\n");
172 for (i=0; i<100; i++)
174 my_new(&t, i)->x = i;
175 my_dump(dump_fb, &t);
177 for (i=0; i<100; i++)
179 int a = i/10, b = i%10, j = a*10 + (b + a) % 10;
180 int res UNUSED = my_delete(&t, j);
182 my_dump(dump_fb, &t);
186 bputs(fb, "Sequential adding and deleting passed\n");
189 for (i=0; i<997; i++)
191 my_new(&t, i*238 % 997)->x = i;
194 my_dump(dump_fb, &t);
196 TREE_FOR_ALL(my, &t, n)
203 for (i=0; i<997; i++)
205 int res UNUSED = my_delete(&t, i*111 % 997);
209 my_dump(dump_fb, &t);
212 bputs(fb, "Complete tree passed\n");
215 for (i=0; i<number; i++)
218 random_string(txt, 30);
221 my2_dump(dump_fb, &t2);
222 TREE_FOR_ALL(my2, &t2, n)
226 for (tmp=n; tmp; tmp = my2_find_next(tmp))
231 bputs(dump_fb, n->key);
232 sprintf(txt, ": %d\n", count);
241 random_string(txt, 30);
242 n = my2_search(&t2, txt);
246 my2_dump(dump_fb, &t2);
249 bputs(fb, "String test passed\n");