Author SHA1 Message Date
Simon 4cfe6d9c50 added test_push and fixed stack cast 2025-11-24 17:53:43 +01:00
Simon 82c72eaf81 first tests 2025-11-24 16:05:31 +01:00
6 changed files with 247 additions and 269 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ StackNode *push(StackNode *stack, void *data)
StackNode *tempNode, *newNode; StackNode *tempNode, *newNode;
newNode = malloc(sizeof(StackNode)); newNode = malloc(sizeof(StackNode));
newNode->value = 3; newNode->value = *(int *)data;
newNode->next = NULL; newNode->next = NULL;
if (stack == NULL) if (stack == NULL)
+27 -49
View File
@@ -3,75 +3,52 @@
#include "stack.h" #include "stack.h"
#include "unity.h" #include "unity.h"
StackNode* setup(int value, StackNode* next) { void test_push(void)
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap {
if (node == NULL) { StackNode *testNode;
perror("malloc failed"); int data = 1;
exit(EXIT_FAILURE); // or handle the error differently
} // Test für leeren Stack
node->value = value; testNode = push(NULL, &data);
node->next = next; TEST_ASSERT_NOT_NULL(&testNode);
return node; TEST_ASSERT_NULL(testNode->next);
TEST_ASSERT_EQUAL_INT(1, testNode->value);
data = 2;
// Test für nicht leeren Stack
testNode = push(testNode, &data);
TEST_ASSERT_NOT_NULL(&testNode);
TEST_ASSERT_NOT_NULL(testNode->next);
TEST_ASSERT_NULL(testNode->next->next);
TEST_ASSERT_EQUAL_INT(1, testNode->value);
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
} }
void test_pop(void) void test_pop(void)
{ {
StackNode* node2 = setup(3, NULL);
StackNode* node1 = setup(2, node2);
StackNode* header = setup(1, node1);
StackNode* temp;
temp = pop(header); TEST_ASSERT_NULL(pop(NULL));
int after = 0;
while(temp)
{
after++;
temp = temp->next;
}
TEST_ASSERT_EQUAL_INT(2, after);
TEST_ASSERT_NULL(node1->next);
} }
void test_top(void) void test_top(void)
{ {
StackNode* node2 = setup(3, NULL);
StackNode* node1 = setup(2, node2);
StackNode* header = setup(1, node1);
int data = *(int *)top(header); TEST_ASSERT_NULL(top(NULL));
TEST_ASSERT_EQUAL_INT(node2->value, data);
} }
void test_clear() void test_clear(void)
{ {
StackNode* node2 = setup(3, NULL);
StackNode* node1 = setup(2, node2);
StackNode* header = setup(1, node1);
StackNode* temp;
clearStack(header); // TEST_ASSERT_NULL(clearStack(NULL));
temp = header;
int after = 0;
while(temp)
{
after++;
temp = temp->next;
} }
void setUp(void) {
TEST_ASSERT_NULL(after);
}
void setUp(void)
{
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
} }
void tearDown(void) void tearDown(void) {
{
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
} }
@@ -81,6 +58,7 @@ int main()
printf("============================\nStack tests\n============================\n"); printf("============================\nStack tests\n============================\n");
RUN_TEST(test_push);
RUN_TEST(test_pop); RUN_TEST(test_pop);
RUN_TEST(test_top); RUN_TEST(test_top);
RUN_TEST(test_clear); RUN_TEST(test_clear);