From 31ceca47eef19064583aa31b0602502d4bee9d7a Mon Sep 17 00:00:00 2001 From: Max Perschke Date: Thu, 12 Nov 2026 10:43:37 +0100 Subject: [PATCH] imageinputtest, verbessert --- Startcode/imageInputTests.c | 164 ++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 81 deletions(-) diff --git a/Startcode/imageInputTests.c b/Startcode/imageInputTests.c index 2f8b0ab..03240ab 100644 --- a/Startcode/imageInputTests.c +++ b/Startcode/imageInputTests.c @@ -6,96 +6,102 @@ #include "imageInput.h" -static void writeHeader(FILE *file, unsigned short count, unsigned short width, unsigned short height) -{ - const char *tag = "__info2_image_file_format__"; - fwrite(tag, 1, strlen(tag), file); - fwrite(&count, sizeof(count), 1, file); - fwrite(&width, sizeof(width), 1, file); - fwrite(&height, sizeof(height), 1, file); -} - -static void prepareImageFile(const char *path, unsigned short width, unsigned short height, - unsigned short count, unsigned char label) +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"); - if (file == NULL) return; - writeHeader(file, count, width, height); - size_t pixelCount = (size_t)width * height; - GrayScalePixelType *pixels = (GrayScalePixelType *)calloc(pixelCount, sizeof(*pixels)); - if (pixels != NULL) + if(file != NULL) { - for (unsigned int i = 0; i < count; ++i) + const char *fileTag = "__info2_image_file_format__"; + GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType)); + + if(zeroBuffer != NULL) { - fwrite(pixels, sizeof(*pixels), pixelCount, file); - fwrite(&label, sizeof(label), 1, file); + fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), 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++) + { + fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file); + fwrite(&label, sizeof(unsigned char), 1, file); + } + + free(zeroBuffer); } - free(pixels); + + fclose(file); } - fclose(file); } + void test_readImagesReturnsCorrectNumberOfImages(void) { + GrayScaleImageSeries *series = NULL; + const unsigned short expectedNumberOfImages = 2; const char *path = "testFile.info2"; - const unsigned short expectedCount = 2; - prepareImageFile(path, 8, 8, expectedCount, 1); - GrayScaleImageSeries *series = readImages(path); + prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); + series = readImages(path); TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_EQUAL_UINT16(expectedCount, series->count); + TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); clearSeries(series); remove(path); } -void test_readImagesReturnsCorrectImageDimensions(void) +void test_readImagesReturnsCorrectImageWidth(void) { + GrayScaleImageSeries *series = NULL; + const unsigned short expectedWidth = 10; const char *path = "testFile.info2"; - prepareImageFile(path, 10, 9, 2, 1); - GrayScaleImageSeries *series = readImages(path); + prepareImageFile(path, expectedWidth, 8, 2, 1); + series = readImages(path); TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_EQUAL_UINT16(10, series->images[0].width); - TEST_ASSERT_EQUAL_UINT16(10, series->images[1].width); - TEST_ASSERT_EQUAL_UINT16(9, series->images[0].height); - TEST_ASSERT_EQUAL_UINT16(9, series->images[1].height); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_EQUAL_UINT16(2, series->count); + TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width); + TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width); + clearSeries(series); + remove(path); +} + +void test_readImagesReturnsCorrectImageHeight(void) +{ + GrayScaleImageSeries *series = NULL; + const unsigned short expectedHeight = 10; + const char *path = "testFile.info2"; + prepareImageFile(path, 8, expectedHeight, 2, 1); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); + TEST_ASSERT_NOT_NULL(series->images); + TEST_ASSERT_EQUAL_UINT16(2, series->count); + TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height); + TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height); clearSeries(series); remove(path); } void test_readImagesReturnsCorrectLabels(void) { + const unsigned char expectedLabel = 15; + + GrayScaleImageSeries *series = NULL; const char *path = "testFile.info2"; - prepareImageFile(path, 8, 8, 2, 15); - GrayScaleImageSeries *series = readImages(path); + prepareImageFile(path, 8, 8, 2, expectedLabel); + series = readImages(path); TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_EQUAL_UINT8(15, series->labels[0]); - TEST_ASSERT_EQUAL_UINT8(15, series->labels[1]); + TEST_ASSERT_NOT_NULL(series->labels); + TEST_ASSERT_EQUAL_UINT16(2, series->count); + for (int i = 0; i < 2; i++) { + TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]); + } clearSeries(series); remove(path); } -void test_readImagesReturnsPixelData(void) +void test_readImagesReturnsNullOnNotExistingPath(void) { - const char *path = "testFile.info2"; - const unsigned char pixels[] = { 0, 17, 128, 255 }; - const unsigned char label = 7; - FILE *file = fopen(path, "wb"); - TEST_ASSERT_NOT_NULL(file); - writeHeader(file, 1, 2, 2); - fwrite(pixels, sizeof(pixels), 1, file); - fwrite(&label, sizeof(label), 1, file); - fclose(file); - - GrayScaleImageSeries *series = readImages(path); - TEST_ASSERT_NOT_NULL(series); - TEST_ASSERT_EQUAL_UINT8_ARRAY(pixels, series->images[0].buffer, sizeof(pixels)); - clearSeries(series); - remove(path); -} - -void test_readImagesReturnsNullOnMissingPath(void) -{ - const char *path = "missingTestFile.info2"; + const char *path = "testFile.txt"; remove(path); TEST_ASSERT_NULL(readImages(path)); } @@ -103,39 +109,35 @@ void test_readImagesReturnsNullOnMissingPath(void) void test_readImagesFailsOnWrongFileTag(void) { const char *path = "testFile.info2"; - FILE *file = fopen(path, "wb"); - TEST_ASSERT_NOT_NULL(file); - fputs("wrong tag", file); - fclose(file); - TEST_ASSERT_NULL(readImages(path)); + FILE *file = fopen(path, "w"); + if(file != NULL) + { + fprintf(file, "some_tag "); + fclose(file); + TEST_ASSERT_NULL(readImages(path)); + } remove(path); } -void test_readImagesFailsOnTruncatedImage(void) -{ - const char *path = "testFile.info2"; - const unsigned char partialPixels[] = { 1, 2 }; - FILE *file = fopen(path, "wb"); - TEST_ASSERT_NOT_NULL(file); - writeHeader(file, 1, 2, 2); - fwrite(partialPixels, sizeof(partialPixels), 1, file); - fclose(file); - TEST_ASSERT_NULL(readImages(path)); - remove(path); +void setUp(void) { + // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden } -void setUp(void) {} -void tearDown(void) {} +void tearDown(void) { + // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +} -int main(void) +int main() { UNITY_BEGIN(); + + printf("\n============================\nImage input tests\n============================\n"); RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); - RUN_TEST(test_readImagesReturnsCorrectImageDimensions); + RUN_TEST(test_readImagesReturnsCorrectImageWidth); + RUN_TEST(test_readImagesReturnsCorrectImageHeight); RUN_TEST(test_readImagesReturnsCorrectLabels); - RUN_TEST(test_readImagesReturnsPixelData); - RUN_TEST(test_readImagesReturnsNullOnMissingPath); + RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesFailsOnWrongFileTag); - RUN_TEST(test_readImagesFailsOnTruncatedImage); + return UNITY_END(); -} +} \ No newline at end of file