blob: 535834636131872b6f6f45bccea9a4bc868e31f2 [file] [log] [blame]
Chandra Pratap808b77e2024-01-21 19:28:45 +00001#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
René Scharfe30ff0502024-03-16 22:09:47 +010022static void test_prio_queue(int *input, size_t input_size,
23 int *result, size_t result_size)
Chandra Pratap808b77e2024-01-21 19:28:45 +000024{
25 struct prio_queue pq = { intcmp };
René Scharfe30ff0502024-03-16 22:09:47 +010026 int j = 0;
Chandra Pratap808b77e2024-01-21 19:28:45 +000027
René Scharfe30ff0502024-03-16 22:09:47 +010028 for (int i = 0; i < input_size; i++) {
Chandra Pratap808b77e2024-01-21 19:28:45 +000029 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;
René Scharfe30ff0502024-03-16 22:09:47 +010036 if (!check_uint(j, <, result_size))
37 break;
René Scharfee6f9cb72024-03-16 21:45:51 +010038 if (!check_int(result[j], ==, show(get)))
39 test_msg(" j: %d", j);
40 j++;
Chandra Pratap808b77e2024-01-21 19:28:45 +000041 break;
42 case DUMP:
43 while ((peek = prio_queue_peek(&pq))) {
44 get = prio_queue_get(&pq);
45 if (!check(peek == get))
46 return;
René Scharfe30ff0502024-03-16 22:09:47 +010047 if (!check_uint(j, <, result_size))
48 break;
René Scharfee6f9cb72024-03-16 21:45:51 +010049 if (!check_int(result[j], ==, show(get)))
50 test_msg(" j: %d", j);
51 j++;
Chandra Pratap808b77e2024-01-21 19:28:45 +000052 }
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 }
René Scharfe30ff0502024-03-16 22:09:47 +010065 check_uint(j, ==, result_size);
Chandra Pratap808b77e2024-01-21 19:28:45 +000066 clear_prio_queue(&pq);
67}
68
69#define BASIC_INPUT 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP
70#define BASIC_RESULT 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10
71
72#define MIXED_PUT_GET_INPUT 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP
73#define MIXED_PUT_GET_RESULT 2, 3, 4, 1, 5, 6
74
75#define EMPTY_QUEUE_INPUT 1, 2, GET, GET, GET, 1, 2, GET, GET, GET
76#define EMPTY_QUEUE_RESULT 1, 2, MISSING, 1, 2, MISSING
77
78#define STACK_INPUT STACK, 8, 1, 5, 4, 6, 2, 3, DUMP
79#define STACK_RESULT 3, 2, 6, 4, 5, 1, 8
80
81#define REVERSE_STACK_INPUT STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP
82#define REVERSE_STACK_RESULT 1, 2, 3, 4, 5, 6
83
84#define TEST_INPUT(INPUT, RESULT, name) \
85 static void test_##name(void) \
86{ \
87 int input[] = {INPUT}; \
88 int result[] = {RESULT}; \
René Scharfe30ff0502024-03-16 22:09:47 +010089 test_prio_queue(input, ARRAY_SIZE(input), \
90 result, ARRAY_SIZE(result)); \
Chandra Pratap808b77e2024-01-21 19:28:45 +000091}
92
93TEST_INPUT(BASIC_INPUT, BASIC_RESULT, basic)
94TEST_INPUT(MIXED_PUT_GET_INPUT, MIXED_PUT_GET_RESULT, mixed)
95TEST_INPUT(EMPTY_QUEUE_INPUT, EMPTY_QUEUE_RESULT, empty)
96TEST_INPUT(STACK_INPUT, STACK_RESULT, stack)
97TEST_INPUT(REVERSE_STACK_INPUT, REVERSE_STACK_RESULT, reverse)
98
99int cmd_main(int argc, const char **argv)
100{
101 TEST(test_basic(), "prio-queue works for basic input");
102 TEST(test_mixed(), "prio-queue works for mixed put & get commands");
103 TEST(test_empty(), "prio-queue works when queue is empty");
104 TEST(test_stack(), "prio-queue works when used as a LIFO stack");
105 TEST(test_reverse(), "prio-queue works when LIFO stack is reversed");
106
107 return test_done();
108}