]> git.ipfire.org Git - thirdparty/git.git/blame - t/unit-tests/t-prio-queue.c
Merge branch 'ba/osxkeychain-updates'
[thirdparty/git.git] / t / unit-tests / t-prio-queue.c
CommitLineData
808b77e5
CP
1#include "test-lib.h"
2#include "prio-queue.h"
3
4static int intcmp(const void *va, const void *vb, void *data UNUSED)
5{
6 const int *a = va, *b = vb;
7 return *a - *b;
8}
9
10
11#define MISSING -1
12#define DUMP -2
13#define STACK -3
14#define GET -4
15#define REVERSE -5
16
17static int show(int *v)
18{
19 return v ? *v : MISSING;
20}
21
30ff0509
RS
22static void test_prio_queue(int *input, size_t input_size,
23 int *result, size_t result_size)
808b77e5
CP
24{
25 struct prio_queue pq = { intcmp };
30ff0509 26 int j = 0;
808b77e5 27
30ff0509 28 for (int i = 0; i < input_size; i++) {
808b77e5
CP
29 void *peek, *get;
30 switch(input[i]) {
31 case GET:
32 peek = prio_queue_peek(&pq);
33 get = prio_queue_get(&pq);
34 if (!check(peek == get))
35 return;
30ff0509
RS
36 if (!check_uint(j, <, result_size))
37 break;
e6f9cb76
RS
38 if (!check_int(result[j], ==, show(get)))
39 test_msg(" j: %d", j);
40 j++;
808b77e5
CP
41 break;
42 case DUMP:
43 while ((peek = prio_queue_peek(&pq))) {
44 get = prio_queue_get(&pq);
45 if (!check(peek == get))
46 return;
30ff0509
RS
47 if (!check_uint(j, <, result_size))
48 break;
e6f9cb76
RS
49 if (!check_int(result[j], ==, show(get)))
50 test_msg(" j: %d", j);
51 j++;
808b77e5
CP
52 }
53 break;
54 case STACK:
55 pq.compare = NULL;
56 break;
57 case REVERSE:
58 prio_queue_reverse(&pq);
59 break;
60 default:
61 prio_queue_put(&pq, &input[i]);
62 break;
63 }
64 }
30ff0509 65 check_uint(j, ==, result_size);
808b77e5
CP
66 clear_prio_queue(&pq);
67}
68
543b2a10
RS
69#define TEST_INPUT(input, result) \
70 test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
808b77e5
CP
71
72int cmd_main(int argc, const char **argv)
73{
543b2a10
RS
74 TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
75 ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })),
76 "prio-queue works for basic input");
77 TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
78 ((int []){ 2, 3, 4, 1, 5, 6 })),
79 "prio-queue works for mixed put & get commands");
80 TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
81 ((int []){ 1, 2, MISSING, 1, 2, MISSING })),
82 "prio-queue works when queue is empty");
83 TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
84 ((int []){ 3, 2, 6, 4, 5, 1, 8 })),
85 "prio-queue works when used as a LIFO stack");
86 TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
87 ((int []){ 1, 2, 3, 4, 5, 6 })),
88 "prio-queue works when LIFO stack is reversed");
808b77e5
CP
89
90 return test_done();
91}