3 Commits
5 changed files with 61 additions and 1 deletions

No files matched your search

+6
View File
@@ -10,6 +10,12 @@
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // 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). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
/*
memcpy
dest - pointer to the memory location to copy to
src - pointer to the memory location to copy from
count - number of bytes to copy
*/
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
-1
View File
@@ -22,7 +22,6 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// wenn neuer Baum dann pushallleft auf wurzel // wenn neuer Baum dann pushallleft auf wurzel
// immer aufräumen!!! // immer aufräumen!!!
// kein vorsortiertes array, sonst entarteter Baum // kein vorsortiertes array, sonst entarteter Baum
void *nextTreeData(TreeNode *root); void *nextTreeData(TreeNode *root);
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
// sortierte Ausgabe // sortierte Ausgabe
+1
View File
@@ -1,2 +1,3 @@
jakob;11860
Jakob;4974 Jakob;4974
player1;3999 player1;3999
+3
View File
@@ -41,6 +41,9 @@ test_numbers:
test_stack: test_stack:
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS) $(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
test_bintree:
$(CC) -o test_bintree test_bintree.c bintree.c stack.c $(unityfolder)/unity.c $(FLAGS)
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
+51
View File
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity/unity.h"
#include "bintree.h"
#include "stack.h"
int cmpInt(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void test_binary_tree_functions(void)
{
TreeNode *root = NULL;
int dup;
int values[] = {5, 3, 8, 2, 6};
for (int i = 0; i < 5; i++) {
root = addToTree(root, &values[i], sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(0, dup);
}
int testDup = 5;
root = addToTree(root, &testDup, sizeof(int), cmpInt, &dup);
TEST_ASSERT_EQUAL_INT(1, dup);
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
int inorderExpected[] = {2, 3, 5, 6, 8};
int idx = 0;
void *data;
for (data = nextTreeData(root); data != NULL; data = nextTreeData(NULL)) {
TEST_ASSERT_EQUAL_INT(inorderExpected[idx], *(int *)data);
idx++;
}
TEST_ASSERT_EQUAL_INT(5, idx);
clearTree(root);
root = NULL;
TEST_ASSERT_NULL(root);
}
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_binary_tree_functions);
return UNITY_END();
}