Skip to content

Commit

Permalink
tests: move t0009-prio-queue.sh to the new unit testing framework
Browse files Browse the repository at this point in the history
t/t0009-prio-queue.sh along with t/helper/test-prio-queue.c unit
tests Git's implementation of a priority queue. Migrate the
test over to the new unit testing framework to simplify debugging
and reduce test run-time. Refactor the required logic and add
a new test case in addition to porting over the original ones in
shell.

Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
  • Loading branch information
Chandra Pratap committed Jan 14, 2024
1 parent 1a87c84 commit 1b1163b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ TEST_BUILTINS_OBJS += test-partial-clone.o
TEST_BUILTINS_OBJS += test-path-utils.o
TEST_BUILTINS_OBJS += test-pcre2-config.o
TEST_BUILTINS_OBJS += test-pkt-line.o
TEST_BUILTINS_OBJS += test-prio-queue.o
TEST_BUILTINS_OBJS += test-proc-receive.o
TEST_BUILTINS_OBJS += test-progress.o
TEST_BUILTINS_OBJS += test-reach.o
Expand Down Expand Up @@ -1340,6 +1339,7 @@ THIRD_PARTY_SOURCES += sha1dc/%

UNIT_TEST_PROGRAMS += t-basic
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-prio-queue
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
Expand Down
51 changes: 0 additions & 51 deletions t/helper/test-prio-queue.c

This file was deleted.

1 change: 0 additions & 1 deletion t/helper/test-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ static struct test_cmd cmds[] = {
{ "path-utils", cmd__path_utils },
{ "pcre2-config", cmd__pcre2_config },
{ "pkt-line", cmd__pkt_line },
{ "prio-queue", cmd__prio_queue },
{ "proc-receive", cmd__proc_receive },
{ "progress", cmd__progress },
{ "reach", cmd__reach },
Expand Down
1 change: 0 additions & 1 deletion t/helper/test-tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ int cmd__partial_clone(int argc, const char **argv);
int cmd__path_utils(int argc, const char **argv);
int cmd__pcre2_config(int argc, const char **argv);
int cmd__pkt_line(int argc, const char **argv);
int cmd__prio_queue(int argc, const char **argv);
int cmd__proc_receive(int argc, const char **argv);
int cmd__progress(int argc, const char **argv);
int cmd__reach(int argc, const char **argv);
Expand Down
66 changes: 0 additions & 66 deletions t/t0009-prio-queue.sh

This file was deleted.

99 changes: 99 additions & 0 deletions t/unit-tests/t-prio-queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "test-lib.h"
#include "prio-queue.h"

static int intcmp(const void *va, const void *vb, void *data UNUSED)
{
const int *a = va, *b = vb;
return *a - *b;
}

static int show(int *v)
{
int ret = -1;
if (v)
ret = *v;
free(v);
return ret;
}

static int test_prio_queue(const char **input, int *result)
{
struct prio_queue pq = { intcmp };
int i = 0;

while (*input) {
if (!strcmp(*input, "get")) {
void *peek = prio_queue_peek(&pq);
void *get = prio_queue_get(&pq);
if (peek != get)
BUG("peek and get results do not match");
result[i++] = show(get);
} else if (!strcmp(*input, "dump")) {
void *peek;
void *get;
while ((peek = prio_queue_peek(&pq))) {
get = prio_queue_get(&pq);
if (peek != get)
BUG("peek and get results do not match");
result[i++] = show(get);
}
} else if (!strcmp(*input, "stack")) {
pq.compare = NULL;
} else if (!strcmp(*input, "reverse")) {
prio_queue_reverse(&pq);
} else {
int *v = xmalloc(sizeof(*v));
*v = atoi(*input);
prio_queue_put(&pq, v);
}
input++;
}

clear_prio_queue(&pq);

return 0;
}

#define INPUT_SIZE 6

#define BASIC_INPUT "1", "2", "3", "4", "5", "5", "dump"
#define BASIC_EXPECTED 1, 2, 3, 4, 5, 5

#define MIXED_PUT_GET_INPUT "6", "2", "4", "get", "5", "3", "get", "get", "1", "dump"
#define MIXED_PUT_GET_EXPECTED 2, 3, 4, 1, 5, 6

#define EMPTY_QUEUE_INPUT "1", "2", "get", "get", "get", "1", "2", "get", "get", "get"
#define EMPTY_QUEUE_EXPECTED 1, 2, -1, 1, 2, -1

#define STACK_INPUT "stack", "1", "5", "4", "6", "2", "3", "dump"
#define STACK_EXPECTED 3, 2, 6, 4, 5, 1

#define REVERSE_STACK_INPUT "stack", "1", "2", "3", "4", "5", "6", "reverse", "dump"
#define REVERSE_STACK_EXPECTED 1, 2, 3, 4, 5, 6

#define TEST_INPUT(INPUT, EXPECTED, name) \
static void test_##name(void) \
{ \
const char *input[] = {INPUT}; \
int expected[] = {EXPECTED}; \
int result[INPUT_SIZE]; \
test_prio_queue(input, result); \
check(!memcmp(expected, result, sizeof(expected))); \
}

TEST_INPUT(BASIC_INPUT, BASIC_EXPECTED, basic)
TEST_INPUT(MIXED_PUT_GET_INPUT, MIXED_PUT_GET_EXPECTED, mixed)
TEST_INPUT(EMPTY_QUEUE_INPUT, EMPTY_QUEUE_EXPECTED, empty)
TEST_INPUT(STACK_INPUT, STACK_EXPECTED, stack)
TEST_INPUT(REVERSE_STACK_INPUT, REVERSE_STACK_EXPECTED, reverse)

int cmd_main(int argc, const char **argv)
{
TEST(test_basic(), "prio-queue works for basic input");
TEST(test_mixed(), "prio-queue works for mixed put & get commands");
TEST(test_empty(), "prio-queue works when queue is empty");
TEST(test_stack(), "prio-queue works when used as a LIFO stack");
TEST(test_reverse(), "prio-queue works when LIFO stack is reversed");

return test_done();
}

0 comments on commit 1b1163b

Please sign in to comment.