stackTest V1

This commit is contained in:
Thomas Rauh 2025-11-18 18:44:14 +01:00
parent 4012f53a37
commit 51fcb12b63
4 changed files with 58 additions and 6 deletions

View File

@ -26,6 +26,9 @@ $(program_obj_filesobj_files): %.o: %.c
unitTests:
echo "needs to be implemented"
stackTests: stack.o stackTests.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests stackTests.c stack.o $(unityfolder)/unity.c
# --------------------------
# Clean
# --------------------------

View File

@ -10,24 +10,45 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
StackNode *new=malloc(sizeof(StackNode));
if(new ==NULL){
return new;
}
new->data = data;
new->dannach = stack;
return new;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
{
StackNode *new=malloc(sizeof(StackNode));
if(new ==NULL){
return new;
}
new = stack->dannach;
stack->dannach = NULL;
free(stack->data);
free(stack);
return new;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
if(stack == NULL){
return;
}
clearStack(stack->dannach);
free(stack->dannach);
free(stack->data);
free(stack);
}

View File

@ -10,9 +10,9 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct{
int items[MAX_STACK_SIZE];
int top;
typedef struct Stack{
void* data ;
StackNode* dannach;
}StackNode;
// Pushes data as pointer onto the stack.

View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "unity.h"
#include "stack.h"
static void pushVorhandenerStack(){
StackNode stack = {(int)1,NULL};
StackNode *tester = push(&stack,(int)2);
TEST_ASSERT_EQUAL_INT(tester->data,2);
TEST_ASSERT_EQUAL_INT(tester->dannach->data,1);
}
int main(){
UNITY_BEGIN();
printf("\n============================\nStack tests\n============================\n");
RUN_TEST(pushVorhandenerStack);
return UNITY_END();
}