Testcases and cleaner code

This commit is contained in:
Arian Karami 2023-03-26 21:58:14 +02:00
parent 73b1e189c4
commit 47fd54f893
5 changed files with 29 additions and 16 deletions

View File

@ -12,7 +12,7 @@ _OBJ=$(SRC:.c=.o)
DEPS=$(patsubst %,$(INCDIR)/%,$(HEADERS))
OBJ=$(patsubst %,$(OBJDIR)/%,$(_OBJ))
.PHONY: clean install uninstall fclean all re
.PHONY: clean install uninstall fclean all re test
all: $(BUILDDIR)/$(NAME)
@ -40,3 +40,13 @@ uninstall:
$(RM) $(DESTDIR)$(PREFIX)/bin/$(NAME)
re: fclean all
SUCCESS=printf '\n\033[32m%s\033[m\n\n' "Success"
FAIL=printf '\n\033[31m%s\033[m\n\n' "Fail"
test: re
$(BUILDDIR)/$(NAME) && $(FAIL) || $(SUCCESS)
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2 " && $(FAIL) || $(SUCCESS)
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 6" && $(FAIL) || $(SUCCESS)
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 a" && $(FAIL) || $(SUCCESS)
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2" whatever && $(FAIL) || $(SUCCESS)
$(BUILDDIR)/$(NAME) "4 3 2 1 1 2 2 2 4 3 2 1 1 2 2 2" && $(SUCCESS) || $(FAIL)

View File

@ -6,7 +6,7 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
/* Updated: 2023/03/26 20:31:24 by akarami ### ########.fr */
/* Updated: 2023/03/26 21:31:51 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,9 +23,10 @@ int str_len(char *str)
return (i);
}
void put_error(void)
int put_error(int exit_code)
{
write(1, "Error\n", 6);
return (exit_code);
}
int *find_position(int **board, int n, int *pos)

View File

@ -6,7 +6,7 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
/* Updated: 2023/03/26 21:23:02 by akarami ### ########.fr */
/* Updated: 2023/03/26 21:28:15 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,15 +14,22 @@
#include <stdio.h>
#include "include/handle_mem.h"
// Allocates memory and initialized with 0's
int **alloc_mem(int n)
{
int i;
int j;
int **board;
board = malloc(n * 8);
i = -1;
while (++i < n)
{
board[i] = malloc(n * 4);
j = -1;
while (++j < n)
board[i][j] = 0;
}
return (board);
}

View File

@ -6,7 +6,7 @@
/* By: akarami <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 19:35:52 by akarami #+# #+# */
/* Updated: 2023/03/26 19:59:09 by akarami ### ########.fr */
/* Updated: 2023/03/26 21:32:10 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,7 +14,7 @@
# define GEN_UTIL_H
int str_len(char *str);
void put_error(void);
int put_error(int exit_code);
void ft_putchar(char c);
int *find_position(int **board, int n, int *pos);
void print_board(int **board, int n);

View File

@ -6,7 +6,7 @@
/* By: tischmid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/26 06:13:07 by tischmid #+# #+# */
/* Updated: 2023/03/26 20:58:51 by akarami ### ########.fr */
/* Updated: 2023/03/26 21:31:20 by akarami ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
void print_board(int **board, int n);
/*
void init_board(int **board)
{
board[0][0] = 1;
@ -39,6 +40,7 @@ void init_board(int **board)
board[3][3] = 3;
board[3][3] = 3;
}
*/
int main(int argc, char **argv)
{
@ -49,18 +51,11 @@ int main(int argc, char **argv)
n = 4;
borders = malloc(n * 4 * 4);
board = alloc_mem(n);
init_board(board);
print_board(board, n);
if (!handle_input(argc, argv, borders, n))
{
put_error();
return (1);
}
return (put_error(1));
if (!backtrack(board, n, borders))
{
put_error();
return (2);
}
return (put_error(2));
free_mem(borders, board, n);
return (0);
}