Merge branch 'maint'
[git/kirr.git] / test-treap.c
blobcdba5111e19cd850f262108a330ce5d4ce63a166
1 /*
2 * test-treap.c: code to exercise the svn importer's treap structure
3 */
5 #include "cache.h"
6 #include "vcs-svn/obj_pool.h"
7 #include "vcs-svn/trp.h"
9 struct int_node {
10 uintmax_t n;
11 struct trp_node children;
14 obj_pool_gen(node, struct int_node, 3)
16 static int node_cmp(struct int_node *a, struct int_node *b)
18 return (a->n > b->n) - (a->n < b->n);
21 trp_gen(static, treap_, struct int_node, children, node, node_cmp)
23 static void strtonode(struct int_node *item, const char *s)
25 char *end;
26 item->n = strtoumax(s, &end, 10);
27 if (*s == '\0' || (*end != '\n' && *end != '\0'))
28 die("invalid integer: %s", s);
31 int main(int argc, char *argv[])
33 struct strbuf sb = STRBUF_INIT;
34 struct trp_root root = { ~0 };
35 uint32_t item;
37 if (argc != 1)
38 usage("test-treap < ints");
40 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
41 item = node_alloc(1);
42 strtonode(node_pointer(item), sb.buf);
43 treap_insert(&root, node_pointer(item));
46 item = node_offset(treap_first(&root));
47 while (~item) {
48 uint32_t next;
49 struct int_node *tmp = node_pointer(node_alloc(1));
51 tmp->n = node_pointer(item)->n;
52 next = node_offset(treap_next(&root, node_pointer(item)));
54 treap_remove(&root, node_pointer(item));
55 item = node_offset(treap_nsearch(&root, tmp));
57 if (item != next && (!~item || node_pointer(item)->n != tmp->n))
58 die("found %"PRIuMAX" in place of %"PRIuMAX"",
59 ~item ? node_pointer(item)->n : ~(uintmax_t) 0,
60 ~next ? node_pointer(next)->n : ~(uintmax_t) 0);
61 printf("%"PRIuMAX"\n", tmp->n);
63 node_reset();
64 return 0;