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
44 static void my_check_order(struct fastbuf *fb, struct my_tree *t)
46 int last_key = 0x80000000;
47 TREE_FOR_ALL(my, t, n)
49 ASSERT(n->key >= last_key);
54 sprintf(tmp, "%d -> %d\n", n->key, n->x);
68 static void my2_dump_key(struct fastbuf *fb, struct my2_node *n)
75 static void my2_dump_data(struct fastbuf *fb UNUSED, struct my2_node *n UNUSED)
79 #define TREE_NODE struct my2_node
80 #define TREE_PREFIX(x) my2_##x
81 #define TREE_KEY_ENDSTRING key
83 #define TREE_WANT_CLEANUP
85 #define TREE_WANT_SEARCH
86 #define TREE_WANT_REMOVE
87 #define TREE_WANT_FIND_NEXT
88 #define TREE_WANT_ITERATOR
89 #define TREE_WANT_DUMP
91 #define TREE_CONSERVE_SPACE
94 static void random_string(char *txt, uns max_len)
96 uns len = random() % max_len;
99 txt[j] = random() % 96 + 32;
103 static char *options = CF_SHORT_OPTS "vn:a";
105 static char *help = "\
106 Usage: test1.bin <options>\n\
109 "-v\tSet verbose mode\n\
110 -n num\tNumber of inserted nodes\n\
111 -a\tProbe some ASSERTs\n\
122 main(int argc, char **argv)
124 int verbose = 0, number = 1000, asserts = 0;
126 struct fastbuf *fb, *dump_fb;
132 while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0)
139 number = atoi(optarg);
150 fb = bfdopen(1, 4096);
157 for (i=0; i<number; i++)
158 my_lookup(&t, random() % 1000000)->x = i;
159 my_dump(dump_fb, &t);
160 my_check_order(dump_fb, &t);
168 bputs(fb, "Load test passed\n");
171 for (i=0; i<100; i++)
173 my_new(&t, i)->x = i;
174 my_dump(dump_fb, &t);
176 for (i=0; i<100; i++)
178 int a = i/10, b = i%10, j = a*10 + (b + a) % 10;
179 int res UNUSED = my_delete(&t, j);
181 my_dump(dump_fb, &t);
185 bputs(fb, "Sequential adding and deleting passed\n");
188 for (i=0; i<997; i++)
190 my_new(&t, i*238 % 997)->x = i;
193 my_dump(dump_fb, &t);
195 TREE_FOR_ALL(my, &t, n)
202 for (i=0; i<997; i++)
204 int res UNUSED = my_delete(&t, i*111 % 997);
208 my_dump(dump_fb, &t);
211 bputs(fb, "Complete tree passed\n");
214 for (i=0; i<number; i++)
217 random_string(txt, 30);
220 my2_dump(dump_fb, &t2);
221 TREE_FOR_ALL(my2, &t2, n)
225 for (tmp=n; tmp; tmp = my2_find_next(tmp))
230 bputs(dump_fb, n->key);
231 sprintf(txt, ": %d\n", count);
240 random_string(txt, 30);
241 n = my2_search(&t2, txt);
245 my2_dump(dump_fb, &t2);
248 bputs(fb, "String test passed\n");