matrix.data zu matrix.buffer umgenannt, da Tests diese Bezeichnung erwarten. Test für matrix.c laufen bis auf test_createMatrixFailsOnZeroDimensions:FAIL. Expected NULL

This commit is contained in:
Laila Mueller 2025-11-10 14:15:41 +01:00
parent 13daa63648
commit 444067262e

View File

@ -10,8 +10,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.data = (float *)malloc(rows * cols * sizeof(MatrixType)); matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
if (matrix.data != NULL) { if (matrix.buffer != NULL) {
} }
return matrix; return matrix;
@ -20,21 +20,21 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix->data != NULL) { if (matrix->buffer != NULL) {
free(matrix->data); free(matrix->buffer);
matrix->data = NULL; matrix->buffer = NULL;
} }
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 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){ if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
return; return;
} }
matrix->data[rowIdx * matrix->cols + colIdx] = value; 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)
@ -43,7 +43,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
return 0; return 0;
} }
MatrixType value; MatrixType value;
value = matrix.data[rowIdx * matrix.cols + colIdx]; value = matrix.buffer[rowIdx * matrix.cols + colIdx];
return value; return value;
} }
@ -51,7 +51,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
Matrix errorMatrix = createMatrix(0, 0); Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.data = NULL; errorMatrix.buffer = NULL;
return errorMatrix; return errorMatrix;
} }
@ -59,7 +59,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
for (unsigned int i = 0; i < matrix1.rows; i++) { for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix1.cols; j++) { for (unsigned int j = 0; j < matrix1.cols; j++) {
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(sum, &result, i, j); setMatrixAt(sum, result, i, j);
} }
} }
return result; return result;
@ -70,7 +70,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.cols != matrix2.rows){ if (matrix1.cols != matrix2.rows){
Matrix errorMatrix = createMatrix(0, 0); Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.data = NULL; errorMatrix.buffer = NULL;
return errorMatrix; return errorMatrix;
} }
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols);
@ -81,7 +81,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
for(size_t k = 0; k < matrix1.cols; k++){ for(size_t k = 0; k < matrix1.cols; k++){
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
} }
setMatrixAt(sum, &matrix3, i, j); setMatrixAt(sum, matrix3, i, j);
} }
} }