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 <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Seyi Kuforiji committed
Jan 17, 2025 at 13:29 UTC
8b702f93dd4d239acd07f9a4991be3297ef333c9
4 files changed
+96
-93
Makefile
+1
-1
@@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
1339
1340
CLAR_TEST_SUITES += u-ctype
1341
CLAR_TEST_SUITES += u-mem-pool
1342
+CLAR_TEST_SUITES += u-prio-queue
1343
CLAR_TEST_SUITES += u-strvec
1344
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1345
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1351,7 +1352,6 @@ UNIT_TEST_PROGRAMS += t-hashmap
1352
UNIT_TEST_PROGRAMS += t-oid-array
1353
UNIT_TEST_PROGRAMS += t-oidmap
1354
UNIT_TEST_PROGRAMS += t-oidtree
1354
-UNIT_TEST_PROGRAMS += t-prio-queue
1355
UNIT_TEST_PROGRAMS += t-reftable-basics
1356
UNIT_TEST_PROGRAMS += t-reftable-block
1357
UNIT_TEST_PROGRAMS += t-reftable-merged
t/meson.build
+1
-1
@@ -1,6 +1,7 @@
1
clar_test_suites = [
2
'unit-tests/u-ctype.c',
3
'unit-tests/u-mem-pool.c',
4
+ 'unit-tests/u-prio-queue.c',
5
'unit-tests/u-strvec.c',
6
]
7
@@ -47,7 +48,6 @@ unit_test_programs = [
48
'unit-tests/t-oid-array.c',
49
'unit-tests/t-oidmap.c',
50
'unit-tests/t-oidtree.c',
50
- 'unit-tests/t-prio-queue.c',
51
'unit-tests/t-reftable-basics.c',
52
'unit-tests/t-reftable-block.c',
53
'unit-tests/t-reftable-merged.c',
t/unit-tests/t-prio-queue.c
deleted
-91
@@ -1,91 +0,0 @@
1
-#include "test-lib.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
- int 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
- if (!check(peek == get))
35
- return;
36
- if (!check_uint(j, <, result_size))
37
- break;
38
- if (!check_int(result[j], ==, show(get)))
39
- test_msg(" j: %d", j);
40
- j++;
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;
47
- if (!check_uint(j, <, result_size))
48
- break;
49
- if (!check_int(result[j], ==, show(get)))
50
- test_msg(" j: %d", j);
51
- j++;
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
- }
65
- check_uint(j, ==, result_size);
66
- clear_prio_queue(&pq);
67
-}
68
-
69
-#define TEST_INPUT(input, result) \
70
- test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
71
-
72
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
73
-{
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");
89
-
90
- return test_done();
91
-}
t/unit-tests/u-prio-queue.c
new
+94
@@ -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
+}