Files
Info2-Pr-NeuroNetz/Startcode/imageInputTests.c
T
Manuel GlockandClaude Opus 5.5 4d6a86f293 Pflichtaufgaben fertig: Matrix-Fixes, Bilder einlesen, NN-Testdatei
- matrix.c: multiply summiert in MatrixType statt int (Nachkommastellen
  gingen verloren), clearMatrix gibt Speicher frei, Indexpruefung pro
  Zeile/Spalte, NULL-Pruefungen in add/multiply
- imageInput.c: fseek-Spruenge entfernt, Dateikopf pruefen, Lesefehler
  behandeln, clearSeries implementiert
- neuralNetworkTests.c: prepareNeuralNetworkFile implementiert
- neuralNetwork.c: clearModel gibt das Layer-Array frei (Speicherleck)
- Zusaetzliche Unittests fuer Matrix und Bildeinlesen
- Testartefakte und Testprogramme in .gitignore, testFile.info2 entfernt

Alle 33 Tests gruen, keine Warnungen, 87.37 % Genauigkeit (wie Referenz).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 12:19:07 +01:00

239 lines
7.2 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "unity.h"
#include "imageInput.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");
if(file != NULL)
{
const char *fileTag = "__info2_image_file_format__";
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
if(zeroBuffer != NULL)
{
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);
}
fclose(file);
}
}
void test_readImagesReturnsCorrectNumberOfImages(void)
{
GrayScaleImageSeries *series = NULL;
const unsigned short expectedNumberOfImages = 2;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
clearSeries(series);
remove(path);
}
void test_readImagesReturnsCorrectImageWidth(void)
{
GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10;
const char *path = "testFile.info2";
prepareImageFile(path, expectedWidth, 8, 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(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, expectedLabel);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
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_readImagesReturnsNullOnNotExistingPath(void)
{
const char *path = "testFile.txt";
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));
}
remove(path);
}
static void writeRawFile(const char *path, const void *data, size_t size)
{
FILE *file = fopen(path, "wb");
if(file != NULL)
{
fwrite(data, 1, size, file);
fclose(file);
}
}
void test_readImagesReturnsCorrectPixelValues(void)
{
const char *path = "testFile.info2";
const char *fileTag = "__info2_image_file_format__";
const unsigned short header[] = {2, 2, 2};
const GrayScalePixelType expectedPixels1[] = {0, 64, 128, 255};
const GrayScalePixelType expectedPixels2[] = {1, 2, 3, 4};
const unsigned char expectedLabels[] = {7, 3};
GrayScaleImageSeries *series = NULL;
FILE *file = fopen(path, "wb");
TEST_ASSERT_NOT_NULL(file);
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
fwrite(header, sizeof(header[0]), 3, file);
fwrite(expectedPixels1, sizeof(GrayScalePixelType), 4, file);
fwrite(&expectedLabels[0], sizeof(unsigned char), 1, file);
fwrite(expectedPixels2, sizeof(GrayScalePixelType), 4, file);
fwrite(&expectedLabels[1], sizeof(unsigned char), 1, file);
fclose(file);
series = readImages(path);
remove(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedPixels1, series->images[0].buffer, 4);
TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedPixels2, series->images[1].buffer, 4);
TEST_ASSERT_EQUAL_UINT8_ARRAY(expectedLabels, series->labels, 2);
clearSeries(series);
}
void test_readImagesFailsOnTruncatedImageData(void)
{
const char *path = "testFile.info2";
long fullSize = 0;
char *content = NULL;
FILE *file = NULL;
prepareImageFile(path, 8, 8, 3, 1);
// Datei einlesen und ohne das letzte Byte (Label des letzten Bildes) neu schreiben
file = fopen(path, "rb");
TEST_ASSERT_NOT_NULL(file);
fseek(file, 0, SEEK_END);
fullSize = ftell(file);
rewind(file);
content = (char *)malloc(fullSize);
TEST_ASSERT_NOT_NULL(content);
TEST_ASSERT_EQUAL_INT(fullSize, fread(content, 1, fullSize, file));
fclose(file);
writeRawFile(path, content, fullSize - 1);
free(content);
TEST_ASSERT_NULL(readImages(path));
remove(path);
}
void test_readImagesFailsOnIncompleteHeader(void)
{
const char *path = "testFile.info2";
const char content[] = "__info2_image_file_format__\x02\x00\x08";
writeRawFile(path, content, sizeof(content) - 1);
TEST_ASSERT_NULL(readImages(path));
remove(path);
}
void test_readImagesFailsOnZeroImages(void)
{
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, 0, 1);
TEST_ASSERT_NULL(readImages(path));
remove(path);
}
void test_clearSeriesAcceptsNull(void)
{
clearSeries(NULL);
TEST_PASS();
}
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
int main()
{
UNITY_BEGIN();
printf("\n============================\nImage input tests\n============================\n");
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
RUN_TEST(test_readImagesReturnsCorrectLabels);
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
RUN_TEST(test_readImagesFailsOnWrongFileTag);
RUN_TEST(test_readImagesReturnsCorrectPixelValues);
RUN_TEST(test_readImagesFailsOnTruncatedImageData);
RUN_TEST(test_readImagesFailsOnIncompleteHeader);
RUN_TEST(test_readImagesFailsOnZeroImages);
RUN_TEST(test_clearSeriesAcceptsNull);
return UNITY_END();
}