Praktikum matrix.h matrix.c

This commit is contained in:
Kristin 2025-11-13 16:39:38 +01:00
parent 55fe58d69a
commit eba831b446
3 changed files with 146 additions and 137 deletions

View File

@ -1,143 +1,134 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "imageInput.h" #include "imageInput.h"
#include "unity.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void prepareImageFile(const char *path, unsigned short int width,
unsigned short int height,
unsigned int short numberOfImages,
unsigned char label) {
FILE *file = fopen(path, "wb");
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label) if (file != NULL) {
{ const char *fileTag = "__info2_image_file_format__";
FILE *file = fopen(path, "wb"); GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(
numberOfImages * width * height, sizeof(GrayScalePixelType));
if(file != NULL) if (zeroBuffer != NULL) {
{ fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
const char *fileTag = "__info2_image_file_format__"; fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType)); fwrite(&width, sizeof(width), 1, file);
fwrite(&height, sizeof(height), 1, file);
if(zeroBuffer != NULL) for (int i = 0; i < numberOfImages; i++) {
{ fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file); fwrite(&label, sizeof(unsigned char), 1, file);
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file); }
fwrite(&width, sizeof(width), 1, file);
fwrite(&height, sizeof(height), 1, file);
for(int i = 0; i < numberOfImages; i++) free(zeroBuffer);
{
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
fwrite(&label, sizeof(unsigned char), 1, file);
}
free(zeroBuffer);
}
fclose(file);
} }
fclose(file);
}
} }
void test_readImagesReturnsCorrectNumberOfImages(void) {
void test_readImagesReturnsCorrectNumberOfImages(void) GrayScaleImageSeries *series = NULL;
{ const unsigned short expectedNumberOfImages = 2;
GrayScaleImageSeries *series = NULL; const char *path = "testFile.info2";
const unsigned short expectedNumberOfImages = 2; prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
const char *path = "testFile.info2"; series = readImages(path);
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); TEST_ASSERT_NOT_NULL(series);
series = readImages(path); TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
TEST_ASSERT_NOT_NULL(series); clearSeries(series);
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); remove(path);
clearSeries(series);
remove(path);
} }
void test_readImagesReturnsCorrectImageWidth(void) void test_readImagesReturnsCorrectImageWidth(void) {
{ GrayScaleImageSeries *series = NULL;
GrayScaleImageSeries *series = NULL; const unsigned short expectedWidth = 10;
const unsigned short expectedWidth = 10; const char *path = "testFile.info2";
const char *path = "testFile.info2"; prepareImageFile(path, expectedWidth, 8, 2, 1);
prepareImageFile(path, 8, expectedWidth, 2, 1); series = readImages(path);
series = readImages(path); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series->images);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_EQUAL_UINT16(2, series->count);
TEST_ASSERT_EQUAL_UINT16(2, series->count); TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width);
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width); TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width);
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width); clearSeries(series);
clearSeries(series); remove(path);
remove(path);
} }
void test_readImagesReturnsCorrectImageHeight(void) void test_readImagesReturnsCorrectImageHeight(void) {
{ GrayScaleImageSeries *series = NULL;
GrayScaleImageSeries *series = NULL; const unsigned short expectedHeight = 10;
const unsigned short expectedHeight = 10; const char *path = "testFile.info2";
const char *path = "testFile.info2"; prepareImageFile(path, 8, expectedHeight, 2, 1);
prepareImageFile(path, expectedHeight, 8, 2, 1); series = readImages(path);
series = readImages(path); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series->images);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_EQUAL_UINT16(2, series->count);
TEST_ASSERT_EQUAL_UINT16(2, series->count); TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height);
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height); TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height);
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height); clearSeries(series);
clearSeries(series); remove(path);
remove(path);
} }
void test_readImagesReturnsCorrectLabels(void) void test_readImagesReturnsCorrectLabels(void) {
{ const unsigned char expectedLabel = 15;
const unsigned char expectedLabel = 15;
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 2, expectedLabel); prepareImageFile(path, 8, 8, 2, expectedLabel);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->labels); TEST_ASSERT_NOT_NULL(series->labels);
TEST_ASSERT_EQUAL_UINT16(2, series->count); TEST_ASSERT_EQUAL_UINT16(2, series->count);
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]); TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
} }
clearSeries(series); clearSeries(series);
remove(path); remove(path);
} }
void test_readImagesReturnsNullOnNotExistingPath(void) void test_readImagesReturnsNullOnNotExistingPath(void) {
{ const char *path = "testFile.txt";
const char *path = "testFile.txt"; remove(path);
remove(path); TEST_ASSERT_NULL(readImages(path));
}
void test_readImagesFailsOnWrongFileTag(void) {
const char *path = "testFile.info2";
FILE *file = fopen(path, "w");
if (file != NULL) {
fprintf(file, "some_tag ");
fclose(file);
TEST_ASSERT_NULL(readImages(path)); TEST_ASSERT_NULL(readImages(path));
} }
remove(path);
void test_readImagesFailsOnWrongFileTag(void)
{
const char *path = "testFile.info2";
FILE *file = fopen(path, "w");
if(file != NULL)
{
fprintf(file, "some_tag ");
fclose(file);
TEST_ASSERT_NULL(readImages(path));
}
remove(path);
} }
void setUp(void) { void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
} }
void tearDown(void) { void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
} }
int main() int main() {
{ UNITY_BEGIN();
UNITY_BEGIN();
printf("\n============================\nImage input tests\n============================\n"); printf("\n============================\nImage input "
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); "tests\n============================\n");
RUN_TEST(test_readImagesReturnsCorrectImageWidth); RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
RUN_TEST(test_readImagesReturnsCorrectImageHeight); RUN_TEST(test_readImagesReturnsCorrectImageWidth);
RUN_TEST(test_readImagesReturnsCorrectLabels); RUN_TEST(test_readImagesReturnsCorrectImageHeight);
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesReturnsCorrectLabels);
RUN_TEST(test_readImagesFailsOnWrongFileTag); RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
RUN_TEST(test_readImagesFailsOnWrongFileTag);
return UNITY_END(); return UNITY_END();
} }

View File

@ -1,35 +1,47 @@
#include "matrix.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) /*typedef struct {
{
unsigned int rows;
unsigned int cols;
MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten
} Matrix;*/
Matrix createMatrix(unsigned int rows, unsigned int cols) {
MatrixType *data =
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
// liefert Zeiger auf Speicher
Matrix newMatrix = {rows, cols, data}; // neue Matric nach struct
return newMatrix;
}
void clearMatrix(Matrix *matrix) {
matrix->data = UNDEFINED_MATRIX_VALUE;
matrix->rows = UNDEFINED_MATRIX_VALUE;
matrix->cols = UNDEFINED_MATRIX_VALUE;
free((*matrix).data); //Speicher freigeben
} }
void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
{ unsigned int colIdx) {
float *rowPtr[matrix->rows] = (*matrix).data
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
unsigned int colIdx) {}
Matrix add(const Matrix matrix1, const Matrix matrix2) {
// broadcasting
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) Matrix multiply(const Matrix matrix1, const Matrix matrix2) {}
{
}
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)
{
}

View File

@ -7,7 +7,13 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *data; //Zeiger auf Speicherbereich Reihen*Spalten
} 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);