2025-12-15 22:58:45 +01:00

114 lines
2.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#include "unity/unity.h"
void test_pushFailsOnNullPointer(StackNode *stack, void *data)
{
StackNode *test = push(NULL, data);
if (test != 0)
{
printf("Pass test_pushFailsOnNullPointerStack\n");
}
else
printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED StackNode\n");
test = push(stack, NULL);
if (test == 0)
{
printf("Pass test_pushFailsOnNullPointerData\n");
return;
}
printf("Did Not Pass test_pushFailsOnNullPointerData EXPECTED 0\n");
return;
}
void test_popFailsOnNullPointer()
{ // pop on null returns NULL
StackNode *test = pop(NULL);
if (test == 0)
{
printf("Pass test_popFailsOnNullPointerStack\n");
return;
}
printf("Did Not Pass test_popFailsOnNullPointerStack EXPECTED 0\n");
return;
}
void test_topFailsOnNullPointer()
{
int *test = (int *)top(NULL);
if (test == 0)
{
printf("Pass test_topFailsOnNullPointerStack\n");
return;
}
printf("Did Not Pass test_topFailsOnNullPointerStack EXPECTED 0\n");
return;
}
void setUp(void)
{
}
void tearDown(void)
{
}
int main()
{
int test0 = 3;
int *dataTest0 = &test0;
StackNode *stack0 = push(NULL, dataTest0);
char test1[5] = "test\0";
char *dataTest1 = test1;
float test2 = 3.14;
float *dataTest2 = &test2;
printf("============================\nstack tests\n============================\n");
test_pushFailsOnNullPointer(stack0, dataTest0);
test_popFailsOnNullPointer();
test_topFailsOnNullPointer();
StackNode *stack1 = push(stack0, dataTest1);
if (strcmp(stack1->data, dataTest1) == 0)
{
printf("Pass test_pushString\n");
}
else
printf("Fails test_pushString\n expected: %s\n was: %s\n", dataTest1, (char *)(stack1->data));
StackNode *stack2 = push(stack1, dataTest2);
if (stack2->data == dataTest2)
{
printf("Pass test_pushFloat\n");
}
else
printf("Fails test_pushFloat\n expected: %f\n was: %f\n", *dataTest2, *(float *)(stack2->data));
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (size_t i = 0; i < 10; i++)
{
stack2 = push(stack2, &array[i]);
}
for (size_t i = 0; i < 10; i++)
{
int *data = (int *)top(stack2);
printf("%d\n", *data);
stack2 = pop(stack2);
}
clearStack(stack2);
return EXIT_SUCCESS;
}