17 Commits
Author SHA1 Message Date
Fabrice 1bbefdb72a Merge branch 'main' into Fabrice 2025-12-07 11:23:07 +01:00
Fabrice a816abfe04 numbers.c finished 2025-12-05 09:28:40 +01:00
wiesendsi102436 39976279e5 reset bintree and numbers 2025-12-05 08:42:58 +01:00
wiesendsi102436 b0284fc53c Merge pull request 'implementation for stack and some tests' (#1) from simon into main
Reviewed-on: #1
2025-12-05 07:40:44 +00:00
Fabrice 74b541a1a0 numbers.c angafangen 2025-12-05 08:05:57 +01:00
wiesendsi102436 fc3933a993 fix: memory leak 2025-11-30 12:15:06 +01:00
wiesendsi102436 5050836020 fix: malloc error handling 2025-11-30 12:12:45 +01:00
wiesendsi102436 1c27338be2 fix: timers on Linux
On Linux the clock() function measures cpu time instead of wall time. This change uses the Apple code path for Linux.
2025-11-30 10:34:01 +01:00
wiesendsi102436 942b38e75d set the seed for the RNG just once at the beginning of the program
this improves randomness for tests where createNumbers() is called in rapid succession
2025-11-30 09:17:08 +01:00
wiesendsi102436 a900d2d147 initial implementation for numbers.c with some testing in test_numbers.c
numbers.c currently uses the qsort() function from stdlib
2025-11-30 09:06:21 +01:00
wiesendsi102436 07262a1fe0 fix makefile to allow debugging 2025-11-29 21:25:04 +01:00
wiesendsi102436 34e5a59196 remove redundant assignment 2025-11-29 21:24:11 +01:00
wiesendsi102436 ab71a3dd74 setup first test for bintree.c 2025-11-29 17:21:51 +01:00
wiesendsi102436 fd51eef1fe initial bintree.c 2025-11-29 17:21:11 +01:00
wiesendsi102436 a48cb6560d update gitignore 2025-11-29 12:57:49 +01:00
wiesendsi102436 ef59987f08 implement stack with some initial testing 2025-11-29 12:57:07 +01:00
wiesendsi102436 fe4c130d72 create initial gitignore with obvious stuff 2025-11-29 09:58:23 +01:00
10 changed files with 250 additions and 28 deletions
+5
View File
@@ -0,0 +1,5 @@
*doble*
*.o
*.exe
.vscode
run*Tests
Binary file not shown.
+6 -1
View File
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "numbers.h"
#include "timer.h"
#include "highscore.h"
@@ -39,6 +40,9 @@ int main(int argc, char *argv[])
{
int exitCode = EXIT_FAILURE;
// set seed
srand(time(NULL));
if(argc != 2)
{
fprintf(stderr, "Usage: %s <player name>\n", argv[0]);
@@ -67,7 +71,7 @@ int main(int argc, char *argv[])
userInput = inputNumber("Welche Zahl kommt doppelt vor: ");
measuredSeconds = stopTimer();
duplicate = getDuplicate(numbers, numberOfElements);
duplicate = getDuplicate(numbers, numberOfElements+1);
// check result and update highscores
if(userInput == duplicate)
@@ -83,6 +87,7 @@ int main(int argc, char *argv[])
saveHighscores(highscorePath);
clearHighscores();
free(numbers);
exitCode = EXIT_SUCCESS;
}
+18 -5
View File
@@ -1,5 +1,5 @@
CC = gcc
FLAGS = -g -Wall -lm
CFLAGS = -g -Wall -lm
ifeq ($(OS),Windows_NT)
include makefile_windows.variables
@@ -27,16 +27,29 @@ doble_initial:
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble
$(CC) $(CFLAGS) $^ -o doble
$(program_obj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@
$(CC) -c $(CFLAGS) $^ -o $@
# --------------------------
# Unit Tests
# --------------------------
unitTests:
echo "needs to be implemented"
TEST_STACK_SOURCES = stack.c test_stack.c $(unityfolder)/unity.c
TEST_BINTREE_SOURCES = bintree.c test_bintree.c stack.c $(unityfolder)/unity.c
TEST_NUMBERS_SOURCES = stack.c numbers.c bintree.c $(unityfolder)/unity.c test_numbers.c
stackTests: $(TEST_STACK_SOURCES) stack.h
$(CC) $(CFLAGS) -I$(unityfolder) $(TEST_STACK_SOURCES) -o runStackTests
./runStackTests
bintreeTests: $(TEST_BINTREE_SOURCES) stack.h bintree.h
$(CC) $(CFLAGS) -I$(unityfolder) $(TEST_BINTREE_SOURCES) -o runBintreeTests
./runBintreeTests
numbersTests: $(TEST_NUMBERS_SOURCES) stack.h bintree.h numbers.h
$(CC) $(CFLAGS) -I$(unityfolder) $(TEST_NUMBERS_SOURCES) -o runNumbersTests
./runNumbersTests
# --------------------------
# Clean
+50 -13
View File
@@ -5,22 +5,59 @@
#include "numbers.h"
#include "bintree.h"
//TODO: getDuplicate und createNumbers implementieren
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
* Duplizieren eines zufälligen Eintrags im Array.
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
// 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.
//Speicher für Array erstellen, zufällige Zahlen von 1-2xlen erzeugen, mittels Binärbaum checken, ob Zahlen einzigartig sind
//Eine Zahl duplizieren, an zufälliger Stelle einfügen und die Zahl an der Stelle ans Ende schieben
unsigned int *createNumbers(unsigned int len)
{
unsigned int *numbers = malloc (sizeof(unsigned int) * len);
unsigned int upperLimit = len * 2;
int isDuplicate = 0;
TreeNode *binTree = NULL;
}
// 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)
for (unsigned int i = 0; i < len; i++) {
do
{
numbers[i] = rand () % upperLimit + 1;
binTree = addToTree(binTree, &numbers[i], sizeof(unsigned int), compare, &isDuplicate);
} while (isDuplicate);
}
unsigned int duplicate = numbers[rand () % len];
int indexDuplicate = rand () % len;
numbers[len] = numbers[indexDuplicate];
numbers[indexDuplicate] = duplicate;
return numbers;
}
//Vergleichsfunktion von qsort
const int compare (const void *a, const void *b) {
unsigned int *x = a;
unsigned int *y = b;
if (*x < *y) {
return -1;
}
else if (*x > *y) {
return 1;
}
else {
return 0;
}
}
//Sortiert Zahlen mit qsort, vergleicht dann benachbarte Elemente und gibt bei Erfolg die doppelte Zahl zurück
unsigned int getDuplicate(unsigned int numbers[], unsigned int len)
{
if (len < 2) {
return 0;
}
qsort(numbers, len, sizeof(unsigned int), compare);
for (int i = 0; i < len-1; i++) {
if (numbers[i] == numbers [i+1]) {
return numbers[i];
}
}
return 0;
}
+25 -3
View File
@@ -10,24 +10,46 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
// this is the new top node
StackNode *newTopNode = malloc(sizeof(StackNode));
if (newTopNode == NULL)
{
return NULL;
}
newTopNode->data = data;
newTopNode->next = stack;
return newTopNode;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
{
if (!stack)
{
return NULL;
}
StackNode *nextNode = stack->next;
free(stack);
return nextNode;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (!stack)
{
return NULL;
}
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while (pop(stack))
;
}
+5 -1
View File
@@ -7,7 +7,11 @@ The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode
{
struct StackNode *next;
void *data;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
+39
View File
@@ -0,0 +1,39 @@
#include "unity.h"
#include "bintree.h"
#include "string.h"
void setUp(void)
{
// set stuff up here
}
void tearDown(void)
{
// set stuff up here
}
// this adds some strings and checks if they are returned in the right order
void test_insert_and_retrieve(void)
{
char *data1 = "a_this";
char *data2 = "b_is";
char *data3 = "c_testdata";
TreeNode *root = addToTree(NULL, data1, strlen(data1) + 1, (CompareFctType)&strcmp, NULL);
addToTree(root, data2, strlen(data2) + 1, (CompareFctType)&strcmp, NULL);
addToTree(root, data3, strlen(data3) + 1, (CompareFctType)&strcmp, NULL);
TEST_ASSERT_EQUAL_STRING(data1, (char *)nextTreeData(root));
TEST_ASSERT_EQUAL_STRING(data2, (char *)nextTreeData(NULL));
TEST_ASSERT_EQUAL_STRING(data3, (char *)nextTreeData(NULL));
clearTree(root);
}
int main(void)
{
printf("============================\nBintree tests\n============================\n");
UNITY_BEGIN();
RUN_TEST(test_insert_and_retrieve);
return UNITY_END();
}
+50
View File
@@ -0,0 +1,50 @@
#include "unity.h"
// #include "bintree.h"
// #include "string.h"
#include "numbers.h"
#include "stdlib.h"
void setUp(void)
{
// set stuff up here
}
void tearDown(void)
{
// set stuff up here
}
// getDuplicate on array without duplicats
// expects 0/error
void test_get_duplicate_error(void)
{
unsigned int input[] = {1, 5, 9, 2, 4};
unsigned int len = sizeof(input) / sizeof(input[0]);
TEST_ASSERT_EQUAL_UINT(0, getDuplicate(input, len));
}
// this tries to brute force a triple
void test_for_triple(void)
{
// this test is less effective if srand is called inside createNumbers()
for (int i = 0; i < 100000; i++)
{
unsigned int *numbers = createNumbers(3);
if (numbers[0] == numbers[1] && numbers[1] == numbers[2])
{
// fail the test
TEST_ASSERT(0);
}
free(numbers);
}
}
int main(void)
{
printf("============================\nNumbers tests\n============================\n");
UNITY_BEGIN();
RUN_TEST(test_get_duplicate_error);
RUN_TEST(test_for_triple);
return UNITY_END();
}
+47
View File
@@ -0,0 +1,47 @@
#include "unity.h"
#include "stack.h"
int data1 = 10;
int data2 = 20;
int data3 = 30;
StackNode *stack = NULL;
void setUp(void)
{
// set stuff up here
}
void tearDown(void)
{
clearStack(stack);
}
void test_push_and_pop(void)
{
stack = push(stack, &data1);
stack = push(stack, &data2);
stack = push(stack, &data3);
TEST_ASSERT_EQUAL_PTR(top(stack), &data3);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(top(stack), &data2);
stack = pop(stack);
TEST_ASSERT_EQUAL_PTR(top(stack), &data1);
stack = pop(stack);
}
void test_handle_NULL(void)
{
TEST_ASSERT_NULL(pop(stack));
TEST_ASSERT_NULL(top(stack));
}
int main(void)
{
printf("============================\nStack tests\n============================\n");
UNITY_BEGIN();
RUN_TEST(test_push_and_pop);
RUN_TEST(test_handle_NULL);
return UNITY_END();
}