Files
dusk/test/asset/test_asset.c
T

243 lines
8.6 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "asset/asset.h"
#include "asset/loader/assetloader.h"
#include "asset/loader/assetentry.h"
#include "util/memory.h"
#include "util/string.h"
// ============================================================
// Stub loader callbacks
// ============================================================
static errorret_t stub_load_success(assetloading_t *loading) {
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
errorOk();
}
static errorret_t stub_load_fail(assetloading_t *loading) {
loading->entry->state = ASSET_ENTRY_STATE_ERROR;
errorThrow("Stub loader failed");
}
static errorret_t stub_dispose(assetentry_t *entry) {
errorOk();
}
// ============================================================
// Per-test setup / teardown
// ============================================================
static assetloadercallbacks_t saved_callbacks[ASSET_LOADER_TYPE_COUNT];
static int asset_setup(void **state) {
// Save real callbacks so we can restore them in teardown.
memoryCopy(saved_callbacks, ASSET_LOADER_CALLBACKS, sizeof(saved_callbacks));
// Manually init ASSET — no thread, no ZIP.
memoryZero(&ASSET, sizeof(ASSET));
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
threadMutexInit(&ASSET.loading[i].mutex);
}
// Replace all loader callbacks with stubs.
for(int i = 0; i < ASSET_LOADER_TYPE_COUNT; i++) {
ASSET_LOADER_CALLBACKS[i].loadSync = stub_load_success;
ASSET_LOADER_CALLBACKS[i].dispose = stub_dispose;
}
return 0;
}
static int asset_teardown(void **state) {
// Dispose any entries that tests left behind.
for(int i = 0; i < ASSET_ENTRY_COUNT_MAX; i++) {
if(ASSET.entries[i].type != ASSET_LOADER_TYPE_NULL) {
errorret_t ret = assetEntryDispose(&ASSET.entries[i]);
if(errorIsNotOk(ret)) errorCatch(ret);
}
}
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
threadMutexDispose(&ASSET.loading[i].mutex);
}
// Restore real callbacks before zeroing state.
memoryCopy(ASSET_LOADER_CALLBACKS, saved_callbacks, sizeof(saved_callbacks));
memoryZero(&ASSET, sizeof(ASSET));
return 0;
}
// ============================================================
// Helper: find which loading slot owns a given entry
// ============================================================
static bool_t loading_slot_has_entry(const assetentry_t *entry) {
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
if(ASSET.loading[i].entry == entry) return true;
}
return false;
}
// ============================================================
// assetGetEntry tests
// ============================================================
static void test_getEntry_creates_new(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assert_non_null(entry);
assert_int_equal(entry->state, ASSET_ENTRY_STATE_NOT_STARTED);
assert_int_equal(entry->type, ASSET_LOADER_TYPE_LOCALE);
assert_true(stringEquals(entry->name, "test.locale"));
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_getEntry_dedup(void **state) {
assetentry_t *a = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assetentry_t *b = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assert_ptr_equal(a, b);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_getEntry_distinct_names(void **state) {
assetentry_t *a = assetGetEntry("a.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assetentry_t *b = assetGetEntry("b.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assert_ptr_not_equal(a, b);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
// ============================================================
// assetUpdate — state machine tests
// ============================================================
static void test_update_entry_reaches_loaded(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assert_int_equal(entry->state, ASSET_ENTRY_STATE_NOT_STARTED);
errorret_t ret = assetUpdate();
assert_true(errorIsOk(ret));
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_update_slot_occupied_after_first_update(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assetUpdate();
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
// Slot not cleared until the second update processes the LOADED case.
assert_true(loading_slot_has_entry(entry));
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_update_slot_cleared_after_second_update(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assetUpdate();
assetUpdate();
assert_false(loading_slot_has_entry(entry));
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_update_four_slots_fill_independently(void **state) {
// ASSET_LOADING_COUNT_MAX concurrent entries should all load in one pass.
assetentry_t *entries[ASSET_LOADING_COUNT_MAX];
for(int i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
char_t name[ASSET_FILE_NAME_MAX];
snprintf(name, sizeof(name), "asset%d.locale", i);
entries[i] = assetGetEntry(name, ASSET_LOADER_TYPE_LOCALE, NULL);
}
errorret_t ret = assetUpdate();
assert_true(errorIsOk(ret));
for(int i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
assert_int_equal(entries[i]->state, ASSET_ENTRY_STATE_LOADED);
}
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_update_error_state(void **state) {
ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_LOCALE].loadSync = stub_load_fail;
assetentry_t *entry = assetGetEntry("fail.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
// First update: dispatches and calls the failing stub.
// assetUpdate itself returns OK here; the error from loadSync is caught internally.
errorret_t ret = assetUpdate();
assert_true(errorIsOk(ret));
assert_int_equal(entry->state, ASSET_ENTRY_STATE_ERROR);
// Second update: sees ERROR state and propagates it.
ret = assetUpdate();
assert_true(errorIsNotOk(ret));
errorCatch(ret);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
// ============================================================
// assetRequireLoaded tests
// ============================================================
static void test_requireLoaded_already_loaded(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assetUpdate();
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
// Should return immediately without calling assetUpdate again.
errorret_t ret = assetRequireLoaded(entry);
assert_true(errorIsOk(ret));
assert_int_equal(memoryGetAllocatedCount(), 0);
}
static void test_requireLoaded_spins_to_loaded(void **state) {
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
assert_int_equal(entry->state, ASSET_ENTRY_STATE_NOT_STARTED);
// requireLoaded calls assetUpdate internally until LOADED.
errorret_t ret = assetRequireLoaded(entry);
assert_true(errorIsOk(ret));
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
assert_int_equal(memoryGetAllocatedCount(), 0);
}
// ============================================================
// main
// ============================================================
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_getEntry_creates_new, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_getEntry_dedup, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_getEntry_distinct_names, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_update_entry_reaches_loaded, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_update_slot_occupied_after_first_update, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_update_slot_cleared_after_second_update, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_update_four_slots_fill_independently, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_update_error_state, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_requireLoaded_already_loaded, asset_setup, asset_teardown),
cmocka_unit_test_setup_teardown(test_requireLoaded_spins_to_loaded, asset_setup, asset_teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}