64 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
hofmannjo99187 92fa60f9dc Merge branch 'branchjonas' 2025-12-10 12:26:54 +01:00
hofmannjo99187 284cdbdcfb made runbintreeTests.exe 2025-12-10 12:18:49 +01:00
hofmannjo99187 e3501e8550 added runbintreeTests.exe and stack.o 2025-12-09 23:18:36 +01:00
hofmannjo99187 6a9461bf5b all binaryTree functions and Tests working 2025-12-09 23:13:39 +01:00
hofmannjo99187 ef3e0b7891 changed binTreeTest makefile, test_nextTreeDataReturnsNextDataCorrectly() not yet working 2025-12-09 22:41:35 +01:00
hofmannjo99187 82b2fb283f merged branchjens 2025-12-09 22:28:31 +01:00
hofmannjo99187 602220ba21 Merge branch 'branchjens' into branchjonas 2025-12-09 22:27:30 +01:00
hofmannjo99187 2368c9f3b9 added stack to makefile 2025-12-09 22:27:14 +01:00
hofmannjo99187 e4a8afabdd wrote test_nextTreeDataReturnsNextDataCorrectly() 2025-12-09 21:48:43 +01:00
hofmannjo99187 9d08970513 wrote nextTreeData() and nextTreeDataRec() 2025-12-09 21:12:03 +01:00
hofmannjo99187 9ee0f9e281 treeSize and treeSize test now workingW 2025-12-09 18:41:02 +01:00
hofmannjo99187 21aa67b165 removed no longer needed comments 2025-12-09 18:04:58 +01:00
hofmannjo99187 2d6f8689f2 corrected mistake in test_clearTreeworksLikeExpected 2025-12-09 17:59:21 +01:00
hofmannjo99187 8754182f79 clear Tree and test now working corectly 2025-12-09 17:51:11 +01:00
hofmannjo99187 144648886f addToTree corrected 2025-12-09 11:19:56 +01:00
hofmannjo99187 748fd0d087 corrected unwanted add 2025-12-09 11:15:01 +01:00
hofmannjo99187 72f1d080a0 adToTree working again 2025-12-09 11:10:29 +01:00
Jens Burger 01bc613189 Kommentare gelöscht 2025-12-09 10:45:10 +01:00
Jens Burger 79a6eb7267 Debugging Unity Aufruf 2025-12-09 10:41:53 +01:00
Jens Burger 53c12ce0ed added makefile command 2025-12-09 10:38:00 +01:00
Jens Burger 8deeb6417e Testfunktion auf Unity umgebaut 2025-12-09 10:31:12 +01:00
Florian Wetzel a6c5060060 test_numbers.c Grundgerüst 2025-12-09 10:24:54 +01:00
hofmannjo99187 02e008c03a debug 2025-12-09 09:59:22 +01:00
Florian Wetzel 2689130b55 numbers.c bearbeitet 2025-12-09 09:57:13 +01:00
hofmannjo99187 7c3df62d86 further debugging 2025-12-09 08:46:56 +01:00
hofmannjo99187 f7549910eb freeing node->data works, freeing node itself does not 2025-12-09 08:09:59 +01:00
hofmannjo99187 14fa122f20 added runbintreeTests.exe to git 2025-12-09 07:22:34 +01:00
hofmannjo99187 1c10994396 clear does not yet work, also TEST_ASSERT_EQUAL datatype is off 2025-12-09 01:40:43 +01:00
hofmannjo99187 402604cb45 removed no longer needed code 2025-12-09 01:07:39 +01:00
hofmannjo99187 5dc45f8590 uncommented not needed code from test_addToTreeExpandsTreeCorrectly and addToTree 2025-12-09 01:03:58 +01:00
hofmannjo99187 6b30677698 addToTreeExpandsTreeCorrectly 2025-12-09 01:00:42 +01:00
hofmannjo99187 d56a2f7a03 adding double working, double not Permitted not working 2025-12-09 00:44:04 +01:00
hofmannjo99187 36ac0d6d9d normal Nodes working in addToTree() 2025-12-09 00:36:27 +01:00
hofmannjo99187 517d363f3e debugging 2025-12-08 22:41:49 +01:00
hofmannjo99187 f91ab68a72 added bintree.o to git 2025-12-08 20:49:01 +01:00
hofmannjo99187 574ed71bae added bintree.o to makefile 2025-12-08 20:48:02 +01:00
hofmannjo99187 c49a8d687c reset highscore.txt 2025-12-08 13:01:23 +01:00
hofmannjo99187 42b83afec0 Merge branch 'branchjens' into branchjonas 2025-12-08 12:58:13 +01:00
hofmannjo99187 b138a8f251 added bintreeTests to makefile as Target 2025-12-08 08:36:00 +01:00
hofmannjo99187 ffde7270db wrote main() in bintreeTests.c 2025-12-08 08:30:10 +01:00
hofmannjo99187 409afc165b wrote test_treeSizeWorkingLikeExpected(void) 2025-12-08 08:24:30 +01:00
hofmannjo99187 4042a7d979 wrote test_clearTreeworksLikeExpected() 2025-12-08 08:19:12 +01:00
hofmannjo99187 1ead9595ce wrote test_addToTreeExpandsTreeCorrectly() 2025-12-08 08:03:16 +01:00
Jens Burger 00fafec3dc test_stack.c erstellt 2025-12-05 14:33:22 +01:00
Jens Burger cc927ad723 Stack Funktionen hinzugefügt 2025-12-05 12:19:11 +01:00
24 changed files with 700 additions and 39 deletions
Vendored
BIN
View File
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "macos-clang-arm64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
""
]
}
],
"version": 4
}
+13
View File
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "/Users/florianwetzel/I2_Praktikum/DobleSpiel",
"program": "/Users/florianwetzel/I2_Praktikum/DobleSpiel/build/Debug/outDebug"
}
]
}
+59
View File
@@ -0,0 +1,59 @@
{
"C_Cpp_Runner.cCompilerPath": "clang",
"C_Cpp_Runner.cppCompilerPath": "clang++",
"C_Cpp_Runner.debuggerPath": "lldb",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
Binary file not shown.
+81 -30
View File
@@ -1,28 +1,31 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "stack.h" #include "stack.h"
#include "bintree.h" #include "bintree.h"
//TODO: binären Suchbaum implementieren //TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done * `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
* `treeSize`: zählt die Knoten im Baum (rekursiv), * `treeSize`: zählt die Knoten im Baum (rekursiv), Done
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
static TreeNode *root = NULL; static StackNode *stackRoot = NULL;
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);
void *nextTreeData (TreeNode *root); void *nextTreeData (TreeNode *root);
void clearTree (TreeNode *root); void clearTree (TreeNode *root);
unsigned int treeSize (const TreeNode *root); unsigned int treeSize (const TreeNode *root);
// self declared functions // self declared functions
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate); TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root);
void clearTreeRec (TreeNode *currentNode); void clearTreeRec (TreeNode *currentNode);
void clearNode (TreeNode *node); void clearNode (TreeNode *node);
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount); int treeSizeRec (const TreeNode *currentNode);
void *nextTreeDataRec (TreeNode *node, StackNode *stack);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
@@ -42,31 +45,42 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
memcpy(newNode->data, data, dataSize); memcpy(newNode->data, data, dataSize);
return addToTreeRec(root, newNode, compareFct, isDuplicate); return addToTreeRec(root, newNode, compareFct, isDuplicate, 1);
} }
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
{ {
if (currentNode == NULL) if (currentNode == NULL)
{ {
isDuplicate = 0; if ((isDuplicate == NULL) || root)
return newNode; {
if (isDuplicate != NULL)
{
*isDuplicate = 0;
}
return newNode;
}
else
{
*isDuplicate = 0;
return currentNode;
}
} }
else if (compareFct(newNode->data, currentNode->data) < 0) else if (compareFct(currentNode->data, newNode->data) < 0)
{ {
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
} }
else if (compareFct(newNode->data, currentNode->data) > 0) else if (compareFct(currentNode->data, newNode->data) > 0)
{ {
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
} }
else else if (compareFct(currentNode->data, newNode->data) == 0)
{ {
//duplicate
if (isDuplicate == NULL) if (isDuplicate == NULL)
{ {
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
} }
else else
{ {
@@ -84,9 +98,40 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
// Needs stack!! // Needs stack!!
// Stack functions: push(), pop(), top(), clearStack()
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
void *pointerToReturn = NULL;
if (root != NULL)
{
// Add tree to stack so that bigest entry is ontop
stackRoot = nextTreeDataRec(root, stackRoot);
pointerToReturn = top(stackRoot);
}
else
{
// return current top entry and then pop that top entry
stackRoot = pop(stackRoot);
pointerToReturn = top(stackRoot);
}
return pointerToReturn;
}
void *nextTreeDataRec(TreeNode *tree, StackNode *stack)
{
if (tree != NULL)
{
stack = nextTreeDataRec (tree->left, stack);
stack = push (stack, tree->data);
stack = nextTreeDataRec (tree->right, stack);
}
return stack;
} }
@@ -102,8 +147,8 @@ void clearTreeRec(TreeNode *currentNode)
if (currentNode != NULL) if (currentNode != NULL)
{ {
clearTree(currentNode->left); clearTree(currentNode->left);
clearNode(currentNode);
clearTree(currentNode->right); clearTree(currentNode->right);
clearNode(currentNode);
} }
} }
@@ -127,19 +172,25 @@ unsigned int treeSize(const TreeNode *root)
unsigned int amountOfNodes = 0; unsigned int amountOfNodes = 0;
treeSizeRec(root, &amountOfNodes); amountOfNodes = treeSizeRec(root);
return amountOfNodes; return amountOfNodes;
} }
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
int treeSizeRec(const TreeNode *currentNode)
{ {
int nodeCount = 0;
if (currentNode != NULL) if (currentNode != NULL)
{ {
treeSizeRec(currentNode->left, nodeCount); nodeCount += treeSizeRec(currentNode->left);
*nodeCount++; nodeCount += treeSizeRec(currentNode->right);
treeSizeRec(currentNode->right, nodeCount); return nodeCount + 1;
} }
return nodeCount;
} }
BIN
View File
Binary file not shown.
+187 -7
View File
@@ -1,43 +1,223 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "unity.h" #include "unity.h"
#include "bintree.h" #include "bintree.h"
#include "stack.h"
#define MAX_TEST_NAME_LEN 10 #define MAX_TEST_NAME_LEN 10
typedef struct void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
static int compareIntEntries(const void *arg1, const void *arg2)
{ {
char name[MAX_TEST_NAME_LEN]; const int *entry1 = (const void *)arg1;
int score; const int *entry2 = (const void *)arg2;
} HighscoreEntry;
int result = *entry2 - *entry1;
return result;
}
// test if addToTree expands tree correctly // test if addToTree expands tree correctly
// by going down the path where the given pice of data is expected // by going down the path where the given pice of data is expected
// and checking if the pice of data is found there // and checking if the pice of data is found there
void test_addToTreeExpandsTreeCorrectlyNoDoubles(void) void test_addToTreeExpandsTreeCorrectly(void)
{ {
TreeNode *testRoot = NULL;
int testIsDouble = 0;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
// Checking the Tree without Doubles
TEST_ASSERT_NOT_NULL(testRoot);
TEST_ASSERT_EQUAL(score1, *(int *)testRoot->data);
TEST_ASSERT_NOT_NULL(testRoot->left);
TEST_ASSERT_EQUAL(score2, *(int *)testRoot->left->data);
TEST_ASSERT_NOT_NULL(testRoot->right);
TEST_ASSERT_EQUAL(score3, *(int *)testRoot->right->data);
TEST_ASSERT_NOT_NULL(testRoot->left->left);
TEST_ASSERT_EQUAL(score4, *(int *)testRoot->left->left->data);
TEST_ASSERT_NOT_NULL(testRoot->left->right);
TEST_ASSERT_EQUAL(score5, *(int *)testRoot->left->right->data);
TEST_ASSERT_NOT_NULL(testRoot->right->left);
TEST_ASSERT_EQUAL(score6, *(int *)testRoot->right->left->data);
TEST_ASSERT_NOT_NULL(testRoot->right->right);
TEST_ASSERT_EQUAL(score7, *(int *)testRoot->right->right->data);
// Adding Double
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
TEST_ASSERT_NOT_NULL(testRoot->left->left->left);
TEST_ASSERT_EQUAL_UINT16(score4, *(int *)testRoot->left->left->left->data);
// Trying to add Double while Doubles not Permitted
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, &testIsDouble);
TEST_ASSERT_NULL(testRoot->right->right->left);
TEST_ASSERT_EQUAL_UINT16(1, testIsDouble);
clearTree(testRoot);
} }
// test if nextTreeData returns the next pice of data correctly // test if nextTreeData returns the next pice of data correctly
// needs Stack!!! // needs Stack!!!
void test_nextTreeDataReturnsNextDataCorrectly(void)
{
TreeNode *testRoot = NULL;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
// Prepare a Tree
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
// Create Stack
StackNode *entry = nextTreeData(testRoot);
// check if nextTreeData returns Data correctly
TEST_ASSERT_NOT_NULL(entry);
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));
TEST_ASSERT_EQUAL(score5, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score2, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score4, *(int *)nextTreeData(NULL));
clearTree(testRoot);
}
// test if clear Tree frees all node.name and node memory AND sets them to zero // test if clear Tree frees all node.name and node memory AND sets them to zero
// aditionally tests if the memoryspaces have been cleared // aditionally tests if the memoryspaces have been cleared
void test_clearTreeworksLikeExpected(void)
{
TreeNode *testRoot = NULL;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
// Save all Adresses
TreeNode *node1 = testRoot;
TreeNode *node2 = testRoot->left;
TreeNode *node3 = testRoot->left->left;
TreeNode *node4 = testRoot->left->right;
TreeNode *node5 = testRoot->right;
TreeNode *node6 = testRoot->right->left;
TreeNode *node7 = testRoot->right->right;
clearTree(testRoot);
// Check if everything has been set to NULL
TEST_ASSERT_NULL(node1->data);
TEST_ASSERT_NULL(node1->data);
TEST_ASSERT_NULL(node2->data);
TEST_ASSERT_NULL(node3->data);
TEST_ASSERT_NULL(node4->data);
TEST_ASSERT_NULL(node5->data);
TEST_ASSERT_NULL(node6->data);
TEST_ASSERT_NULL(node7->data);
}
// tests if treeSize returns correct amount of nodes in Tree // tests if treeSize returns correct amount of nodes in Tree
// by using addNode a given number of times and testing to see if // by using addToTree a given number of times and testing to see if
// the treeSize matches the number of nodes added // the treeSize matches the number of nodes added
void test_treeSizeWorkingLikeExpected(void)
{
TreeNode *testRoot = NULL;
int nodeCount = 7;
unsigned int testTreeSize = 0;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
// Fill Tree
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
testTreeSize = treeSize(testRoot);
TEST_ASSERT_EQUAL(nodeCount, testTreeSize);
clearTree(testRoot);
}
// main, strings together all tests // main, strings together all tests
int main() int main()
{ {
printf("\n============================\nBinary Tree tests\n============================\n"); UNITY_BEGIN();
printf("\n============================\nBinary Tree tests\n============================\n");
RUN_TEST(test_addToTreeExpandsTreeCorrectly);
RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
RUN_TEST(test_clearTreeworksLikeExpected);
RUN_TEST(test_treeSizeWorkingLikeExpected);
return UNITY_END();
} }
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+18 -2
View File
@@ -35,8 +35,24 @@ $(program_obj_filesobj_files): %.o: %.c
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests: # unitTests:
echo "needs to be implemented" # echo "needs to be implemented"
bintree: bintree.c
$(CC) $(FLAGS) -c bintree bintree.c
bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o stack.o $(unityfolder)/unity.c
stack: stack.c
$(CC) $(FLAGS) -c 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 # Clean
+96
View File
@@ -14,13 +14,109 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries. // Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while // Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers. // creating random numbers.
static int compareUInt(const void *a, const void *b)
{
unsigned int A = *(unsigned int*)a;
unsigned int B = *(unsigned int*)b;
if (A < B) return -1;
if (A > B) return 1;
return 0;
}
static int existsInTree(TreeNode *root, unsigned int value)
{
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;
}
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
if (len < 2) return NULL;
srand((unsigned int)time(NULL));
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
if (!numbers) return NULL;
TreeNode *root = NULL;
unsigned int value;
// Array mit eindeutigen Zufallszahlen füllen
for (unsigned int i = 0; i < len; i++)
{
do
{
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
unsigned int idx1 = rand() % len;
unsigned int idx2;
do
{
idx2 = rand() % len;
}
while (idx2 == idx1);
numbers[idx2] = numbers[idx1];
clearTree(root);
return numbers;
} }
// Returns only the only number in numbers which is present twice. Returns zero on errors. // 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) unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{ {
if (!numbers || len < 2) return 0;
unsigned int *copy = malloc(len * sizeof(unsigned int));
if (!copy) return 0;
// Array kopieren
memcpy(copy, numbers, len * sizeof(unsigned int));
// Sortieren
qsort(copy, len, sizeof(unsigned int), compareUInt);
// Doppelte Zahl finden
unsigned int duplicate = 0;
for (unsigned int i = 0; i < len - 1; i++)
{
if (copy[i] == copy[i + 1])
{
duplicate = copy[i];
break;
}
}
free(copy);
return duplicate;
} }
BIN
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+40
View File
@@ -10,24 +10,64 @@
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
StackNode *newNode = malloc(sizeof(StackNode));
if(newNode == NULL)
{
//printf("Fehler Bei Speicherallozierung!");
return NULL;
}
newNode->data = data;
newNode->next = stack;
return newNode;
} }
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if(stack == NULL)
{
//printf("Fehlerhafte Adresse uebergeben");
return NULL;
}
StackNode *next = stack->next;
free(stack);
return next;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
if(stack == NULL)
{
//printf("Fehlerhafte Adresse uebergeben");
return NULL;
}
return stack->data;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
if(stack == NULL)
{
//printf("Fehlerhafte Adresse uebergeben");
return;
}
StackNode *currentNode = stack;
StackNode *nextNode;
while(currentNode != NULL)
{
nextNode = currentNode->next;
free(currentNode);
currentNode = nextNode;
}
} }
+5
View File
@@ -8,6 +8,11 @@ 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 struct StackNode
{
void *data;
struct StackNode *next;
} StackNode;
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data); StackNode *push(StackNode *stack, void *data);
BIN
View File
Binary file not shown.
+63
View File
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "numbers.h"
#include "unity/unity.h"
void setUp(void) {
// gehört zu unit-Grundaufbau
}
void tearDown(void) {
// 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();
}
+120
View File
@@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "stack.h"
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
void testeStackBeschreiben()
{
//printf("=== Test: push() ===\n");
StackNode *stack = NULL;
int wert1 = 42;
stack = push(stack, &wert1);
int *topValue = (int *)(stack->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(42, *topValue);
int wert2 = 12;
stack = push(stack, &wert2);
topValue = (int *)(stack->data);
int *secondValue = (int *)((stack->next)->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(12, *topValue);
TEST_ASSERT_NOT_NULL(secondValue);
TEST_ASSERT_EQUAL_INT(42, *secondValue);
//printf("=== Ende Test: push() ===\n\n");
return;
}
void testepop()
{
//printf("=== Test: pop() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
stack = pop(stack);
int *topValue = (int *)(stack->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(20, *topValue);
//printf("=== Ende Test: pop() ===\n\n");
}
void testetop()
{
//printf("=== Test: top() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
int *topValue = top(stack);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(74, *topValue);
//printf("=== Ende Test: top() ===\n\n");
}
void testeclearStack()
{
//printf("=== Test: clearStack() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
clearStack(stack);
//printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
//printf("=== Ende Test: clearStack() ===\n\n");
}
int main()
{
UNITY_BEGIN();
//printf("...");
RUN_TEST(testeStackBeschreiben);
RUN_TEST(testepop);
RUN_TEST(testetop);
RUN_TEST(testeclearStack);
/*testeStackBeschreiben();
testepop();
testetop();
testeclearStack();*/
return UNITY_END();
}
BIN
View File
Binary file not shown.