4 #include <ccan/heap/heap.h>
5 /* Include the C files directly. */
6 #include <ccan/heap/heap.c>
7 #include <ccan/tap/tap.h>
14 static bool heap_ok(const struct heap *h, heap_less_func_t less, int i)
22 if (less(h->data[l], h->data[i])) {
23 fprintf(stderr, "heap property violation\n");
26 if (!heap_ok(h, less, l))
30 if (less(h->data[r], h->data[i])) {
31 fprintf(stderr, "heap property violation\n");
34 if (!heap_ok(h, less, r))
40 static bool less(const struct item *a, const struct item *b)
45 static bool __less(const void *a, const void *b)
50 static bool more(const struct item *a, const struct item *b)
55 static bool __more(const void *a, const void *b)
60 static bool some_test(size_t n, bool is_less)
62 struct item *items = calloc(n, sizeof(*items));
63 struct item *item, *prev;
73 h = heap_init(__less);
75 h = heap_init(__more);
81 for (i = 0; i < n; i++) {
85 printf("pushing %d\n", item->v);
87 if (!heap_ok(h, is_less ? __less : __more, 0))
92 if (!heap_ok(h, __more, 0))
95 if (!heap_ok(h, __less, 0))
99 if (!heap_ok(h, __more, 0))
103 for (i = 0; i < n; i++) {
105 if (!heap_ok(h, is_less ? __less : __more, 0))
107 printf("popped %d\n", item->v);
110 if (less(item, prev))
113 if (more(item, prev))
128 ok1(some_test(5000, true));
129 ok1(some_test(1, true));
130 ok1(some_test(33, false));
132 return exit_status();