Skip to content

Commit 8b702f9

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: adapt priority queue test to use clar test framework
Convert the prio-queue test script to clar framework by using clar assertions where necessary. Test functions are created as a standalone to test different cases. update the type of the variable `j` from int to `size_t`, this ensures compatibility with the type used for result_size, which is also size_t, preventing a potential warning or error caused by comparisons between signed and unsigned integers. Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Acked-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c143dfa commit 8b702f9

File tree

4 files changed

+96
-93
lines changed

4 files changed

+96
-93
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
13391339

13401340
CLAR_TEST_SUITES += u-ctype
13411341
CLAR_TEST_SUITES += u-mem-pool
1342+
CLAR_TEST_SUITES += u-prio-queue
13421343
CLAR_TEST_SUITES += u-strvec
13431344
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13441345
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1351,7 +1352,6 @@ UNIT_TEST_PROGRAMS += t-hashmap
13511352
UNIT_TEST_PROGRAMS += t-oid-array
13521353
UNIT_TEST_PROGRAMS += t-oidmap
13531354
UNIT_TEST_PROGRAMS += t-oidtree
1354-
UNIT_TEST_PROGRAMS += t-prio-queue
13551355
UNIT_TEST_PROGRAMS += t-reftable-basics
13561356
UNIT_TEST_PROGRAMS += t-reftable-block
13571357
UNIT_TEST_PROGRAMS += t-reftable-merged

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
clar_test_suites = [
22
'unit-tests/u-ctype.c',
33
'unit-tests/u-mem-pool.c',
4+
'unit-tests/u-prio-queue.c',
45
'unit-tests/u-strvec.c',
56
]
67

@@ -47,7 +48,6 @@ unit_test_programs = [
4748
'unit-tests/t-oid-array.c',
4849
'unit-tests/t-oidmap.c',
4950
'unit-tests/t-oidtree.c',
50-
'unit-tests/t-prio-queue.c',
5151
'unit-tests/t-reftable-basics.c',
5252
'unit-tests/t-reftable-block.c',
5353
'unit-tests/t-reftable-merged.c',

t/unit-tests/t-prio-queue.c

Lines changed: 0 additions & 91 deletions
This file was deleted.

t/unit-tests/u-prio-queue.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "unit-test.h"
2+
#include "prio-queue.h"
3+
4+
static 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+
17+
static int show(int *v)
18+
{
19+
return v ? *v : MISSING;
20+
}
21+
22+
static void test_prio_queue(int *input, size_t input_size,
23+
int *result, size_t result_size)
24+
{
25+
struct prio_queue pq = { intcmp };
26+
size_t j = 0;
27+
28+
for (size_t i = 0; i < input_size; i++) {
29+
void *peek, *get;
30+
switch(input[i]) {
31+
case GET:
32+
peek = prio_queue_peek(&pq);
33+
get = prio_queue_get(&pq);
34+
cl_assert(peek == get);
35+
cl_assert(j < result_size);
36+
cl_assert_equal_i(result[j], show(get));
37+
j++;
38+
break;
39+
case DUMP:
40+
while ((peek = prio_queue_peek(&pq))) {
41+
get = prio_queue_get(&pq);
42+
cl_assert(peek == get);
43+
cl_assert(j < result_size);
44+
cl_assert_equal_i(result[j], show(get));
45+
j++;
46+
}
47+
break;
48+
case STACK:
49+
pq.compare = NULL;
50+
break;
51+
case REVERSE:
52+
prio_queue_reverse(&pq);
53+
break;
54+
default:
55+
prio_queue_put(&pq, &input[i]);
56+
break;
57+
}
58+
}
59+
cl_assert_equal_i(j, result_size);
60+
clear_prio_queue(&pq);
61+
}
62+
63+
#define TEST_INPUT(input, result) \
64+
test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
65+
66+
void test_prio_queue__basic(void)
67+
{
68+
TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
69+
((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 }));
70+
}
71+
72+
void test_prio_queue__mixed(void)
73+
{
74+
TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
75+
((int []){ 2, 3, 4, 1, 5, 6 }));
76+
}
77+
78+
void test_prio_queue__empty(void)
79+
{
80+
TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
81+
((int []){ 1, 2, MISSING, 1, 2, MISSING }));
82+
}
83+
84+
void test_prio_queue__stack(void)
85+
{
86+
TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
87+
((int []){ 3, 2, 6, 4, 5, 1, 8 }));
88+
}
89+
90+
void test_prio_queue__reverse_stack(void)
91+
{
92+
TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
93+
((int []){ 1, 2, 3, 4, 5, 6 }));
94+
}

0 commit comments

Comments
 (0)