Skip to content

Commit

Permalink
implement is_invalid_argument
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Jan 11, 2024
1 parent f1e4d2d commit 6f914f7
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 17 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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)"
Expand All @@ -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)
8 changes: 6 additions & 2 deletions include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <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));
}
8 changes: 3 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
}
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);
}

0 comments on commit 6f914f7

Please sign in to comment.