Assets are slightly optimized
This commit is contained in:
@@ -468,6 +468,102 @@ static void test_requireLoaded_propagates_error(void **state) {
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// assetReapUnused tests
|
||||
// ============================================================
|
||||
|
||||
static void test_update_does_not_reap_automatically(void **state) {
|
||||
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
|
||||
assetEntryLock(entry);
|
||||
assetUpdate();
|
||||
assetUpdate(); // slot freed
|
||||
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
|
||||
assetEntryUnlock(entry);
|
||||
|
||||
// Unlike the old behavior, assetUpdate no longer reaps zero-ref entries on
|
||||
// its own - a LOADED entry must survive further updates untouched.
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
|
||||
assert_int_equal(entry->type, ASSET_LOADER_TYPE_LOCALE);
|
||||
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
|
||||
|
||||
errorret_t ret = assetEntryDispose(entry);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_reapUnused_disposes_zero_ref_loaded(void **state) {
|
||||
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
|
||||
assetEntryLock(entry);
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
|
||||
assetEntryUnlock(entry);
|
||||
|
||||
errorret_t ret = assetReapUnused();
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_int_equal(entry->type, ASSET_LOADER_TYPE_NULL);
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_reapUnused_ignores_referenced_entries(void **state) {
|
||||
assetentry_t *entry = assetGetEntry("test.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
|
||||
assetEntryLock(entry);
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
|
||||
|
||||
// Still locked - a reap must leave it alone.
|
||||
errorret_t ret = assetReapUnused();
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_int_equal(entry->type, ASSET_LOADER_TYPE_LOCALE);
|
||||
|
||||
assetEntryUnlock(entry);
|
||||
errorret_t disposeRet = assetEntryDispose(entry);
|
||||
assert_true(errorIsOk(disposeRet));
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
static void test_getEntry_reaps_when_pool_full(void **state) {
|
||||
// Fill every entry slot with a zero-ref LOADED entry.
|
||||
for(int i = 0; i < ASSET_ENTRY_COUNT_MAX; i++) {
|
||||
char_t name[ASSET_FILE_NAME_MAX];
|
||||
snprintf(name, sizeof(name), "full%d.locale", i);
|
||||
assetentry_t *entry = assetGetEntry(name, ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
assetEntryLock(entry);
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
assert_int_equal(entry->state, ASSET_ENTRY_STATE_LOADED);
|
||||
assetEntryUnlock(entry);
|
||||
}
|
||||
|
||||
// The pool is now completely full of zero-ref LOADED entries with no
|
||||
// ASSET_LOADER_TYPE_NULL slots left. Requesting one more must trigger an
|
||||
// implicit reap instead of asserting.
|
||||
assetentry_t *fresh = assetGetEntry("fresh.locale", ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
assert_non_null(fresh);
|
||||
assert_int_equal(fresh->state, ASSET_ENTRY_STATE_NOT_STARTED);
|
||||
|
||||
assetEntryLock(fresh);
|
||||
assetUpdate();
|
||||
assetUpdate();
|
||||
assert_int_equal(fresh->state, ASSET_ENTRY_STATE_LOADED);
|
||||
assetEntryUnlock(fresh);
|
||||
|
||||
errorret_t ret = assetEntryDispose(fresh);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
assert_int_equal(memoryGetAllocatedCount(), 0);
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// main
|
||||
// ============================================================
|
||||
@@ -503,6 +599,12 @@ int main(void) {
|
||||
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),
|
||||
cmocka_unit_test_setup_teardown(test_requireLoaded_propagates_error, asset_setup, asset_teardown),
|
||||
|
||||
// assetReapUnused
|
||||
cmocka_unit_test_setup_teardown(test_update_does_not_reap_automatically, asset_setup, asset_teardown),
|
||||
cmocka_unit_test_setup_teardown(test_reapUnused_disposes_zero_ref_loaded, asset_setup, asset_teardown),
|
||||
cmocka_unit_test_setup_teardown(test_reapUnused_ignores_referenced_entries, asset_setup, asset_teardown),
|
||||
cmocka_unit_test_setup_teardown(test_getEntry_reaps_when_pool_full, asset_setup, asset_teardown),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user