#include "stack.h"
#include "unity/unity.h"
#include "unity/unity_internals.h"

/* wird nicht mehr gebraucht

// Ein Blick in den Stack - listet den Stack-Inhalt der Reihe nach auf
void inspectStack(StackNode *stack)
{    
    if (stack != NULL)
    {
        printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->data);
        while (stack->next != NULL)
        {
            printf(" %d", *(int*)stack->next->data);
            stack = stack->next;
        }
        putchar('\n');
    }
    else
    {
        printf("Der Stack ist leer\n");
    }
}
*/

// Setup-Funktionen von Unity, die zumindest als Platzhalter definiert sein müssen
void setUp(void) {};
void tearDown(void) {};

void test_createNodeAbortsOnZeroData(void)
{
    void *data = NULL;
    StackNode *stack = NULL;
    stack = push(stack, data);
    TEST_ASSERT_EQUAL_PTR(stack, NULL);

    clearStack(stack);
}

void test_createdNodeIsFirstInList(void)
{
    int data = 42;
    StackNode *stack = NULL;
    stack = push(stack, &data);
    TEST_ASSERT_NOT_NULL(stack);
    TEST_ASSERT_EQUAL_PTR(&data, stack->data);
    TEST_ASSERT_NULL(stack->next);

    clearStack(stack);
}

void test_createNodeCorrectOrder(void)
{
    int data[3] = {0, 1, 2};
    StackNode *stack = NULL;
    stack = push(stack, &data[2]);
    stack = push(stack, &data[1]);
    stack = push(stack, &data[0]);

    TEST_ASSERT_NOT_NULL(stack);
    TEST_ASSERT_EQUAL_PTR(&data[0], stack->data);
    TEST_ASSERT_EQUAL_PTR(&data[1], stack->next->data);
    TEST_ASSERT_EQUAL_PTR(&data[2], stack->next->next->data);
    TEST_ASSERT_NULL(stack->next->next->next);

    clearStack(stack);
}

void test_removeNodeAbortsOnZero(void)
{
    StackNode *stack = NULL;
    TEST_ASSERT_NULL(pop(stack));

    clearStack(stack);
}

void test_removeOnlyNodeEmptysList(void)
{
    int data = 42;
    StackNode *stack = NULL;
    stack = pop(push(stack, &data));

    TEST_ASSERT_NULL(stack);

    clearStack(stack);
}

void test_removeNodeRemovesFirst(void)
{
    int data[2] = {0, 1};
    StackNode *stack = NULL;
    stack = push(stack, &data[1]);
    stack = push(stack, &data[0]);
    stack = pop(stack);

    TEST_ASSERT_NOT_NULL(stack);
    TEST_ASSERT_EQUAL_PTR(&data[1], stack->data);
    TEST_ASSERT_NULL(stack->next);

    clearStack(stack);
}

void test_outputTopFailsOnZero(void)
{
    StackNode *stack = NULL;
    
    TEST_ASSERT_NULL(top(stack));

    clearStack(stack);
}

void test_outputTopCorrect(void)
{
    int data[2] = {0, 1};
    StackNode *stack = NULL;
    stack = push(stack, &data[1]);
    stack = push(stack, &data[0]);

    TEST_ASSERT_NOT_NULL(stack);
    TEST_ASSERT_EQUAL_PTR(top(stack), stack->data);

    clearStack(stack);
}


/* Speicher freigabe lässt sich nicht mit Unity testen, am besten Programm mit valgrind ausführen
void test_clearStackFreesMemory(void)
{
    int data[2] = {22, 17};
    StackNode *lowNode = (StackNode *)malloc(sizeof(StackNode));
    lowNode->next = NULL;
    lowNode->data = &data[0];
    StackNode *highNode = (StackNode *)malloc(sizeof(StackNode));
    highNode->next = lowNode;
    highNode->data = &data[1];
    StackNode *stack = highNode;

    clearStack(stack);

    TEST_ASSERT_NULL(lowNode);
    TEST_ASSERT_NULL(highNode);
    TEST_ASSERT_NULL(stack);
}
*/

void test_push_should_add_new_node_at_top(void)
{
    StackNode *stack = NULL;

    int value = 42;
    stack = push(stack, &value);

    TEST_ASSERT_NOT_NULL(stack);
    TEST_ASSERT_EQUAL_PTR(&value, stack->data);
    TEST_ASSERT_NULL(stack->next);

    clearStack(stack);
}

void test_push_multiple_should_chain_nodes_correctly(void)
{
    int a = 1, b = 2;
    
    StackNode *first = push(NULL, &a);
    
    StackNode *second = push(first, &b);

    TEST_ASSERT_EQUAL_PTR(&b, second->data);
    TEST_ASSERT_EQUAL_PTR(first, second->next);
    TEST_ASSERT_EQUAL_PTR(&a, first->data);

    clearStack(second);
}

void test_top_should_return_null_on_empty_stack(void)
{
    StackNode *stack = NULL;
    TEST_ASSERT_NULL(top(stack));
}

void test_top_should_return_data_of_existing_node(void)
{
    StackNode node;
    int x = 99;

    node.data = &x;
    node.next = NULL;

    TEST_ASSERT_EQUAL_PTR(&x, top(&node));
}

void test_pop_should_return_null_when_stack_empty(void)
{
    StackNode *stack = NULL;
    TEST_ASSERT_NULL(pop(stack));
}

void test_pop_should_remove_single_element(void)
{
    StackNode *stack = malloc(sizeof(StackNode));
    int x = 7;

    stack->data = &x;
    stack->next = NULL;

    StackNode *result = pop(stack);

    TEST_ASSERT_NULL(result); // Kein Element mehr übrig
}

void test_pop_should_remove_top_node_only(void)
{
    // Manuell verkettete Liste aufbauen
    StackNode *n1 = malloc(sizeof(StackNode));
    StackNode *n2 = malloc(sizeof(StackNode));
    int a = 1, b = 2;

    n1->data = &a;
    n1->next = NULL;

    n2->data = &b;
    n2->next = n1;

    StackNode *result = pop(n2);

    TEST_ASSERT_EQUAL_PTR(n1, result);

    clearStack(result);
}

int main()
{
    UNITY_BEGIN();

    printf("\n============================\n Stack tests\n============================\n");
    printf("-> Create Nodes (push)\n");
    RUN_TEST(test_createNodeAbortsOnZeroData);
    RUN_TEST(test_createdNodeIsFirstInList);
    RUN_TEST(test_createNodeCorrectOrder);

    printf("\n-> Remove Nodes (pop)\n");
    RUN_TEST(test_removeNodeAbortsOnZero);
    RUN_TEST(test_removeOnlyNodeEmptysList);
    RUN_TEST(test_removeNodeRemovesFirst);

    printf("\n-> Output Top (top)\n");
    RUN_TEST(test_outputTopFailsOnZero);
    RUN_TEST(test_outputTopCorrect);

    RUN_TEST(test_push_should_add_new_node_at_top);
    RUN_TEST(test_push_multiple_should_chain_nodes_correctly);
    RUN_TEST(test_top_should_return_null_on_empty_stack);
    RUN_TEST(test_top_should_return_data_of_existing_node);
    RUN_TEST(test_pop_should_return_null_when_stack_empty);
    RUN_TEST(test_pop_should_remove_single_element);
    RUN_TEST(test_pop_should_remove_top_node_only);

    return UNITY_END();
}