diff --git a/.gitignore b/.gitignore index 5b4cfc979f4ff496c13843a8f6855713f440365a..d250e2019542fe420edbfcd807e80ca8094e72f2 100644 --- a/.gitignore +++ b/.gitignore @@ -37,7 +37,6 @@ *.orig *.rej *~ -puissance4 # Testing files *.gcno @@ -52,6 +51,11 @@ desktop.ini # macOS-specific files .DS_Store - # test files -*.cand \ No newline at end of file +*.cand +*.txt +!src/actual_output.txt + +# binary file +puissance4 +test_board \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d7e1d58f30207dfa553f35a779c41c8fb988d2c1..15bb36541ef4f40367b0dd50e969fc0c3dc7a3cb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -34,6 +34,12 @@ test_smart_ai: - echo "Running smart_ai tests..." - make -C testbed/smart_ai +test_board: + stage: test + script: + - echo "Running board tests..." + - make tests_board + clean: stage: clean script: diff --git a/Makefile b/Makefile index a6096e2bd022f1957ca2ec85c5c76b221d81166a..f8649732e840c9546f7bdbdcc11c1e239bf1fc11 100644 --- a/Makefile +++ b/Makefile @@ -26,4 +26,10 @@ tests_rand_ai: puissance4 $(MAKE) -C testbed/rand_ai tests_smart_ai: puissance4 - $(MAKE) -C testbed/smart_ai \ No newline at end of file + $(MAKE) -C testbed/smart_ai + +generate_board_test: src/test_board.o src/board.o + $(CC) $(CFLAGS) $^ -o src/test_board $(LDLIBS) $(LDFLAGS) + +tests_board: generate_board_test + ./src/test_board \ No newline at end of file diff --git a/puissance4.c b/puissance4.c index 2bb6759a43661d00730618c0d3a34cf3fcd5f81b..9e91dbc364e1b5a22c45e941c1876046539ae704 100644 --- a/puissance4.c +++ b/puissance4.c @@ -52,7 +52,7 @@ int main(int argc, char const* argv[]) { return EXIT_FAILURE; } - init_board(rows, cols, board); + fill_board(rows, cols, board); printf("Board size is %dx%d (rows x col)", rows, cols); print_board(rows, cols, board); @@ -246,69 +246,3 @@ bool does_player_win(int row, int col, int rows, int cols, int** board) { } return false; } - -// char transform_state_to_char(enum BOARD_STATE state) { -// switch (state) { -// case PLAYER1: -// return 'X'; -// break; -// case PLAYER2: -// return 'O'; -// break; -// default: -// return ' '; -// break; -// } -// } - -// void print_board(int rows, int cols, int** board) { -// // Top border of the board -// printf("\n┌"); -// for (int col = 0; col < cols; col++) { -// printf("─"); -// if (col < cols - 1) { -// printf("┬"); -// } -// } -// printf("┐\n"); - -// // Print the board rows -// for (int row = 0; row < rows; row++) { -// // Print left vertical border -// printf("│"); -// // Print data and vertical separators -// for (int col = 0; col < cols; col++) { -// printf("%c", transform_state_to_char(board[row][col])); -// if (col < cols - 1) { -// printf("│"); -// } -// } -// // Print right vertical border -// printf("│\n"); - -// // Print row separator -// if (row < rows - 1) { -// printf("├"); -// for (int col = 0; col < cols; col++) { -// printf("─"); -// if (col < cols - 1) { -// printf("┼"); -// } -// } -// printf("┤\n"); -// } -// } - -// // Print bottom border of the board -// printf("└"); -// for (int col = 0; col < cols; col++) { -// printf("─"); -// if (col < cols - 1) { -// printf("┴"); -// } -// } -// printf("┘\n"); -// for (int i = 1; i < cols + 1; i++) { -// printf(" %d", i); -// } -// } diff --git a/src/actual_output.txt b/src/actual_output.txt new file mode 100644 index 0000000000000000000000000000000000000000..8885cb0d6e401e3a28108ea54c3f63c656356eea --- /dev/null +++ b/src/actual_output.txt @@ -0,0 +1,15 @@ + +┌─┬─┬─┬─┬─┬─┬─┐ +│ │ │ │ │ │ │ │ +├─┼─┼─┼─┼─┼─┼─┤ +│ │ │ │ │ │ │ │ +├─┼─┼─┼─┼─┼─┼─┤ +│ │ │ │ │ │ │ │ +├─┼─┼─┼─┼─┼─┼─┤ +│ │ │ │ │ │ │ │ +├─┼─┼─┼─┼─┼─┼─┤ +│ │ │ │ │ │ │ │ +├─┼─┼─┼─┼─┼─┼─┤ +│ │ │ │ │ │ │ │ +└─┴─┴─┴─┴─┴─┴─┘ + 1 2 3 4 5 6 7 \ No newline at end of file diff --git a/src/board.c b/src/board.c index 432a70a1ddfb0ced52270db3abed450cb19efc4d..4072590dc6d363adef67282e906b9cacc9be070b 100644 --- a/src/board.c +++ b/src/board.c @@ -2,7 +2,7 @@ #include <stdlib.h> #include <stdio.h> -void init_board(int rows, int cols, int** board) { +void fill_board(int rows, int cols, int** board) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { board[i][j] = EMPTY; diff --git a/src/board.h b/src/board.h index d40388d7712881b75478a0ca69b629239d0b2f8a..38ea7654c4bfb02080e145567adf7defdcfc052d 100644 --- a/src/board.h +++ b/src/board.h @@ -6,9 +6,9 @@ enum BOARD_STATE { PLAYER2 = -1 }; -void init_board(int rows, int cols, int** board); -void print_board(int rows, int cols, int** board); int** create_board(int rows, int cols); +void fill_board(int rows, int cols, int** board); +void print_board(int rows, int cols, int** board); int destroy_board(int rows, int** board); char transform_state_to_char(enum BOARD_STATE state); diff --git a/src/test_board.c b/src/test_board.c new file mode 100644 index 0000000000000000000000000000000000000000..050c064b11949e16bf50090020636f65816cfafd --- /dev/null +++ b/src/test_board.c @@ -0,0 +1,108 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <string.h> +#include "../src/board.h" + +#define EXPECTED_OUTPUT "src/expected_output.txt" +#define ACTUAL_OUTPUT "src/actual_output.txt" + +#define GREEN "\033[1;32m" +#define RED "\033[1;31m" +#define RESET "\033[0m" + +void test_destroy_board(); +void test_fill_board(); +// void test_print_board(); +void test_create_board(); + +int main() { + test_create_board(); + test_fill_board(); + test_destroy_board(); + // test_print_board(); + return EXIT_SUCCESS; +} + +void test_destroy_board() { + int rows = 6; + int cols = 7; + int** board = create_board(rows, cols); + if (board == NULL) { + fprintf(stderr, RED "test_destroy_board: FAILED\n" RESET); + exit(EXIT_FAILURE); + } + + fill_board(rows, cols, board); + assert(destroy_board(rows, board) == EXIT_SUCCESS); + printf(GREEN "test_destroy_board: PASSED\n" RESET); +} + +// void test_print_board() { +// int rows = 6; +// int cols = 7; +// int** board = create_board(rows, cols); +// assert(board != NULL && "Board creation failed"); + +// fill_board(rows, cols, board); + +// FILE* expected_output_file = fopen(EXPECTED_OUTPUT, "w"); +// assert(expected_output_file != NULL && "Failed to open expected output file"); +// freopen(EXPECTED_OUTPUT, "w", stdout); +// print_board(rows, cols, board); +// fclose(expected_output_file); + +// freopen("/dev/tty", "w", stdout); + +// FILE* actual_output_file = fopen(EXPECTED_OUTPUT, "r"); +// assert(actual_output_file != NULL && "Failed to open actual output file"); + +// char actual_output[1024] = { 0 }; +// fread(actual_output, sizeof(char), 1024, actual_output_file); +// fclose(actual_output_file); + +// FILE* expected_output_file_read = fopen(EXPECTED_OUTPUT, "r"); +// assert(expected_output_file_read != NULL && "Failed to open expected output file for reading"); + +// char expected_output[1024] = { 0 }; +// fread(expected_output, sizeof(char), 1024, expected_output_file_read); +// fclose(expected_output_file_read); + +// assert(strncmp(actual_output, expected_output, strlen(expected_output)) == 0 +// && "Output does not match expected output"); + +// printf(GREEN "test_print_board: PASSED\n" RESET); +// destroy_board(rows, board); +// } + +void test_create_board() { + int rows = 6; + int cols = 7; + int** board = create_board(rows, cols); + assert(board != NULL && "Board creation failed"); + + for (int i = 0; i < rows; i++) { + assert(board[i] != NULL && "Row allocation failed"); + } + + printf(GREEN "test_create_board: PASSED\n" RESET); + destroy_board(rows, board); +} + +void test_fill_board() { + int rows = 6; + int cols = 7; + int** board = create_board(rows, cols); + assert(board != NULL && "Board creation failed"); + + fill_board(rows, cols, board); + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + assert(board[i][j] == EMPTY); + } + } + + printf(GREEN "test_fill_board: PASSED\n" RESET); + destroy_board(rows, board); +}