From 6e07d3b24ea1c58c507bb40f1a1bd0bd4dc219a2 Mon Sep 17 00:00:00 2001 From: rask24 <70057885+rask24@users.noreply.github.com> Date: Thu, 11 Jan 2024 16:29:18 +0900 Subject: [PATCH] implement argument checker (#3) * fix Makefile lift link * implement exit_with_error * implement is_invalid_argument --- Makefile | 14 +++--- include/push_swap.h | 10 ++++- src/is_invalid_argument.c | 72 +++++++++++++++++++++++++++++++ src/main.c | 9 ++-- src/utils/exit_with_error.c | 19 ++++++++ test/test.cpp | 7 --- test/test_is_invalid_argument.cpp | 55 +++++++++++++++++++++++ 7 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 src/is_invalid_argument.c create mode 100644 src/utils/exit_with_error.c delete mode 100644 test/test.cpp create mode 100644 test/test_is_invalid_argument.cpp diff --git a/Makefile b/Makefile index f9ae0c6..cbb9af6 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ SRC_DIR = ./src BUILD_DIR = ./build INC_DIR = ./include LIBFT_DIR = ./libft -SRC = $(SRC_DIR)/main.c +SRC = $(SRC_DIR)/main.c \ + $(SRC_DIR)/is_invalid_argument.c \ + $(SRC_DIR)/utils/exit_with_error.c OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)) DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRC)) DEPFLAGS = -MMD -MP @@ -13,7 +15,8 @@ INCLUDE = -I $(INC_DIR) CXXFLAGS = -std=c++20 -Wall -Wextra -Werror TEST_DIR = ./test -TEST_SRC = $(TEST_DIR)/test.cpp +TEST_SRC = $(TEST_DIR)/test_is_invalid_argument.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 GTEST_ARCHIVE = v$(GTEST_VERSION).tar.gz @@ -32,7 +35,7 @@ all: title $(NAME) $(NAME): $(OBJ) @printf "\n" @make -C $(LIBFT_DIR) - @$(CC) $^ -o $@ + @$(CC) $^ -L $(LIBFT_DIR) -lft -o $@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(@D) @@ -52,8 +55,9 @@ re: fclean all test: $(GTEST_DIR) @echo "$(BLUE)test$(RESET)" @$(CXX) $(CXXFLAGS) -I $(TEST_DIR) $(INCLUDE) -L $(LIBFT_DIR) -l ft -lpthread -o tester \ - $(TEST_SRC) $(GTEST_DIR)/gtest_main.cc $(GTEST_DIR)/gtest-all.cc + $(TEST_SRC) $(GTEST_DIR)/gtest_main.cc $(GTEST_DIR)/gtest-all.cc $(TEST_OBJ) @./tester # --gtest_filter=Vector.other + @$(RM) tester $(GTEST_DIR): @echo "$(BLUE)fetching google test$(RESET)" @@ -72,6 +76,6 @@ norm: title: @echo "$(BLUE)push_swap$(RESET)" -.PHONY: all clean fclean re +.PHONY: all clean fclean re test norm title -include $(DEP) diff --git a/include/push_swap.h b/include/push_swap.h index fdfacbe..692d942 100644 --- a/include/push_swap.h +++ b/include/push_swap.h @@ -6,19 +6,25 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ -/* Updated: 2024/01/10 14:55:50 by reasuke ### ########.fr */ +/* Updated: 2024/01/11 15:45:14 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PUSH_SWAP_H # define PUSH_SWAP_H -# include +# include +# include # include +# include // TODO: delete # include # include "../libft/libft.h" +bool is_invalid_argument(int argc, char **argv); + +void exit_with_error(void); + #endif diff --git a/src/is_invalid_argument.c b/src/is_invalid_argument.c new file mode 100644 index 0000000..0c8d714 --- /dev/null +++ b/src/is_invalid_argument.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_invalid_argument.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:44:55 by reasuke #+# #+# */ +/* Updated: 2024/01/11 16:18:03 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +static bool _has_not_digit(int argc, char **argv) +{ + int i; + char *end_ptr; + + i = 1; + while (i < argc) + { + ft_strtol(argv[i], &end_ptr, 10); + if (*end_ptr) + return (true); + i++; + } + return (false); +} + +static bool _has_overflowed(int argc, char **argv) +{ + int i; + long num; + + i = 1; + while (i < argc) + { + num = ft_strtol(argv[i], NULL, 10); + if (num < INT_MIN || INT_MAX < num) + return (true); + i++; + } + return (false); +} + +static bool _has_duplicate(int argc, char **argv) +{ + int i; + int j; + + i = 1; + while (i < argc) + { + j = i + 1; + while (j < argc) + { + if (ft_strtol(argv[i], NULL, 10) == ft_strtol(argv[j], NULL, 10)) + return (true); + j++; + } + i++; + } + return (false); +} + +bool is_invalid_argument(int argc, char **argv) +{ + return (_has_not_digit(argc, argv) + || _has_overflowed(argc, argv) + || _has_duplicate(argc, argv)); +} diff --git a/src/main.c b/src/main.c index c76828c..79b9ee1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,14 +6,17 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ -/* Updated: 2024/01/10 14:59:00 by reasuke ### ########.fr */ +/* Updated: 2024/01/11 15:56:52 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -int main(void) +int main(int argc, char **argv) { - printf("This is push_swap main file.\n"); + if (argc < 2) + return (0); + if (is_invalid_argument(argc, argv)) + exit_with_error(); return (0); } diff --git a/src/utils/exit_with_error.c b/src/utils/exit_with_error.c new file mode 100644 index 0000000..4f82387 --- /dev/null +++ b/src/utils/exit_with_error.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit_with_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/11 14:38:17 by reasuke #+# #+# */ +/* Updated: 2024/01/11 14:38:59 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void exit_with_error(void) +{ + ft_putendl_fd("Error", STDERR_FILENO); + exit(1); +} diff --git a/test/test.cpp b/test/test.cpp deleted file mode 100644 index 70587e6..0000000 --- a/test/test.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "gtest/gtest.h" - -extern "C" { - #include "push_swap.h" -} - -TEST(FunctionTest, Case) {} diff --git a/test/test_is_invalid_argument.cpp b/test/test_is_invalid_argument.cpp new file mode 100644 index 0000000..b231501 --- /dev/null +++ b/test/test_is_invalid_argument.cpp @@ -0,0 +1,55 @@ +// Copyright 2024 reasuke + +#include "gtest/gtest.h" + +extern "C" { + #include "push_swap.h" +} + +TEST(is_invalid_argument, one_argument_valid) { + int argc = 2; + const char *args[] = { "push_swap", "1" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), false); +} + +TEST(is_invalid_argument, one_argument_not_digit) { + int argc = 2; + const char *args[] = { "push_swap", "abc" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), true); +} + +TEST(is_invalid_argument, one_argument_including_not_digit) { + int argc = 2; + const char *args[] = { "push_swap", "42abc" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), true); +} + +TEST(is_invalid_argument, one_argument_max_overflow) { + int argc = 2; + const char *args[] = { "push_swap", "2147483648" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), true); +} + +TEST(is_invalid_argument, one_argument_min_overflow) { + int argc = 2; + const char *args[] = { "push_swap", "-2147483649" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), true); +} + +TEST(is_invalid_argument, multiple_argument_has_duplicate) { + int argc = 5; + const char *args[] = { "push_swap", "1", "3", "5", "+1" }; + char **argv = const_cast(args); + + EXPECT_EQ(is_invalid_argument(argc, argv), true); +}