Added complete stack functionality
This commit is contained in:
parent
bcff2c2372
commit
204f2a2526
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stdio.h": "c",
|
||||||
|
"stdlib.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
2
makefile
2
makefile
@ -37,6 +37,8 @@ doble_initial:
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c ${LDFLAGS}
|
||||||
unitTests:
|
unitTests:
|
||||||
echo "needs to be implemented"
|
echo "needs to be implemented"
|
||||||
|
|
||||||
|
|||||||
37
stack.c
37
stack.c
@ -8,26 +8,53 @@
|
|||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode* push(StackNode* stack, void* data)
|
||||||
{
|
{
|
||||||
|
//pointer onto new struct
|
||||||
|
StackNode* newNode = malloc(sizeof(StackNode));
|
||||||
|
newNode->data = data;
|
||||||
|
//*stack := first node of the stack; is now the second to first node
|
||||||
|
newNode->nextNode = stack;
|
||||||
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode* pop(StackNode* stack)
|
||||||
{
|
{
|
||||||
|
//getting the second to first node
|
||||||
|
StackNode* tmp = stack->nextNode;
|
||||||
|
|
||||||
|
// data should be freed by the caller
|
||||||
|
// free(stack->data);
|
||||||
|
// stack->data = NULL;
|
||||||
|
free(stack);
|
||||||
|
stack = NULL;
|
||||||
|
|
||||||
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void* top(StackNode* stack)
|
||||||
{
|
{
|
||||||
|
//No stack --> No data-pointer
|
||||||
|
if (stack == NULL) return NULL;
|
||||||
|
|
||||||
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode* stack)
|
||||||
{
|
{
|
||||||
|
if (stack == NULL) return;
|
||||||
|
|
||||||
|
if (stack->nextNode != NULL) clearStack(stack->nextNode);
|
||||||
|
|
||||||
|
// date should be freed by the caller.
|
||||||
|
// free(stack->data);
|
||||||
|
// stack->data = NULL;
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
// technically not needed (dangling pointer)
|
||||||
|
stack = NULL;
|
||||||
}
|
}
|
||||||
11
stack.h
11
stack.h
@ -7,17 +7,20 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
typedef struct StackNode {
|
||||||
|
void* data;
|
||||||
|
struct StackNode* nextNode;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode* push(StackNode *stack, void *data);
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack);
|
StackNode* pop(StackNode *stack);
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack);
|
void* top(StackNode *stack);
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack);
|
void clearStack(StackNode *stack);
|
||||||
|
|||||||
BIN
stackTests
Normal file
BIN
stackTests
Normal file
Binary file not shown.
77
test_stack.c
Normal file
77
test_stack.c
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#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_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_topReturnsCorrectValue);
|
||||||
|
RUN_TEST(test_popRemovesElements);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user