Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6586a25fdb | ||
|
|
6e233b6695 | ||
|
|
a01aa986e6 | ||
|
|
aae75ade4b | ||
|
|
5d976c50c1 | ||
|
|
92fa60f9dc | ||
|
|
748fd0d087 | ||
|
|
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
|
||||||
|
}
|
||||||
@@ -12,7 +12,6 @@
|
|||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
|
||||||
|
|
||||||
|
|
||||||
static TreeNode *root = NULL;
|
|
||||||
static StackNode *stackRoot = NULL;
|
static StackNode *stackRoot = NULL;
|
||||||
|
|
||||||
|
|
||||||
@@ -185,5 +184,7 @@ int treeSizeRec(const TreeNode *currentNode)
|
|||||||
nodeCount += treeSizeRec(currentNode->right);
|
nodeCount += treeSizeRec(currentNode->right);
|
||||||
return nodeCount + 1;
|
return nodeCount + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nodeCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ stack: stack.c
|
|||||||
|
|
||||||
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(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
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -14,13 +14,106 @@
|
|||||||
// 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;
|
||||||
|
}
|
||||||
|
// Sortiervergleich für qsort
|
||||||
|
int compareQsort(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
unsigned int A = *(const unsigned int*)a;
|
||||||
|
unsigned int B = *(const unsigned int*)b;
|
||||||
|
if (A < B) return -1;
|
||||||
|
if (A > B) return +1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len < 2) return NULL;
|
||||||
|
|
||||||
|
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
|
||||||
|
unsigned int value;
|
||||||
|
int isDuplicate;
|
||||||
|
|
||||||
|
//Array mit eindeutigen Zufallszahlen füllen
|
||||||
|
for (unsigned int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//genau eine Zufallszahl duplizieren
|
||||||
|
unsigned int idx1 = rand() % len;
|
||||||
|
unsigned int idx2 = rand() % len;
|
||||||
|
|
||||||
|
while (idx2 == idx1)
|
||||||
|
idx2 = rand() % len;
|
||||||
|
|
||||||
|
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.
|
// 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), compareQsort);
|
||||||
|
|
||||||
|
// 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,87 @@
|
|||||||
|
#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
|
||||||
|
}
|
||||||
|
|
||||||
|
// prüft, ob ein Array nur EIN Duplikat enthält
|
||||||
|
static int countDuplicates(const unsigned int *arr, unsigned int len) {
|
||||||
|
int count = 0;
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
for (unsigned int j = i + 1; j < len; j++) {
|
||||||
|
if (arr[i] == arr[j]) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prüfen, ob createNumbers ein korrektes Array liefert
|
||||||
|
static void test_createNumbers_basic(void)
|
||||||
|
{
|
||||||
|
unsigned int len = 100;
|
||||||
|
unsigned int *arr = createNumbers(len);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(arr); // prüft ob Speicher korrekt
|
||||||
|
|
||||||
|
// Prüfen: Länge stimmt
|
||||||
|
|
||||||
|
// Prüfen: Array enthält GENAU EIN Duplikat
|
||||||
|
int dupCount = countDuplicates(arr, len);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, dupCount);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 2: Prüfen, ob getDuplicate die richtige doppelte Zahl erkennt
|
||||||
|
static void test_getDuplicate_correctValue(void)
|
||||||
|
{
|
||||||
|
unsigned int len = 200;
|
||||||
|
unsigned int *arr = createNumbers(len);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(arr);
|
||||||
|
|
||||||
|
unsigned int duplicate = getDuplicate(arr, len);
|
||||||
|
|
||||||
|
// Manuelle Kontrolle: Der gefundene Wert muss tatsächlich doppelt vorkommen
|
||||||
|
int occurrences = 0;
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
if (arr[i] == duplicate) {
|
||||||
|
occurrences++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, occurrences);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST 3: getDuplicate gibt 0 aus bei Fehlern
|
||||||
|
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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user