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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
#include "./pke-test-serialization.h"
#include "game-settings.hpp"
#include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
#include "camera.hpp"
#include "ecs.hpp"
#include "physics.hpp"
#include "pk.h"
#include "scene.hpp"
#include "serialization-camera.hpp"
#include "serialization-component.hpp"
#include "serialization.hpp"
#include <cstring>
#include <sstream>
pk_membucket *bkt = nullptr;
const char *test_scene_name = "srlztn_test_scene";
pke_scene *test_scene = nullptr;
void pke_test_serialization_spinup() {
pkeSettings.isSimulationPaused = true;
Physics_Init();
ECS_Init();
PkeCamera_Init();
pke_scene_master_init();
test_scene = pke_scene_create(test_scene_name);
};
void pke_test_serialization_teardown() {
// pke_scene_remove(test_scene->scene_handle); // TODO this doesn't work?
test_scene = nullptr;
pke_scene_master_teardown();
PkeCamera_Teardown();
ECS_Teardown();
Physics_Teardown();
};
int pke_test_serialization_999() {
int64_t err_index = 0;
pke_kve_container kvec{};
pke_kve kve{};
const pk_handle zed_handle = {0,0};
PKE_TEST_ASSERT(kvec.srlztn_handle == zed_handle, err_index);
PKE_TEST_ASSERT(kvec.type_code.length == 0, err_index);
PKE_TEST_ASSERT(kvec.type_code.reserved == 0, err_index);
PKE_TEST_ASSERT(kvec.type_code.val == nullptr, err_index);
PKE_TEST_ASSERT(kvec.bkt == nullptr, err_index);
// 6
PKE_TEST_ASSERT(kvec.child_handles.next == 0, err_index);
PKE_TEST_ASSERT(kvec.child_handles.reserved == 0, err_index);
PKE_TEST_ASSERT(kvec.child_handles.stride == sizeof(pk_handle), err_index);
PKE_TEST_ASSERT(kvec.child_handles.alignment == alignof(pk_handle), err_index);
PKE_TEST_ASSERT(kvec.child_handles.bkt == nullptr, err_index);
PKE_TEST_ASSERT(kvec.child_handles.data == nullptr, err_index);
// 12
PKE_TEST_ASSERT(kvec.children.next == 0, err_index);
PKE_TEST_ASSERT(kvec.children.reserved == 0, err_index);
PKE_TEST_ASSERT(kvec.children.stride == sizeof(pke_kve_container*), err_index);
PKE_TEST_ASSERT(kvec.children.alignment == alignof(pke_kve_container*), err_index);
PKE_TEST_ASSERT(kvec.children.bkt == nullptr, err_index);
PKE_TEST_ASSERT(kvec.children.data == nullptr, err_index);
// 18
PKE_TEST_ASSERT(kvec.arr.next == 0, err_index);
PKE_TEST_ASSERT(kvec.arr.reserved == 0, err_index);
PKE_TEST_ASSERT(kvec.arr.stride == sizeof(pke_kve), err_index);
PKE_TEST_ASSERT(kvec.arr.alignment == alignof(pke_kve), err_index);
PKE_TEST_ASSERT(kvec.arr.bkt == nullptr, err_index);
PKE_TEST_ASSERT(kvec.arr.data == nullptr, err_index);
// 24
PKE_TEST_ASSERT(kve.key == nullptr, err_index);
PKE_TEST_ASSERT(kve.val == nullptr, err_index);
PKE_TEST_ASSERT(kve.end == nullptr, err_index);
return 0;
}
const char *const test_001_str = R"VOGON(:PKFB:
:0:
InstPos:00000000!00000000
Position:0.000000;1.000000;2.000000
Rotation:7.000000;8.000000;9.000000;6.000000
Scale:5.000000;4.000000;3.000000
:PKFE:)VOGON";
int pke_test_serialization_001() {
int64_t err_index = 0, i;
srlztn_serialize_helper *h;
std::stringstream ss;
try {
bkt = pk_bucket_create("pke_test_serialization", PK_DEFAULT_BUCKET_SIZE, false);
h = pke_serialize_init(bkt);
glm::vec3 pos = glm::vec3(0,1,2);
glm::quat rot = glm::quat(6,7,8,9);
glm::vec3 scale = glm::vec3(5,4,3);
pke_serialize_inst_pos(h, pos, rot, scale);
pke_serialize_scene_to_stream(ss, h);
PKE_TEST_ASSERT(h->bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr.next == 3, err_index);
for (i = 0; i < 3; ++i) {
// 6,9,12
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].key != nullptr, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].val != nullptr, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].end != nullptr, err_index);
}
std::string s = ss.str();
// 15
PKE_TEST_ASSERT(strstr(test_001_str, s.c_str()) != nullptr, err_index);
pke_serialize_teardown(h);
} catch (const std::exception &ex) {
pk_bucket_destroy(bkt);
throw;
}
pk_bucket_destroy(bkt);
return 0;
}
int pke_test_deserialization_101() {
int64_t err_index = 0, i;
srlztn_deserialize_helper *h;
std::stringstream ss(test_001_str);
try {
bkt = pk_bucket_create("pke_test_serialization", PK_DEFAULT_BUCKET_SIZE, false);
h = pke_deserialize_init(bkt);
pke_deserialize_scene_from_stream(ss, h);
PKE_TEST_ASSERT(h->bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr.next == 3, err_index);
for (i = 0; i < 3; ++i) {
// 6,9,12
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].key != nullptr, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].val != nullptr, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr[i].end != nullptr, err_index);
}
glm::vec3 pos;
glm::quat rot;
glm::vec3 scale;
pke_deserialize_inst_pos(h, &h->kvp_containers[0], pos, rot, scale);
// 15
PKE_TEST_ASSERT(pos == glm::vec3(0,1,2), err_index);
PKE_TEST_ASSERT(rot == glm::quat(6,7,8,9), err_index);
PKE_TEST_ASSERT(scale == glm::vec3(5,4,3), err_index);
pke_deserialize_teardown(h);
} catch (const std::exception &ex) {
pk_bucket_destroy(bkt);
throw;
}
pk_bucket_destroy(bkt);
return 0;
}
const char *const test_002_str = R"VOGON(:PKFB:
:0:
InstPos:00000000!00000000
Rotation:0.000000;0.000000;0.000000;1.000000
Instance:00000000!00000001
ChildId:00000000!00000000
UUID:67eec271-ab64-78d2-a38e-1f2946e87ccd
Mass:1.000000
CollisionLayer:0x0000000000000001
CollisionMask:0x0000000000000001
InstPos:00000000!00000002
Position:0.000000;-10.000000;-10.000000
Rotation:0.000000;0.000000;0.000000;1.000000
Scale:5.000000;4.000000;3.000000
Camera:00000000!00000003
ChildId:00000000!00000002
UUID:67eec26e-56c4-7aeb-a43c-986966334873
Type:0x01
Orientation:0x01
TargetUUID:67eec271-ab64-78d2-a38e-1f2946e87ccd
IsPrimary:1
:PKFE:)VOGON";
int pke_test_serialization_002() {
int64_t err_index = 0;
uint32_t i, k;
srlztn_serialize_helper *h = nullptr;
pke_kve *kve = nullptr;
std::stringstream ss;
try {
bkt = pk_bucket_create("pke_test_serialization", PK_DEFAULT_BUCKET_SIZE, false);
h = pke_serialize_init(bkt);
const pk_uuid camera_uuid = { .uuid = { 0x67,0xEE,0xC2,0x6E,0x56,0xC4,0x7A,0xEB,0xA4,0x3C,0x98,0x69,0x66,0x33,0x48,0x73 } };
const pk_uuid target_uuid = { .uuid = { 0x67,0xEE,0xC2,0x71,0xAB,0x64,0x78,0xD2,0xA3,0x8E,0x1F,0x29,0x46,0xE8,0x7C,0xCD } };
// reminder that 'targeting' moves and rotates the camera, so we shouldn't see these values in the output
InstPos cam_inst_pos{};
cam_inst_pos.posRot.setIdentity();
cam_inst_pos.posRot.setOrigin(btVector3(0,0,0));
cam_inst_pos.posRot.setRotation(btQuaternion(0.f,0.f,0.f,1));
cam_inst_pos.scale = btVector3(1,1,1);
cam_inst_pos.mass = 1.f;
Entity_Base *target_ent = ECS_CreateGenericEntity();
CompInstance *target_ent_comp_inst = ECS_CreateInstance(target_ent, target_uuid, nullptr, &cam_inst_pos);
cam_inst_pos.scale = btVector3(5,4,3);
PkeCamera &cam = PkeCamera_Register(camera_uuid, cam_inst_pos);
cam.type = PKE_CAMERA_TYPE_PERSPECTIVE;
cam.view = PKE_CAMERA_VIEW_TARGET;
PkeCamera_SetPrimary(cam.camHandle);
PkeCamera_TargetInstance(cam.camHandle, target_ent_comp_inst);
pke_serialize_scene(h);
pke_serialize_scene_to_stream(ss, h);
std::string s = ss.str();
PKE_TEST_ASSERT(h->bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.next == 4, err_index);
// instpos
PKE_TEST_ASSERT(h->kvp_containers[0].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr.next == 1, err_index);
// instance
PKE_TEST_ASSERT(h->kvp_containers[1].child_handles.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[1].arr.next == 4, err_index);
// instpos
PKE_TEST_ASSERT(h->kvp_containers[2].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[2].arr.next == 3, err_index);
// camera
PKE_TEST_ASSERT(h->kvp_containers[3].child_handles.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[3].arr.next == 5, err_index);
// 12
for (k = 0; k < h->kvp_containers.next; ++k) {
for (i = 0; i < h->kvp_containers[k].arr.next; ++i) {
// 12 + (3 * (1 + 4 + 3 + 5))
kve = &h->kvp_containers[k].arr[i];
PKE_TEST_ASSERT(kve->key != nullptr, err_index);
PKE_TEST_ASSERT(kve->val != nullptr, err_index);
PKE_TEST_ASSERT(kve->end != nullptr, err_index);
}
}
// 51
PKE_TEST_ASSERT(strstr(test_002_str, s.c_str()) != nullptr, err_index);
pke_serialize_teardown(h);
} catch (const std::exception &ex) {
pk_bucket_destroy(bkt);
throw;
}
pk_bucket_destroy(bkt);
return 0;
}
int pke_test_deserialization_102() {
int64_t err_index = 0;
uint32_t i, k;
pke_kve *kve;
srlztn_deserialize_helper *h;
std::stringstream ss(test_002_str);
try {
bkt = pk_bucket_create("pke_test_serialization", PK_DEFAULT_BUCKET_SIZE, false);
h = pke_deserialize_init(bkt);
h->scene = test_scene;
const pk_uuid camera_uuid = { .uuid = { 0x67,0xEE,0xC2,0x6E,0x56,0xC4,0x7A,0xEB,0xA4,0x3C,0x98,0x69,0x66,0x33,0x48,0x73 } };
const pk_uuid target_uuid = { .uuid = { 0x67,0xEE,0xC2,0x71,0xAB,0x64,0x78,0xD2,0xA3,0x8E,0x1F,0x29,0x46,0xE8,0x7C,0xCD } };
pke_deserialize_scene_from_stream(ss, h);
PKE_TEST_ASSERT(h->bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.bkt == bkt, err_index);
PKE_TEST_ASSERT(h->kvp_containers.next == 4, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[1].child_handles.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[2].child_handles.next == 0, err_index);
PKE_TEST_ASSERT(h->kvp_containers[3].child_handles.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[0].arr.next == 1, err_index);
PKE_TEST_ASSERT(h->kvp_containers[1].arr.next == 4, err_index);
PKE_TEST_ASSERT(h->kvp_containers[2].arr.next == 3, err_index);
PKE_TEST_ASSERT(h->kvp_containers[3].arr.next == 5, err_index);
// 12
for (k = 0; k < h->kvp_containers.next; ++k) {
for (i = 0; i < h->kvp_containers[k].arr.next; ++i) {
// 12 + (3 * (1 + 4 + 3 + 5))
kve = &h->kvp_containers[k].arr[i];
PKE_TEST_ASSERT(kve->key != nullptr, err_index);
PKE_TEST_ASSERT(kve->val != nullptr, err_index);
PKE_TEST_ASSERT(kve->end != nullptr, err_index);
}
}
glm::vec3 pos = glm::vec3(0,0,0);
glm::quat rot = glm::quat(1,0,0,0);
glm::vec3 scale = glm::vec3(1,1,1);
pke_deserialize_inst_pos(h, &h->kvp_containers[0], pos, rot, scale);
// 51
PKE_TEST_ASSERT(pos == glm::vec3(0,0,0), err_index);
PKE_TEST_ASSERT(rot == glm::quat(1,0,0,0), err_index);
PKE_TEST_ASSERT(scale == glm::vec3(1,1,1), err_index);
pke_deserialize_inst_pos(h, &h->kvp_containers[2], pos, rot, scale);
// 54
PKE_TEST_ASSERT(pos == glm::vec3(0,-10,-10), err_index);
PKE_TEST_ASSERT(rot == glm::quat(1,0,0,0), err_index);
PKE_TEST_ASSERT(scale == glm::vec3(5,4,3), err_index);
pke_deserialize_scene(h);
pk_handle_item_index_T item_index;
PkeCamera *des_cam = &PkeCamera_GetCameras(0, item_index)[0];
// 57
PKE_TEST_ASSERT(des_cam != nullptr, err_index);
PKE_TEST_ASSERT(des_cam->uuid == camera_uuid, err_index);
PKE_TEST_ASSERT(des_cam->type == PKE_CAMERA_TYPE_PERSPECTIVE, err_index);
PKE_TEST_ASSERT(des_cam->view == PKE_CAMERA_VIEW_TARGET, err_index);
PKE_TEST_ASSERT(des_cam->phys.target_inst_uuid == target_uuid, err_index);
PKE_TEST_ASSERT(des_cam->isPrimary == true, err_index);
pke_deserialize_teardown(h);
} catch (const std::exception &ex) {
pk_bucket_destroy(bkt);
throw;
}
pk_bucket_destroy(bkt);
return 0;
}
struct pke_test_group *pke_test_serialization_get_group() {
static const uint64_t test_count = 5;
static struct pke_test tests[test_count] = {
{
.title = "test 999",
.func = pke_test_serialization_999,
.expected_result = 0,
},
{
.title = "test 001",
.func = pke_test_serialization_001,
.expected_result = 0,
},
{
.title = "test 101",
.func = pke_test_deserialization_101,
.expected_result = 0,
},
{
.title = "test 002",
.func = pke_test_serialization_002,
.expected_result = 0,
},
{
.title = "test 102",
.func = pke_test_deserialization_102,
.expected_result = 0,
},
};
static struct pke_test_group group = {};
group.title = "de/serialization";
group.group_setup = nullptr;
group.group_teardown = nullptr;
group.test_setup = pke_test_serialization_spinup;
group.test_teardown = pke_test_serialization_teardown;
group.n_tests = test_count;
group.tests = &tests[0];
return &group;
}
|