diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-09 20:40:56 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-05-09 20:40:56 -0400 |
| commit | ecf91229fb5c9150f2d60d97652bf0024a5c3435 (patch) | |
| tree | d8fcffa8d436222c4304d9f4d538165ddd734055 /tests | |
| parent | 9693ff2d4be85d356e07e3192baaa2262a7140ff (diff) | |
pke-test-serialization: add more complex tests
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/pke-test-serialization.cpp | 274 |
1 files changed, 270 insertions, 4 deletions
diff --git a/tests/pke-test-serialization.cpp b/tests/pke-test-serialization.cpp index 64523d8..c514578 100644 --- a/tests/pke-test-serialization.cpp +++ b/tests/pke-test-serialization.cpp @@ -1,14 +1,85 @@ #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; +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: @@ -18,8 +89,7 @@ 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"; +:PKFE:)VOGON"; int pke_test_serialization_001() { int64_t err_index = 0, i; srlztn_serialize_helper *h; @@ -103,10 +173,192 @@ int pke_test_deserialization_101() { 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 = 2; + 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, @@ -116,9 +368,23 @@ struct pke_test_group *pke_test_serialization_get_group() { .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]; |
