forked from freudenreichan/info2Praktikum-NeuronalesNetz
data umbennenung in buffer wegen unittest
This commit is contained in:
parent
9dd4eff0d7
commit
ed1325cfba
28
matrix.c
28
matrix.c
@ -13,7 +13,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
{
|
{
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
matrix.data = NULL;
|
matrix.buffer = NULL;
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,9 +21,9 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
matrix.rows = rows;
|
matrix.rows = rows;
|
||||||
matrix.cols = cols;
|
matrix.cols = cols;
|
||||||
|
|
||||||
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||||
|
|
||||||
if (matrix.data == NULL)
|
if (matrix.buffer == NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
@ -36,7 +36,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
{
|
{
|
||||||
for (int j = 0; j < cols; j++)
|
for (int j = 0; j < cols; j++)
|
||||||
{
|
{
|
||||||
matrix.data[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
|
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
@ -44,10 +44,10 @@ 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;
|
||||||
@ -62,7 +62,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
|||||||
return; // abbruch falls fehler
|
return; // abbruch falls fehler
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
@ -73,7 +73,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
return UNDEFINED_MATRIX_VALUE;
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return matrix.data[rowIdx * matrix.cols + colIdx];
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
@ -89,7 +89,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
if (result.data == NULL)
|
if (result.buffer == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
||||||
return result;
|
return result;
|
||||||
@ -99,7 +99,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
for (int j = 0; j < matrix1.cols; j++)
|
for (int j = 0; j < matrix1.cols; j++)
|
||||||
{
|
{
|
||||||
result.data[i * result.cols + j] = matrix1.data[i * matrix1.cols + j] + matrix2.data[i * matrix2.cols + j];
|
result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@ -118,7 +118,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
|
||||||
if (result.data == NULL)
|
if (result.buffer == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
||||||
return result;
|
return result;
|
||||||
@ -132,9 +132,9 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
for (int k = 0; k < matrix1.cols; k++)
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
{
|
{
|
||||||
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
|
sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
||||||
}
|
}
|
||||||
result.data[i * result.cols +j] = sum;
|
result.buffer[i * result.cols +j] = sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
3
matrix.h
3
matrix.h
@ -10,8 +10,7 @@ typedef float MatrixType;
|
|||||||
typedef struct Matrix {
|
typedef struct Matrix {
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
MatrixType *data;
|
MatrixType *buffer;
|
||||||
#define buffer data
|
|
||||||
} Matrix;
|
} Matrix;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -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};
|
||||||
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user