added clear top top test
This commit is contained in:
parent
a027c070d2
commit
b76ffa054a
71
test_stack.c
71
test_stack.c
@ -3,16 +3,75 @@
|
||||
#include "stack.h"
|
||||
#include "unity.h"
|
||||
|
||||
void test_pop(void)
|
||||
{
|
||||
TEST_ASSERT_NULL(pop(NULL));
|
||||
StackNode* setup(int value, StackNode* next) {
|
||||
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||
if (node == NULL) {
|
||||
perror("malloc failed");
|
||||
exit(EXIT_FAILURE); // or handle the error differently
|
||||
}
|
||||
node->value = value;
|
||||
node->next = next;
|
||||
return node;
|
||||
}
|
||||
|
||||
void setUp(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);
|
||||
int after = 0;
|
||||
while(temp)
|
||||
{
|
||||
after++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(2, after);
|
||||
TEST_ASSERT_NULL(node1->next);
|
||||
}
|
||||
|
||||
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_EQUAL_INT(node2->value, data);
|
||||
}
|
||||
|
||||
void test_clear()
|
||||
{
|
||||
StackNode* node2 = setup(3, NULL);
|
||||
StackNode* node1 = setup(2, node2);
|
||||
StackNode* header = setup(1, node1);
|
||||
StackNode* temp;
|
||||
|
||||
clearStack(header);
|
||||
temp = header;
|
||||
|
||||
int after = 0;
|
||||
while(temp)
|
||||
{
|
||||
after++;
|
||||
temp = temp->next;
|
||||
}
|
||||
|
||||
|
||||
TEST_ASSERT_NULL(after);
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
void tearDown(void)
|
||||
{
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
@ -23,6 +82,8 @@ int main()
|
||||
printf("============================\nStack tests\n============================\n");
|
||||
|
||||
RUN_TEST(test_pop);
|
||||
RUN_TEST(test_top);
|
||||
RUN_TEST(test_clear);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user