generated from freudenreichan/info2Praktikum-DobleSpiel
bintree add-function + Test
This commit is contained in:
parent
7c89058ff1
commit
da3bd18637
@ -12,7 +12,45 @@
|
|||||||
// 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).
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
if (!data){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!root){
|
||||||
|
TreeNode *new = malloc(sizeof(TreeNode));
|
||||||
|
if (!new){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new->data = malloc(dataSize);
|
||||||
|
if (!new->data){
|
||||||
|
free(new);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(new->data, data, dataSize);
|
||||||
|
new->left = new->right = NULL;
|
||||||
|
|
||||||
|
if (isDuplicate){
|
||||||
|
*isDuplicate = 0;
|
||||||
|
}
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmp = compareFct(data, root->data);
|
||||||
|
|
||||||
|
if (cmp < 0) {
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
} else if (cmp > 0) {
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
} else {
|
||||||
|
if (isDuplicate == NULL) {
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, NULL);
|
||||||
|
} else {
|
||||||
|
*isDuplicate = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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.
|
||||||
|
|||||||
BIN
Start_Windows/bintree.o
Normal file
BIN
Start_Windows/bintree.o
Normal file
Binary file not shown.
72
Start_Windows/bintreeTests.c
Normal file
72
Start_Windows/bintreeTests.c
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include "unity.h"
|
||||||
|
#include "bintree.h"
|
||||||
|
|
||||||
|
int compareInt(const void *arg1, const void *arg2) {
|
||||||
|
int a = *(const int *)arg1;
|
||||||
|
int b = *(const int *)arg2;
|
||||||
|
return a - b;
|
||||||
|
}
|
||||||
|
int compareString(const void *arg1, const void *arg2) {
|
||||||
|
const char *str1 = (const char *)arg1;
|
||||||
|
const char *str2 = (const char *)arg2;
|
||||||
|
return strcmp(str1, str2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void testAddToTreeNewRoot(){
|
||||||
|
TreeNode *root=NULL;
|
||||||
|
int a = 1;
|
||||||
|
root = addToTree(root,&a, sizeof(int),compareInt,0);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(a,*(int*)(root->data));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testAddToTreeToExistingRoot(){
|
||||||
|
TreeNode *root=NULL;
|
||||||
|
int a = 1;
|
||||||
|
int b = 3;
|
||||||
|
int c = 4;
|
||||||
|
int d = 2;
|
||||||
|
root = addToTree(root,&a, sizeof(int),compareInt,0);
|
||||||
|
root = addToTree(root,&b, sizeof(int),compareInt,0);
|
||||||
|
root = addToTree(root,&c, sizeof(int),compareInt,0);
|
||||||
|
root = addToTree(root,&d, sizeof(int),compareInt,0);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(a,*(int*)(root->data));
|
||||||
|
TEST_ASSERT_EQUAL_INT(b,*(int*)(root->right->data));
|
||||||
|
TEST_ASSERT_EQUAL_INT(c,*(int*)(root->right->right->data));
|
||||||
|
TEST_ASSERT_EQUAL_INT(d,*(int*)(root->right->left->data));
|
||||||
|
}
|
||||||
|
static void testAddToTreeNoData(){
|
||||||
|
TreeNode *root=NULL;
|
||||||
|
root = addToTree(root,NULL, sizeof(int),compareInt,0);
|
||||||
|
|
||||||
|
TEST_ASSERT_NULL(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void setUp(void){
|
||||||
|
|
||||||
|
}
|
||||||
|
void tearDown(void){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
UNITY_BEGIN();
|
||||||
|
printf("\n============================\nBintree tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(testAddToTreeNewRoot);
|
||||||
|
RUN_TEST(testAddToTreeToExistingRoot);
|
||||||
|
RUN_TEST(testAddToTreeNoData);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@ -29,6 +29,10 @@ unitTests:
|
|||||||
stackTests: stack.o stackTests.c $(unityfolder)/unity.c
|
stackTests: stack.o stackTests.c $(unityfolder)/unity.c
|
||||||
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests stackTests.c stack.o $(unityfolder)/unity.c
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests stackTests.c stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests bintreeTests.c stack.o bintree.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
BIN
Start_Windows/runBintreeTests.exe
Normal file
BIN
Start_Windows/runBintreeTests.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user