added stack and test file

This commit is contained in:
Thilo 2025-12-09 17:37:26 +01:00
parent 9363ed22aa
commit 6dc857cc63
2 changed files with 133 additions and 11 deletions

View File

@ -35,15 +35,28 @@ $(program_obj_filesobj_files): %.o: %.c
# --------------------------
# Unit Tests
# --------------------------
unitTests:
echo "needs to be implemented"
tets_sources = test_stack.c stack.c
test_objects = $(test_sources:.c=.o)
test_runner: $(test_objects)
$(CC) $(FLAGS) $^ -o $@
unitTests: test_runner
@echo "============================="
@echo "Running Stack Unit Tests..."
@echo "============================="
./test_runner
@echo "============================="
@echo "Stack Unit Tests Finished."
@echo "============================="
# --------------------------
# Clean
# --------------------------
clean:
ifeq ($(OS),Windows_NT)
del /f *.o doble
del /f *.o doble test_runner
else
rm -f *.o doble
rm -f *.o doble test_runner
endif

View File

@ -4,29 +4,138 @@
void test_on_empty_stack(void)
{
StackNode *stack = malloc(sizeof(StackNode));
char *status_a = "leer";
char *status_b = "leer";
StackNode *stack = NULL;
StackNode *a = pop(stack);
void* b = top(stack);
if (a == NULL)
{
status_a = "PASSED";
}
else status_a = "FAILED";
if (b == NULL)
{
status_b = "PASSED";
}
else status_b = "FAILED";
printf("Test on empty stack in pop: %s - Expected: NULL, Was: %p\n",status_a, (void*)a);
printf("Test on empty stack in top: %s - Expected: NULL, Was: %p\n",status_b, b);
printf("%s", stack);
}
void test_push(void)
{
int *test_value = malloc(sizeof(int));
test_value = (int*)1;
StackNode *stack = malloc(sizeof(StackNode));
int a[3] = 0;
StackNode *stack = NULL;
StackNode *new_stack = push(stack, test_value);
for (int i = 1; i < 4; i++)
{
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
printf("Error in allocating memory in test_push!\n");
exit(EXIT_FAILURE);
}
*var_push = i;
stack = push(stack, var_push);
void *top_data = top(stack);
if (top_data != NULL && *(int*)top_data == i) {
a[i-1] = 1;
} else {
a[i-1] = 0;
}
}
if (a[0] == 1 && a[1] == 1 && a[2] == 1)
{
printf("Test push: SUCCEESS!\n");
}
else printf("Test pus: FAILED - Expected: all 1, Was: %d, %d, %d\n", a[0], a[1], a[2]);
stack = clearStack(stack);
}
void test_pop(void)
{
StackNode *stack = NULL;
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
fprintf(stderr, "Error in allocating memory in test_pop! Exiting.\n");
exit(EXIT_FAILURE);
}
*var_push = 1;
stack = push(stack, var_push);
stack = pop(stack);
if (stack == NULL) printf("Test pop: SUCCESS!\n");
else printf("Test pop: FAIL! - Stack is not empty! Head: %p\n", (void*)stack);
stack = clearStack(stack);
}
void test_top(void)
{
StackNode *stack = NULL;
int *var_push = malloc(sizeof(int));
*var_push = 1;
stack = push(stack, var_push);
void *b = top(stack);
if (b != 0 && *(int*)b == 1) printf("Test top: SUCCESS!\n");
else printf("Test top: FAIL! - Expected 1, was %p\n", b);
stack = clearStack(stack);
}
void test_clearStack(void)
{
StackNode *stack = NULL;
for (int i = 1; i < 4; i++)
{
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
fprintf(stderr, "Error in allocating memory in test_clearStack! Exiting.\n");
exit(EXIT_FAILURE);
}
*var_push = i;
stack = push(stack, var_push);
}
stack = clearStack(stack);
void *a = top(stack);
if (a == NULL) printf("Test clear: SUCESS!\n");
else printf("Test clear: FAILED! - Expected: NULL, Was:%p\n", a);
}
int main()
{
printf("============================\nStack Tests\n============================\n");
test_on_empty_stack;
test_on_empty_stack();
test_push();
test_pop();
test_top();
test_clearStack();
return EXIT_SUCCESS;
}