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 "unity.h"
#include <stdlib.h>
#include "bintree.h"
static int compare(const void *a, const void *b) 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); root = addToTree(root, &value, sizeof(int), compare, &duplicate);
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte 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(5, *(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(0, duplicate); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
clearTree(root); clearTree(root);
} }
@ -34,7 +33,7 @@ void test_add_single_element_to_Tree()
void test_add_multiple_elements_to_Tree() void test_add_multiple_elements_to_Tree()
{ {
TreeNode *root = NULL; TreeNode *root = NULL;
int value = [2, 5, 7, 9]; int value[] = {2, 5, 7, 9};
int duplicate = -1; int duplicate = -1;
for(int j = 0; j < 4; ++j) 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); 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); 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 //Detects the size of a tree
void test_detect_empty_size() 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 //checks, wether size of tree is correctly determined and wether clearTree() works
void test_detect_size() // Test: Duplikate nicht erlaubt (isDuplicate != NULL)
{ void test_detect_size() {
//fuellt Baum
TreeNode *root = NULL; TreeNode *root = NULL;
int value = [1, 3, 1, 4, 5, 6, 7, 5, 9, 10]; int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
int duplicate = -1; int duplicate = 0; // wird pro Einfügen gesetzt
for(int j = 0; j < 10; ++j)
{ for (int j = 0; j < 10; ++j) {
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate); 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 duplicate = 0; // zurücksetzen für nächstes Einfügen
TEST_ASSERT_EQUAl_INT(9, sizeofTree(root));
} }
// Prüfen der Baumgröße ohne Duplikate
TEST_ASSERT_EQUAL_INT(8, treeSize(root));
clearTree(root); clearTree(root);
root = NULL; }
TEST_ASSERT_EQUAL_INT(0, treeSize(root));
//Traverses the three inorder to check wether nextTreeData works // Test: Duplikate erlaubt (isDuplicate == NULL)
void test_inorder() 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; TreeNode *root = NULL;
int values[] = {5, 3, 7, 2, 4, 6, 8}; int values[] = {5, 3, 7, 2, 4, 6, 8};
// Einfügen // Baum füllen
for (int i = 0; i < 7; i++) for (int i = 0; i < 7; i++) {
root = addToTree(root, &values[i], sizeof(int), compare, NULL); 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 expected[] = {2,3,4,5,6,7,8};
int idx = 0; int idx = 0;
void *p = nextTreeData(root); // Iterator starten inorderCheck(root, expected, &idx);
while (p != NULL) // Alle Einträge geprüft?
{ TEST_ASSERT_EQUAL_INT(7, idx);
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
clearTree(root); clearTree(root);
} }
int main() int main()
{ {
UNITY_BEGIN(); UNITY_BEGIN();
RUN_TEST(test_add_single_element_to_Tree()); RUN_TEST(test_add_single_element_to_Tree);
RUN_TEST(test_add_multiple_elements_to_Tree()); RUN_TEST(test_add_multiple_elements_to_Tree);
RUN_TEST(test_add_multiplie_elements_one_dup()); RUN_TEST(test_add_multiplie_elements_one_dup);
RUN_TEST(test_detect_empty_size()); RUN_TEST(test_detect_empty_size);
RUN_TEST(test_detect_size()); RUN_TEST(test_detect_size);
RUN_TEST(test_inorder()); RUN_TEST(test_inorder);
return UNITY_END(); return UNITY_END();
} }

View File

@ -126,7 +126,7 @@ unsigned int treeSize(const TreeNode *root)
{ {
if(root == NULL) 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 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 # Unit Tests
# -------------------------- # --------------------------
unitTests: unity_src = $(unityfolder)/unity.c
unity_src = $(unityfolder)/unity.c
unitTests: numbersTest stackTest bintreeTest unitTests: numbersTest stackTest bintreeTest
#./runNumbersTest # ./runNumbersTest
# ./runStackTest # ./runStackTest
./runBinTreeTest ./runBintreeTest
#numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src) stack.o numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src)
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
#stackTest: stack.o stackTest.c $(unity_src) stackTest: stack.o stackTest.c $(unity_src)
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTest $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
bintreeTest: bintree.o binTreeTest.c $(unity_src) stack.o binTreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBintreeTest $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBinTreeTest
%.o: %.c
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $< -o $@
# -------------------------- # --------------------------
# Clean # 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> #include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef{ typedef struct Node{
void* data; void* data;
struct StackNode *next; struct Node *next;
}StackNode; }StackNode;

BIN
stack.o Normal file

Binary file not shown.