Skip to content

Commit

Permalink
Start working on rom_load unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlockoS committed Sep 6, 2024
1 parent 7505b0b commit da32f27
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
10 changes: 6 additions & 4 deletions cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ bool cd_memory_map(MemoryMap *map) {
return ret;
}

#if 0
/// Load CDROM data from file.
bool cd_load(const char* filename, size_t start, size_t len, size_t sector_size, uint8_t page, size_t offset, MemoryMap* map) {
bool ret = false;
Expand All @@ -90,10 +89,14 @@ bool cd_load(const char* filename, size_t start, size_t len, size_t sector_size,
size_t current_page = physical >> 0x0D;
size_t current_addr = physical & 0x1FFF;

size_t bank_offset = current_addr + (map->page[current_page].bank * PCE_BANK_SIZE);

// [todo] test that map->page[current_page].id != PCE_MEMORY_NONE

ret = false;
if(fseek(in, (long int)file_offset, SEEK_SET)) {
if(fseek(in, (long int)file_offset, SEEK_SET) < 0) {
ERROR_MSG("Offset out of bound : %s", strerror(errno));
} else if(fread(map->page[current_page] + current_addr, 1, count, in) != count) {
} else if(fread(map->memory[map->page[current_page].id].data+bank_offset, 1, count, in) != count) {
ERROR_MSG("Failed to read %d bytes : %s", count, strerror(errno));
} else {
ret = true;
Expand All @@ -109,4 +112,3 @@ bool cd_load(const char* filename, size_t start, size_t len, size_t sector_size,
}
return ret;
}
#endif
2 changes: 1 addition & 1 deletion cd.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ bool cd_memory_map(MemoryMap *map);
/// \param [out] map Memory map.
/// \return true
/// \return false
int cd_load(const char* filename, size_t start, size_t len, size_t sector_size, uint8_t page, size_t offset, MemoryMap* map);
bool cd_load(const char* filename, size_t start, size_t len, size_t sector_size, uint8_t page, size_t offset, MemoryMap* map);

#endif // ETRIPATOR_CD_H
6 changes: 4 additions & 2 deletions rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static bool rom_load_data(const char *filename, MemoryMap *map) {
if (size & 0x200U) {
// Jump header
size &= ~0x200U;
if (fseek(in, 0x200U, SEEK_SET)) {
if (fseek(in, 0x200U, SEEK_SET) < 0) {
ERROR_MSG("Failed to jump rom header in %s: %s", filename, strerror(errno));
}
}
Expand All @@ -76,7 +76,7 @@ static bool rom_load_data(const char *filename, MemoryMap *map) {
size_t count = (size < memory->length) ? size : memory->length;
size_t nread = fread(memory->data, 1, count, in);
if (nread != count) {
ERROR_MSG("Failed to read ROM data from %s : %s", filename, strerror(errno));
ERROR_MSG("Failed to read ROM data from %s (expected %zu, read %zu): %s", filename, count, nread, strerror(errno));
memory_destroy(memory);
} else {
ret = true;
Expand All @@ -90,6 +90,8 @@ static bool rom_load_data(const char *filename, MemoryMap *map) {

// Load ROM from file and update memory map.
bool rom_load(const char* filename, MemoryMap* map) {
assert(filename != NULL);
assert(map != NULL);
FILE *in;
bool ret = false;
if(rom_load_data(filename, map)) {
Expand Down
4 changes: 3 additions & 1 deletion test/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ MunitResult cd_memory_map_test(const MunitParameter params[] __attribute__((unus
return MUNIT_OK;
}

// [todo] cd_load

static MunitTest cd_tests[] = {
{ "/memory_map", cd_memory_map_test, setup, tear_down, MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

static const MunitSuite cd_suite = {
"ROM test suite", cd_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
"CDROM test suite", cd_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
};

int main (int argc, char* const* argv) {
Expand Down
76 changes: 72 additions & 4 deletions test/rom.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,102 @@

DEFINE_FFF_GLOBALS;

// [NOTE] munit is using fileno. If a test fails, munit will loop indefinitely :/

FAKE_VALUE_FUNC(FILE*, __wrap_fopen, const char*, const char*)
FAKE_VALUE_FUNC(int, __wrap_fileno, FILE*)
FAKE_VALUE_FUNC(int, __wrap_fstat, int, struct stat*)
FAKE_VALUE_FUNC(int, __wrap_fseek, FILE*, long, int)
FAKE_VALUE_FUNC(size_t, __wrap_fread, void*, size_t, size_t, FILE*)
FAKE_VALUE_FUNC(int, __wrap_fclose, FILE*)

static int fstat_empty_file(int fd __attribute__((unused)), struct stat* infos) {
memset(infos, 0, sizeof(struct stat));
infos->st_size = 0;
return 0;
}

static size_t g_dummy_file_size;

static int fstat_dummy_file(int fd __attribute__((unused)), struct stat* infos) {
infos->st_size = g_dummy_file_size;
return 0;
}

static size_t fread_dummy(void* out, size_t size, size_t nmemb, FILE* in __attribute__((unused))) {
uint8_t *ptr = (uint8_t*)out;
uint8_t b = 0;
for(size_t j=0; j<nmemb; j++) {
for(size_t i=0; i<size; i++) {
*ptr++ = b++;
}
}
return size*nmemb;
}

MemoryMap g_map;

void* setup(const MunitParameter params[] __attribute__((unused)), void* user_data __attribute__((unused))) {
RESET_FAKE(__wrap_fopen);
RESET_FAKE(__wrap_fileno);
RESET_FAKE(__wrap_fstat);
RESET_FAKE(__wrap_fseek);
RESET_FAKE(__wrap_fread);
RESET_FAKE(__wrap_fclose);


munit_assert_true(memory_map_init(&g_map));

return NULL;
}

void tear_down(void* fixture __attribute__((unused))) {
memory_map_destroy(&g_map);
}

MunitResult rom_load_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) {
// [todo]
MunitResult rom_load_small_test(const MunitParameter params[] __attribute__((unused)), void* fixture __attribute__((unused))) {
__wrap_fopen_fake.return_val = NULL;
__wrap_fileno_fake.return_val = -1;
__wrap_fstat_fake.return_val = -1;
__wrap_fseek_fake.return_val = -1;
__wrap_fread_fake.return_val = 0;
__wrap_fclose_fake.return_val = -1;

munit_assert_false(rom_load("dummy.pce", &g_map));

__wrap_fopen_fake.return_val = stdin;
munit_assert_false(rom_load("dummy.pce", &g_map));

__wrap_fileno_fake.return_val = 0;
munit_assert_false(rom_load("dummy.pce", &g_map));

__wrap_fstat_fake.custom_fake = fstat_empty_file;
munit_assert_false(rom_load("dummy.pce", &g_map));

g_dummy_file_size = 10U;
__wrap_fstat_fake.custom_fake = fstat_dummy_file;
munit_assert_false(rom_load("dummy.pce", &g_map));

__wrap_fseek_fake.return_val = 0;
munit_assert_false(rom_load("dummy.pce", &g_map));

__wrap_fread_fake.custom_fake = fread_dummy;
__wrap_fclose_fake.return_val = 0;
munit_assert_true(rom_load("dummy.pce", &g_map));
munit_assert_not_null(g_map.memory[PCE_MEMORY_ROM].data);
munit_assert_size(g_map.memory[PCE_MEMORY_ROM].length, ==, PCE_BANK_SIZE);
for (size_t i = 0; i < 128; i++) {
munit_assert_int(g_map.page[i].id, ==, PCE_MEMORY_ROM);
munit_assert_size(g_map.page[i].bank, ==, 0);
}

return MUNIT_OK;
}

// [todo] test header jump
// [todo] test ROM > 8kb

static MunitTest rom_tests[] = {
{ "/load", rom_load_test, setup, tear_down, MUNIT_TEST_OPTION_NONE, NULL },
{ "/load/small", rom_load_small_test, setup, tear_down, MUNIT_TEST_OPTION_NONE, NULL },
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

Expand Down

0 comments on commit da32f27

Please sign in to comment.