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

Map Validations #2

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
63 changes: 47 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: jovicto2 <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/11/20 14:56:17 by jovicto2 #+# #+# #
# Updated: 2024/11/20 14:56:26 by jovicto2 ### ########.org.br #
# #
# **************************************************************************** #

# Variables
NAME := cub3D

Expand All @@ -8,10 +20,13 @@ LIBRARIES_DIR := libraries/
LIBFT_DIR := $(addprefix $(LIBRARIES_DIR), libft/)
LIBFT_FILE := libft.a

MAKE := make
MAKE_LIBS := $(MAKE) -sC
TEST_NAME := test_cub3D
TEST_DIR := tests/

MAKE := make -s
MAKE_LIBS := $(MAKE) -C
CC := cc
CFLAGS := -Wall -Wextra -Werror -Wunreachable-code -Ofast -g3
CFLAGS := -Wall -Wextra -Wunreachable-code -O3 -g3 -Wno-unused-result
MKDIR := mkdir -p
RM := rm -rf

Expand All @@ -24,24 +39,33 @@ LIBFT := $(addprefix $(LIBFT_DIR), $(LIBFT_FILE))
LIBFT_HEADER := $(addprefix $(LIBFT_DIR), includes/libft.h)
HEADERS := -I $(HEADERS_DIR)

MESSAGE1 := "------------------Compiling cub3d Objects!-------------------"
MESSAGE2 := "---------------Objects Compiled Successfully!----------------"
MESSAGE3 := "-----------------'.cub3d' File Compiled Successfully!----------------"
# Tests
TEST_FILES := test_execution

TEST_SOURCES := $(addprefix $(TEST_DIR), $(addsuffix .c, $(TEST_FILES)))
TEST_FLAGS := -lcriterion

# Colors
RED := \033[0;31m
GREEN := \033[0;32m
RESET := \033[0m

# Messages
MESSAGE2 := "$(GREEN)Compiled $(NAME) successfully!$(RESET)"
MESSAGE3 := "$(GREEN)\nRun './$(NAME) <valid_map.cub>' to execute.$(RESET)\n"
MESSAGE4 := "$(GREEN)Cleaned objects from $(NAME) successfully!$(RESET)"
MESSAGE5 := "$(GREEN)Removed $(NAME) successfully!$(RESET)"

# Rules
.PHONY: all clean fclean re bonus

all: $(NAME)

$(NAME): $(LIBFT) $(OBJECTS)
@ echo ' '
@ echo $(MESSAGE1)
@ echo ' '
@echo $(MESSAGE1)
@$(CC) $(CFLAGS) $^ $(LIBFT) -o $(NAME) $(INCLUDES) $(LFLAGS)
@ echo $(MESSAGE2)
@ echo ' '
@ echo $(MESSAGE3)
@ echo ' '
@echo $(MESSAGE2)
@echo $(MESSAGE3)

cleanlibft:
@$(MAKE_LIBS) $(LIBFT_DIR) clean
Expand All @@ -50,13 +74,15 @@ fcleanlibft:
@$(MAKE_LIBS) $(LIBFT_DIR) fclean

clean: cleanlibft
@ rm -rf $(OBJECTS_DIR)
@$(RM) $(OBJECTS_DIR)
@echo $(MESSAGE4)

fclean: clean fcleanlibft
@$(RM) $(NAME)
@echo $(MESSAGE5)

re: fclean
@$(MAKE) -s
@$(MAKE)

bonus: all

Expand All @@ -67,4 +93,9 @@ $(OBJECTS_DIR)%.o: $(SOURCES_DIR)%.c
libraries: $(LIBFT)

$(LIBFT):
@$(MAKE_LIBS) $(LIBFT_DIR)
@$(MAKE_LIBS) $(LIBFT_DIR)

test: $(NAME) $(TEST_SOURCES)
@$(CC) $(CFLAGS) $(TEST_FLAGS) $(TEST_SOURCES) $(LIBFT) -o $(TEST_NAME) $(HEADERS)
@./$(TEST_NAME)
@$(RM) $(TEST_NAME)
9 changes: 6 additions & 3 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
# define CUB3D_H

# include "../libraries/libft/includes/libft.h"
# include <fcntl.h>
# include <sys/types.h>
# include <sys/wait.h>

# define MSG_ERROR "\033[1;41mError!\033[0m\n\033[0;31m"
# define MSG_LEN_ERROR 25
# define MSG_ERROR "\033[1;41mError\033[0m\n\033[0;31m"
# define MSG_LEN_ERROR 24

# define MSG_ARG_COUNT "Arguments: Invalid number of arguments.\n\tUsage: ./cub3d <map.cub>"
# define MSG_ARG_COUNT " Arguments: Invalid number of arguments.\n Usage: ./cub3d <map.cub>"
# define MSG_INV_EXEC "Invalid .cub map file\n"
# define MSG_INV_TEXTURES "Invalid texture configurations\n"
# define MSG_INV_COLORS "Invalid RGB colors configurations\n"
Expand Down
20 changes: 17 additions & 3 deletions sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: pmelo-ca <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/24 22:54:02 by pmelo-ca #+# #+# */
/* Updated: 2024/09/25 12:49:29 by pmelo-ca ### ########.fr */
/* Updated: 2024/11/13 23:08:10 by jovicto2 ### ########.org.br */
/* */
/* ************************************************************************** */

Expand All @@ -16,8 +16,22 @@ int main(int argc, char **argv)
{
// t_scene scene;
(void)argv;
if (argc != 2)
handle_error(MSG_ARG_COUNT);
// char *i = malloc(33);
// ft_memset(i, 0, 33);
// i[0] = '0';
// printf("still reachable main: %c\n", *i);
// i = malloc(33);
// ft_memset(i, 0, 33);
// printf("possibly lost main: %c\n", *i);
// ssize_t f = open("Makefile", O_RDONLY);
// pid_t pido = fork();
// if (pido != 0)
// execl("/bin/ls", "ls", "42", NULL);
// else {
if (argc != 2)
handle_error(MSG_ARG_COUNT);
// wait(NULL);
// }
// load_scene(&scene, ++argv);
return (EXIT_SUCCESS);
}
Expand Down
103 changes: 103 additions & 0 deletions tests/test_execution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 15:12:37 by jovicto2 #+# #+# */
/* Updated: 2024/11/20 15:12:41 by jovicto2 ### ########.org.br */
/* */
/* ************************************************************************** */

//TODO: change main
//TODO: test log with cube and args
//TODO: check redirect for cub outputs
//TODO: separate tests made in main to a test file
//TODO: update valgrind to last version

#include <criterion/criterion.h>
#include "../includes/cub3d.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <valgrind/valgrind.h>

void close_all_fds_except_std() {
long max_fds = sysconf(_SC_OPEN_MAX);
if (max_fds == -1) {
perror("sysconf failed");
return;
}

for (int fd = 3; fd < max_fds; ++fd) {// Skip stdin(0), stdout(1), stderr(2)
close(fd);
}
}

int valgrind_exec(const char *command) {
pid_t pid = fork();
if (pid == -1) {
perror("fork failed");
return -1;
}

if (pid == 0) { // Child process
close_all_fds_except_std(); // Close all FDs except std
execl("/bin/sh", "sh", "-c", command, NULL);
}

// Parent process waits for child
int status;
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid failed");
return -1;
}

return status;
}

Test(end_to_end, test_execution_with_no_args)
{
const char *command = "valgrind "
"--leak-check=full "
"--show-leak-kinds=all "
"--errors-for-leak-kinds=all "
"--track-origins=yes "
// "--track-fds=yes "
"--trace-children=yes "
"--quiet "
"./cub3D "
"1> tests/cub3d_stdout.log ";
// "2> tests/cub3d_stderr.log "
// "2> tests/valgrind_output.log";

int status = valgrind_exec(command);

cr_assert_neq(status, -1, "system() call failed");

cr_assert(WIFEXITED(status), "Valgrind did not exit normally");

FILE *log_file = fopen("tests/valgrind_output.log", "r");
cr_assert_not_null(log_file, "Failed to open Valgrind output file");

fseek(log_file, 0, SEEK_END);
long log_size = ftell(log_file);
fseek(log_file, 0, SEEK_SET);

char *valgrind_output = malloc(log_size + 1);
fread(valgrind_output, 1, log_size, log_file);
valgrind_output[log_size] = '\0';

fclose(log_file);
// remove("tests/valgrind_output.txt");

cr_assert(!log_size,
"Valgrind detected memory leaks:\n%s", valgrind_output);

int exit_status = WEXITSTATUS(status);
cr_assert_eq(exit_status, EXIT_FAILURE,
"Program should return EXIT_FAILURE with no arguments");
}