Skip to content

Commit

Permalink
implement sort test
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Jan 16, 2024
1 parent da3e994 commit 8c11ad3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ TEST_SRC = $(TEST_DIR)/test_check_args.cpp \
$(TEST_DIR)/test_push_stack.cpp \
$(TEST_DIR)/test_reverse_rotate_stack.cpp \
$(TEST_DIR)/test_rotate_stack.cpp \
$(TEST_DIR)/test_swap_stack.cpp
$(TEST_DIR)/test_swap_stack.cpp \
$(TEST_DIR)/test_sort.cpp
TEST_OBJ = $(filter-out $(BUILD_DIR)/main.o, $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)))
GTEST_VERSION = 1.14.0
GTEST_DIR = ./test/gtest
Expand Down
77 changes: 77 additions & 0 deletions test/test_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024, reasuke

#include <vector>
#include <algorithm>
#include <sstream>

std::stringstream cout;

#include "gtest/gtest.h"

extern "C" {
#include "push_swap.h"
}

static void sort_test_main(int N) {
// initialize vector
// e.g. N = 3 -> v = {1, 2, 3}
std::vector<int> v(N);
for (int i = 0; i < N; ++i) {
v[i] = i + 1;
}

// generate permutation of v (N! patterns)
// e.g. N = 3, generate the below vectors (3! = 6 patterns)
// {1, 2, 3}
// {1, 3, 2}
// {2, 1, 3}
// {2, 3, 1}
// {3, 1, 2}
// {3, 2, 1}
do {
// initialize stack
t_list *stack_a = NULL;
t_list *stack_b = NULL;
for (int &i : v) {
ft_lstadd_back(&stack_a, ft_lstnew(new int(i)));
}
// sort
sort(&stack_a, &stack_b, N);
// check if the order is appropreate
for (int i = 1; stack_a; ++i, stack_a = stack_a->next) {
EXPECT_EQ(*(int *)stack_a->content, i);
}
} while (std::next_permutation(v.begin(), v.end()));
}

static void sort_test(int N) {
// save stdout
int stdout_copy = dup(STDOUT_FILENO);
// redirect stdout to /dev/null
int dev_null = open("/dev/null", O_WRONLY);
dup2(dev_null, 1);
close(dev_null);

// execute test
sort_test_main(N);

// revert stdout
dup2(stdout_copy, STDOUT_FILENO);
close(stdout_copy);
}

TEST(sort, oneElement) {
sort_test(1);
}

TEST(sort, twoElements) {
sort_test(2);
}

TEST(sort, threeEelments) {
sort_test(3);
}

TEST(sort, fourElements) {
sort_test(4);
}

0 comments on commit 8c11ad3

Please sign in to comment.