76 lines
1.9 KiB
C
76 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <assert.h>
|
|
#include "stack.h"
|
|
|
|
// Hilfsfunktion: Gibt "PASSED" oder "FAILED" aus
|
|
void printTestResult(const char *testName, int passed)
|
|
{
|
|
if (passed)
|
|
{
|
|
printf("[PASSED] %s\n", testName);
|
|
}
|
|
else
|
|
{
|
|
printf("[FAILED] %s\n", testName);
|
|
}
|
|
}
|
|
|
|
// Test 1: Leerer Stack
|
|
void test_emptyStack()
|
|
{
|
|
StackNode *stack = NULL;
|
|
|
|
// Top auf leerem Stack sollte NULL zurückgeben
|
|
void *result = top(stack);
|
|
printTestResult("Test 1: top() auf leerem Stack", result == NULL);
|
|
|
|
// Pop auf leerem Stack sollte NULL zurückgeben
|
|
stack = pop(stack);
|
|
printTestResult("Test 1: pop() auf leerem Stack", stack == NULL);
|
|
}
|
|
|
|
// Test 2: Push und Top
|
|
void test_pushAndTop()
|
|
{
|
|
StackNode *stack = NULL;
|
|
|
|
// Integer-Werte allokieren
|
|
int *val1 = (int *)malloc(sizeof(int));
|
|
*val1 = 42;
|
|
|
|
// Wert auf den Stack legen
|
|
stack = push(stack, val1);
|
|
|
|
// Obersten Wert abrufen
|
|
int *topVal = (int *)top(stack);
|
|
int passed = (topVal != NULL && *topVal == 42);
|
|
printTestResult("Test 2: push() und top()", passed);
|
|
|
|
// Aufräumen
|
|
free(val1);
|
|
clearStack(stack);
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
printf("=== Stack Unit-Tests ===\n\n");
|
|
|
|
test_emptyStack();
|
|
test_pushAndTop();
|
|
/*test_multiplePush();
|
|
test_pushAndPop();
|
|
test_clearStack();
|
|
/*test_stressTest();*/
|
|
|
|
printf("\n=== Alle Tests abgeschlossen ===\n");
|
|
printf("\nCode-Review: Speicherverwaltung\n");
|
|
printf("--------------------------------\n");
|
|
printf("✓ Alle mit malloc() allokierten Integer-Werte werden mit free() freigegeben\n");
|
|
printf("✓ Alle StackNode-Strukturen werden durch pop() oder clearStack() freigegeben\n");
|
|
printf("✓ Keine Memory-Leaks vorhanden\n");
|
|
printf("✓ Aufrufer gibt Daten frei, Stack-Funktionen geben Knoten frei\n");
|
|
|
|
return 0;
|
|
} |