From d802593175885ab599990c49b3fe9300e7e6f830 Mon Sep 17 00:00:00 2001 From: duernbergerjo100488 Date: Thu, 13 Nov 2025 14:39:24 +0100 Subject: [PATCH 1/2] Matrix in Matrix.h definiert. --- .vscode/settings.json | 3 + JD/main.c_.txt | 68 +++++++++++++++ JD/matrix.c_.txt | 35 ++++++++ JD/matrix.h_.txt | 19 ++++ JD/matrixTests.c_.txt | 197 ++++++++++++++++++++++++++++++++++++++++++ JD/unittests.h_.txt | 10 +++ matrix.h | 7 ++ 7 files changed, 339 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 JD/main.c_.txt create mode 100644 JD/matrix.c_.txt create mode 100644 JD/matrix.h_.txt create mode 100644 JD/matrixTests.c_.txt create mode 100644 JD/unittests.h_.txt diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..082b194 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "makefile.configureOnOpen": false +} \ No newline at end of file diff --git a/JD/main.c_.txt b/JD/main.c_.txt new file mode 100644 index 0000000..a95cba3 --- /dev/null +++ b/JD/main.c_.txt @@ -0,0 +1,68 @@ +#include +#include +#include "imageInput.h" +#include "mnistVisualization.h" +#include "neuralNetwork.h" + +int main(int argc, char *argv[]) +{ + const unsigned int windowWidth = 800; + const unsigned int windowHeight = 600; + + int exitCode = EXIT_FAILURE; + + if(argc == 3) + { + const char *pathToMnist = argv[1]; + const char *pathToModel = argv[2]; + GrayScaleImageSeries *series = readImages(pathToMnist); + + if(series != NULL) + { + NeuralNetwork model = {NULL, 0}; + printf("Loaded %u images ... \n", series->count); + + model = loadModel(pathToModel); + + if(model.numberOfLayers > 0) + { + unsigned char *predictions = NULL; + + printf("Processing %u images ...\n", series->count); + + predictions = predict(model, series->images, series->count); + + if(predictions != NULL) + { + showMnist(windowWidth, windowHeight, series, predictions); + free(predictions); + exitCode = EXIT_SUCCESS; + } + else + { + fprintf(stderr, "Error while processing images ...\n"); + exitCode = EXIT_FAILURE; + } + clearModel(&model); + } + else + { + fprintf(stderr, "Error while loading model from %s ...\n", pathToModel); + exitCode = EXIT_FAILURE; + } + } + else + { + fprintf(stderr, "Error while reading images from %s ...\n", pathToMnist); + exitCode = EXIT_FAILURE; + } + clearSeries(series); + } + else + { + fprintf(stderr, "Usage: %s \n", argv[0]); + exitCode = EXIT_FAILURE; + } + + return exitCode; +} \ No newline at end of file diff --git a/JD/matrix.c_.txt b/JD/matrix.c_.txt new file mode 100644 index 0000000..ad00628 --- /dev/null +++ b/JD/matrix.c_.txt @@ -0,0 +1,35 @@ +#include +#include +#include "matrix.h" + +// TODO Matrix-Funktionen implementieren + +Matrix createMatrix(unsigned int rows, unsigned int cols) +{ + +} + +void clearMatrix(Matrix *matrix) +{ + +} + +void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +{ + +} + +MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) +{ + +} + +Matrix add(const Matrix matrix1, const Matrix matrix2) +{ + +} + +Matrix multiply(const Matrix matrix1, const Matrix matrix2) +{ + +} \ No newline at end of file diff --git a/JD/matrix.h_.txt b/JD/matrix.h_.txt new file mode 100644 index 0000000..cc640d1 --- /dev/null +++ b/JD/matrix.h_.txt @@ -0,0 +1,19 @@ +#ifndef MATRIX_H +#define MATRIX_H + +#define UNDEFINED_MATRIX_VALUE 0 + +typedef float MatrixType; + +// TODO Matrixtyp definieren + + +Matrix createMatrix(unsigned int rows, unsigned int cols); +void clearMatrix(Matrix *matrix); +void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); +MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); +Matrix add(const Matrix matrix1, const Matrix matrix2); +Matrix multiply(const Matrix matrix1, const Matrix matrix2); + + +#endif diff --git a/JD/matrixTests.c_.txt b/JD/matrixTests.c_.txt new file mode 100644 index 0000000..80b1daa --- /dev/null +++ b/JD/matrixTests.c_.txt @@ -0,0 +1,197 @@ + +#include +#include +#include "matrix.h" +#include "unity.h" + + +void test_createMatrixFailsOnZeroDimensions(void) +{ + Matrix matrixToTest1 = createMatrix(0, 1); + Matrix matrixToTest2 = createMatrix(1, 0); + TEST_ASSERT_NULL(matrixToTest1.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.cols); + TEST_ASSERT_NULL(matrixToTest2.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.cols); +} + +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); + free(matrixToTest.buffer); +} + +void test_clearMatrixSetsMembersToNull(void) +{ + Matrix matrixToTest = createMatrix(10, 10); + clearMatrix(&matrixToTest); + TEST_ASSERT_NULL(matrixToTest.buffer); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.rows); + TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.cols); +} + +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 result = add(matrix1, matrix2); + + float expectedResults[] = {8, 10, 12, 14, 16, 18}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows); + 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_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); + free(result.buffer); +} + +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 result = add(matrix1, matrix2); + + TEST_ASSERT_NULL(result.buffer); + TEST_ASSERT_EQUAL_UINT32(0, result.rows); + TEST_ASSERT_EQUAL_UINT32(0, result.cols); +} + +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 result1 = add(matrix1, matrix2); + Matrix result2 = add(matrix2, matrix1); + + float expectedResults[] = {8, 9, 10, 12, 13, 14}; + + TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows); + TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols); + 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_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_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows); + + free(result1.buffer); + free(result2.buffer); +} + +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 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_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); + + free(result.buffer); +} + +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 result = multiply(matrix1, matrix2); + + TEST_ASSERT_NULL(result.buffer); + TEST_ASSERT_EQUAL_UINT32(0, result.rows); + TEST_ASSERT_EQUAL_UINT32(0, result.cols); +} + +void test_getMatrixAtReturnsCorrectResult(void) +{ + MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + int expectedResult = buffer[4]; + + TEST_ASSERT_EQUAL_INT(expectedResult, getMatrixAt(matrixToTest, 1, 1)); +} + +void test_getMatrixAtFailsOnIndicesOutOfRange(void) +{ + MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; + + TEST_ASSERT_EQUAL_INT(UNDEFINED_MATRIX_VALUE, getMatrixAt(matrixToTest, 2, 3)); +} + +void test_setMatrixAtSetsCorrectValue(void) +{ + const int expectedResult = -1; + MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; + Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer}; + + setMatrixAt(expectedResult, matrixUnderTest, 1, 2); + TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]); +} + +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}; + + setMatrixAt(-1, matrixToTest, 2, 3); + TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows); +} + +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("============================\nMatrix tests\n============================\n"); + RUN_TEST(test_createMatrixReturnsCorrectMatrixDimensions); + RUN_TEST(test_createMatrixFailsOnZeroDimensions); + RUN_TEST(test_clearMatrixSetsMembersToNull); + RUN_TEST(test_addReturnsCorrectResult); + RUN_TEST(test_addFailsOnDifferentInputDimensions); + RUN_TEST(test_addSupportsBroadcasting); + RUN_TEST(test_multiplyReturnsCorrectResults); + RUN_TEST(test_multiplyFailsOnWrongInputDimensions); + RUN_TEST(test_getMatrixAtReturnsCorrectResult); + RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange); + RUN_TEST(test_setMatrixAtSetsCorrectValue); + RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange); + + return UNITY_END(); +} \ No newline at end of file diff --git a/JD/unittests.h_.txt b/JD/unittests.h_.txt new file mode 100644 index 0000000..fc327ca --- /dev/null +++ b/JD/unittests.h_.txt @@ -0,0 +1,10 @@ +#ifndef UNITTTESTS_H +#define UNITTTESTS_H + +#include + +typedef int (*UnitTestType)(void); + +#define RUN_UNIT_TEST(fct) printf("%80s: %d\n", #fct, fct()) + +#endif diff --git a/matrix.h b/matrix.h index cc640d1..132c143 100644 --- a/matrix.h +++ b/matrix.h @@ -6,6 +6,13 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + unsigned int rows; + unsigned int cols; + MatrixType *buffer; +} Matrix; +// ***************************************************** + Matrix createMatrix(unsigned int rows, unsigned int cols); From ea24a8e34e0947d7958aa39e31116a956a186e7a Mon Sep 17 00:00:00 2001 From: duernbergerjo100488 Date: Thu, 13 Nov 2025 14:43:13 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=C3=9Cberfl=C3=BCssigen=20Ordner=20gel?= =?UTF-8?q?=C3=B6scht.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JD/main.c_.txt | 68 --------------- JD/matrix.c_.txt | 35 -------- JD/matrix.h_.txt | 19 ---- JD/matrixTests.c_.txt | 197 ------------------------------------------ JD/unittests.h_.txt | 10 --- 5 files changed, 329 deletions(-) delete mode 100644 JD/main.c_.txt delete mode 100644 JD/matrix.c_.txt delete mode 100644 JD/matrix.h_.txt delete mode 100644 JD/matrixTests.c_.txt delete mode 100644 JD/unittests.h_.txt diff --git a/JD/main.c_.txt b/JD/main.c_.txt deleted file mode 100644 index a95cba3..0000000 --- a/JD/main.c_.txt +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include "imageInput.h" -#include "mnistVisualization.h" -#include "neuralNetwork.h" - -int main(int argc, char *argv[]) -{ - const unsigned int windowWidth = 800; - const unsigned int windowHeight = 600; - - int exitCode = EXIT_FAILURE; - - if(argc == 3) - { - const char *pathToMnist = argv[1]; - const char *pathToModel = argv[2]; - GrayScaleImageSeries *series = readImages(pathToMnist); - - if(series != NULL) - { - NeuralNetwork model = {NULL, 0}; - printf("Loaded %u images ... \n", series->count); - - model = loadModel(pathToModel); - - if(model.numberOfLayers > 0) - { - unsigned char *predictions = NULL; - - printf("Processing %u images ...\n", series->count); - - predictions = predict(model, series->images, series->count); - - if(predictions != NULL) - { - showMnist(windowWidth, windowHeight, series, predictions); - free(predictions); - exitCode = EXIT_SUCCESS; - } - else - { - fprintf(stderr, "Error while processing images ...\n"); - exitCode = EXIT_FAILURE; - } - clearModel(&model); - } - else - { - fprintf(stderr, "Error while loading model from %s ...\n", pathToModel); - exitCode = EXIT_FAILURE; - } - } - else - { - fprintf(stderr, "Error while reading images from %s ...\n", pathToMnist); - exitCode = EXIT_FAILURE; - } - clearSeries(series); - } - else - { - fprintf(stderr, "Usage: %s \n", argv[0]); - exitCode = EXIT_FAILURE; - } - - return exitCode; -} \ No newline at end of file diff --git a/JD/matrix.c_.txt b/JD/matrix.c_.txt deleted file mode 100644 index ad00628..0000000 --- a/JD/matrix.c_.txt +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include "matrix.h" - -// TODO Matrix-Funktionen implementieren - -Matrix createMatrix(unsigned int rows, unsigned int cols) -{ - -} - -void clearMatrix(Matrix *matrix) -{ - -} - -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - -} - -MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ - -} - -Matrix add(const Matrix matrix1, const Matrix matrix2) -{ - -} - -Matrix multiply(const Matrix matrix1, const Matrix matrix2) -{ - -} \ No newline at end of file diff --git a/JD/matrix.h_.txt b/JD/matrix.h_.txt deleted file mode 100644 index cc640d1..0000000 --- a/JD/matrix.h_.txt +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef MATRIX_H -#define MATRIX_H - -#define UNDEFINED_MATRIX_VALUE 0 - -typedef float MatrixType; - -// TODO Matrixtyp definieren - - -Matrix createMatrix(unsigned int rows, unsigned int cols); -void clearMatrix(Matrix *matrix); -void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); -MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); -Matrix add(const Matrix matrix1, const Matrix matrix2); -Matrix multiply(const Matrix matrix1, const Matrix matrix2); - - -#endif diff --git a/JD/matrixTests.c_.txt b/JD/matrixTests.c_.txt deleted file mode 100644 index 80b1daa..0000000 --- a/JD/matrixTests.c_.txt +++ /dev/null @@ -1,197 +0,0 @@ - -#include -#include -#include "matrix.h" -#include "unity.h" - - -void test_createMatrixFailsOnZeroDimensions(void) -{ - Matrix matrixToTest1 = createMatrix(0, 1); - Matrix matrixToTest2 = createMatrix(1, 0); - TEST_ASSERT_NULL(matrixToTest1.buffer); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.rows); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest1.cols); - TEST_ASSERT_NULL(matrixToTest2.buffer); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.rows); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest2.cols); -} - -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); - free(matrixToTest.buffer); -} - -void test_clearMatrixSetsMembersToNull(void) -{ - Matrix matrixToTest = createMatrix(10, 10); - clearMatrix(&matrixToTest); - TEST_ASSERT_NULL(matrixToTest.buffer); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.rows); - TEST_ASSERT_EQUAL_UINT32(0, matrixToTest.cols); -} - -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 result = add(matrix1, matrix2); - - float expectedResults[] = {8, 10, 12, 14, 16, 18}; - - TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result.rows); - 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_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); - free(result.buffer); -} - -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 result = add(matrix1, matrix2); - - TEST_ASSERT_NULL(result.buffer); - TEST_ASSERT_EQUAL_UINT32(0, result.rows); - TEST_ASSERT_EQUAL_UINT32(0, result.cols); -} - -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 result1 = add(matrix1, matrix2); - Matrix result2 = add(matrix2, matrix1); - - float expectedResults[] = {8, 9, 10, 12, 13, 14}; - - TEST_ASSERT_EQUAL_UINT32(matrix1.rows, result1.rows); - TEST_ASSERT_EQUAL_UINT32(matrix1.cols, result1.cols); - 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_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_FLOAT_ARRAY(expectedResults, result2.buffer, result2.cols * result2.rows); - - free(result1.buffer); - free(result2.buffer); -} - -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 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_FLOAT_ARRAY(expectedResults, result.buffer, result.cols * result.rows); - - free(result.buffer); -} - -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 result = multiply(matrix1, matrix2); - - TEST_ASSERT_NULL(result.buffer); - TEST_ASSERT_EQUAL_UINT32(0, result.rows); - TEST_ASSERT_EQUAL_UINT32(0, result.cols); -} - -void test_getMatrixAtReturnsCorrectResult(void) -{ - MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; - Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; - int expectedResult = buffer[4]; - - TEST_ASSERT_EQUAL_INT(expectedResult, getMatrixAt(matrixToTest, 1, 1)); -} - -void test_getMatrixAtFailsOnIndicesOutOfRange(void) -{ - MatrixType buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; - - TEST_ASSERT_EQUAL_INT(UNDEFINED_MATRIX_VALUE, getMatrixAt(matrixToTest, 2, 3)); -} - -void test_setMatrixAtSetsCorrectValue(void) -{ - const int expectedResult = -1; - MatrixType buffer[] = {1, 2, 3, 4, 5, 6}; - Matrix matrixUnderTest = {.rows=2, .cols=3, .buffer=buffer}; - - setMatrixAt(expectedResult, matrixUnderTest, 1, 2); - TEST_ASSERT_EQUAL_INT(expectedResult, buffer[5]); -} - -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}; - - setMatrixAt(-1, matrixToTest, 2, 3); - TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows); -} - -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("============================\nMatrix tests\n============================\n"); - RUN_TEST(test_createMatrixReturnsCorrectMatrixDimensions); - RUN_TEST(test_createMatrixFailsOnZeroDimensions); - RUN_TEST(test_clearMatrixSetsMembersToNull); - RUN_TEST(test_addReturnsCorrectResult); - RUN_TEST(test_addFailsOnDifferentInputDimensions); - RUN_TEST(test_addSupportsBroadcasting); - RUN_TEST(test_multiplyReturnsCorrectResults); - RUN_TEST(test_multiplyFailsOnWrongInputDimensions); - RUN_TEST(test_getMatrixAtReturnsCorrectResult); - RUN_TEST(test_getMatrixAtFailsOnIndicesOutOfRange); - RUN_TEST(test_setMatrixAtSetsCorrectValue); - RUN_TEST(test_setMatrixAtFailsOnIndicesOutOfRange); - - return UNITY_END(); -} \ No newline at end of file diff --git a/JD/unittests.h_.txt b/JD/unittests.h_.txt deleted file mode 100644 index fc327ca..0000000 --- a/JD/unittests.h_.txt +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef UNITTTESTS_H -#define UNITTTESTS_H - -#include - -typedef int (*UnitTestType)(void); - -#define RUN_UNIT_TEST(fct) printf("%80s: %d\n", #fct, fct()) - -#endif