Merge branch 'rs/nth-switch-code-simplification'
[git] / t / helper / test-prio-queue.c
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "prio-queue.h"
4
5 static int intcmp(const void *va, const void *vb, void *data)
6 {
7         const int *a = va, *b = vb;
8         return *a - *b;
9 }
10
11 static void show(int *v)
12 {
13         if (!v)
14                 printf("NULL\n");
15         else
16                 printf("%d\n", *v);
17         free(v);
18 }
19
20 int cmd__prio_queue(int argc, const char **argv)
21 {
22         struct prio_queue pq = { intcmp };
23
24         while (*++argv) {
25                 if (!strcmp(*argv, "get")) {
26                         void *peek = prio_queue_peek(&pq);
27                         void *get = prio_queue_get(&pq);
28                         if (peek != get)
29                                 BUG("peek and get results do not match");
30                         show(get);
31                 } else if (!strcmp(*argv, "dump")) {
32                         void *peek;
33                         void *get;
34                         while ((peek = prio_queue_peek(&pq))) {
35                                 get = prio_queue_get(&pq);
36                                 if (peek != get)
37                                         BUG("peek and get results do not match");
38                                 show(get);
39                         }
40                 } else if (!strcmp(*argv, "stack")) {
41                         pq.compare = NULL;
42                 } else {
43                         int *v = xmalloc(sizeof(*v));
44                         *v = atoi(*argv);
45                         prio_queue_put(&pq, v);
46                 }
47         }
48
49         return 0;
50 }