90 lines
1.9 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include "unity.h"
#include "stack.h"
void test_topReturnsCorrectValue(void) {
// 1. Arrange
int* value1;
int* value2;
int* outVal1;
int* outVal2;
value1 = malloc(sizeof(int));
value2 = malloc(sizeof(int));
StackNode* startNode = NULL;
// 2. Act
*value1 = 1234;
*value2 = 5678;
startNode = push(startNode, value1);
// new top node is node 2
startNode = push(startNode, value2);
outVal2 = top(startNode);
// node 1 should now be the top node
startNode = pop(startNode);
outVal1 = top(startNode);
// 3. Assert
//Also tests for the functionality of 'push'
TEST_ASSERT_EQUAL_INT32(*value1, *outVal1);
TEST_ASSERT_EQUAL_INT32(*value2, *outVal2);
free(value1);
value1 = NULL;
free(value2);
value2 = NULL;
clearStack(startNode);
}
void test_topReturnsNullOnNotExistingStack(void) {
// 1. Arrange
StackNode* startNode = NULL;
int* data;
// 2. Act
data = top(startNode);
//3. Assert
TEST_ASSERT_NULL(data);
}
void test_popRemovesElements(void) {
// 1. Arrange
StackNode* startNode = NULL;
// 2. Act
// Entering some Elements into Stack (could be added manually for testing)
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
// Now removing created Elements
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
// 3. Assert
TEST_ASSERT_NULL(startNode);
}
void setUp(void){
}
void tearDown(void){
}
int main(void) {
UNITY_BEGIN();
printf("\n============================\nStack tests\n============================\n");
RUN_TEST(test_topReturnsNullOnNotExistingStack);
RUN_TEST(test_topReturnsCorrectValue);
RUN_TEST(test_popRemovesElements);
return UNITY_END();
}