From 6895c2b827f23b3adf9b9f2d8f8a0edd7a69a0c4 Mon Sep 17 00:00:00 2001
From: "dario.genga" <dario.genga@etu.hesge.ch>
Date: Tue, 23 Nov 2021 15:18:33 +0100
Subject: [PATCH] Add stack tests

Some of the tests need a verification for the asserts.
---
 stack.c |  25 ++++++++++++++
 test.c  | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 128 insertions(+)

diff --git a/stack.c b/stack.c
index b5030a9..4137848 100644
--- a/stack.c
+++ b/stack.c
@@ -6,3 +6,28 @@
 #include "stack.h"
 #include <stdio.h>
 
+void stack_init(stack *s, int size) {
+
+}
+
+bool stack_is_empty(stack s) {
+    bool is_empty = true;
+
+    return is_empty;
+}
+
+void stack_push(stack *s, int val) {
+
+}
+
+void stack_pop(stack *s, int *val) {
+
+}
+
+void stack_peek(stack s, int *val) {
+
+}
+
+void stack_destroy(stack *s) {
+
+}
diff --git a/test.c b/test.c
index 79981eb..ae29034 100644
--- a/test.c
+++ b/test.c
@@ -7,7 +7,110 @@
 #include <stdbool.h>
 #include <stdio.h>
 
+void test_stack_init(stack *s, int size) {
+    printf("Testing stack_init()...\n");
+    stack_init(s, size);
+    assert(s != NULL);
+    assert(s->size == size);
+    printf("OK.\n");
+}
+
+void test_stack_destroy(stack *s) {
+    printf("Testing stack_destroy()...\n");
+    stack_destroy(s);
+    // TODO: Verify if s is already null or not
+    assert(s->data == NULL);
+    assert(s->top == -1);
+    printf("OK.\n");
+}
+
+void test_stack_is_empty(stack *s) {
+    printf("Testing stack_is_empty()...\n");
+    assert(stack_is_empty(*s) == true);
+    printf("OK.\n");
+}
+
+void test_stack_is_not_empty(stack *s) {
+    printf("Testing stack_is_not_empty()...\n");
+    assert(stack_is_empty(*s) == false);
+    printf("OK.\n");
+}
+
+void test_stack_peek(stack s, int *val) {
+    printf("Testing stack_peek()...\n");
+    stack_peek(s, val);
+    // TODO: Verify test
+    assert(val == &s.top);
+    printf("OK.\n");
+}
+
+void test_stack_push(stack *s, int val) {
+    printf("Testing stack_push()...\n");
+    stack_push(s, val);
+    // Testing push with remaining spaces
+    assert(s->top == val);
+    // Testing push on full stack
+    stack *stack_full;
+    int value_top_full_stack_value = 100;
+    stack_init(stack_full, 1);
+    stack_push(stack_full, value_top_full_stack_value);
+    stack_push(stack_full, val);
+    assert(stack_full->top == value_top_full_stack_value);
+    printf("OK.\n");
+}
+
+void test_stack_pop(stack *s, int *val) {
+    printf("Testing stack_pop()...\n");
+    int top_before_pop;
+    // Test with non-empty stack
+    if (s-> size == 0) {
+        // Add a value if the stack is empty
+        stack_push(s, 5);
+    }
+    top_before_pop = s->top;
+    stack_pop(s, val);
+    // TODO: Verify test
+    assert(val == &top_before_pop);
+
+    // Clear the stack
+    while(s->size != 0) {
+        stack_pop(s, val);
+    }
+    // Test pop when stack is empty
+    val = NULL;
+    stack_pop(s, val);
+    assert(val == NULL);
+
+    printf("OK.\n");
+}
+
 int main() {
+    stack *s, *non_empty_stack;
+    int size = 10, value_to_add = 7;
+    int *val;
+    printf("Starting the tests...\n");
+    // Test init and destroy
+    test_stack_init(s, size);
+    test_stack_destroy(s);
+
+    // Reinitialization
+    stack_init(s, size);
+    stack_init(non_empty_stack, 3);
+    non_empty_stack->data[0] = 1;
+    non_empty_stack->top = non_empty_stack->data[0];
+
+    // Test the other functions
+    test_stack_is_empty(s);
+    test_stack_is_not_empty(non_empty_stack);
+    test_stack_peek(*s, val);
+    test_stack_push(s, value_to_add);
+    test_stack_pop(s, val);
+
+    // Free the memory
+    stack_destroy(s);
+    stack_destroy(non_empty_stack);
+
+    // End the tests
     printf("The tests are completed and were successful !\n");
     return EXIT_SUCCESS;
 }
-- 
GitLab