Skip to content

Commit 107023e

Browse files
committed
Merge branch 'cp/unit-test-prio-queue'
The priority queue test has been migrated to the unit testing framework. * cp/unit-test-prio-queue: tests: move t0009-prio-queue.sh to the new unit testing framework
2 parents 235986b + 808b77e commit 107023e

File tree

6 files changed

+99
-120
lines changed

6 files changed

+99
-120
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ TEST_BUILTINS_OBJS += test-partial-clone.o
831831
TEST_BUILTINS_OBJS += test-path-utils.o
832832
TEST_BUILTINS_OBJS += test-pcre2-config.o
833833
TEST_BUILTINS_OBJS += test-pkt-line.o
834-
TEST_BUILTINS_OBJS += test-prio-queue.o
835834
TEST_BUILTINS_OBJS += test-proc-receive.o
836835
TEST_BUILTINS_OBJS += test-progress.o
837836
TEST_BUILTINS_OBJS += test-reach.o
@@ -1346,6 +1345,7 @@ UNIT_TEST_PROGRAMS += t-basic
13461345
UNIT_TEST_PROGRAMS += t-mem-pool
13471346
UNIT_TEST_PROGRAMS += t-strbuf
13481347
UNIT_TEST_PROGRAMS += t-ctype
1348+
UNIT_TEST_PROGRAMS += t-prio-queue
13491349
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
13501350
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
13511351
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o

t/helper/test-prio-queue.c

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

t/helper/test-tool.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static struct test_cmd cmds[] = {
5555
{ "path-utils", cmd__path_utils },
5656
{ "pcre2-config", cmd__pcre2_config },
5757
{ "pkt-line", cmd__pkt_line },
58-
{ "prio-queue", cmd__prio_queue },
5958
{ "proc-receive", cmd__proc_receive },
6059
{ "progress", cmd__progress },
6160
{ "reach", cmd__reach },

t/helper/test-tool.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ int cmd__partial_clone(int argc, const char **argv);
4848
int cmd__path_utils(int argc, const char **argv);
4949
int cmd__pcre2_config(int argc, const char **argv);
5050
int cmd__pkt_line(int argc, const char **argv);
51-
int cmd__prio_queue(int argc, const char **argv);
5251
int cmd__proc_receive(int argc, const char **argv);
5352
int cmd__progress(int argc, const char **argv);
5453
int cmd__reach(int argc, const char **argv);

t/t0009-prio-queue.sh

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

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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, int *result, size_t input_size)
23+
{
24+
struct prio_queue pq = { intcmp };
25+
26+
for (int i = 0, j = 0; i < input_size; i++) {
27+
void *peek, *get;
28+
switch(input[i]) {
29+
case GET:
30+
peek = prio_queue_peek(&pq);
31+
get = prio_queue_get(&pq);
32+
if (!check(peek == get))
33+
return;
34+
if(!check_int(result[j++], ==, show(get)))
35+
test_msg("failed at result[] index %d", j-1);
36+
break;
37+
case DUMP:
38+
while ((peek = prio_queue_peek(&pq))) {
39+
get = prio_queue_get(&pq);
40+
if (!check(peek == get))
41+
return;
42+
if(!check_int(result[j++], ==, show(get)))
43+
test_msg("failed at result[] index %d", j-1);
44+
}
45+
break;
46+
case STACK:
47+
pq.compare = NULL;
48+
break;
49+
case REVERSE:
50+
prio_queue_reverse(&pq);
51+
break;
52+
default:
53+
prio_queue_put(&pq, &input[i]);
54+
break;
55+
}
56+
}
57+
clear_prio_queue(&pq);
58+
}
59+
60+
#define BASIC_INPUT 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP
61+
#define BASIC_RESULT 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10
62+
63+
#define MIXED_PUT_GET_INPUT 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP
64+
#define MIXED_PUT_GET_RESULT 2, 3, 4, 1, 5, 6
65+
66+
#define EMPTY_QUEUE_INPUT 1, 2, GET, GET, GET, 1, 2, GET, GET, GET
67+
#define EMPTY_QUEUE_RESULT 1, 2, MISSING, 1, 2, MISSING
68+
69+
#define STACK_INPUT STACK, 8, 1, 5, 4, 6, 2, 3, DUMP
70+
#define STACK_RESULT 3, 2, 6, 4, 5, 1, 8
71+
72+
#define REVERSE_STACK_INPUT STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP
73+
#define REVERSE_STACK_RESULT 1, 2, 3, 4, 5, 6
74+
75+
#define TEST_INPUT(INPUT, RESULT, name) \
76+
static void test_##name(void) \
77+
{ \
78+
int input[] = {INPUT}; \
79+
int result[] = {RESULT}; \
80+
test_prio_queue(input, result, ARRAY_SIZE(input)); \
81+
}
82+
83+
TEST_INPUT(BASIC_INPUT, BASIC_RESULT, basic)
84+
TEST_INPUT(MIXED_PUT_GET_INPUT, MIXED_PUT_GET_RESULT, mixed)
85+
TEST_INPUT(EMPTY_QUEUE_INPUT, EMPTY_QUEUE_RESULT, empty)
86+
TEST_INPUT(STACK_INPUT, STACK_RESULT, stack)
87+
TEST_INPUT(REVERSE_STACK_INPUT, REVERSE_STACK_RESULT, reverse)
88+
89+
int cmd_main(int argc, const char **argv)
90+
{
91+
TEST(test_basic(), "prio-queue works for basic input");
92+
TEST(test_mixed(), "prio-queue works for mixed put & get commands");
93+
TEST(test_empty(), "prio-queue works when queue is empty");
94+
TEST(test_stack(), "prio-queue works when used as a LIFO stack");
95+
TEST(test_reverse(), "prio-queue works when LIFO stack is reversed");
96+
97+
return test_done();
98+
}

0 commit comments

Comments
 (0)