forked from freudenreichan/info2Praktikum-NeuronalesNetz
TODO fix custom test (getPixelData should error)
This commit is contained in:
1 parent
1f08837b74
commit
d9bb6fd600
2 files changed
+50
-2
No files matched your search
@@ -24,7 +24,7 @@ static int openAndCheck(FILE **fd, const char *path)
|
|||||||
status = 2;
|
status = 2;
|
||||||
goto ending_openAndCheck;
|
goto ending_openAndCheck;
|
||||||
}
|
}
|
||||||
|
// size_t buf = 0;
|
||||||
if (!fread(headerRead, headerLen-1, 1, *fd))
|
if (!fread(headerRead, headerLen-1, 1, *fd))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error: could not read file %s\n", path);
|
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)
|
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 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
void test_readImagesReturnsCorrectNumberOfImages(void)
|
||||||
{
|
{
|
||||||
@@ -119,6 +145,25 @@ void test_readImagesFailsOnWrongFileTag(void)
|
|||||||
remove(path);
|
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) {
|
void setUp(void) {
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||||
}
|
}
|
||||||
@@ -138,6 +183,8 @@ int main()
|
|||||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||||
|
RUN_TEST(test_sizes_wrong);
|
||||||
|
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user