Compare commits
33
Commits
2368c9f3b9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a7a939fc4 | ||
|
|
44d96647bb | ||
|
|
8605da95b6 | ||
|
|
a3e12875aa | ||
|
|
b7fa24f2e9 | ||
|
|
b69cfb0640 | ||
|
|
686fe8d844 | ||
|
|
c37ce6327c | ||
|
|
111b9c5532 | ||
|
|
8e76b27760 | ||
|
|
a575259cac | ||
|
|
d391d60597 | ||
|
|
07796c1390 | ||
|
|
25da0d20df | ||
|
|
6586a25fdb | ||
|
|
6e233b6695 | ||
|
|
a01aa986e6 | ||
|
|
aae75ade4b | ||
|
|
5d976c50c1 | ||
|
|
92fa60f9dc | ||
|
|
284cdbdcfb | ||
|
|
e3501e8550 | ||
|
|
6a9461bf5b | ||
|
|
ef3e0b7891 | ||
|
|
82b2fb283f | ||
|
|
602220ba21 | ||
|
|
748fd0d087 | ||
|
|
01bc613189 | ||
|
|
79a6eb7267 | ||
|
|
53c12ce0ed | ||
|
|
8deeb6417e | ||
|
|
a6c5060060 | ||
|
|
2689130b55 |
Vendored
+18
@@ -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
|
||||
}
|
||||
Vendored
+13
@@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
Vendored
+59
@@ -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.
@@ -6,13 +6,12 @@
|
||||
|
||||
|
||||
//TODO: binären Suchbaum implementieren
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv), Done
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
|
||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv), Done
|
||||
* `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;
|
||||
}
|
||||
|
||||
|
||||
+3
-4
@@ -109,14 +109,13 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
|
||||
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 *)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));
|
||||
@@ -124,7 +123,7 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
|
||||
TEST_ASSERT_EQUAL(score2, *(int *)nextTreeData(NULL));
|
||||
TEST_ASSERT_EQUAL(score4, *(int *)nextTreeData(NULL));
|
||||
|
||||
clearStack(entry);
|
||||
|
||||
clearTree(testRoot);
|
||||
}
|
||||
|
||||
@@ -215,7 +214,7 @@ int main()
|
||||
|
||||
printf("\n============================\nBinary Tree tests\n============================\n");
|
||||
RUN_TEST(test_addToTreeExpandsTreeCorrectly);
|
||||
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
|
||||
RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
|
||||
RUN_TEST(test_clearTreeworksLikeExpected);
|
||||
RUN_TEST(test_treeSizeWorkingLikeExpected);
|
||||
|
||||
|
||||
BIN
Binary file not shown.
@@ -38,17 +38,21 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# unitTests:
|
||||
# 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
|
||||
|
||||
bintree: bintree.c
|
||||
$(CC) $(FLAGS) -c bintree bintree.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
|
||||
|
||||
bintreeTests: bintree.o bintreeTests.c $(unityfolder)/unity.c
|
||||
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o $(unityfolder)/unity.c
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
|
||||
@@ -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 NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||
// 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)
|
||||
{
|
||||
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.
|
||||
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;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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();
|
||||
}
|
||||
+43
-44
@@ -1,24 +1,30 @@
|
||||
#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");
|
||||
//printf("=== Test: push() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 42;
|
||||
stack = push(stack, &wert1);
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
if(topValue != NULL && *topValue == 42)
|
||||
{
|
||||
printf("Test 1: Erstes Element erfolgreich gepusht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 1: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(42, *topValue);
|
||||
|
||||
|
||||
int wert2 = 12;
|
||||
@@ -27,23 +33,19 @@ void testeStackBeschreiben()
|
||||
topValue = (int *)(stack->data);
|
||||
int *secondValue = (int *)((stack->next)->data);
|
||||
|
||||
if(topValue != NULL && *topValue == 12.25 && secondValue != NULL && *secondValue == 42)
|
||||
{
|
||||
printf("Test 2: Zweites Element erfolgreich gepusht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 2: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
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");
|
||||
//printf("=== Ende Test: push() ===\n\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void testepop()
|
||||
{
|
||||
printf("=== Test: pop() ===\n");
|
||||
//printf("=== Test: pop() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -56,21 +58,15 @@ void testepop()
|
||||
|
||||
int *topValue = (int *)(stack->data);
|
||||
|
||||
if(topValue != NULL && *topValue == 20)
|
||||
{
|
||||
printf("Test: Erstes Element erfolgreich gelöscht!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test 1: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(20, *topValue);
|
||||
|
||||
printf("=== Ende Test: pop() ===\n\n");
|
||||
//printf("=== Ende Test: pop() ===\n\n");
|
||||
}
|
||||
|
||||
void testetop()
|
||||
{
|
||||
printf("=== Test: top() ===\n");
|
||||
//printf("=== Test: top() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -81,21 +77,15 @@ void testetop()
|
||||
|
||||
int *topValue = top(stack);
|
||||
|
||||
if(topValue != NULL && *topValue == 74)
|
||||
{
|
||||
printf("Test: top() gibt korrektes Element zurück!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Test: FEHLGESCHLAGEN!\n");
|
||||
}
|
||||
TEST_ASSERT_NOT_NULL(topValue);
|
||||
TEST_ASSERT_EQUAL_INT(74, *topValue);
|
||||
|
||||
printf("=== Ende Test: top() ===\n\n");
|
||||
//printf("=== Ende Test: top() ===\n\n");
|
||||
}
|
||||
|
||||
void testeclearStack()
|
||||
{
|
||||
printf("=== Test: clearStack() ===\n");
|
||||
//printf("=== Test: clearStack() ===\n");
|
||||
|
||||
StackNode *stack = NULL;
|
||||
int wert1 = 20;
|
||||
@@ -106,16 +96,25 @@ void testeclearStack()
|
||||
|
||||
clearStack(stack);
|
||||
|
||||
printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
|
||||
printf("=== Ende Test: clearStack() ===\n\n");
|
||||
//printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
|
||||
//printf("=== Ende Test: clearStack() ===\n\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
testeStackBeschreiben();
|
||||
UNITY_BEGIN();
|
||||
|
||||
//printf("...");
|
||||
|
||||
RUN_TEST(testeStackBeschreiben);
|
||||
RUN_TEST(testepop);
|
||||
RUN_TEST(testetop);
|
||||
RUN_TEST(testeclearStack);
|
||||
|
||||
/*testeStackBeschreiben();
|
||||
testepop();
|
||||
testetop();
|
||||
testeclearStack();
|
||||
testeclearStack();*/
|
||||
|
||||
return 0;
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user