From f1a4de61bb02120b3637297f61f1f307e1cd321a Mon Sep 17 00:00:00 2001 From: Sabrina <sabrinalap98@gmail.com> Date: Fri, 4 Apr 2025 21:30:26 +0200 Subject: [PATCH] add Unit tests --- UnitTests/Makefile | 6 ++ UnitTests/unitTests.c | 154 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 UnitTests/Makefile create mode 100644 UnitTests/unitTests.c diff --git a/UnitTests/Makefile b/UnitTests/Makefile new file mode 100644 index 0000000..69f006e --- /dev/null +++ b/UnitTests/Makefile @@ -0,0 +1,6 @@ +unitTests: unitTests.o + gcc unitTests.o -o unit_tests + + +unitTests.o : unitTests.c + gcc -Wall -Wextra -c unitTests.c \ No newline at end of file diff --git a/UnitTests/unitTests.c b/UnitTests/unitTests.c new file mode 100644 index 0000000..0dac56e --- /dev/null +++ b/UnitTests/unitTests.c @@ -0,0 +1,154 @@ +#include "../src/board.h" +#include "../src/winnerCheck.h" +#include "../src/smartAI.h" +#include <assert.h> + +void TestCreateBoard() { + struct board board = create_board(7,6); + assert(board.col != 0 && "Fail of board creation"); + assert(board.line != 0 && "Fail of board creation"); + assert(board.last_pos_x != -1 && "Fail of board creation"); + assert(board.last_pos_y != -1 && "Fail of board creation"); + assert(board.last_symbole != Vide && "Fail of board creation"); + printf("Sucess of board creation\n"); +} + +void TestInitBoard() { + struct board board = create_board(7,6); + init_board(&board); + for(int i = 0; i < 6; i++){ + for(int j = 0; j < 7; j++){ + assert(board.data[i][j] != Vide && "Fail of init case"); + + } + } + printf("Success of init board\n"); +} + +void TestCurrentLine() { + struct board board = create_board(7,6); + init_board(&board); + int no_col1 = 2; + int no_col2 = 7; + board.data[5][no_col1] = Croix; + board.data[4][no_col1] = Cercle; + assert(current_line(board, no_col1) == 3 && "Fail of get line to column"); + assert(current_line(board, no_col2) == 5 && "Fail of get line to column"); + printf("Success of get lines to column %d and %d\n", no_col1,no_col2); +} + +void TestFullBoard() { + struct board board = create_board(7,6); + init_board(&board); + for(int i = 0; i < 6; i++){ + for(int j = 0; j < 7; j++){ + board.data[i][j] = Croix; + } + } + assert(is_full_board(board) == true && "Fail to check if the board is full"); + printf("Success to check if the board is full\n"); +} + +void TestCheckRowof4() { + struct board board = create_board(7,6); + init_board(&board); + board.data[5][0] = Croix; + board.data[5][1] = Croix; + board.data[5][2] = Croix; + board.data[5][3] = Croix; + board.data[4][0] = Cercle; + board.data[4][1] = Cercle; + board.data[4][2] = Cercle; + assert(row_of_four(&board) == false && "Fail to check a row of 4\n"); + printf("Success to check a row of 4\n"); +} + +void TestCheckColof4() { + struct board board = create_board(7,6); + init_board(&board); + board.data[5][4] = Croix; + board.data[4][4] = Croix; + board.data[3][4] = Croix; + board.data[2][4] = Croix; + board.data[5][0] = Cercle; + board.data[4][0] = Cercle; + board.data[3][0] = Cercle; + assert(col_of_four(&board) == false && "Fail to check a col of 4\n"); + printf("Success to check a col of 4\n"); +} + +void TestCheckDiagRLof4() { + struct board board = create_board(7,6); + init_board(&board); + board.data[5][0] = Croix; + board.data[5][2] = Croix; + board.data[4][1] = Croix; + board.data[4][2] = Croix; + board.data[3][2] = Croix; + board.data[2][3] = Croix; + board.data[4][0] = Cercle; + board.data[5][1] = Cercle; + board.data[5][3] = Cercle; + board.data[4][3] = Cercle; + board.data[3][3] = Cercle; + assert(diag_of_four_right_left(&board) == false && "Fail to check a diag RL of 4\n"); + printf("Success to check a diag RL of 4\n"); +} + +void TestCheckDiagLRof4() { + struct board board = create_board(7,6); + init_board(&board); + board.data[5][0] = Croix; + board.data[3][0] = Croix; + board.data[5][1] = Croix; + board.data[4][1] = Croix; + board.data[5][2] = Croix; + board.data[4][0] = Cercle; + board.data[2][0] = Cercle; + board.data[3][1] = Cercle; + board.data[4][2] = Cercle; + board.data[5][3] = Cercle; + assert(diag_of_four_left_right(&board) == false && "Fail to check a diag LR of 4\n"); + printf("Success to check a dia LR of 4\n"); +} + +void TestSimulateWin() { + struct board board = create_board(7,6); + init_board(&board); + board.data[5][0] = Croix; + board.data[4][0] = Croix; + board.data[4][1] = Croix; + board.data[4][2] = Croix; + board.data[5][1] = Cercle; + board.data[5][2] = Cercle; + board.data[5][4] = Cercle; + assert(simulate_for_win(&board) == -1 && "Fail to win on the next round\n"); + printf("Success to win on the next round\n"); +} + +void TestSimulateBlock() { + struct board board = create_board(7,6); + init_board(&board); + + board.data[5][0] = Croix; + board.data[4][0] = Croix; + board.data[3][0] = Croix; + board.data[5][1] = Cercle; + board.data[5][4] = Cercle; + assert(simulate_for_block_player(&board) == -1 && "Fail to block the player\n"); + printf("Success to block the player\n"); +} + +int main(){ + TestCreateBoard(); + TestInitBoard(); + TestFullBoard(); + TestCurrentLine(); + TestCheckRowof4(); + TestCheckColof4(); + TestCheckDiagLRof4(); + TestCheckDiagRLof4(); + TestSimulateWin(); + TestSimulateBlock(); + return EXIT_SUCCESS; +} \ No newline at end of file -- GitLab