Compare commits

..

No commits in common. "36ac0d6d9d3c4b2e9a9dc0db4d4a67cbcdfaa540" and "f91ab68a72fba81de27e3c132c71ca55ba57c100" have entirely different histories.

3 changed files with 23 additions and 74 deletions

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
@ -40,8 +39,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
newNode->data = calloc(1, dataSize); newNode->data = calloc(1, dataSize);
newNode->left = NULL; newNode->left = NULL;
newNode->right = NULL; newNode->right = NULL;
memcpy(&newNode->data, data, dataSize); memcpy(newNode->data, data, dataSize);
// printf("node created\n");
return addToTreeRec(root, newNode, compareFct, isDuplicate); return addToTreeRec(root, newNode, compareFct, isDuplicate);
@ -50,31 +48,22 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate)
{ {
printf("entered addRec\n");
if (currentNode == NULL) if (currentNode == NULL)
{ {
printf("currentNode == NULL\n"); isDuplicate = 0;
if (isDuplicate != NULL)
{
*isDuplicate = 0;
}
return newNode; return newNode;
} }
else if (compareFct(&currentNode->data, &newNode->data) < 0) else if (compareFct(newNode->data, currentNode->data) < 0)
{ {
printf("compareFct(currentNode->data, newNode->data) < 0\n");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
} }
else if (compareFct(&currentNode->data, &newNode->data) > 0) else if (compareFct(newNode->data, currentNode->data) > 0)
{ {
printf("compareFct(currentNode->data, newNode->data) > 0\n");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate);
} }
else if (compareFct(&currentNode->data, &newNode->data) == 0) else
{ {
//duplicate //duplicate
printf("duplicate\n");
if (isDuplicate == NULL) if (isDuplicate == NULL)
{ {
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate);
@ -84,7 +73,7 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
*isDuplicate = 1; *isDuplicate = 1;
} }
} }
printf("passed everything\n");
return currentNode; return currentNode;
} }

BIN
bintree.o

Binary file not shown.

View File

@ -1,32 +1,18 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "unity.h" #include "unity.h"
#include "bintree.h" #include "bintree.h"
#define MAX_TEST_NAME_LEN 10 #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) static int compareIntEntries(const void *arg1, const void *arg2)
{ {
// printf("in comp\n"); const int *entry1 = (const int *)arg1;
const int *entry1 = (const void *)arg1; const int *entry2 = (const int *)arg2;
const int *entry2 = (const void *)arg2;
// printf("const set\n");
int result = *entry2 - *entry1; int result = *entry2 - *entry1;
// int result = entry2 - entry1;
// printf("exit comp\n");
return result; return result;
} }
@ -48,69 +34,43 @@ void test_addToTreeExpandsTreeCorrectly(void)
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
printf("add1passed\n");
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
printf("add2passed\n");
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
printf("add3passed\n");
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
printf("add4passed\n");
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
printf("add5passed\n");
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
printf("add6passed\n");
/*
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
printf("add7passed\n");
*/
// Checking the Tree without Doubles // Checking the Tree without Doubles
TEST_ASSERT_NOT_NULL(testRoot); TEST_ASSERT_NOT_NULL(testRoot);
TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data); TEST_ASSERT_EQUAL(score1, testRoot);
printf("node1passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left); TEST_ASSERT_NOT_NULL(testRoot->left);
TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data); TEST_ASSERT_EQUAL(score2, testRoot->left);
printf("node2passed\n");
TEST_ASSERT_NOT_NULL(testRoot->right); TEST_ASSERT_NOT_NULL(testRoot->right);
TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data); TEST_ASSERT_EQUAL(score3, testRoot->right);
printf("node3passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left->left); TEST_ASSERT_NOT_NULL(testRoot->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data); TEST_ASSERT_EQUAL(score4, testRoot->left->left);
printf("node4passed\n");
TEST_ASSERT_NOT_NULL(testRoot->left->right); TEST_ASSERT_NOT_NULL(testRoot->left->right);
TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data); TEST_ASSERT_EQUAL(score5, testRoot->left->right);
printf("node5passed\n");
TEST_ASSERT_NOT_NULL(testRoot->right->left); TEST_ASSERT_NOT_NULL(testRoot->right->left);
TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data); TEST_ASSERT_EQUAL(score6, testRoot->right->left);
printf("node6passed\n");
/*
TEST_ASSERT_NOT_NULL(testRoot->right->right); TEST_ASSERT_NOT_NULL(testRoot->right->right);
TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data); TEST_ASSERT_EQUAL(score7, testRoot->right->right);
printf("node7passed\n");
/*
// Adding Double // Adding Double
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
TEST_ASSERT_NOT_NULL(testRoot->left->left->left); TEST_ASSERT_NOT_NULL(testRoot->left->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->left); TEST_ASSERT_EQUAL(score4, testRoot->left->left->left);
// Trying to add Double while Doubles not Permitted // Trying to add Double while Doubles not Permitted
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, testIsDouble); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, testIsDouble);
TEST_ASSERT_NULL(testRoot->right->right->left); TEST_ASSERT_NULL(testRoot->right->right->left);
TEST_ASSERT_EQUAL_UINT16(1, testIsDouble); TEST_ASSERT_EQUAL(1, testIsDouble);
clearTree(testRoot); clearTree(testRoot);
// */
} }
@ -212,8 +172,8 @@ int main()
printf("\n============================\nBinary Tree tests\n============================\n"); printf("\n============================\nBinary Tree tests\n============================\n");
RUN_TEST(test_addToTreeExpandsTreeCorrectly); RUN_TEST(test_addToTreeExpandsTreeCorrectly);
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly); // RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
// RUN_TEST(test_clearTreeworksLikeExpected); RUN_TEST(test_clearTreeworksLikeExpected);
// RUN_TEST(test_treeSizeWorkingLikeExpected); RUN_TEST(test_treeSizeWorkingLikeExpected);
return UNITY_END(); return UNITY_END();
} }