Compare commits
No commits in common. "70c0f9bcafa8c6610343b73e6f19576af7fef930" and "06168693b73d3224ee0a03e984716a5dbf42a6b0" have entirely different histories.
70c0f9bcaf
...
06168693b7
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -1,6 +0,0 @@
|
|||||||
{
|
|
||||||
"files.associations": {
|
|
||||||
"stdio.h": "c",
|
|
||||||
"stdlib.h": "c"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
2
makefile
2
makefile
@ -37,8 +37,6 @@ 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"
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
unsigned int* createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
37
stack.c
37
stack.c
@ -8,53 +8,26 @@
|
|||||||
* `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,20 +7,17 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef struct StackNode {
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
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
BIN
stackTests
Binary file not shown.
90
test_stack.c
90
test_stack.c
@ -1,90 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user