diff --git a/framework/common_components/CMakeLists.txt b/framework/common_components/CMakeLists.txt index 79b8a71..4491f04 100644 --- a/framework/common_components/CMakeLists.txt +++ b/framework/common_components/CMakeLists.txt @@ -4,6 +4,8 @@ project(common_components CXX) add_library(common_components INTERFACE) +add_library(MM::common_components ALIAS common_components) + target_include_directories(common_components INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src") target_link_libraries(common_components INTERFACE @@ -13,8 +15,23 @@ target_link_libraries(common_components INTERFACE ########################## +add_library(common_components_serialize_s6zer INTERFACE) + +add_library(MM::common_components_serialize_s6zer ALIAS common_components_serialize_s6zer) + +target_include_directories(common_components_serialize_s6zer INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src") + +target_link_libraries(common_components_serialize_s6zer INTERFACE + common_components + s6zer +) + +########################## + add_library(common_components_serialize_json INTERFACE) +add_library(MM::common_components_serialize_json ALIAS common_components_serialize_json) + target_include_directories(common_components_serialize_json INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src") target_link_libraries(common_components_serialize_json INTERFACE diff --git a/framework/common_components/src/mm/components/serialize/s6zer_color.hpp b/framework/common_components/src/mm/components/serialize/s6zer_color.hpp new file mode 100644 index 0000000..9f06bb6 --- /dev/null +++ b/framework/common_components/src/mm/components/serialize/s6zer_color.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include + +namespace MM::Components { + +MM_DEFINE_SERIALIZE(Color, + MM_S6ZER_BAIL(stream.serializeFloat(data.color.r)) + MM_S6ZER_BAIL(stream.serializeFloat(data.color.g)) + MM_S6ZER_BAIL(stream.serializeFloat(data.color.b)) + MM_S6ZER_BAIL(stream.serializeFloat(data.color.a)) +) + +} // MM::Components + diff --git a/framework/common_components/test/CMakeLists.txt b/framework/common_components/test/CMakeLists.txt index a03479b..ae8d40e 100644 --- a/framework/common_components/test/CMakeLists.txt +++ b/framework/common_components/test/CMakeLists.txt @@ -7,10 +7,25 @@ target_include_directories(common_component_json_serialization_test PRIVATE ".") target_link_libraries(common_component_json_serialization_test common_components_serialize_json - engine gtest_main ) add_test(NAME common_component_json_serialization_test COMMAND common_component_json_serialization_test) +######################################## + +add_executable(common_component_s6zer_serialization_test + component_s6zer_serialization_test.cpp +) + +target_include_directories(common_component_s6zer_serialization_test PRIVATE ".") + +target_link_libraries(common_component_s6zer_serialization_test + common_components_serialize_s6zer + + gtest_main +) + +add_test(NAME common_component_s6zer_serialization_test COMMAND common_component_s6zer_serialization_test) + diff --git a/framework/common_components/test/component_json_serialization_test.cpp b/framework/common_components/test/component_json_serialization_test.cpp index 026281f..6ab8a94 100644 --- a/framework/common_components/test/component_json_serialization_test.cpp +++ b/framework/common_components/test/component_json_serialization_test.cpp @@ -1,4 +1,3 @@ -#include "mm/components/position2d.hpp" #include #include diff --git a/framework/common_components/test/component_s6zer_serialization_test.cpp b/framework/common_components/test/component_s6zer_serialization_test.cpp new file mode 100644 index 0000000..080eb59 --- /dev/null +++ b/framework/common_components/test/component_s6zer_serialization_test.cpp @@ -0,0 +1,289 @@ +#include + +#include + +#include + +//#include +#include + +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include +//#include + +#define TEST_JSON_SERL_EXPAND(x) x +#define TEST_JSON_SERL_IN_OUT(TYPE, JSON_STR, TEST_CORPUS) \ +TEST(common_components_json_serialization, in_out_##TYPE) { \ + MM::Components::TYPE comp; \ + { /* in */ \ + auto j = nlohmann::json::parse(JSON_STR); \ + comp = j; \ + TEST_CORPUS \ + } \ + { /* out */ \ + nlohmann::json j; \ + j = comp; \ + ASSERT_EQ(JSON_STR, j.dump()); \ + } \ +} + + +//TEST(common_components_json_serialization, in_out_name) { + //MM::Components::Name comp; + + //const char* json_test_file = "{\"str\":\"test_name\"}"; + + //{ // in + //auto j = nlohmann::json::parse(json_test_file); + + //EXPECT_NO_THROW({ + //comp = j; + //}); + + //ASSERT_EQ(comp.str, "test_name"); + //} + + //{ // out + //nlohmann::json j; + + //EXPECT_NO_THROW({ + //j = comp; + //}); + + //ASSERT_EQ(json_test_file, j.dump()); + //} +//} + +//TEST(common_components_json_serialization, in_out_name_fail) { + //MM::Components::Name name_comp; + + //// intentional malformed json string + //const char* json_test_file = "{\"strasdf\":\"test_name\"}"; + + //{ // in + //auto j = nlohmann::json::parse(json_test_file); + + //ASSERT_ANY_THROW({ + //name_comp = j; + //}); + //} +//} + +// ############################################################## + +//TEST_S6ZER_SERL_IN_OUT( + //Color, + //R"({"color":{"w":1337.0,"x":0.0,"y":1.0,"z":3.0}})", + //TEST_JSON_SERL_EXPAND({ + //glm::vec4 comp_val(0.f, 1.f, 3.f, 1337.f); + //ASSERT_EQ(comp.color.x, comp_val.x); + //ASSERT_EQ(comp.color.y, comp_val.y); + //ASSERT_EQ(comp.color.z, comp_val.z); + //ASSERT_EQ(comp.color.w, comp_val.w); + //}) +//) + +TEST(common_components_s6zer_serialization, out_in_color) { + const MM::Components::Color comp_out{ + {1337.f, 0.f, 1.f, 3.f} + }; + + std::array buffer; + size_t buffer_size = buffer.size()*sizeof(uint32_t); + + { // to bits + MM::s6zer::StreamWriter writer{buffer.data(), buffer_size}; + ASSERT_TRUE(MM::Components::mm_serialize(writer, comp_out)); + + ASSERT_TRUE(writer.flush()); + buffer_size = writer.bytesWritten(); + } + + MM::Components::Color comp_in; + + { // from bits + MM::s6zer::StreamReader reader{buffer.data(), buffer_size}; + + ASSERT_TRUE(MM::Components::mm_serialize(reader, comp_in)); + + ASSERT_EQ(reader.bytesRead(), buffer_size); + } + + ASSERT_EQ(comp_out.color.x, comp_in.color.x); + ASSERT_EQ(comp_out.color.y, comp_in.color.y); + ASSERT_EQ(comp_out.color.z, comp_in.color.z); + ASSERT_EQ(comp_out.color.w, comp_in.color.w); +} + +#if 0 + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Position2D, + R"({"pos":{"x":42.0,"y":6.0}})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.pos.x, 42.f); + ASSERT_EQ(comp.pos.y, 6.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Position2D_ZOffset, + R"({"z_offset":3.0})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.z_offset, 3.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Position3D, + R"({"pos":{"x":42.0,"y":6.0,"z":44.0}})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.pos.x, 42.f); + ASSERT_EQ(comp.pos.y, 6.f); + ASSERT_EQ(comp.pos.z, 44.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Rotation2D, + R"({"rot":42.0})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.rot, 42.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Scale2D, + R"({"scale":{"x":42.0,"y":6.0}})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.scale.x, 42.f); + ASSERT_EQ(comp.scale.y, 6.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Transform4x4, + R"({"trans":[{"w":0.0,"x":1.0,"y":0.0,"z":0.0},{"w":0.0,"x":0.0,"y":1.0,"z":0.0},{"w":0.0,"x":0.0,"y":0.0,"z":1.0},{"w":1.0,"x":0.0,"y":0.0,"z":0.0}]})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.trans, glm::mat4x4{1.f}); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Velocity2DPosition, + R"({"pos_vel":{"x":42.0,"y":6.0}})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.pos_vel.x, 42.f); + ASSERT_EQ(comp.pos_vel.y, 6.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Velocity2DPositionIntent, + R"({"intent":{"x":42.0,"y":6.0}})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.intent.x, 42.f); + ASSERT_EQ(comp.intent.y, 6.f); + ) +) + +// ############################################################## + +TEST_JSON_SERL_IN_OUT( + Velocity2DRotation, + R"({"rot_vel":42.0})", + TEST_JSON_SERL_EXPAND( + ASSERT_EQ(comp.rot_vel, 42.f); + ) +) + +// ############################################################## + +TEST(common_components_json_serialization, in_out_view_dir2d) { + MM::Components::ViewDir2D comp; + + const char* json_test_file = R"({"dir":6.0})"; + + { // in + auto j = nlohmann::json::parse(json_test_file); + + EXPECT_NO_THROW({ + comp = j; + }); + + ASSERT_EQ(comp.dir, 6.f); + } + + { // out + nlohmann::json j; + + EXPECT_NO_THROW({ + j = comp; + }); + + ASSERT_EQ(json_test_file, j.dump()); + } +} + +// ############################################################## + +TEST(common_components_json_serialization, in_out_view_dir3d) { + MM::Components::ViewDir3D comp; + + const char* json_test_file = R"({"pitch":6.0,"roll":99.0,"yaw":42.0})"; + + { // in + auto j = nlohmann::json::parse(json_test_file); + + EXPECT_NO_THROW({ + comp = j; + }); + + ASSERT_EQ(comp.yaw, 42.f); + ASSERT_EQ(comp.pitch, 6.f); + ASSERT_EQ(comp.roll, 99.f); + } + + { // out + nlohmann::json j; + + EXPECT_NO_THROW({ + j = comp; + }); + + ASSERT_EQ(json_test_file, j.dump()); + } +} + +#endif + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} +