Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
01bc613189 | ||
|
|
79a6eb7267 | ||
|
|
53c12ce0ed | ||
|
|
8deeb6417e |
@@ -1,156 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "stack.h"
|
||||
#include "bintree.h"
|
||||
|
||||
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
|
||||
|
||||
static TreeNode *root = NULL;
|
||||
|
||||
|
||||
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
|
||||
void *nextTreeData (TreeNode *root);
|
||||
void clearTree (TreeNode *root);
|
||||
unsigned int treeSize (const TreeNode *root);
|
||||
|
||||
// self declared functions
|
||||
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate);
|
||||
void clearTreeRec (TreeNode *currentNode);
|
||||
void clearNode (TreeNode *node);
|
||||
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount);
|
||||
|
||||
|
||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
|
||||
// returned Value is new root
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
// create a node
|
||||
TreeNode *newNode;
|
||||
|
||||
|
||||
newNode = calloc(1, sizeof(TreeNode));
|
||||
newNode->data = calloc(1, dataSize);
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
memcpy(&newNode->data, data, dataSize);
|
||||
// printf("node created\n");
|
||||
|
||||
|
||||
return addToTreeRec(root, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
|
||||
|
||||
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
printf("entered addRec\n");
|
||||
if (currentNode == NULL)
|
||||
{
|
||||
printf("currentNode == NULL\n");
|
||||
if (isDuplicate != NULL)
|
||||
{
|
||||
*isDuplicate = 0;
|
||||
}
|
||||
|
||||
return newNode;
|
||||
}
|
||||
else if (compareFct(¤tNode->data, &newNode->data) < 0)
|
||||
{
|
||||
printf("compareFct(currentNode->data, newNode->data) < 0\n");
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else if (compareFct(¤tNode->data, &newNode->data) > 0)
|
||||
{
|
||||
printf("compareFct(currentNode->data, newNode->data) > 0\n");
|
||||
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else if (compareFct(¤tNode->data, &newNode->data) == 0)
|
||||
{
|
||||
//duplicate
|
||||
printf("duplicate\n");
|
||||
if (isDuplicate == NULL)
|
||||
{
|
||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
|
||||
}
|
||||
else
|
||||
{
|
||||
*isDuplicate = 1;
|
||||
}
|
||||
}
|
||||
printf("passed everything\n");
|
||||
|
||||
return currentNode;
|
||||
}
|
||||
|
||||
|
||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
|
||||
// push the top node and push all its left nodes.
|
||||
|
||||
// Needs stack!!
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
clearTreeRec(root);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void clearTreeRec(TreeNode *currentNode)
|
||||
{
|
||||
if (currentNode != NULL)
|
||||
{
|
||||
clearTree(currentNode->left);
|
||||
clearNode(currentNode);
|
||||
clearTree(currentNode->right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void clearNode(TreeNode *node)
|
||||
{
|
||||
free(node->data);
|
||||
node->data = NULL;
|
||||
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
|
||||
free(node);
|
||||
node = NULL;
|
||||
}
|
||||
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
unsigned int amountOfNodes = 0;
|
||||
|
||||
|
||||
treeSizeRec(root, &amountOfNodes);
|
||||
|
||||
|
||||
return amountOfNodes;
|
||||
}
|
||||
|
||||
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
|
||||
{
|
||||
if (currentNode != NULL)
|
||||
{
|
||||
treeSizeRec(currentNode->left, nodeCount);
|
||||
*nodeCount++;
|
||||
treeSizeRec(currentNode->right, nodeCount);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-220
@@ -1,220 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "bintree.h"
|
||||
|
||||
#define MAX_TEST_NAME_LEN 10
|
||||
|
||||
|
||||
void setUp(void) {
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
|
||||
static int compareIntEntries(const void *arg1, const void *arg2)
|
||||
{
|
||||
// printf("in comp\n");
|
||||
const int *entry1 = (const void *)arg1;
|
||||
const int *entry2 = (const void *)arg2;
|
||||
|
||||
// printf("const set\n");
|
||||
int result = *entry2 - *entry1;
|
||||
|
||||
// int result = entry2 - entry1;
|
||||
|
||||
// printf("exit comp\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// test if addToTree expands tree correctly
|
||||
// by going down the path where the given pice of data is expected
|
||||
// and checking if the pice of data is found there
|
||||
void test_addToTreeExpandsTreeCorrectly(void)
|
||||
{
|
||||
TreeNode *testRoot = NULL;
|
||||
int *testIsDouble = 0;
|
||||
int score1 = 12;
|
||||
int score2 = 6;
|
||||
int score3 = 18;
|
||||
int score4 = 3;
|
||||
int score5 = 9;
|
||||
int score6 = 15;
|
||||
int score7 = 21;
|
||||
|
||||
|
||||
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add1passed\n");
|
||||
|
||||
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add2passed\n");
|
||||
|
||||
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add3passed\n");
|
||||
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add4passed\n");
|
||||
|
||||
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add5passed\n");
|
||||
|
||||
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add6passed\n");
|
||||
/*
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
|
||||
printf("add7passed\n");
|
||||
*/
|
||||
|
||||
// Checking the Tree without Doubles
|
||||
TEST_ASSERT_NOT_NULL(testRoot);
|
||||
TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data);
|
||||
printf("node1passed\n");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data);
|
||||
printf("node2passed\n");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data);
|
||||
printf("node3passed\n");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data);
|
||||
printf("node4passed\n");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data);
|
||||
printf("node5passed\n");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data);
|
||||
printf("node6passed\n");
|
||||
/*
|
||||
TEST_ASSERT_NOT_NULL(testRoot->right->right);
|
||||
TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data);
|
||||
printf("node7passed\n");
|
||||
/*
|
||||
// Adding Double
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
TEST_ASSERT_NOT_NULL(testRoot->left->left->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->left);
|
||||
|
||||
// Trying to add Double while Doubles not Permitted
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, testIsDouble);
|
||||
TEST_ASSERT_NULL(testRoot->right->right->left);
|
||||
TEST_ASSERT_EQUAL_UINT16(1, testIsDouble);
|
||||
|
||||
|
||||
clearTree(testRoot);
|
||||
// */
|
||||
}
|
||||
|
||||
|
||||
// test if nextTreeData returns the next pice of data correctly
|
||||
// needs Stack!!!
|
||||
void test_nextTreeDataReturnsNextDataCorrectly(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// test if clear Tree frees all node.name and node memory AND sets them to zero
|
||||
// aditionally tests if the memoryspaces have been cleared
|
||||
void test_clearTreeworksLikeExpected(void)
|
||||
{
|
||||
TreeNode *testRoot = NULL;
|
||||
int score1 = 12;
|
||||
int score2 = 6;
|
||||
int score3 = 18;
|
||||
int score4 = 3;
|
||||
int score5 = 9;
|
||||
int score6 = 15;
|
||||
int score7 = 21;
|
||||
|
||||
|
||||
// Fill Tree
|
||||
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
|
||||
// Save all Adresses
|
||||
TreeNode *node1 = testRoot->left;
|
||||
TreeNode *node2 = testRoot->left->left;
|
||||
TreeNode *node3 = testRoot->left->right;
|
||||
TreeNode *node4 = testRoot->right;
|
||||
TreeNode *node5 = testRoot->right->left;
|
||||
TreeNode *node6 = testRoot->right->right;
|
||||
TreeNode *node7 = testRoot->left->left->left;
|
||||
|
||||
clearTree(testRoot);
|
||||
|
||||
// Check if everything has been set to NULL
|
||||
TEST_ASSERT_NULL(testRoot);
|
||||
TEST_ASSERT_NULL(node1);
|
||||
TEST_ASSERT_NULL(node2);
|
||||
TEST_ASSERT_NULL(node3);
|
||||
TEST_ASSERT_NULL(node4);
|
||||
TEST_ASSERT_NULL(node5);
|
||||
TEST_ASSERT_NULL(node6);
|
||||
TEST_ASSERT_NULL(node7);
|
||||
}
|
||||
|
||||
|
||||
// tests if treeSize returns correct amount of nodes in Tree
|
||||
// by using addToTree a given number of times and testing to see if
|
||||
// the treeSize matches the number of nodes added
|
||||
void test_treeSizeWorkingLikeExpected(void)
|
||||
{
|
||||
TreeNode *testRoot = NULL;
|
||||
int nodeCount = 7;
|
||||
unsigned int testTreeSize = 0;
|
||||
int score1 = 12;
|
||||
int score2 = 6;
|
||||
int score3 = 18;
|
||||
int score4 = 3;
|
||||
int score5 = 9;
|
||||
int score6 = 15;
|
||||
int score7 = 21;
|
||||
|
||||
|
||||
// Fill Tree
|
||||
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
|
||||
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
|
||||
|
||||
testTreeSize = treeSize(testRoot);
|
||||
|
||||
TEST_ASSERT_EQUAL(nodeCount, testTreeSize);
|
||||
|
||||
clearTree(testRoot);
|
||||
}
|
||||
|
||||
|
||||
// main, strings together all tests
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nBinary Tree tests\n============================\n");
|
||||
RUN_TEST(test_addToTreeExpandsTreeCorrectly);
|
||||
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
|
||||
// RUN_TEST(test_clearTreeworksLikeExpected);
|
||||
// RUN_TEST(test_treeSizeWorkingLikeExpected);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -1 +1,2 @@
|
||||
player_name;9943
|
||||
player1;3999
|
||||
|
||||
@@ -35,15 +35,14 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
# unitTests:
|
||||
# echo "needs to be implemented"
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
|
||||
bintree: bintree.c
|
||||
$(CC) $(FLAGS) -c bintree bintree.c
|
||||
|
||||
bintreeTests: bintree.o bintreeTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o $(unityfolder)/unity.c
|
||||
stack: stack.c
|
||||
$(CC) $(FLAGS) -c stack stack.c
|
||||
|
||||
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
|
||||
+43
-44
@@ -1,24 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "unity.h"
|
||||
#include "stack.h"
|
||||
|
||||
void setUp(void) {
|
||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void) {
|
||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
|
||||
|
||||
void testeStackBeschreiben()
|
||||
{
|
||||
printf("=== Test: push() ===\n");
|
||||
//printf("=== Test: push() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 42;
|
||||
stack = push(stack, &wert1);
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
if(topValue != NULL && *topValue == 42)
|
||||
{
|
||||
printf("Test 1: Erstes Element erfolgreich gepusht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 1: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(42, *topValue);
|
||||
|
||||
|
||||
int wert2 = 12;
|
||||
@@ -27,23 +33,19 @@ void testeStackBeschreiben()
|
||||
topValue = (int *)(stack->data);
|
||||
int *secondValue = (int *)((stack->next)->data);
|
||||
|
||||
if(topValue != NULL && *topValue == 12.25 && secondValue != NULL && *secondValue == 42)
|
||||
{
|
||||
printf("Test 2: Zweites Element erfolgreich gepusht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 2: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(12, *topValue);
|
||||
TEST_ASSERT_NOT_NULL(secondValue);
|
||||
TEST_ASSERT_EQUAL_INT(42, *secondValue);
|
||||
|
||||
|
||||
printf("=== Ende Test: push() ===\n\n");
|
||||
//printf("=== Ende Test: push() ===\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void testepop()
|
||||
{
|
||||
printf("=== Test: pop() ===\n");
|
||||
//printf("=== Test: pop() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -56,21 +58,15 @@ void testepop()
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
|
||||
if(topValue != NULL && *topValue == 20)
|
||||
{
|
||||
printf("Test: Erstes Element erfolgreich gelöscht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 1: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(20, *topValue);
|
||||
|
||||
printf("=== Ende Test: pop() ===\n\n");
|
||||
//printf("=== Ende Test: pop() ===\n\n");
|
||||
}
|
||||
|
||||
void testetop()
|
||||
{
|
||||
printf("=== Test: top() ===\n");
|
||||
//printf("=== Test: top() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -81,21 +77,15 @@ void testetop()
|
||||
|
||||
int *topValue = top(stack);
|
||||
|
||||
if(topValue != NULL && *topValue == 74)
|
||||
{
|
||||
printf("Test: top() gibt korrektes Element zurück!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(74, *topValue);
|
||||
|
||||
printf("=== Ende Test: top() ===\n\n");
|
||||
//printf("=== Ende Test: top() ===\n\n");
|
||||
}
|
||||
|
||||
void testeclearStack()
|
||||
{
|
||||
printf("=== Test: clearStack() ===\n");
|
||||
//printf("=== Test: clearStack() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -106,16 +96,25 @@ void testeclearStack()
|
||||
|
||||
clearStack(stack);
|
||||
|
||||
printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
|
||||
printf("=== Ende Test: clearStack() ===\n\n");
|
||||
//printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
|
||||
//printf("=== Ende Test: clearStack() ===\n\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
testeStackBeschreiben();
|
||||
UNITY_BEGIN();
|
||||
|
||||
//printf("...");
|
||||
|
||||
RUN_TEST(testeStackBeschreiben);
|
||||
RUN_TEST(testepop);
|
||||
RUN_TEST(testetop);
|
||||
RUN_TEST(testeclearStack);
|
||||
|
||||
/*testeStackBeschreiben();
|
||||
testepop();
|
||||
testetop();
|
||||
testeclearStack();
|
||||
testeclearStack();*/
|
||||
|
||||
return 0;
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user