prio-queue: add 'peek' operation
[git/git.git] / t / helper / test-prio-queue.c
CommitLineData
15b75817 1#include "test-tool.h"
b4b594a3
JH
2#include "cache.h"
3#include "prio-queue.h"
4
5static int intcmp(const void *va, const void *vb, void *data)
6{
7 const int *a = va, *b = vb;
8 return *a - *b;
9}
10
11static void show(int *v)
12{
13 if (!v)
14 printf("NULL\n");
15 else
16 printf("%d\n", *v);
17 free(v);
18}
19
15b75817 20int cmd__prio_queue(int argc, const char **argv)
b4b594a3
JH
21{
22 struct prio_queue pq = { intcmp };
23
24 while (*++argv) {
aca4240f
DS
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 {
b4b594a3
JH
43 int *v = malloc(sizeof(*v));
44 *v = atoi(*argv);
45 prio_queue_put(&pq, v);
46 }
47 }
48
49 return 0;
50}