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 checker #30

Merged
merged 1 commit into from
Feb 14, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ build/
push_swap
checker_Mac
checker_linux
checker
tester

!src/checker

# Subject File
en.subject.pdf

Expand Down
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
NAME = push_swap
CHECKER = checker
CFLAGS = -Werror -Wextra -Wall
CXXFLAGS = -std=c++17 -Wall -Wextra -Werror
PROD_FLAGS = -O3
Expand Down Expand Up @@ -42,6 +43,19 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/utils/stack_size.c
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRC))
BONUS_SRC = $(SRC_DIR)/checker/checker_main.c \
$(SRC_DIR)/initialization/check_args.c \
$(SRC_DIR)/initialization/exit_with_error.c \
$(SRC_DIR)/initialization/generate_stack.c \
$(SRC_DIR)/sort/is_sorted_stack.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
$(SRC_DIR)/stack_operations/reverse_rotate.c \
$(SRC_DIR)/utils/get_content.c \
$(SRC_DIR)/utils/stack_size.c
BONUS_OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(BONUS_SRC))
BONUS_DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(BONUS_SRC))
OBJ_FILTER_MAIN = $(filter-out $(BUILD_DIR)/main.o, $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)))
TEST_NAME = tester
TEST_SRC = $(TEST_DIR)/test_check_args.cpp \
Expand Down Expand Up @@ -77,11 +91,20 @@ all: CFLAGS += $(PROD_FLAGS)
all: title
all: $(NAME)

bonus: CFLAGS += $(PROD_FLAGS)
bonus: bonus_title
bonus: $(CHECKER)

$(NAME): $(OBJ)
@printf "\n"
@make -C $(LIBFT_DIR)
@$(CC) $(CFLAGS) $^ -L $(LIBFT_DIR) -lft -o $@

$(CHECKER): $(BONUS_OBJ)
@printf "\n"
@make -C $(LIBFT_DIR)
@$(CC) $(CFLAGS) $^ -L $(LIBFT_DIR) -lft -o $@

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(@D)
@$(CC) $(CFLAGS) $(INCLUDE) $(DEPFLAGS) -c $< -o $@
Expand Down Expand Up @@ -151,6 +174,9 @@ norm:
title:
@echo "$(BLUE)push_swap$(RESET)"

bonus_title:
@echo "$(BLUE)bonus$(RESET)"

.PHONY: all clean fclean re dev redev leak releak test norm title

-include $(DEP)
19 changes: 19 additions & 0 deletions include/checker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 10:26:54 by reasuke #+# #+# */
/* Updated: 2024/02/14 10:29:44 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CHECKER_H
# define CHECKER_H

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

#endif
95 changes: 95 additions & 0 deletions src/checker/checker_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/14 10:25:27 by reasuke #+# #+# */
/* Updated: 2024/02/14 13:50:38 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "checker.h"
#include "initialization.h"
#include "sort.h"
#include "stack_operations.h"

static bool _is_operation_present(const char *op)
{
bool present;

present = false;
present |= !ft_strcmp(op, "sa\n");
present |= !ft_strcmp(op, "sb\n");
present |= !ft_strcmp(op, "ss\n");
present |= !ft_strcmp(op, "pa\n");
present |= !ft_strcmp(op, "pb\n");
present |= !ft_strcmp(op, "ra\n");
present |= !ft_strcmp(op, "rb\n");
present |= !ft_strcmp(op, "rr\n");
present |= !ft_strcmp(op, "rra\n");
present |= !ft_strcmp(op, "rrb\n");
present |= !ft_strcmp(op, "rrr\n");
return (present);
}

static void _stack_operation(t_stack **p_a, t_stack **p_b, const char *op)
{
if (!ft_strcmp(op, "sa\n") || !ft_strcmp(op, "ss\n"))
swap_stack(p_a);
if (!ft_strcmp(op, "sb\n") || !ft_strcmp(op, "ss\n"))
swap_stack(p_b);
if (!ft_strcmp(op, "pa\n"))
push_stack(p_b, p_a);
if (!ft_strcmp(op, "pb\n"))
push_stack(p_a, p_b);
if (!ft_strcmp(op, "ra\n") || !ft_strcmp(op, "rr\n"))
rotate_stack(p_a);
if (!ft_strcmp(op, "rb\n") || !ft_strcmp(op, "rr\n"))
rotate_stack(p_b);
if (!ft_strcmp(op, "rra\n") || !ft_strcmp(op, "rrr\n"))
reverse_rotate_stack(p_a);
if (!ft_strcmp(op, "rrb\n") || !ft_strcmp(op, "rrr\n"))
reverse_rotate_stack(p_b);
}

static void _sort_based_on_operation(t_stack **p_a, t_stack **p_b)
{
char *op;

while (true)
{
op = get_next_line(STDIN_FILENO);
if (!op)
break ;
if (!_is_operation_present(op))
exit_with_error();
_stack_operation(p_a, p_b, op);
}
}

static void _check_sorted(t_stack *st_a, int size_a)
{
if (stack_size(st_a) != size_a)
ft_putendl_fd("KO", STDOUT_FILENO);
if (is_sorted_stack(st_a))
ft_putendl_fd("OK", STDOUT_FILENO);
else
ft_putendl_fd("KO", STDOUT_FILENO);
}

int main(int argc, char **argv)
{
t_stack *st_a;
t_stack *st_b;

if (argc == 1)
return (0);
check_args(argc, argv);
st_a = generate_stack(argc, argv);
st_b = NULL;
_sort_based_on_operation(&st_a, &st_b);
_check_sorted(st_a, argc - 1);
return (0);
}
Loading