From 6f914f70b946589e9902b17e5e35f6dc1510a8d8 Mon Sep 17 00:00:00 2001 From: rask24 Date: Thu, 11 Jan 2024 16:25:24 +0900 Subject: [PATCH] implement is_invalid_argument --- Makefile | 9 ++-- include/push_swap.h | 8 +++- src/is_invalid_argument.c | 72 +++++++++++++++++++++++++++++++ src/main.c | 8 ++-- test/test.cpp | 7 --- test/test_is_invalid_argument.cpp | 55 +++++++++++++++++++++++ 6 files changed, 142 insertions(+), 17 deletions(-) create mode 100644 src/is_invalid_argument.c delete mode 100644 test/test.cpp create mode 100644 test/test_is_invalid_argument.cpp diff --git a/Makefile b/Makefile index 00202c0..cbb9af6 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ BUILD_DIR = ./build INC_DIR = ./include LIBFT_DIR = ./libft 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)) @@ -14,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 @@ -53,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)" @@ -73,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 73489d1..692d942 100644 --- a/include/push_swap.h +++ b/include/push_swap.h @@ -6,21 +6,25 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ -/* Updated: 2024/01/11 14:39:54 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 ffa310f..79b9ee1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:36 by reasuke #+# #+# */ -/* Updated: 2024/01/11 14:42:21 by reasuke ### ########.fr */ +/* Updated: 2024/01/11 15:56:52 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,7 @@ int main(int argc, char **argv) { if (argc < 2) return (0); - // if (!is_valid_argument()) - exit_with_error(); - // TODO: delete - (void)argv; + if (is_invalid_argument(argc, argv)) + exit_with_error(); return (0); } 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); +}