Info2-Aufgabe3/Start_Windows/bintreeTests.c
Thomas Rauh Desktop f4cf06ff53 nextTreeData + Test
2025-11-23 00:35:38 +01:00

94 lines
2.4 KiB
C

#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);
}
static void testNextTreeDataInorderTraversal(){
TreeNode *root = NULL;
int values[] = {5, 3, 8, 1, 4};
int expectedInorder[] = {1, 3, 4, 5, 8};
for(int i = 0; i < 5; i++) {
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL); //Tree befüllen
}
void *data = nextTreeData(root);
TEST_ASSERT_NOT_NULL(data);
for(int i = 0; i < 5; i++){
TEST_ASSERT_NOT_NULL(data);
TEST_ASSERT_EQUAL_INT(expectedInorder[i], *(int*)data);
data = nextTreeData(NULL); // Nächstes Element
}
TEST_ASSERT_NULL(data); // Nach letzten Element muss NULL kommen
}
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();
}