TODO fix custom test (getPixelData should error)

This commit is contained in:
reeceal105988 committed 2026-11-05 15:10:10 +01:00
1 parent 1f08837b74
commit d9bb6fd600
2 files changed
+50 -2

No files matched your search

+3 -2
View File
@@ -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;
}
+47
View File
@@ -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();
}