add color component as s6zer, will do more later

This commit is contained in:
Green Sky 2022-12-07 00:19:17 +01:00 committed by Erik Scholz
parent f27300a8fa
commit 4ab1d99529
5 changed files with 339 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,17 @@
#pragma once
#include <mm/s6zer/serialize.hpp>
#include <mm/components/color.hpp>
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

View File

@ -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)

View File

@ -1,4 +1,3 @@
#include "mm/components/position2d.hpp"
#include <gtest/gtest.h>
#include <mm/components/serialize/json_name.hpp>

View File

@ -0,0 +1,289 @@
#include <gtest/gtest.h>
#include <array>
#include <mm/s6zer/serialize.hpp>
//#include <mm/components/serialize/json_name.hpp>
#include <mm/components/serialize/s6zer_color.hpp>
//#include <mm/components/serialize/json_position2d.hpp>
//#include <mm/components/serialize/json_position2d_zoffset.hpp>
//#include <mm/components/serialize/json_position3d.hpp>
//#include <mm/components/serialize/json_rotation2d.hpp>
//#include <mm/components/serialize/json_scale2d.hpp>
//#include <mm/components/serialize/json_transform4x4.hpp>
//#include <mm/components/serialize/json_velocity2d_position.hpp>
//#include <mm/components/serialize/json_velocity2d_position_intent.hpp>
//#include <mm/components/serialize/json_velocity2d_rotation.hpp>
//#include <mm/components/serialize/json_view_dir2d.hpp>
//#include <mm/components/serialize/json_view_dir3d.hpp>
#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<uint32_t, 128> 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();
}