Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement argument checker #3

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ 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
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
Expand All @@ -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)
Expand All @@ -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)"
Expand All @@ -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)
10 changes: 8 additions & 2 deletions include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <unistd.h>
# include <limits.h>
# include <stdbool.h>
# include <stdlib.h>
# include <unistd.h>

// TODO: delete
# include <stdio.h>

# include "../libft/libft.h"

bool is_invalid_argument(int argc, char **argv);

void exit_with_error(void);

#endif
72 changes: 72 additions & 0 deletions src/is_invalid_argument.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_invalid_argument.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}
9 changes: 6 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
19 changes: 19 additions & 0 deletions src/utils/exit_with_error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit_with_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
7 changes: 0 additions & 7 deletions test/test.cpp

This file was deleted.

55 changes: 55 additions & 0 deletions test/test_is_invalid_argument.cpp
Original file line number Diff line number Diff line change
@@ -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<char **>(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<char **>(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<char **>(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<char **>(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<char **>(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<char **>(args);

EXPECT_EQ(is_invalid_argument(argc, argv), true);
}
Loading