-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_assetmanager.cpp
More file actions
351 lines (277 loc) · 10 KB
/
test_assetmanager.cpp
File metadata and controls
351 lines (277 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "catch.hpp"
#include "../src/assets/AssetManager.hpp"
#include "../src/assets/HotReloader.hpp"
#include "../src/core/io/CafWriter.hpp"
#include "../src/core/io/CafTypes.hpp"
#include <cstring>
#include <cstdio>
#include <thread>
#include <chrono>
using namespace Caffeine;
using namespace Caffeine::IO;
using namespace Caffeine::Assets;
namespace {
const char* kTexturePath = "am_test_texture.caf";
const char* kAudioPath = "am_test_audio.caf";
const char* kShaderPath = "am_test_shader.caf";
void writeTextureCaf(const char* path, u32 w, u32 h) {
TextureMetadata meta{};
meta.width = w;
meta.height = h;
meta.format = 0;
meta.mipLevels = 1;
const u32 pixelCount = w * h * 4;
u8* pixels = new u8[pixelCount];
std::memset(pixels, 0xAB, pixelCount);
CafWriter::write(path, AssetType::Texture, CAF_FLAG_NONE,
&meta, sizeof(meta), pixels, pixelCount);
delete[] pixels;
}
void writeAudioCaf(const char* path) {
AudioMetadata meta{};
meta.sampleRate = 44100;
meta.channels = 2;
meta.bitsPerSample = 16;
meta.sampleCount = 512;
const u32 dataSize = meta.sampleCount * meta.channels * (meta.bitsPerSample / 8);
u8* pcm = new u8[dataSize];
std::memset(pcm, 0, dataSize);
CafWriter::write(path, AssetType::Audio, CAF_FLAG_NONE,
&meta, sizeof(meta), pcm, dataSize);
delete[] pcm;
}
void writeShaderCaf(const char* path) {
ShaderMetadata meta{};
meta.stage = 0;
meta.reserved = 0;
u8 bytecode[64]{};
std::memset(bytecode, 0xCC, sizeof(bytecode));
CafWriter::write(path, AssetType::Shader, CAF_FLAG_NONE,
&meta, sizeof(meta), bytecode, sizeof(bytecode));
}
struct TestFixture {
TestFixture() {
writeTextureCaf(kTexturePath, 16, 16);
writeAudioCaf(kAudioPath);
writeShaderCaf(kShaderPath);
}
~TestFixture() {
std::remove(kTexturePath);
std::remove(kAudioPath);
std::remove(kShaderPath);
}
};
} // anonymous namespace
TEST_CASE("AssetManager - construction with null JobSystem", "[assets]") {
AssetManager mgr(nullptr, "");
CacheStats s = mgr.cacheStats();
REQUIRE(s.totalCachedBytes == 0);
REQUIRE(s.textureCount == 0);
REQUIRE(s.audioCount == 0);
REQUIRE(s.pendingJobs == 0);
}
TEST_CASE("AssetManager - loadSync Texture", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<Texture>(kTexturePath);
REQUIRE(handle.isValid());
REQUIRE(handle.isReady());
REQUIRE(handle.get() != nullptr);
const Texture* tex = handle.get();
REQUIRE(tex->width == 16);
REQUIRE(tex->height == 16);
REQUIRE(tex->mipLevels == 1);
REQUIRE(tex->pixels != nullptr);
REQUIRE(tex->pixelDataSize == 16 * 16 * 4);
}
TEST_CASE("AssetManager - loadSync AudioClip", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<AudioClip>(kAudioPath);
REQUIRE(handle.isReady());
const AudioClip* clip = handle.get();
REQUIRE(clip != nullptr);
REQUIRE(clip->sampleRate == 44100);
REQUIRE(clip->channels == 2);
REQUIRE(clip->bitsPerSample == 16);
REQUIRE(clip->sampleCount == 512);
REQUIRE(clip->pcmData != nullptr);
}
TEST_CASE("AssetManager - loadSync ShaderBlob", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<ShaderBlob>(kShaderPath);
REQUIRE(handle.isReady());
const ShaderBlob* sh = handle.get();
REQUIRE(sh != nullptr);
REQUIRE(sh->stage == 0);
REQUIRE(sh->bytecode != nullptr);
REQUIRE(sh->bytecodeSize == 64);
}
TEST_CASE("AssetManager - second load of same path is cache hit", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto h1 = mgr.loadSync<Texture>(kTexturePath);
auto h2 = mgr.loadSync<Texture>(kTexturePath);
REQUIRE(h1.id() == h2.id());
CacheStats s = mgr.cacheStats();
REQUIRE(s.cacheHitRate > 0.0f);
}
TEST_CASE("AssetManager - cacheStats counts textures and audio", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto th = mgr.loadSync<Texture>(kTexturePath);
auto ah = mgr.loadSync<AudioClip>(kAudioPath);
CacheStats s = mgr.cacheStats();
REQUIRE(s.textureCount == 1);
REQUIRE(s.audioCount == 1);
REQUIRE(s.totalCachedBytes > 0);
}
TEST_CASE("AssetManager - collectGarbage unloads unreferenced assets", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
{
auto handle = mgr.loadSync<Texture>(kTexturePath);
REQUIRE(handle.isReady());
}
mgr.collectGarbage();
CacheStats s = mgr.cacheStats();
REQUIRE(s.textureCount == 0);
REQUIRE(s.totalCachedBytes == 0);
}
TEST_CASE("AssetManager - collectGarbage does not unload referenced assets", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<Texture>(kTexturePath);
REQUIRE(handle.isReady());
mgr.collectGarbage();
REQUIRE(handle.isReady());
CacheStats s = mgr.cacheStats();
REQUIRE(s.textureCount == 1);
}
TEST_CASE("AssetManager - handle copy increments ref, original still valid", "[assets]") {
TestFixture fix;
AssetManager mgr(nullptr, "");
auto h1 = mgr.loadSync<Texture>(kTexturePath);
auto h2 = h1;
REQUIRE(h1.isReady());
REQUIRE(h2.isReady());
REQUIRE(h1.id() == h2.id());
}
TEST_CASE("AssetManager - loadAsync returns handle that becomes ready", "[assets]") {
TestFixture fix;
Threading::JobSystem jobs(2);
AssetManager mgr(&jobs, "");
auto handle = mgr.loadAsync<Texture>(kTexturePath);
REQUIRE(handle.isValid());
const int maxWaitMs = 2000;
int waited = 0;
while (!handle.isReady() && waited < maxWaitMs) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
waited += 10;
}
REQUIRE(handle.isReady());
REQUIRE(handle.get() != nullptr);
REQUIRE(handle.get()->width == 16);
}
TEST_CASE("AssetManager - failed load returns handle with Failed status", "[assets]") {
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<Texture>("nonexistent_file_xyz.caf");
REQUIRE(handle.isValid());
REQUIRE_FALSE(handle.isReady());
REQUIRE(handle.get() == nullptr);
}
TEST_CASE("AssetManager - tick advances frame index", "[assets]") {
AssetManager mgr(nullptr, "");
mgr.tick(42);
CacheStats s = mgr.cacheStats();
REQUIRE(s.totalCachedBytes == 0);
}
// ============================================================================
// HotReloader tests
// ============================================================================
TEST_CASE("HotReloader - default state is not running", "[hotreload]") {
HotReloader hr;
REQUIRE_FALSE(hr.IsRunning());
}
TEST_CASE("HotReloader - Start/Stop lifecycle", "[hotreload]") {
auto tmpDir = std::filesystem::temp_directory_path() / "caffeine_hr_start_stop";
std::filesystem::create_directories(tmpDir);
HotReloader hr;
REQUIRE_FALSE(hr.IsRunning());
hr.Start(nullptr, tmpDir, 10);
REQUIRE(hr.IsRunning());
hr.Stop();
REQUIRE_FALSE(hr.IsRunning());
hr.Stop();
REQUIRE_FALSE(hr.IsRunning());
std::filesystem::remove_all(tmpDir);
}
TEST_CASE("HotReloader - Start twice does not crash", "[hotreload]") {
auto tmpDir = std::filesystem::temp_directory_path() / "caffeine_hr_start_twice";
std::filesystem::create_directories(tmpDir);
HotReloader hr;
hr.Start(nullptr, tmpDir, 10);
REQUIRE(hr.IsRunning());
hr.Start(nullptr, tmpDir, 10);
REQUIRE(hr.IsRunning());
hr.Stop();
REQUIRE_FALSE(hr.IsRunning());
std::filesystem::remove_all(tmpDir);
}
TEST_CASE("HotReloader - Update is safe when not running", "[hotreload]") {
HotReloader hr;
hr.Update();
auto tmpDir = std::filesystem::temp_directory_path() / "caffeine_hr_update_safe";
std::filesystem::create_directories(tmpDir);
hr.Start(nullptr, tmpDir);
hr.Stop();
hr.Update();
REQUIRE_FALSE(hr.IsRunning());
std::filesystem::remove_all(tmpDir);
}
TEST_CASE("HotReloader - integration reload and callback", "[hotreload]") {
auto tmpDir = std::filesystem::temp_directory_path() / "caffeine_hr_reload";
std::filesystem::remove_all(tmpDir);
std::filesystem::create_directories(tmpDir);
auto cafPath = tmpDir / "test_reload.caf";
{
TextureMetadata meta{};
meta.width = 2; meta.height = 2; meta.format = 0; meta.mipLevels = 1;
u8 pixels[16];
std::memset(pixels, 0xAB, sizeof(pixels));
CafWriter::write(cafPath.string().c_str(), AssetType::Texture,
CAF_FLAG_NONE, &meta, sizeof(meta), pixels, sizeof(pixels));
}
AssetManager mgr(nullptr, "");
auto handle = mgr.loadSync<Texture>(cafPath.string().c_str());
REQUIRE(handle.isReady());
REQUIRE(handle.get()->pixels[0] == 0xAB);
bool callbackFired = false;
Caffeine::Assets::HotReloadedAsset callbackInfo;
{
HotReloader hr;
hr.SetNotificationCallback([&](const Caffeine::Assets::HotReloadedAsset& a) {
callbackFired = true;
callbackInfo = a;
});
hr.Start(&mgr, tmpDir, 10);
std::this_thread::sleep_for(std::chrono::milliseconds(600));
{
TextureMetadata meta{};
meta.width = 2; meta.height = 2; meta.format = 0; meta.mipLevels = 1;
u8 pixels[16];
std::memset(pixels, 0xCD, sizeof(pixels));
CafWriter::write(cafPath.string().c_str(), AssetType::Texture,
CAF_FLAG_NONE, &meta, sizeof(meta), pixels, sizeof(pixels));
}
std::this_thread::sleep_for(std::chrono::milliseconds(1100));
hr.Update();
REQUIRE(handle.get()->pixels[0] == 0xCD);
REQUIRE(callbackFired);
REQUIRE(callbackInfo.assetId == handle.id());
REQUIRE(callbackInfo.type == AssetType::Texture);
hr.Stop();
}
std::filesystem::remove_all(tmpDir);
}