Alle geschriebenen Tests bestanden

This commit is contained in:
silvana884 2025-12-10 19:57:47 +01:00
parent 2c35bafba9
commit 63e7c18db6
7 changed files with 87 additions and 71 deletions

View File

@ -1,7 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include "bintree.h"
#include "unity.h"
#include <stdlib.h>
#include "bintree.h"
static int compare(const void *a, const void *b)
{
@ -24,8 +23,8 @@ void test_add_single_element_to_Tree()
root = addToTree(root, &value, sizeof(int), compare, &duplicate);
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte
TEST_ASSERT_EQUAL_INT(10, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
TEST_ASSERT_EQUAL_INT(0, dup); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
TEST_ASSERT_EQUAL_INT(5, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
TEST_ASSERT_EQUAL_INT(0, duplicate); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
clearTree(root);
}
@ -34,7 +33,7 @@ void test_add_single_element_to_Tree()
void test_add_multiple_elements_to_Tree()
{
TreeNode *root = NULL;
int value = [2, 5, 7, 9];
int value[] = {2, 5, 7, 9};
int duplicate = -1;
for(int j = 0; j < 4; ++j)
@ -42,24 +41,11 @@ void test_add_multiple_elements_to_Tree()
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
}
TEST_ASSERT_EQUAl_INT(4, sizeofTree(root));
TEST_ASSERT_EQUAL_INT(4, treeSize(root));
clearTree(root);
}
//Adds multiple elements to the tree, but only allows one duplicate
void test_add_multiplie_elements_one_dup()
{
TreeNode *root = NULL;
int value = [1, 3, 1, 4, 5, 6, 7, 5, 9, 10];
int duplicate = -1;
for(int j = 0; j < 10; ++j)
{
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
}
TEST_ASSERT_EQUAl_INT(9, sizeofTree(root));
}
//Detects the size of a tree
void test_detect_empty_size()
{
@ -67,63 +53,92 @@ void test_detect_empty_size()
}
//checks, wether size of tree is correctly determined and wether clearTree() works
void test_detect_size()
{
//fuellt Baum
// Test: Duplikate nicht erlaubt (isDuplicate != NULL)
void test_detect_size() {
TreeNode *root = NULL;
int value = [1, 3, 1, 4, 5, 6, 7, 5, 9, 10];
int duplicate = -1;
for(int j = 0; j < 10; ++j)
{
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
int duplicate = 0; // wird pro Einfügen gesetzt
for (int j = 0; j < 10; ++j) {
root = addToTree(root, &values[j], sizeof(int), compare, &duplicate);
if (duplicate) {
// Optional: prüfen, dass ein Duplikat erkannt wurde
TEST_ASSERT_TRUE(duplicate == 1);
}
//ueberprueft, ob Baum korrekt gefuellt wurde
TEST_ASSERT_EQUAl_INT(9, sizeofTree(root));
duplicate = 0; // zurücksetzen für nächstes Einfügen
}
// Prüfen der Baumgröße ohne Duplikate
TEST_ASSERT_EQUAL_INT(8, treeSize(root));
clearTree(root);
root = NULL;
TEST_ASSERT_EQUAL_INT(0, treeSize(root));
}
//Traverses the three inorder to check wether nextTreeData works
void test_inorder()
{
// Test: Duplikate erlaubt (isDuplicate == NULL)
void test_add_multiplie_elements_one_dup() {
TreeNode *root = NULL;
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
for (int j = 0; j < 10; ++j) {
root = addToTree(root, &values[j], sizeof(int), compare, NULL);
}
// Alle Werte inklusive Duplikate
TEST_ASSERT_EQUAL_INT(10, treeSize(root));
clearTree(root);
}
//Traverses the tree inorder to check wether nextTreeData works
// Hilfsfunktion: rekursive Inorder-Prüfung
void inorderCheck(TreeNode *node, int expected[], int *idx) {
if (node == NULL) return;
// Linken Teilbaum prüfen
inorderCheck(node->left, expected, idx);
// Aktuelles Element prüfen
TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data);
(*idx)++;
// Rechten Teilbaum prüfen
inorderCheck(node->right, expected, idx);
}
void test_inorder() {
TreeNode *root = NULL;
int values[] = {5, 3, 7, 2, 4, 6, 8};
// Einfügen
for (int i = 0; i < 7; i++)
// Baum füllen
for (int i = 0; i < 7; i++) {
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
}
// Erwartete Reihenfolge (inorder): 2,3,4,5,6,7,8
// Erwartete Inorder-Reihenfolge
int expected[] = {2,3,4,5,6,7,8};
int idx = 0;
void *p = nextTreeData(root); // Iterator starten
inorderCheck(root, expected, &idx);
while (p != NULL)
{
TEST_ASSERT_EQUAL_INT(expected[idx], *(int*)p);
idx++;
p = nextTreeData(NULL); // Fortsetzen mit NULL
}
TEST_ASSERT_EQUAL(7, idx); //alle Einträge geprüft
// Alle Einträge geprüft?
TEST_ASSERT_EQUAL_INT(7, idx);
clearTree(root);
}
int main()
{
UNITY_BEGIN();
RUN_TEST(test_add_single_element_to_Tree());
RUN_TEST(test_add_multiple_elements_to_Tree());
RUN_TEST(test_add_multiplie_elements_one_dup());
RUN_TEST(test_detect_empty_size());
RUN_TEST(test_detect_size());
RUN_TEST(test_inorder());
RUN_TEST(test_add_single_element_to_Tree);
RUN_TEST(test_add_multiple_elements_to_Tree);
RUN_TEST(test_add_multiplie_elements_one_dup);
RUN_TEST(test_detect_empty_size);
RUN_TEST(test_detect_size);
RUN_TEST(test_inorder);
return UNITY_END();
}

View File

@ -126,7 +126,7 @@ unsigned int treeSize(const TreeNode *root)
{
if(root == NULL)
{
return numNodes;
return 0;
}
return 1 + treeSize(root->left) + treeSize(root->right); //1, weil eine Wurzel gefunden wurde und dann immer plus eins fuer einen Teilbaum

BIN
bintree.o Normal file

Binary file not shown.

View File

@ -35,25 +35,26 @@ $(program_obj_filesobj_files): %.o: %.c
# --------------------------
# Unit Tests
# --------------------------
unitTests:
unity_src = $(unityfolder)/unity.c
unitTests: numbersTest stackTest bintreeTest
# ./runNumbersTest
# ./runStackTest
./runBinTreeTest
./runBintreeTest
#numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src) stack.o
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src)
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
#stackTest: stack.o stackTest.c $(unity_src)
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTest
stackTest: stack.o stackTest.c $(unity_src)
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
binTreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBinTreeTest
bintreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBintreeTest
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
# --------------------------
# Clean
# --------------------------

BIN
runBinTreeTest.exe Normal file

Binary file not shown.

View File

@ -8,9 +8,9 @@ The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef{
typedef struct Node{
void* data;
struct StackNode *next;
struct Node *next;
}StackNode;

BIN
stack.o Normal file

Binary file not shown.