From d9bb6fd6007b4183e5d947dfd15e9344d8e058ed Mon Sep 17 00:00:00 2001 From: Alex Reece Date: Thu, 5 Nov 2026 15:10:10 +0100 Subject: [PATCH] TODO fix custom test (getPixelData should error) --- Startcode/imageInput.c | 5 ++-- Startcode/imageInputTests.c | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index 688b4b0..20e4acf 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -24,7 +24,7 @@ static int openAndCheck(FILE **fd, const char *path) status = 2; goto ending_openAndCheck; } - + // size_t buf = 0; if (!fread(headerRead, headerLen-1, 1, *fd)) { fprintf(stderr, "Error: could not read file %s\n", path); @@ -61,7 +61,8 @@ static int getUChar(FILE **fd, unsigned char *dest) static int getPixelData(FILE **fd, const size_t bufSize, GrayScalePixelType *pixelData) { - if (!fread((char *)pixelData, bufSize, 1, *fd)) + size_t readLen = 0; + if (!(readLen = fread((char *)pixelData, bufSize, 1, *fd)) || readLen != 1) return 1; return 0; } diff --git a/Startcode/imageInputTests.c b/Startcode/imageInputTests.c index 03240ab..e16f2fa 100644 --- a/Startcode/imageInputTests.c +++ b/Startcode/imageInputTests.c @@ -35,6 +35,32 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne } } +static int writeImagesToFile(const char *path, GrayScaleImageSeries *series) +{ + FILE *file = fopen(path, "wb"); + + if (file != NULL) + { + const char *fileTag = "__info2_image_file_format__"; + const short count = (short)series->count; + const short width = (short)(series->images[0].width); + const short height = (short)(series->images[0].height); + fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file); + fwrite(&count, sizeof(short), 1, file); + fwrite(&width, sizeof(short), 1, file); + fwrite(&height, sizeof(short), 1, file); + + for (int i = 0; i < series->count; i++) + { + fwrite(series->images[i].buffer, sizeof(GrayScalePixelType), width * height, file); + fwrite(&(series->labels[i]), sizeof(unsigned char), 1, file); + } + + fclose(file); + return 0; + } + return 1; +} void test_readImagesReturnsCorrectNumberOfImages(void) { @@ -119,6 +145,25 @@ void test_readImagesFailsOnWrongFileTag(void) remove(path); } +// Test if the sizes specify buffers which are larger than can be read from the file +// Errors as it runs into the end of the file +void test_sizes_wrong(void) +{ + const unsigned char expectedLabel = 15; + + GrayScaleImageSeries *series = NULL; + const char *path = "testFile.info2"; + prepareImageFile(path, 8, 8, 2, expectedLabel); + series = readImages(path); + series->images[0].width = series->images[0].width + 1; + writeImagesToFile(path, series); + + + TEST_ASSERT_NULL(readImages(path)); + remove(path); + +} + void setUp(void) { // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden } @@ -138,6 +183,8 @@ int main() RUN_TEST(test_readImagesReturnsCorrectLabels); RUN_TEST(test_readImagesReturnsNullOnNotExistingPath); RUN_TEST(test_readImagesFailsOnWrongFileTag); + RUN_TEST(test_sizes_wrong); + return UNITY_END(); } \ No newline at end of file