-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |