2025-12-07 18:35:22 +01:00

111 lines
2.1 KiB
C

#include "unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void test_createNode(void) {
int testInt = 26;
StackNode *testNode = createNode(&testInt);
TEST_ASSERT_NOT_NULL(testNode);
TEST_ASSERT_EQUAL_PTR(&testInt, testNode->data);
TEST_ASSERT_NULL(testNode->next);
TEST_ASSERT_NULL(testNode->prev);
free(testNode);
}
void test_pushDataToStack(void) {
int testInt = 27;
StackNode *testNode = push(NULL, &testInt);
TEST_ASSERT_NOT_NULL(testNode);
TEST_ASSERT_EQUAL_PTR(&testInt, testNode->data);
TEST_ASSERT_NULL(testNode->next);
TEST_ASSERT_NULL(testNode->prev);
free(testNode);
}
void test_deleteTopElement(void) {
int testInts[] = {10, 20, 30};
StackNode *stack = NULL;
for (int i = 0; i < 3; i++) {
stack = push(stack, &testInts[i]);
}
TEST_ASSERT_EQUAL_PTR(&testInts[2], stack->data);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(&testInts[1], stack->data);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(&testInts[0], stack->data);
stack = pop(stack);
TEST_ASSERT_NULL(stack);
}
void test_returnData(void) {
int testInts[] = {10, 20, 30};
StackNode *stack = NULL;
for (int i = 0; i < 3; i++) {
stack = push(stack, &testInts[i]);
}
TEST_ASSERT_EQUAL_PTR(&testInts[2], top(stack));
if (stack->next != NULL) {
TEST_ASSERT_EQUAL_PTR(&testInts[1], stack->next->data);
if (stack->next->next != NULL) {
TEST_ASSERT_EQUAL_PTR(&testInts[0], stack->next->next->data);
}
}
StackNode *last = stack;
while (last->next != NULL) {
last = last->next;
}
TEST_ASSERT_NULL(last->next);
}
void test_clearStack(void) {
int testInts[] = {1, 2, 3, 4, 5};
StackNode *stack = NULL;
for (int i = 0; i < 5; i++) {
stack = push(stack, &testInts[i]);
}
clearStack(&stack);
TEST_ASSERT_NULL(stack);
}
void setUp(void) {}
void tearDown(void) {}
int main(void) {
UNITY_BEGIN();
printf("\n------------------------stack test------------------------\n\n");
RUN_TEST(test_createNode);
RUN_TEST(test_pushDataToStack);
RUN_TEST(test_deleteTopElement);
RUN_TEST(test_returnData);
RUN_TEST(test_clearStack);
return UNITY_END();
}