From 63a13d1ea536af8ae9e0225f6039e337555c57a5 Mon Sep 17 00:00:00 2001
From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch>
Date: Fri, 17 Jun 2022 20:30:53 +0200
Subject: [PATCH] Added tool init/detroy test and fixed leaks due to unproper
 setup prior test

---
 test.c                    |  1 +
 tool/test/tool-test.c     |  7 +++++++
 tool/test/tool-test.h     |  1 +
 tool/tool.c               |  1 +
 wordle/test/wordle-test.c | 14 +++++++-------
 5 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/test.c b/test.c
index ebb653c..4a0fb48 100644
--- a/test.c
+++ b/test.c
@@ -24,6 +24,7 @@ int main(void) {
     RUN_TEST(test_generate_pattern_should_return_all_0);
 
     // Tool
+    RUN_TEST(test_tool_init_destroy);
     RUN_TEST(test_compute_matches_should_remain_BALAI);
 
     return UNITY_END();
diff --git a/tool/test/tool-test.c b/tool/test/tool-test.c
index 8756485..c25caf7 100644
--- a/tool/test/tool-test.c
+++ b/tool/test/tool-test.c
@@ -1,5 +1,12 @@
 #include "tool-test.h"
 
+void test_tool_init_destroy(void) {
+   set_gamemode(TOOL_ASSISTED);
+   initialize_game();
+
+   destroy_game();
+}
+
 void test_compute_matches_should_remain_BALAI(void) {
     // Arrange
     char **target = malloc(sizeof(char *));
diff --git a/tool/test/tool-test.h b/tool/test/tool-test.h
index 537255e..9762640 100644
--- a/tool/test/tool-test.h
+++ b/tool/test/tool-test.h
@@ -5,6 +5,7 @@
 #include "../../wordle/wordle.h"
 #include "../tool.h"
 
+void test_tool_init_destroy(void);
 void test_compute_matches_should_remain_BALAI(void);
 
 #endif
diff --git a/tool/tool.c b/tool/tool.c
index d377ec0..dd33536 100644
--- a/tool/tool.c
+++ b/tool/tool.c
@@ -191,6 +191,7 @@ void init_tool() {
 void destroy_tool() {
     for (int i = 0; i < remaining_bank_count; i++)
         free(remaining_bank[i]);
+
     free(remaining_bank);
 
     if (possibilities != NULL)
diff --git a/wordle/test/wordle-test.c b/wordle/test/wordle-test.c
index 398cf83..9e099f3 100644
--- a/wordle/test/wordle-test.c
+++ b/wordle/test/wordle-test.c
@@ -1,34 +1,34 @@
 #include "wordle-test.h"
 
 void test_gamemode_should_be_solo(void) {
-    initialize_game();
     set_gamemode(SOLO);
+    initialize_game();
 
     TEST_ASSERT_EQUAL_INT(SOLO, get_gamemode());
     destroy_game();
 }
 
 void test_gamemode_should_be_versus(void) {
-    initialize_game();
     set_gamemode(VERSUS);
+    initialize_game();
 
     TEST_ASSERT_EQUAL_INT(VERSUS, get_gamemode());
     destroy_game();
 }
 
 void test_gamemode_should_be_computer(void) {
-    initialize_game();
     set_gamemode(TOOL_ASSISTED);
+    initialize_game();
 
     TEST_ASSERT_EQUAL_INT(TOOL_ASSISTED, get_gamemode());
     destroy_game();
 }
 
 void test_answer_shoud_be_balai(void) {
+    set_gamemode(SOLO);
     initialize_game();
     char target[WORD_LENGHT + 1] = "BALAI";
 
-    set_gamemode(SOLO);
     set_answer(target);
 
     TEST_ASSERT_EQUAL_STRING(target, get_answer());
@@ -36,8 +36,8 @@ void test_answer_shoud_be_balai(void) {
 }
 
 void test_validate_letter_should_be_false(void) {
-    initialize_game();
     set_gamemode(SOLO);
+    initialize_game();
 
     for (int i = '\0'; i <= '@'; i++)
         TEST_ASSERT_FALSE(validate_letter(i));
@@ -45,8 +45,8 @@ void test_validate_letter_should_be_false(void) {
 }
 
 void test_validate_letter_should_be_true(void) {
-    initialize_game();
     set_gamemode(SOLO);
+    initialize_game();
 
     for (int i = 'a'; i <= 'z'; i++)
         TEST_ASSERT_TRUE(validate_letter(i));
@@ -57,9 +57,9 @@ void test_validate_letter_should_be_true(void) {
 }
 
 void test_generate_pattern_should_return_all_0(void) {
+    set_gamemode(SOLO);
     initialize_game();
     Pattern target[WORD_LENGHT] = {WRONG, WRONG, WRONG, WRONG, WRONG};
-    set_gamemode(SOLO);
 
     set_answer("BALAI");
     set_try("PONTS");
-- 
GitLab