Compare commits

...
10 Commits
Author SHA1 Message Date
schenkwa99189 7e75758d1f 1 2025-12-15 23:33:32 +01:00
schenkwa99189 593a86acaa formatierung 2025-12-15 22:58:45 +01:00
schenkwa99189 74e2421312 kompellierung fertig 2025-12-15 22:54:10 +01:00
schenkwa99189 ecd873b47c makefile 2025-12-15 12:04:55 +01:00
buschto99186 deb68dd71a test 2025-12-15 11:57:37 +01:00
buschto99186 d27b558ddc bugfixingV1 2025-12-15 11:52:02 +01:00
buschto99186 ef6946fa0b test_numbers 2025-12-15 11:25:35 +01:00
buschto99186 b7882d9e71 numbersFinal 2025-12-14 22:28:01 +01:00
schenkwa99189 19b9fec1bf klmnop 2025-12-14 22:12:19 +01:00
schenkwa99189 ec0909f4c9 ghj 2025-12-14 22:09:38 +01:00
22 changed files with 217 additions and 87 deletions

No files matched your search

BIN
View File
Binary file not shown.
+11 -10
View File
@@ -14,13 +14,14 @@ static TreeNode *createNode(const void *data, size_t dataSize)
if (!newNode)
return NULL;
newNode->data = malloc(dataSize);
newNode->data = malloc(dataSize); // hier kein malloc -> nur data pointer?
if (!newNode->data)
{
free(newNode);
return 0;
}
newNode->data = data;
// newNode->data = data;
memcpy(newNode->data, data, dataSize);
return newNode;
}
@@ -35,12 +36,11 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
*isDuplicate = 0;
}
if (!root)
{ // wurzerl == 0;
{ // wurzel == 0;
TreeNode *newNode = createNode(data, dataSize);
return newNode;
}
// wenn duplicate NUll -> doppelt hinzufügen
//
if (compareFct(data, root->data) < 0)
{
@@ -61,6 +61,8 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
}
return root;
}
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL,
@@ -123,12 +125,11 @@ void clearTree(TreeNode *root)
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
unsigned int treeSize = 0;
TreeNode* data = nextTreeData(root);
while (data != NULL)
if (root == NULL)
{
treeSize++;
data = nextTreeData(NULL);
return 0;
}
return treeSize;
return 1 + treeSize(root->left) + treeSize(root->right);
/// ich + alles links + alles rechts
}
+5 -4
View File
@@ -5,18 +5,19 @@
typedef int (*CompareFctType)(const void *arg1, const void *arg2); // ganz zahlen
void foo(void* ptr) {
/*void foo(void* ptr) {
unsigned int* casted_pointer = ptr;
unsigned int** a = &casted_pointer; //pointer pointer
unsigned int* b = casted_pointer; //pointer
unsigned int c = *casted_pointer; //wert
}
*/
typedef struct node
typedef struct nodeT
{
void *data;
struct node *left;
struct node *right;
struct nodeT *left;
struct nodeT *right;
} TreeNode;
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
BIN
View File
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
+3 -5
View File
@@ -1,5 +1,3 @@
player2;17901
player_name2;14920
player2;14844
player_name;6977
player1;3999
ÐÀ;1
ðÀ;0
°À;0
+3 -1
View File
@@ -14,7 +14,9 @@ int inputNumber(const char *promptText)
{
printf("%s", promptText);
numberOfInputs = scanf("%u", &number);
while(getchar() != '\n') {} // clear input buffer
while (getchar() != '\n')
{
} // clear input buffer
}
return number;
+46 -29
View File
@@ -1,8 +1,11 @@
CC = gcc
FLAGS = -g -Wall -lm
CFLAGS = -g -Wall
LIBS = -lm
ifeq ($(OS),Windows_NT)
include makefile_windows.variables
RM_CMD = del /f
EXT = .exe
else
UNAME = $(shell uname)
ifeq ($(UNAME),Linux)
@@ -10,53 +13,67 @@ else
else
include makefile_mac.variables
endif
RM_CMD = rm -f
EXT =
endif
raylibfolder = ./raylib
unityfolder = ./unity
# --------------------------
# Initiales Programm bauen (zum ausprobieren)
# --------------------------
doble_initial:
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
# --------------------------
# Selbst implementiertes Programm bauen
# Objekt-Dateien Definition
# --------------------------
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
# main.o separat oder hier hinzufügen
OBJ = main.o $(program_obj_files)
doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble
# --------------------------
# Initiales Programm
# --------------------------
doble_initial:
# Hinweis: $(BINARIES) muss in den include-Dateien definiert sein!
$(CC) -o doble_initial$(EXT) $(BINARIES)/libdoble_complete.a $(LIBS)
# HINWEIS: Hier war ein Tippfehler im Original (obj_filesobj_files), ich habe ihn korrigiert:
$(program_obj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@
# --------------------------
# Hauptprogramm (doble)
# --------------------------
doble: $(OBJ)
# WICHTIG: $(LIBS) ganz am Ende!
$(CC) $(CFLAGS) $^ -o doble$(EXT) $(LIBS)
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
# --------------------------
# Unit Tests
# --------------------------
# Name des Test-Programms
TEST_BIN = test_runner
TEST_BIN = test_runner$(EXT)
# Das Target 'unitTests' baut das Test-Programm und führt es aus
# Es benötigt unittest.o und stack.o (damit der Stack getestet werden kann)
unitTests: unittest.o stack.o
@echo "--- Erstelle Test-Executable ---"
$(CC) $(FLAGS) unittest.o stack.o -o $(TEST_BIN)
@echo "--- Starte Tests ---"
./$(TEST_BIN)
TEST_STACK_BIN = test_stack$(EXT)
TEST_NUMBERS_BIN = test_numbers$(EXT)
testStack: unittest.o stack.o unity/unity.o
@echo "--- Baue und starte Stack Tests ---"
$(CC) $(CFLAGS) unittest.o stack.o unity/unity.o -o $(TEST_STACK_BIN) $(LIBS)
./$(TEST_STACK_BIN)
testNumbers: unittestTree.o numbers.o bintree.o stack.o unity/unity.o
@echo "--- Baue und starte Numbers Tests ---"
$(CC) $(CFLAGS) unittestTree.o numbers.o bintree.o stack.o unity/unity.o -o $(TEST_NUMBERS_BIN) $(LIBS)
./$(TEST_NUMBERS_BIN)
# Regel, um unittest.c zu kompilieren
unittest.o: test_stack.c
$(CC) -c $(FLAGS) test_stack.c -o unittest.o
$(CC) -c $(CFLAGS) test_stack.c -o unittest.o
unittestTree.o: test_numbers.c
$(CC) -c $(CFLAGS) test_numbers.c -o unittestTree.o
unity/unity.o: unity/unity.c
$(CC) -c $(CFLAGS) unity/unity.c -o unity/unity.o
# --------------------------
# Clean
# --------------------------
clean:
ifeq ($(OS),Windows_NT)
del /f *.o doble $(TEST_BIN)
else
rm -f *.o doble $(TEST_BIN)
endif
$(RM_CMD) *.o doble$(EXT) doble_initial$(EXT) $(TEST_BIN)
+25 -15
View File
@@ -5,7 +5,7 @@
#include "numbers.h"
#include "bintree.h"
CompareFctType compareFct(const void *arg1, const void *arg2)
int compareFct(const void *arg1, const void *arg2)
{
const unsigned int *entry1 = arg1;
const unsigned int *entry2 = arg2;
@@ -28,18 +28,21 @@ unsigned int *createNumbers(unsigned int len)
{
srand(time(NULL));
TreeNode *numbers;
int isDuplicate = 0;
unsigned int number = rand() % (2 * len) + 1;
unsigned int *array = malloc(len * sizeof(int));
unsigned int *array = malloc(len * sizeof(unsigned int));
if (array == NULL)
return NULL;
numbers = addToTree(NULL, &number, sizeof(int), compareFct, &isDuplicate);
TreeNode *numbers = addToTree(NULL, &number, sizeof(unsigned int), compareFct, &isDuplicate);
array[0] = number;
for (int i = 1; i < len; i++)
for (unsigned int i = 1; i < len; i++)
{
number = rand() % (2 * len) + 1;
addToTree(numbers, &number, sizeof(int), compareFct, &isDuplicate);
numbers = addToTree(numbers, &number, sizeof(unsigned int), compareFct, &isDuplicate);
if (isDuplicate == 1)
{
isDuplicate = 0;
@@ -53,8 +56,8 @@ unsigned int *createNumbers(unsigned int len)
clearTree(numbers);
int duplicatePosition1 = rand() % len;
int duplicatePosition2 = rand() % len;
unsigned int duplicatePosition1 = rand() % len;
unsigned int duplicatePosition2 = rand() % len;
while (duplicatePosition2 == duplicatePosition1)
{
@@ -69,21 +72,28 @@ unsigned int *createNumbers(unsigned int len)
// 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 *copyNumbers = malloc(sizeof(int) * len);
for (int i = 0; i < len; i++)
unsigned int *copyNumbers = malloc(sizeof(unsigned int) * len);
if (copyNumbers == NULL)
return 0;
unsigned int duplicateValue = 0;
for (unsigned int i = 0; i < len; i++)
{
copyNumbers[i] = numbers[i];
}
qsort(copyNumbers, len, sizeof(int), compareFct);
qsort(copyNumbers, len, sizeof(unsigned int), compareFct);
for (int i = 0; i < len - 1; i++)
for (unsigned int i = 0; i < len - 1; i++)
{
if (copyNumbers[i] == copyNumbers[i + 1])
{
return copyNumbers[i];
duplicateValue = copyNumbers[i];
free(copyNumbers);
return duplicateValue;
}
}
free(copyNumbers);
return 0;
}
BIN
View File
Binary file not shown.
+2 -1
View File
@@ -8,7 +8,8 @@ The latest element is taken from the stack. */
#include <stdlib.h>
// TODO: passenden Datentyp als struct anlegen
typedef struct node {
typedef struct node
{
void *data;
struct node *prev;
} StackNode;
BIN
View File
Binary file not shown.
+73
View File
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
#include "numbers.h"
#include "bintree.h"
#include "unity/unity.h"
void test_getDuplicateLogic()
{
unsigned int test1[] = {1, 9, 3, 5, 2, 5, 8};
unsigned int len1 = 7;
unsigned int res1 = getDuplicate(test1, len1);
TEST_ASSERT_EQUAL_UINT32(5, res1);
unsigned int test2[] = {10, 1, 2, 3, 10};
unsigned int len2 = 5;
unsigned int res2 = getDuplicate(test2, len2);
TEST_ASSERT_EQUAL_UINT32(10, res2);
unsigned int test3[] = {1, 2, 3, 4, 5};
unsigned int len3 = 5;
unsigned int res3 = getDuplicate(test3, len3);
TEST_ASSERT_EQUAL_UINT32(0, res3);
}
void test_Integration(unsigned int len)
{
unsigned int *arr = createNumbers(len);
TEST_ASSERT_NOT_NULL(arr);
unsigned int duplicate = getDuplicate(arr, len);
TEST_ASSERT_NOT_EQUAL_UINT32(0, duplicate);
int count = 0;
for (unsigned int i = 0; i < len; i++)
{
if (arr[i] == duplicate)
{
count++;
}
}
TEST_ASSERT_EQUAL_UINT32(2, count);
free(arr);
}
void test_IntegrationVar()
{
test_Integration(10);
test_Integration(100);
test_Integration(1000);
}
void setUp(void)
{
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void)
{
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
int main()
{
UNITY_BEGIN();
printf("============================\nNumbers tests\n============================\n");
RUN_TEST(test_getDuplicateLogic);
RUN_TEST(test_IntegrationVar);
return UNITY_END();
}
BIN
View File
Binary file not shown.
+37 -14
View File
@@ -2,17 +2,22 @@
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#include "unity/unity.h"
void test_pushFailsOnNullPointer(StackNode *stack, void *data) {
void test_pushFailsOnNullPointer(StackNode *stack, void *data)
{
StackNode *test = push(NULL, data);
if (test != 0) {
if (test != 0)
{
printf("Pass test_pushFailsOnNullPointerStack\n");
} else
}
else
printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED StackNode\n");
test = push(stack, NULL);
if (test == 0) {
if (test == 0)
{
printf("Pass test_pushFailsOnNullPointerData\n");
return;
}
@@ -20,10 +25,12 @@ void test_pushFailsOnNullPointer(StackNode *stack, void *data) {
return;
}
void test_popFailsOnNullPointer() { //pop on null returns NULL
void test_popFailsOnNullPointer()
{ // pop on null returns NULL
StackNode *test = pop(NULL);
if (test == 0) {
if (test == 0)
{
printf("Pass test_popFailsOnNullPointerStack\n");
return;
}
@@ -32,10 +39,12 @@ void test_popFailsOnNullPointer() { //pop on null returns NULL
return;
}
void test_topFailsOnNullPointer() {
void test_topFailsOnNullPointer()
{
int *test = (int *)top(NULL);
if (test == 0) {
if (test == 0)
{
printf("Pass test_topFailsOnNullPointerStack\n");
return;
}
@@ -44,6 +53,14 @@ void test_topFailsOnNullPointer() {
return;
}
void setUp(void)
{
}
void tearDown(void)
{
}
int main()
{
@@ -63,23 +80,29 @@ int main()
test_topFailsOnNullPointer();
StackNode *stack1 = push(stack0, dataTest1);
if(strcmp(stack1->data ,dataTest1) == 0) {
if (strcmp(stack1->data, dataTest1) == 0)
{
printf("Pass test_pushString\n");
} else
}
else
printf("Fails test_pushString\n expected: %s\n was: %s\n", dataTest1, (char *)(stack1->data));
StackNode *stack2 = push(stack1, dataTest2);
if(stack2->data == dataTest2) {
if (stack2->data == dataTest2)
{
printf("Pass test_pushFloat\n");
} else
}
else
printf("Fails test_pushFloat\n expected: %f\n was: %f\n", *dataTest2, *(float *)(stack2->data));
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(size_t i = 0; i<10; i++) {
for (size_t i = 0; i < 10; i++)
{
stack2 = push(stack2, &array[i]);
}
for(size_t i = 0; i<10; i++) {
for (size_t i = 0; i < 10; i++)
{
int *data = (int *)top(stack2);
printf("%d\n", *data);
stack2 = pop(stack2);
BIN
View File
Binary file not shown.
+2 -1
View File
@@ -21,7 +21,8 @@ double stopTimer()
double measuredSeconds = (double)delta_us / 1000000.;
if(start.tv_nsec > 0) {
if (start.tv_nsec > 0)
{
start.tv_nsec = 0;
start.tv_sec = 0;
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+4 -3
View File
@@ -4,7 +4,6 @@
#include "stack.h"
#include "unity.h"
void test_createMatrixFailsOnZeroDimensions(void)
{
Matrix matrixToTest1 = createMatrix(0, 1);
@@ -167,11 +166,13 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer) / sizeof(MatrixType));
}
void setUp(void) {
void setUp(void)
{
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
void tearDown(void)
{
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
+6 -4
View File
@@ -112,7 +112,12 @@ void verifyTest(void);
/* It is not necessary for you to call PASS. A PASS condition is assumed if nothing fails.
* This method allows you to abort a test immediately with a PASS state, ignoring the remainder of the test. */
#define TEST_PASS() TEST_ABORT()
#define TEST_PASS_MESSAGE(message) do { UnityMessage((message), __LINE__); TEST_ABORT(); } while (0)
#define TEST_PASS_MESSAGE(message) \
do \
{ \
UnityMessage((message), __LINE__); \
TEST_ABORT(); \
} while (0)
/*-------------------------------------------------------
* Build Directives
@@ -295,7 +300,6 @@ void verifyTest(void);
#define TEST_ASSERT_HEX64_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_HEX64_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, NULL)
#define TEST_ASSERT_CHAR_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_CHAR_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, NULL)
/* Structs and Strings */
#define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, NULL)
@@ -466,7 +470,6 @@ void verifyTest(void);
#define TEST_ASSERT_NOT_EQUAL_HEX64_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_HEX64((threshold), (actual), __LINE__, (message))
#define TEST_ASSERT_NOT_EQUAL_CHAR_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_CHAR((threshold), (actual), __LINE__, (message))
/* Integer Greater Than/ Less Than (of all sizes) */
#define TEST_ASSERT_GREATER_THAN_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_GREATER_THAN_INT((threshold), (actual), __LINE__, (message))
#define TEST_ASSERT_GREATER_THAN_INT_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_GREATER_THAN_INT((threshold), (actual), __LINE__, (message))
@@ -578,7 +581,6 @@ void verifyTest(void);
#define TEST_ASSERT_HEX64_ARRAY_WITHIN_MESSAGE(delta, expected, actual, num_elements, message) UNITY_TEST_ASSERT_HEX64_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, (message))
#define TEST_ASSERT_CHAR_ARRAY_WITHIN_MESSAGE(delta, expected, actual, num_elements, message) UNITY_TEST_ASSERT_CHAR_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, (message))
/* Structs and Strings */
#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, (message))
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, (message))
BIN
View File
Binary file not shown.