Compare commits

..
12 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
schenkwa99189 833dd6b20c def 2025-12-14 22:05:23 +01:00
schenkwa99189 a8c68f80e4 abc 2025-12-14 21:58:12 +01:00
24 changed files with 875 additions and 732 deletions

No files matched your search

BIN
View File
Binary file not shown.
+13 -12
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,13 +36,12 @@ 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)
{
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
@@ -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,
@@ -121,14 +123,13 @@ void clearTree(TreeNode *root)
}
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode* 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
}
+8 -7
View File
@@ -3,27 +3,28 @@
#include <stdlib.h>
typedef int (*CompareFctType)(const void *arg1, const void *arg2); //ganz zahlen
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
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,
// push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root);
// Releases all memory resources (including data copies).
BIN
View File
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
+11 -11
View File
@@ -23,7 +23,7 @@ static int compareHighscoreEntries(const void *arg1, const void *arg2)
int result = entry2->score - entry1->score;
if(result == 0)
if (result == 0)
result = strcmp(entry1->name, entry2->name);
return result;
@@ -34,10 +34,10 @@ static HighscoreEntry createHighscoreEntry(const char *name, int score)
{
HighscoreEntry entry = {"", score};
if(name != NULL)
if (name != NULL)
{
strncpy(entry.name, name, MAX_PLAYER_NAME_LEN);
entry.name[MAX_PLAYER_NAME_LEN-1] = '\0';
entry.name[MAX_PLAYER_NAME_LEN - 1] = '\0';
}
return entry;
@@ -46,7 +46,7 @@ static HighscoreEntry createHighscoreEntry(const char *name, int score)
// Calculate score based on time used and number of shown numbers.
static int calculateScore(double timeInSeconds, unsigned int len)
{
return (1000.0 - timeInSeconds) * len;
return (1000.0 - timeInSeconds) * len;
}
// Load highscores from file into memory.
@@ -54,16 +54,16 @@ void loadHighscores(const char *path)
{
FILE *file = fopen(path, "r");
if(file != NULL)
if (file != NULL)
{
char buffer[MAX_LINE_LEN+1];
char buffer[MAX_LINE_LEN + 1];
while(fgets(buffer, MAX_LINE_LEN+1, file) != NULL)
while (fgets(buffer, MAX_LINE_LEN + 1, file) != NULL)
{
char *name = strtok(buffer, ";\n");
char *scoreStr = strtok(NULL, ";\n");
if(name != NULL && scoreStr != NULL)
if (name != NULL && scoreStr != NULL)
{
HighscoreEntry entry = createHighscoreEntry(name, strtol(scoreStr, NULL, 10));
highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL);
@@ -99,7 +99,7 @@ void showHighscores()
printf("|%*.*s%s%*.*s|\n", blankSpace, blankSpace, blanks, header, blankSpace, blankSpace, blanks);
printf("+%*.*s+\n", lineWidth, lineWidth, stripes);
for(int i = 0; i < NUMBER_OF_SHOWN_HIGHSCORES && entry != NULL; i++)
for (int i = 0; i < NUMBER_OF_SHOWN_HIGHSCORES && entry != NULL; i++)
{
printf("| %-*s | %*d |\n", MAX_PLAYER_NAME_LEN, entry->name, MAX_PLAYER_NAME_LEN, entry->score);
printf("+%*.*s+\n", lineWidth, lineWidth, stripes);
@@ -112,11 +112,11 @@ void saveHighscores(const char *path)
{
FILE *file = fopen(path, "w");
if(file != NULL)
if (file != NULL)
{
HighscoreEntry *entry = nextTreeData(highscoreTree);
for(int i = 0; i < NUMBER_OF_SHOWN_HIGHSCORES && entry != NULL; i++)
for (int i = 0; i < NUMBER_OF_SHOWN_HIGHSCORES && entry != NULL; i++)
{
fprintf(file, "%s;%d\n", entry->name, entry->score);
entry = nextTreeData(NULL);
+3 -5
View File
@@ -1,5 +1,3 @@
player2;17901
player_name2;14920
player2;14844
player_name;6977
player1;3999
ÐÀ;1
ðÀ;0
°À;0
+9 -7
View File
@@ -10,11 +10,13 @@ int inputNumber(const char *promptText)
unsigned int number;
int numberOfInputs = 0;
while(numberOfInputs != 1)
while (numberOfInputs != 1)
{
printf("%s", promptText);
numberOfInputs = scanf("%u", &number);
while(getchar() != '\n') {} // clear input buffer
while (getchar() != '\n')
{
} // clear input buffer
}
return number;
@@ -23,11 +25,11 @@ int inputNumber(const char *promptText)
// Print an array of numbers.
void showNumbers(const unsigned int *numbers, unsigned int len)
{
if(numbers != NULL)
if (numbers != NULL)
{
printf("Numbers:");
for(int i = 0; i < len; i++)
for (int i = 0; i < len; i++)
printf(" %5d", numbers[i]);
printf("\n");
@@ -39,7 +41,7 @@ int main(int argc, char *argv[])
{
int exitCode = EXIT_FAILURE;
if(argc != 2)
if (argc != 2)
{
fprintf(stderr, "Usage: %s <player name>\n", argv[0]);
exitCode = EXIT_FAILURE;
@@ -55,7 +57,7 @@ int main(int argc, char *argv[])
unsigned int numberOfElements = 0;
// ask until valid number of elements (3..1000)
while(numberOfElements < 3 || numberOfElements > 1000)
while (numberOfElements < 3 || numberOfElements > 1000)
numberOfElements = inputNumber("Wie viele Zahlen sollen gezeigt werden: ");
// create numbers and show them
@@ -70,7 +72,7 @@ int main(int argc, char *argv[])
duplicate = getDuplicate(numbers, numberOfElements);
// check result and update highscores
if(userInput == duplicate)
if (userInput == duplicate)
{
int score = addHighscore(playerName, measuredSeconds, numberOfElements);
printf("Sie haben die korrekte Zahl in %.6lf Sekunde(n) gefunden und %u Punkte erzielt.\n", measuredSeconds, score);
+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)
+42 -19
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;
@@ -24,35 +24,43 @@ CompareFctType compareFct(const void *arg1, const void *arg2)
// 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.
unsigned int* createNumbers(unsigned int len)
unsigned int *createNumbers(unsigned int len)
{
srand(time(NULL));
TreeNode *numbers;
int isDuplicate = 0;
int number = rand() % (2 * len) + 1;
int* array = malloc(len*sizeof(int));
unsigned int number = rand() % (2 * len) + 1;
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;
i--;
}else{
}
else
{
array[i] = number;
}
}
int duplicatePosition1 = rand() % len;
int duplicatePosition2 = rand() % len;
clearTree(numbers);
while(duplicatePosition2 == duplicatePosition1){
unsigned int duplicatePosition1 = rand() % len;
unsigned int duplicatePosition2 = rand() % len;
while (duplicatePosition2 == duplicatePosition1)
{
duplicatePosition2 = rand() % len;
}
@@ -64,13 +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)
{
qsort(numbers, len, sizeof(int), compareFct);
for(int i = 0; i < len-1; i++){
if(numbers[i] == numbers[i+1]){
return numbers[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(unsigned int), compareFct);
for (unsigned int i = 0; i < len - 1; i++)
{
if (copyNumbers[i] == copyNumbers[i + 1])
{
duplicateValue = copyNumbers[i];
free(copyNumbers);
return duplicateValue;
}
}
free(copyNumbers);
return 0;
}
BIN
View File
Binary file not shown.
+17 -17
View File
@@ -1,22 +1,22 @@
#include <stdlib.h>
#include "stack.h"
//TODO: grundlegende Stackfunktionen implementieren:
// TODO: grundlegende Stackfunktionen implementieren:
/* * `push`: legt ein Element oben auf den Stack,
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
* `pop`: entfernt das oberste Element,
* `top`: liefert das oberste Element zurück,
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
StackNode* push(StackNode* stack, void* data)
StackNode *push(StackNode *stack, void *data)
{
//if(!data)
//return 0; -> nicht notwendig
StackNode* newNode = calloc(1, sizeof(StackNode)); //Speicher reserviert für data und prev, jeweils nur zeiger
// if(!data)
// return 0; -> nicht notwendig
StackNode *newNode = calloc(1, sizeof(StackNode)); // Speicher reserviert für data und prev, jeweils nur zeiger
if (!newNode)
return NULL;
newNode->prev = stack;
newNode->data = data;
@@ -25,28 +25,28 @@ StackNode* push(StackNode* stack, void* data)
// 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)
StackNode *pop(StackNode *stack)
{
if(!stack)
if (!stack)
return NULL;
StackNode* prev = stack->prev;
StackNode *prev = stack->prev;
free(stack);
return prev;
}
// Returns the data of the top element.
void* top(StackNode* stack)
void *top(StackNode *stack)
{
if(!stack)
if (!stack)
return NULL;
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack) //Annahme: erstes Element: prev = NULL;
void clearStack(StackNode *stack) // Annahme: erstes Element: prev = NULL;
{
if(!stack)
if (!stack)
return;
clearStack(stack->prev);
+7 -6
View File
@@ -1,16 +1,17 @@
#ifndef STACK_H
#define STACK_H
/* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
This means that with each new element all other elements are pushed deeper into the stack.
/* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
This means that with each new element all other elements are pushed deeper into the stack.
The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef struct node {
void* data;
struct node* prev;
// TODO: passenden Datentyp als struct anlegen
typedef struct node
{
void *data;
struct node *prev;
} StackNode;
// Pushes data as pointer onto the stack.
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.
+54 -31
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) {
StackNode* test = push(NULL, data);
if (test != 0) {
void test_pushFailsOnNullPointer(StackNode *stack, void *data)
{
StackNode *test = push(NULL, data);
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,42 +25,54 @@ void test_pushFailsOnNullPointer(StackNode *stack, void *data) {
return;
}
void test_popFailsOnNullPointer() { //pop on null returns NULL
StackNode* test = pop(NULL);
if (test == 0) {
void test_popFailsOnNullPointer()
{ // pop on null returns NULL
StackNode *test = pop(NULL);
if (test == 0)
{
printf("Pass test_popFailsOnNullPointerStack\n");
return;
}
printf("Did Not Pass test_popFailsOnNullPointerStack EXPECTED 0\n");
return;
}
void test_topFailsOnNullPointer() {
int* test = (int*)top(NULL);
if (test == 0) {
void test_topFailsOnNullPointer()
{
int *test = (int *)top(NULL);
if (test == 0)
{
printf("Pass test_topFailsOnNullPointerStack\n");
return;
}
printf("Did Not Pass test_topFailsOnNullPointerStack EXPECTED 0\n");
return;
}
void setUp(void)
{
}
void tearDown(void)
{
}
int main()
{
int test0 = 3;
int* dataTest0 = &test0;
int *dataTest0 = &test0;
StackNode *stack0 = push(NULL, dataTest0);
char test1[5] = "test\0";
char* dataTest1 = test1;
char *dataTest1 = test1;
float test2 = 3.14;
float* dataTest2 = &test2;
float *dataTest2 = &test2;
printf("============================\nstack tests\n============================\n");
test_pushFailsOnNullPointer(stack0, dataTest0);
@@ -63,24 +80,30 @@ 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
printf("Fails test_pushString\n expected: %s\n was: %s\n", dataTest1, (char*)(stack1->data));
}
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
printf("Fails test_pushFloat\n expected: %f\n was: %f\n", *dataTest2, *(float*)(stack2->data));
}
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};
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++) {
int* data = (int*)top(stack2);
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.
+4 -3
View File
@@ -14,14 +14,15 @@ void startTimer()
double stopTimer()
{
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long long delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
double measuredSeconds = (double)delta_us / 1000000.;
if(start.tv_nsec > 0) {
if (start.tv_nsec > 0)
{
start.tv_nsec = 0;
start.tv_sec = 0;
}
@@ -45,7 +46,7 @@ double stopTimer()
{
double measuredSeconds = (clock() - (double)startClocks) / CLOCKS_PER_SEC;
if(startClocks > 0)
if (startClocks > 0)
startClocks = 0;
else
measuredSeconds = -1;
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+26 -25
View File
@@ -4,7 +4,6 @@
#include "stack.h"
#include "unity.h"
void test_createMatrixFailsOnZeroDimensions(void)
{
Matrix matrixToTest1 = createMatrix(0, 1);
@@ -21,7 +20,7 @@ void test_createMatrixReturnsCorrectMatrixDimensions(void)
{
const unsigned int expectedRows = 15;
const unsigned int expectedCols = 10;
Matrix matrixToTest = createMatrix(expectedRows, expectedCols);
TEST_ASSERT_EQUAL_UINT32(expectedRows, matrixToTest.rows);
TEST_ASSERT_EQUAL_UINT32(expectedCols, matrixToTest.cols);
@@ -41,8 +40,8 @@ void test_addReturnsCorrectResult(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
Matrix matrix1 = {.rows = 2, .cols = 3, .buffer = buffer1};
Matrix matrix2 = {.rows = 2, .cols = 3, .buffer = buffer2};
Matrix result = add(matrix1, matrix2);
@@ -52,7 +51,7 @@ void test_addReturnsCorrectResult(void)
TEST_ASSERT_EQUAL_UINT32(matrix2.rows, result.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result.cols);
TEST_ASSERT_EQUAL_UINT32(matrix2.cols, result.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result.rows * result.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults) / sizeof(expectedResults[0]), result.rows * result.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows);
free(result.buffer);
}
@@ -61,9 +60,9 @@ void test_addFailsOnDifferentInputDimensions(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=2, .buffer=buffer2};
Matrix matrix1 = {.rows = 2, .cols = 3, .buffer = buffer1};
Matrix matrix2 = {.rows = 3, .cols = 2, .buffer = buffer2};
Matrix result = add(matrix1, matrix2);
TEST_ASSERT_NULL(result.buffer);
@@ -75,8 +74,8 @@ void test_addSupportsBroadcasting(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=1, .buffer=buffer2};
Matrix matrix1 = {.rows = 2, .cols = 3, .buffer = buffer1};
Matrix matrix2 = {.rows = 2, .cols = 1, .buffer = buffer2};
Matrix result1 = add(matrix1, matrix2);
Matrix result2 = add(matrix2, matrix1);
@@ -88,9 +87,9 @@ void test_addSupportsBroadcasting(void)
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result2.rows);
TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result2.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result1.rows * result1.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults) / sizeof(expectedResults[0]), result1.rows * result1.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result1.buffer, result1.cols * result1.rows);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result2.rows * result2.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults) / sizeof(expectedResults[0]), result2.rows * result2.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows);
free(result1.buffer);
@@ -101,16 +100,16 @@ void test_multiplyReturnsCorrectResults(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=3, .cols=4, .buffer=buffer2};
Matrix matrix1 = {.rows = 2, .cols = 3, .buffer = buffer1};
Matrix matrix2 = {.rows = 3, .cols = 4, .buffer = buffer2};
Matrix result = multiply(matrix1, matrix2);
float expectedResults[] = {74, 80, 86, 92, 173, 188, 203, 218};
TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows);
TEST_ASSERT_EQUAL_UINT32(matrix2.cols, result.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults)/sizeof(expectedResults[0]), result.rows * result.cols);
TEST_ASSERT_EQUAL_INT(sizeof(expectedResults) / sizeof(expectedResults[0]), result.rows * result.cols);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows);
free(result.buffer);
@@ -120,8 +119,8 @@ void test_multiplyFailsOnWrongInputDimensions(void)
{
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
MatrixType buffer2[] = {7, 8, 9, 10, 11, 12};
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
Matrix matrix1 = {.rows = 2, .cols = 3, .buffer = buffer1};
Matrix matrix2 = {.rows = 2, .cols = 3, .buffer = buffer2};
Matrix result = multiply(matrix1, matrix2);
@@ -133,7 +132,7 @@ void test_multiplyFailsOnWrongInputDimensions(void)
void test_getMatrixAtReturnsCorrectResult(void)
{
MatrixType buffer[] = {1, 2, 3, 4, 5, 6};
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
Matrix matrixToTest = {.rows = 2, .cols = 3, .buffer = buffer};
int expectedResult = buffer[4];
TEST_ASSERT_EQUAL_INT(expectedResult, getMatrixAt(matrixToTest, 1, 1));
@@ -142,7 +141,7 @@ void test_getMatrixAtReturnsCorrectResult(void)
void test_getMatrixAtFailsOnIndicesOutOfRange(void)
{
MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
Matrix matrixToTest = {.rows = 2, .cols = 3, .buffer = buffer};
TEST_ASSERT_EQUAL_INT(UNDEFINED_MATRIX_VALUE, getMatrixAt(matrixToTest, 2, 3));
}
@@ -151,7 +150,7 @@ void test_setMatrixAtSetsCorrectValue(void)
{
const int expectedResult = -1;
MatrixType buffer[] = {1, 2, 3, 4, 5, 6};
Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer};
Matrix matrixUnderTest = {.rows = 2, .cols = 3, .buffer = buffer};
setMatrixAt(expectedResult, matrixUnderTest, 1, 2);
TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]);
@@ -161,17 +160,19 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
{
const float expectedResults[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
Matrix matrixToTest = {.rows = 2, .cols = 3, .buffer = buffer};
setMatrixAt(-1, matrixToTest, 2, 3);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
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
}
+562 -560
View File
File diff suppressed because it is too large. Load diff
BIN
View File
Binary file not shown.