-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_scenemanager.cpp
More file actions
284 lines (236 loc) · 8.39 KB
/
test_scenemanager.cpp
File metadata and controls
284 lines (236 loc) · 8.39 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
#include "catch.hpp"
#include "../src/Caffeine.hpp"
#include "../src/scene/SceneComponents.hpp"
#include "../src/scene/SceneSerializer.hpp"
#include "../src/scene/SceneManager.hpp"
#include <cmath>
#include <cstring>
#include <vector>
using namespace Caffeine;
using namespace Caffeine::ECS;
using namespace Caffeine::Scene;
static constexpr f32 kEps = 0.001f;
static bool approxEq(f32 a, f32 b) {
return fabsf(a - b) < kEps;
}
TEST_CASE("Parent - default construction", "[scene]") {
Parent p{};
REQUIRE_FALSE(p.parent.isValid());
REQUIRE(p.dirty == true);
}
TEST_CASE("WorldTransform - default is identity", "[scene]") {
WorldTransform wt{};
Mat4 identity = Mat4::identity();
const f32* a = wt.matrix.data();
const f32* b = identity.data();
for (int i = 0; i < 16; ++i) {
REQUIRE(approxEq(a[i], b[i]));
}
}
TEST_CASE("SceneSerializer - serialize empty world to memory returns true", "[scene]") {
World world;
SceneSerializer s(world);
std::vector<u8> bytes;
REQUIRE(s.serializeToMemory(bytes));
REQUIRE(bytes.size() >= sizeof(CafHeader));
}
TEST_CASE("SceneSerializer - memory output has valid CafHeader magic", "[scene]") {
World world;
SceneSerializer s(world);
std::vector<u8> bytes;
s.serializeToMemory(bytes);
CafHeader hdr{};
memcpy(&hdr, bytes.data(), sizeof(hdr));
REQUIRE(hdr.magic == CafHeader::kMagic);
REQUIRE(hdr.type == AssetType::Scene);
}
TEST_CASE("SceneSerializer - round-trip Position2D", "[scene]") {
World world;
Entity e = world.create();
world.add<Position2D>(e);
world.get<Position2D>(e)->x = 3.0f;
world.get<Position2D>(e)->y = 7.0f;
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
REQUIRE(SceneSerializer(world2).deserializeFromMemory(bytes));
bool found = false;
ComponentQuery q; q.with<Position2D>();
world2.forEach<Position2D>(q, [&](Entity, Position2D& p) {
REQUIRE(approxEq(p.x, 3.0f));
REQUIRE(approxEq(p.y, 7.0f));
found = true;
});
REQUIRE(found);
}
TEST_CASE("SceneSerializer - round-trip Health", "[scene]") {
World world;
Entity e = world.create();
world.add<Health>(e);
world.get<Health>(e)->current = 50;
world.get<Health>(e)->max = 200;
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
SceneSerializer(world2).deserializeFromMemory(bytes);
bool found = false;
ComponentQuery q; q.with<Health>();
world2.forEach<Health>(q, [&](Entity, Health& h) {
REQUIRE(h.current == 50u);
REQUIRE(h.max == 200u);
found = true;
});
REQUIRE(found);
}
TEST_CASE("SceneSerializer - round-trip entity count", "[scene]") {
World world;
for (int i = 0; i < 5; ++i) {
Entity e = world.create();
world.add<Position2D>(e);
}
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
SceneSerializer(world2).deserializeFromMemory(bytes);
REQUIRE(world2.entityCount() == 5u);
}
TEST_CASE("SceneSerializer - round-trip multiple component types on same entity", "[scene]") {
World world;
Entity e = world.create();
world.add<Position2D>(e);
world.get<Position2D>(e)->x = 1.0f;
world.get<Position2D>(e)->y = 2.0f;
world.add<Health>(e);
world.get<Health>(e)->current = 75;
world.get<Health>(e)->max = 100;
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
SceneSerializer(world2).deserializeFromMemory(bytes);
bool found = false;
ComponentQuery q; q.with<Position2D, Health>();
world2.forEach<Position2D, Health>(q, [&](Entity, Position2D& p, Health& h) {
REQUIRE(approxEq(p.x, 1.0f));
REQUIRE(h.current == 75u);
found = true;
});
REQUIRE(found);
}
TEST_CASE("SceneSerializer - round-trip WorldTransform", "[scene]") {
World world;
Entity e = world.create();
world.add<WorldTransform>(e);
world.get<WorldTransform>(e)->matrix = Mat4::translation(5.0f, 10.0f, 0.0f);
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
SceneSerializer(world2).deserializeFromMemory(bytes);
bool found = false;
ComponentQuery q; q.with<WorldTransform>();
world2.forEach<WorldTransform>(q, [&](Entity, WorldTransform& wt) {
REQUIRE(approxEq(wt.matrix.data()[12], 5.0f));
REQUIRE(approxEq(wt.matrix.data()[13], 10.0f));
found = true;
});
REQUIRE(found);
}
TEST_CASE("SceneSerializer - round-trip Parent dirty flag", "[scene]") {
World world;
Entity parent = world.create();
Entity child = world.create();
world.add<Parent>(child);
world.get<Parent>(child)->parent = parent;
world.get<Parent>(child)->dirty = false;
std::vector<u8> bytes;
SceneSerializer(world).serializeToMemory(bytes);
World world2;
SceneSerializer(world2).deserializeFromMemory(bytes);
bool found = false;
ComponentQuery q; q.with<Parent>();
world2.forEach<Parent>(q, [&](Entity, Parent& p) {
REQUIRE(p.dirty == false);
REQUIRE(p.parent.isValid());
found = true;
});
REQUIRE(found);
}
TEST_CASE("SceneSerializer - deserializeFromMemory empty data returns false", "[scene]") {
World world;
std::vector<u8> empty;
REQUIRE_FALSE(SceneSerializer(world).deserializeFromMemory(empty));
}
TEST_CASE("SceneSerializer - deserializeFromMemory bad magic returns false", "[scene]") {
World world;
std::vector<u8> garbage(64, 0xAB);
REQUIRE_FALSE(SceneSerializer(world).deserializeFromMemory(garbage));
}
TEST_CASE("SceneManager - construct, activeWorld is nullptr", "[scene]") {
SceneManager mgr;
REQUIRE(mgr.activeWorld() == nullptr);
}
TEST_CASE("SceneManager - pushWorld, activeWorld returns correct pointer", "[scene]") {
SceneManager mgr;
auto world = std::make_unique<World>();
World* raw = world.get();
mgr.pushWorld(std::move(world));
REQUIRE(mgr.activeWorld() == raw);
}
TEST_CASE("SceneManager - popScene after pushWorld, activeWorld is nullptr", "[scene]") {
SceneManager mgr;
mgr.pushWorld(std::make_unique<World>());
mgr.popScene();
REQUIRE(mgr.activeWorld() == nullptr);
}
TEST_CASE("SceneManager - pushWorld twice, popScene returns first world", "[scene]") {
SceneManager mgr;
auto world1 = std::make_unique<World>();
World* raw1 = world1.get();
mgr.pushWorld(std::move(world1));
mgr.pushWorld(std::make_unique<World>());
mgr.popScene();
REQUIRE(mgr.activeWorld() == raw1);
}
TEST_CASE("SceneManager - isTransitioning initially false", "[scene]") {
SceneManager mgr;
REQUIRE_FALSE(mgr.isTransitioning());
}
TEST_CASE("SceneManager - update with dt=0 does not crash", "[scene]") {
SceneManager mgr;
mgr.pushWorld(std::make_unique<World>());
mgr.update(0.0);
REQUIRE(mgr.activeWorld() != nullptr);
}
TEST_CASE("WorldTransform - 3 levels of hierarchy propagated correctly", "[scene]") {
World world;
Entity root = world.create();
Entity child = world.create();
Entity grand = world.create();
world.add<Position2D>(root); world.get<Position2D>(root)->x = 10.0f;
world.add<WorldTransform>(root);
world.add<Position2D>(child); world.get<Position2D>(child)->x = 5.0f;
world.add<WorldTransform>(child);
world.add<Parent>(child); world.get<Parent>(child)->parent = root;
world.add<Position2D>(grand); world.get<Position2D>(grand)->x = 2.0f;
world.add<WorldTransform>(grand);
world.add<Parent>(grand); world.get<Parent>(grand)->parent = child;
auto propagate = [&](Entity e) {
Position2D* pos = world.get<Position2D>(e);
WorldTransform* wt = world.get<WorldTransform>(e);
Parent* par = world.get<Parent>(e);
if (!pos || !wt) return;
Mat4 local = Mat4::translation(pos->x, pos->y, 0.0f);
if (par && par->parent.isValid()) {
WorldTransform* pwt = world.get<WorldTransform>(par->parent);
wt->matrix = pwt ? pwt->matrix * local : local;
} else {
wt->matrix = local;
}
};
propagate(root);
propagate(child);
propagate(grand);
WorldTransform* grandWT = world.get<WorldTransform>(grand);
REQUIRE(grandWT != nullptr);
REQUIRE(approxEq(grandWT->matrix.data()[12], 17.0f));
REQUIRE(approxEq(grandWT->matrix.data()[13], 0.0f));
}