Compare commits

..

4 Commits
main ... main

4 changed files with 33 additions and 61 deletions

Binary file not shown.

View File

@ -1,77 +1,29 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
#include <stdio.h>
// TODO Matrix-Funktionen implementieren ... ok // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix matrix;
if (rows == 0 || cols == 0)
{
matrix.rows = 0;
matrix.cols =0;
matrix.data = NULL;
return matrix;
}
matrix.rows = rows;
matrix.cols = cols;
matrix.data = (MatrixTyype *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data == NULL)
{
matrix.rows = 0;
matrix.cols = 0;
return matrix;
}
for (int i = 0; i<rows; i++)
{
for (int j = 0; j<cols; j++)
{
matrix.data[i * matrix.cols +j] = UNDEFINED_MATRIX_VALUE;
}
}
return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->buffer != NULL){
free (matrix->buffer);
matrix->buffer = NULL;
}
matrix->rows = 0;
matrix->cols = 0;
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
return; // abbruch falls fehler
}
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
return UNDEFINED_MATRIX_VALUE;
}
return matrix.buffer[rowIdx * matrix.cols + colIdx];
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {

View File

@ -7,13 +7,6 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct Matrix
{
unsigend int rows;
unsigned int cols;
Matrixtype *data;
#define buffer data
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);

View File

@ -71,6 +71,32 @@ void test_addFailsOnDifferentInputDimensions(void)
TEST_ASSERT_EQUAL_UINT32(0, result.cols); 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) void test_multiplyReturnsCorrectResults(void)
{ {
MatrixType buffer1[] = {1, 2, 3, 4, 5, 6}; MatrixType buffer1[] = {1, 2, 3, 4, 5, 6};
@ -138,7 +164,7 @@ void test_setMatrixAtFailsOnIndicesOutOfRange(void)
Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer}; Matrix matrixToTest = {.rows=2, .cols=3, .buffer=buffer};
setMatrixAt(-1, matrixToTest, 2, 3); setMatrixAt(-1, matrixToTest, 2, 3);
TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, matrixToTest.cols * matrixToTest.rows); TEST_ASSERT_EQUAL_FLOAT_ARRAY(expectedResults, matrixToTest.buffer, sizeof(buffer)/sizeof(MatrixType));
} }
void setUp(void) { void setUp(void) {
@ -159,6 +185,7 @@ int main()
RUN_TEST(test_clearMatrixSetsMembersToNull); RUN_TEST(test_clearMatrixSetsMembersToNull);
RUN_TEST(test_addReturnsCorrectResult); RUN_TEST(test_addReturnsCorrectResult);
RUN_TEST(test_addFailsOnDifferentInputDimensions); RUN_TEST(test_addFailsOnDifferentInputDimensions);
RUN_TEST(test_addSupportsBroadcasting);
RUN_TEST(test_multiplyReturnsCorrectResults); RUN_TEST(test_multiplyReturnsCorrectResults);
RUN_TEST(test_multiplyFailsOnWrongInputDimensions); RUN_TEST(test_multiplyFailsOnWrongInputDimensions);
RUN_TEST(test_getMatrixAtReturnsCorrectResult); RUN_TEST(test_getMatrixAtReturnsCorrectResult);