From 4c5933477f9da41bb3612c72f73d28f4cab5fe7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9ophyl?=
Date: Fri, 10 Dec 2021 01:46:14 +0100
Subject: [PATCH] finished history + tests
---
CMakeLists.txt | 22 ++++-
history.c | 69 ++++++++++++---
history.h | 12 ++-
history_test.c | 221 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 308 insertions(+), 16 deletions(-)
create mode 100644 history_test.c
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d9796b0..09c7f15 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,13 +16,17 @@ add_executable(game_text game_text.c)
add_executable(game_test_thoguet game_test_thoguet.c)
add_executable(game_test_thperrier game_test_thperrier.c)
add_executable(game_test_nnunes game_test_nnunes.c)
+add_executable(history_test history_test.c)
#les dépendances sont calculées automatiquement
target_link_libraries(game_text game)
target_link_libraries(game_test_thoguet game)
target_link_libraries(game_test_thperrier game)
target_link_libraries(game_test_nnunes game)
+target_link_libraries(history_test game history)
+
add_library (game game.c game_aux.c)
+add_library(history history.c)
add_test(test_thoguet_dummy ./game_test_thoguet dummy)
add_test(test_thoguet_game_new ./game_test_thoguet game_new)
@@ -52,4 +56,20 @@ add_test(test_nnunes_game_get_state ./game_test_nnunes game_get_state)
add_test(test_nnunes_game_get_flags ./game_test_nnunes game_get_flags)
add_test(test_nnunes_game_is_marked ./game_test_nnunes game_is_marked)
add_test(test_nnunes_game_is_lighted ./game_test_nnunes game_is_lighted)
-add_test(test_nnunes_game_has_error ./game_test_nnunes game_has_error)
\ No newline at end of file
+add_test(test_nnunes_game_has_error ./game_test_nnunes game_has_error)
+
+add_test(test_history_history_alloc ./history_test history_alloc)
+add_test(test_history_create_empty ./history_test history_create_empty)
+add_test(test_history_delete_entire_history ./history_test history_delete_entire_history)
+add_test(test_history_is_empty ./history_test history_is_empty)
+add_test(test_history_next ./history_test history_next)
+add_test(test_history_prev ./history_test history_prev)
+add_test(test_history_insert_first ./history_test history_insert_first)
+add_test(test_history_delete_first ./history_test history_delete_first)
+add_test(test_history_insert_after ./history_test history_insert_after)
+add_test(test_history_delete_after ./history_test history_delete_after)
+add_test(test_history_insert_before ./history_test history_insert_before)
+add_test(test_history_delete_before ./history_test history_delete_before)
+add_test(test_history_last ./history_test history_last)
+add_test(test_history_first ./history_test history_first)
+add_test(test_history_append ./history_test history_append)
\ No newline at end of file
diff --git a/history.c b/history.c
index 5e58035..734c425 100644
--- a/history.c
+++ b/history.c
@@ -77,18 +77,6 @@ history history_insert_first(history h, square state, uint i, uint j) {
return new;
}
-history history_delete_first(history h) {
- if (!history_is_empty(h)) {
- history tmp = h->next;
- history_free(h);
- h = tmp;
- if (!history_is_empty(h)) {
- h->prev = NULL;
- }
- }
- return h;
-}
-
history history_insert_after(history h, history p, square state, uint i, uint j) {
if (history_is_empty(h) || history_is_empty(p))
errPointer();
@@ -105,7 +93,7 @@ history history_insert_after(history h, history p, square state, uint i, uint j)
}
history history_delete_after(history h, history p) {
- if (history_is_empty(h) || history_is_empty(p))
+ if (history_is_empty(p))
errPointer();
if (!history_is_empty(history_next(p))) {
history to_be_deleted = p->next;
@@ -145,4 +133,59 @@ history history_delete_before(history h, history p) {
history_free(to_be_deleted);
}
return h;
+}
+
+history history_last(history h) {
+ if (history_is_empty(h))
+ errPointer();
+ if (history_is_empty(history_next(h)))
+ return h;
+ return history_last(history_next(h));
+}
+
+history history_first(history h) {
+ if (history_is_empty(h))
+ errPointer();
+ if (history_is_empty(history_prev(h)))
+ return h;
+ return history_first(history_prev(h));
+}
+
+history history_delete_first(history h) {
+ if (!history_is_empty(h)) {
+ h = history_first(h);
+ history tmp = h->next;
+ history_free(h);
+ h = tmp;
+ if (!history_is_empty(h)) {
+ h->prev = NULL;
+ }
+ }
+ return h;
+}
+
+history history_append(history h, square state, uint i, uint j) {
+ if (history_is_empty(h))
+ return history_insert_first(h, state, i, j);
+ if (history_is_empty(history_next(h)))
+ return history_insert_after(h, h, state, i, j);
+ history_append(history_next(h), state, i, j);
+ return h;
+}
+
+history history_delete_all_after(history h, history p) {
+ if (history_is_empty(p))
+ return h;
+ if (history_is_empty(history_next(p)))
+ return h;
+ history_delete_all_after(h, history_next(p));
+ return history_delete_after(h, p);
+}
+
+void history_delete_entire_history(history h) {
+ if (history_is_empty(h))
+ return;
+ if (history_is_empty(history_prev(h)))
+ return history_free(history_delete_all_after(h, h));
+ return history_delete_entire_history(history_prev(h));
}
\ No newline at end of file
diff --git a/history.h b/history.h
index e117741..9efc4dc 100644
--- a/history.h
+++ b/history.h
@@ -46,9 +46,8 @@ history history_create_empty(void);
* @brief Delete every element after h
*
* @param h the head of the history
- * @return the new history (NULL)
*/
-history history_delete_entire_history(history h);
+void history_delete_entire_history(history h);
/**
* @brief Test if the given history is empty or not.
@@ -199,4 +198,13 @@ history history_first(history h);
* @return h
*/
history history_append(history h, square state, uint i, uint j);
+
+/**
+ * @brief Delete all the elements after p
+ *
+ * @param h an history
+ * @param p an element of h
+ * @return h
+ */
+history history_delete_all_after(history h, history p);
#endif
\ No newline at end of file
diff --git a/history_test.c b/history_test.c
new file mode 100644
index 0000000..1222520
--- /dev/null
+++ b/history_test.c
@@ -0,0 +1,221 @@
+#include "history.h"
+#include
+#include
+#include
+#include
+#include
+
+bool test_history_alloc(void) {
+ history new = history_alloc();
+ assert(new);
+ history_free(new);
+ return true;
+}
+
+bool test_history_create_empty(void) {
+ assert(history_create_empty() == NULL);
+ return true;
+}
+
+bool test_history_delete_entire_history(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history_insert_after(new, new, S_MARK, 3, 3);
+ assert(new->next);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_is_empty(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ assert(!history_is_empty(new));
+ assert(history_is_empty(history_create_empty()));
+ history_free(new);
+ return true;
+}
+
+bool test_history_next(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ new = history_insert_after(new, new, S_BLACKU, 2, 6);
+ assert(new->next == history_next(new));
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_prev(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ new = history_insert_after(new, new, S_BLACKU, 2, 6);
+ new = history_next(new);
+ assert(new->prev == history_prev(new));
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_insert_first(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history_free(new);
+ return true;
+}
+
+bool test_history_delete_first(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ new = history_insert_after(new, new, S_BLACK3, 2, 5);
+ new = history_next(new);
+ history_delete_first(new);
+ assert(new->prev == NULL);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_insert_after(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ history_insert_after(new, new, S_BLACK2, 4, 3);
+ assert(new->next->next == newafter);
+ assert(new == newafter->prev->prev);
+ assert(new->next->state == S_BLACK2);
+ assert(new->next->i == 4);
+ assert(new->next->j == 3);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_delete_after(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ history_insert_after(new, new, S_BLACK2, 4, 3);
+ history_delete_after(new, new);
+ assert(new->next == newafter);
+ assert(new == newafter->prev);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_insert_before(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ history_insert_before(new, newafter, S_BLACK2, 4, 3);
+ assert(new->next->next == newafter);
+ assert(new == newafter->prev->prev);
+ assert(new->next->state == S_BLACK2);
+ assert(new->next->i == 4);
+ assert(new->next->j == 3);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_delete_before(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ history_insert_after(new, new, S_BLACK2, 4, 3);
+ history_delete_before(new, newafter);
+ assert(new->next == newafter);
+ assert(new == newafter->prev);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_last(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ assert(history_last(new) == newafter);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_first(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history newafter = history_alloc();
+ newafter->prev = new;
+ new->next = newafter;
+ assert(history_first(newafter) == new);
+ history_delete_entire_history(new);
+ return true;
+}
+
+bool test_history_append(void) {
+ history new = history_insert_first(history_create_empty(), S_LIGHTBULB, 4, 3);
+ assert(new);
+ history_append(new, S_BLACK3, 2, 4);
+ history last = history_last(new);
+ assert(new->next == last);
+ assert(last->state == S_BLACK3);
+ assert(last->i == 2);
+ assert(last->j == 4);
+ history_delete_entire_history(new);
+ return true;
+}
+
+int usage(int argc, char* argv[]) {
+ fprintf(stderr, "Usage: %s [<...>]\n", argv[0]);
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char* argv[]) {
+ if (argc == 1) {
+ usage(argc, argv);
+ }
+ fprintf(stderr, "=> Start test \"%s\"\n", argv[1]);
+ bool ok = true;
+ if (strcmp("history_alloc", argv[1]) == 0)
+ ok = test_history_alloc();
+ else if (strcmp("history_create_empty", argv[1]) == 0)
+ ok = test_history_create_empty();
+ else if (strcmp("history_delete_entire_history", argv[1]) == 0)
+ ok = test_history_delete_entire_history();
+ else if (strcmp("history_is_empty", argv[1]) == 0)
+ ok = test_history_is_empty();
+ else if (strcmp("history_next", argv[1]) == 0)
+ ok = test_history_next();
+ else if (strcmp("history_prev", argv[1]) == 0)
+ ok = test_history_prev();
+ else if (strcmp("history_insert_first", argv[1]) == 0)
+ ok = test_history_insert_first();
+ else if (strcmp("history_delete_first", argv[1]) == 0)
+ ok = test_history_delete_first();
+ else if (strcmp("history_insert_after", argv[1]) == 0)
+ ok = test_history_insert_after();
+ else if (strcmp("history_delete_after", argv[1]) == 0)
+ ok = test_history_delete_after();
+ else if (strcmp("history_insert_before", argv[1]) == 0)
+ ok = test_history_insert_before();
+ else if (strcmp("history_delete_before", argv[1]) == 0)
+ ok = test_history_delete_before();
+ else if (strcmp("history_last", argv[1]) == 0)
+ ok = test_history_last();
+ else if (strcmp("history_first", argv[1]) == 0)
+ ok = test_history_first();
+ else if (strcmp("history_append", argv[1]) == 0)
+ ok = test_history_append();
+ else {
+ fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]);
+ exit(EXIT_FAILURE);
+ }
+ // print test result
+ if (ok) {
+ fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]);
+ return EXIT_SUCCESS;
+ } else {
+ fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+}
\ No newline at end of file