From 44ea4e497f9e1449c5e9afe3a2e6d301aa3da9b4 Mon Sep 17 00:00:00 2001 From: saipubw Date: Tue, 10 Oct 2023 11:39:12 +0800 Subject: [PATCH] [struct_pack] add test for cross_platform (#471) --- src/struct_pack/tests/BUILD.bazel | 4 ++ src/struct_pack/tests/CMakeLists.txt | 11 +++ src/struct_pack/tests/binary_data/BUILD.bazel | 1 + .../tests/binary_data/test_cross_platform.dat | Bin 0 -> 255 bytes ...test_cross_platform_without_debug_info.dat | Bin 0 -> 184 bytes src/struct_pack/tests/test_cross_platform.cpp | 66 ++++++++++++++++++ src/struct_pack/tests/test_data_struct2.cpp | 16 +---- src/struct_pack/tests/test_struct.hpp | 20 ++++++ 8 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 src/struct_pack/tests/binary_data/BUILD.bazel create mode 100644 src/struct_pack/tests/binary_data/test_cross_platform.dat create mode 100644 src/struct_pack/tests/binary_data/test_cross_platform_without_debug_info.dat create mode 100644 src/struct_pack/tests/test_cross_platform.cpp diff --git a/src/struct_pack/tests/BUILD.bazel b/src/struct_pack/tests/BUILD.bazel index efa25b89c..211717cd2 100644 --- a/src/struct_pack/tests/BUILD.bazel +++ b/src/struct_pack/tests/BUILD.bazel @@ -8,4 +8,8 @@ cc_test( deps = [ "//:ylt" ], + data = [ + "//src/struct_pack/tests/binary_data:test_cross_platform.dat", + "//src/struct_pack/tests/binary_data:test_cross_platform_without_debug_info.dat" + ], ) diff --git a/src/struct_pack/tests/CMakeLists.txt b/src/struct_pack/tests/CMakeLists.txt index 5d6d9d4eb..e0d13e98c 100644 --- a/src/struct_pack/tests/CMakeLists.txt +++ b/src/struct_pack/tests/CMakeLists.txt @@ -13,7 +13,18 @@ add_executable(struct_pack_test test_compatible.cpp test_non_aggregated_type.cpp test_derived.cpp + test_cross_platform.cpp main.cpp ) add_test(NAME struct_pack_test COMMAND struct_pack_test) target_compile_definitions(struct_pack_test PRIVATE STRUCT_PACK_ENABLE_UNPORTABLE_TYPE) +add_custom_command( + TARGET struct_pack_test PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_SOURCE_DIR}/binary_data + ${CMAKE_BINARY_DIR}/output/tests/binary_data) +add_custom_command( + TARGET struct_pack_test PRE_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_SOURCE_DIR}/binary_data + ${CMAKE_BINARY_DIR}/src/struct_pack/tests/binary_data) diff --git a/src/struct_pack/tests/binary_data/BUILD.bazel b/src/struct_pack/tests/binary_data/BUILD.bazel new file mode 100644 index 000000000..9a0ce2ba0 --- /dev/null +++ b/src/struct_pack/tests/binary_data/BUILD.bazel @@ -0,0 +1 @@ +exports_files(["test_cross_platform.dat","test_cross_platform_without_debug_info.dat"]) \ No newline at end of file diff --git a/src/struct_pack/tests/binary_data/test_cross_platform.dat b/src/struct_pack/tests/binary_data/test_cross_platform.dat new file mode 100644 index 0000000000000000000000000000000000000000..0338937e82b3d47ab52d68fe0a942d1e63fcdd6e GIT binary patch literal 255 zcma#P6!?whFC$|EPs?8*@xKMgU~FOpv!PUT15YzTh_MkY*9@ipHt>M?|Nk?9fEEz5 zW~Aoi +#include +#include + +#include "doctest.h" +#include "test_struct.hpp" +using namespace struct_pack; +using namespace doctest; + +TEST_CASE("testing deserialize other platform data") { + std::ifstream ifs("binary_data/test_cross_platform.dat"); + if (!ifs.is_open()) { + ifs.open("src/struct_pack/tests/binary_data/test_cross_platform.dat"); + } + REQUIRE(ifs.is_open()); + auto result = struct_pack::deserialize(ifs); + REQUIRE(result.has_value()); + auto object = create_complicated_object(); + CHECK(result.value() == object); +} + +TEST_CASE("testing deserialize other platform data without debug info") { + std::ifstream ifs("binary_data/test_cross_platform_without_debug_info.dat"); + if (!ifs.is_open()) { + ifs.open( + "src/struct_pack/tests/binary_data/" + "test_cross_platform_without_debug_info.dat"); + } + REQUIRE(ifs.is_open()); + auto result = struct_pack::deserialize(ifs); + REQUIRE(result.has_value()); + auto object = create_complicated_object(); + CHECK(result.value() == object); +} + +TEST_CASE("testing serialize other platform data") { + std::ifstream ifs("binary_data/test_cross_platform.dat"); + if (!ifs.is_open()) { + ifs.open("src/struct_pack/tests/binary_data/test_cross_platform.dat"); + } + REQUIRE(ifs.is_open()); + std::string content(std::istreambuf_iterator{ifs}, + std::istreambuf_iterator{}); + auto object = create_complicated_object(); + auto buffer = + struct_pack::serialize(object); + CHECK(buffer == content); +} + +TEST_CASE("testing serialize other platform data") { + std::ifstream ifs("binary_data/test_cross_platform_without_debug_info.dat"); + if (!ifs.is_open()) { + ifs.open( + "src/struct_pack/tests/binary_data/" + "test_cross_platform_without_debug_info.dat"); + } + REQUIRE(ifs.is_open()); + std::string content(std::istreambuf_iterator{ifs}, + std::istreambuf_iterator{}); + auto object = create_complicated_object(); + auto buffer = + struct_pack::serialize(object); + CHECK(buffer == content); +} \ No newline at end of file diff --git a/src/struct_pack/tests/test_data_struct2.cpp b/src/struct_pack/tests/test_data_struct2.cpp index 349cd4be3..b984cce9c 100644 --- a/src/struct_pack/tests/test_data_struct2.cpp +++ b/src/struct_pack/tests/test_data_struct2.cpp @@ -49,21 +49,7 @@ TEST_CASE("test expected") { } TEST_CASE("testing object with containers, enum, tuple array, and pair") { - complicated_object v{Color::red, - 42, - "hello", - {{20, "tom"}, {22, "jerry"}}, - {"hello", "world"}, - {1, 2}, - {{1, {20, "tom"}}}, - {{1, {20, "tom"}}, {1, {22, "jerry"}}}, - {"aa", "bb"}, - {1, 2}, - {{1, {20, "tom"}}, {1, {22, "jerry"}}}, - {{1, 2}, {1, 3}}, - {person{20, "tom"}, {22, "jerry"}}, - {person{20, "tom"}, {22, "jerry"}}, - std::make_pair("aa", person{20, "tom"})}; + complicated_object v = create_complicated_object(); auto ret = serialize(v); complicated_object v1{}; diff --git a/src/struct_pack/tests/test_struct.hpp b/src/struct_pack/tests/test_struct.hpp index 028f3f5cf..38b29d6b2 100644 --- a/src/struct_pack/tests/test_struct.hpp +++ b/src/struct_pack/tests/test_struct.hpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +64,24 @@ struct complicated_object { } }; +inline complicated_object create_complicated_object() { + return complicated_object{Color::red, + 42, + "hello", + {{20, "tom"}, {22, "jerry"}}, + {"hello", "world"}, + {1, 2}, + {{1, {20, "tom"}}}, + {{1, {20, "tom"}}, {1, {22, "jerry"}}}, + {"aa", "bb"}, + {1, 2}, + {{1, {20, "tom"}}}, + {{1, 2}}, + {person{20, "tom"}, {22, "jerry"}}, + {person{15, "tom"}, {31, "jerry"}}, + std::make_pair("aa", person{20, "tom"})}; +} + struct nested_object { int id; std::string name;