19 Commits
Author SHA1 Message Date
hofmannjo99187 7a7a939fc4 presented code 2025-12-16 11:09:26 +01:00
hofmannjo99187 44d96647bb testing 2025-12-15 20:55:55 +01:00
hofmannjo99187 8605da95b6 adapted bintreeTest to nex nextTreeData Function 2025-12-15 20:51:07 +01:00
hofmannjo99187 a3e12875aa Aufgabenstellungs-pdf durch neues Aufgabenstellungs-pdf ersetzt 2025-12-15 20:45:00 +01:00
hofmannjo99187 b7fa24f2e9 sucessfull testing 2025-12-13 16:48:04 +01:00
hofmannjo99187 b69cfb0640 changed numbers.c so that it works with new nextTreeData() function 2025-12-13 16:35:36 +01:00
hofmannjo99187 686fe8d844 fixed bug in nextTreeData() 2025-12-13 16:21:24 +01:00
hofmannjo99187 c37ce6327c tested entire program 2025-12-13 15:26:51 +01:00
hofmannjo99187 111b9c5532 removed wrong isduplicate assignment 2025-12-13 15:23:28 +01:00
hofmannjo99187 8e76b27760 made tests 2025-12-13 15:17:38 +01:00
hofmannjo99187 a575259cac removed not needed comments 2025-12-13 15:09:35 +01:00
Florian Wetzel d391d60597 Anpassungen in numbers.c und in den entsprechenden Tests 2025-12-12 21:37:00 +01:00
hofmannjo99187 07796c1390 changed addToTreeRec so it now sets inDuplicate to 0 if needed 2025-12-12 06:50:50 +01:00
Florian Wetzel 25da0d20df Troubleshooting wegen gefailter Tests. Aenderungen in bintree.c bei Duplikatsüberprüfung 2025-12-11 15:37:48 +01:00
hofmannjo99187 6586a25fdb created all tests 2025-12-11 08:09:45 +01:00
hofmannjo99187 6e233b6695 created all tests 2025-12-11 08:09:39 +01:00
hofmannjo99187 a01aa986e6 corrected treeSizeRec cause previously end of non void function could be reached 2025-12-11 08:07:23 +01:00
Florian Wetzel aae75ade4b Ergänzung makefile um test_numbers 2025-12-10 14:28:12 +01:00
Florian Wetzel 5d976c50c1 test_numbers.c vervollständigt 2025-12-10 14:19:37 +01:00
16 changed files with 112 additions and 50 deletions
Binary file not shown.
+14 -7
View File
@@ -12,7 +12,6 @@
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
static TreeNode *root = NULL;
static StackNode *stackRoot = NULL;
@@ -52,26 +51,32 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
{
if ((currentNode == NULL))
if (currentNode == NULL)
{
if ((isDuplicate == NULL) || root)
{
if (isDuplicate != NULL)
{
*isDuplicate = 0;
}
return newNode;
}
else
{
*isDuplicate = 0;
return currentNode;
}
}
else if ((compareFct(currentNode->data, newNode->data) < 0))
else if (compareFct(currentNode->data, newNode->data) < 0)
{
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
}
else if ((compareFct(currentNode->data, newNode->data) > 0))
else if (compareFct(currentNode->data, newNode->data) > 0)
{
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
}
else if ((compareFct(currentNode->data, newNode->data) == 0))
else if (compareFct(currentNode->data, newNode->data) == 0)
{
if (isDuplicate == NULL)
{
@@ -102,13 +107,13 @@ void *nextTreeData(TreeNode *root)
{
// Add tree to stack so that bigest entry is ontop
stackRoot = nextTreeDataRec(root, stackRoot);
pointerToReturn = stackRoot;
pointerToReturn = top(stackRoot);
}
else
{
// return current top entry and then pop that top entry
pointerToReturn = top(stackRoot);
stackRoot = pop(stackRoot);
pointerToReturn = top(stackRoot);
}
@@ -185,5 +190,7 @@ int treeSizeRec(const TreeNode *currentNode)
nodeCount += treeSizeRec(currentNode->right);
return nodeCount + 1;
}
return nodeCount;
}
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -115,7 +115,7 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
// check if nextTreeData returns Data correctly
TEST_ASSERT_NOT_NULL(entry);
TEST_ASSERT_EQUAL(score7, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score7, *(int *)entry);
TEST_ASSERT_EQUAL(score3, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score6, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score1, *(int *)nextTreeData(NULL));
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+5
View File
@@ -49,6 +49,11 @@ stack: stack.c
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
test_numbers: numbers.o bintree.o stack.o test_numbers.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o run_numbersTests test_numbers.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
# --------------------------
# Clean
# --------------------------
+39 -36
View File
@@ -23,13 +23,22 @@ static int compareUInt(const void *a, const void *b)
if (A > B) return 1;
return 0;
}
// Sortiervergleich für qsort
int compareQsort(const void *a, const void *b)
static int existsInTree(TreeNode *root, unsigned int value)
{
unsigned int A = *(const unsigned int*)a;
unsigned int B = *(const unsigned int*)b;
if (A < B) return -1;
if (A > B) return +1;
if (!root) return 0;
unsigned int *data;
// Traversierung initialisieren
data = nextTreeData(root);
do
{
if (*data == value)
return 1;
} while ((data = nextTreeData(NULL)) != NULL);
return 0;
}
@@ -39,56 +48,50 @@ unsigned int *createNumbers(unsigned int len)
srand((unsigned int)time(NULL));
// Speicher für das Array
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
if (!numbers) return NULL;
TreeNode *root = NULL; // Baumwurzel
TreeNode *root = NULL;
unsigned int value;
int isDuplicate;
//Array mit eindeutigen Zufallszahlen füllen
// Array mit eindeutigen Zufallszahlen füllen
for (unsigned int i = 0; i < len; i++)
{
while (1)
do
{
value = (rand() % (2 * len)) + 1; // Zufallszahl 1..2*len
isDuplicate = 0;
TreeNode *newRoot = addToTree(
root,
&value,
sizeof(unsigned int),
compareUInt,
&isDuplicate
);
if (!isDuplicate)
{
// Neue Zahl - akzeptieren
root = newRoot;
numbers[i] = value;
break;
}
// Sonst neue Zahl generieren
value = (rand() % (2 * len)) + 1;
}
while (existsInTree(root, value));
// Baum wie highscore benutzen (immer NULL)
root = addToTree(
root,
&value,
sizeof(unsigned int),
compareUInt,
NULL
);
numbers[i] = value;
}
//genau eine Zufallszahl duplizieren
// genau eine Zufallszahl duplizieren
unsigned int idx1 = rand() % len;
unsigned int idx2 = rand() % len;
unsigned int idx2;
while (idx2 == idx1)
do
{
idx2 = rand() % len;
}
while (idx2 == idx1);
numbers[idx2] = numbers[idx1];
// Baum wieder freigeben
clearTree(root);
return numbers;
}
// Returns only the only number in numbers which is present twice. Returns zero on errors.
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{
@@ -101,7 +104,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
memcpy(copy, numbers, len * sizeof(unsigned int));
// Sortieren
qsort(copy, len, sizeof(unsigned int), compareQsort);
qsort(copy, len, sizeof(unsigned int), compareUInt);
// Doppelte Zahl finden
unsigned int duplicate = 0;
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+51 -4
View File
@@ -3,14 +3,61 @@
#include <string.h>
#include <math.h>
#include "numbers.h"
#include "unity.h"
#include "unity/unity.h"
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
// gehört zu unit-Grundaufbau
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
// gehört zu unit-Grundaufbau
}
// Testet, ob createNumbers ein gültiges Array erzeugt
// und genau ein Duplikat enthalten ist
void test_createNumbers_basic(void)
{
unsigned int len = 10;
unsigned int *numbers = createNumbers(len);
// Array muss existieren
TEST_ASSERT_NOT_NULL(numbers);
// Es muss genau EIN Duplikat geben
unsigned int duplicate = getDuplicate(numbers, len);
TEST_ASSERT_NOT_EQUAL(0, duplicate);
free(numbers);
}
/// Testet, ob getDuplicate die korrekte doppelte Zahl erkennt
void test_getDuplicate_correctValue(void)
{
unsigned int numbers[] = {1, 3, 5, 7, 3};
unsigned int len = 5;
unsigned int dup = getDuplicate(numbers, len);
TEST_ASSERT_EQUAL(3, dup);
}
// Testet Fehlerfälle von getDuplicate
static void test_getDuplicate_errors(void)
{
TEST_ASSERT_EQUAL_UINT(0, getDuplicate(NULL, 10));
TEST_ASSERT_EQUAL_UINT(0, getDuplicate((unsigned int*)1, 1)); // len < 2
}
// Testbereich
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_createNumbers_basic);
RUN_TEST(test_getDuplicate_correctValue);
RUN_TEST(test_getDuplicate_errors);
return UNITY_END();
}
BIN
View File
Binary file not shown.