Skip to content

Commit

Permalink
[struct_pack] add test for cross_platform (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Oct 10, 2023
1 parent 62dd9f2 commit 44ea4e4
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/struct_pack/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
)
11 changes: 11 additions & 0 deletions src/struct_pack/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions src/struct_pack/tests/binary_data/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["test_cross_platform.dat","test_cross_platform_without_debug_info.dat"])
Binary file not shown.
Binary file not shown.
66 changes: 66 additions & 0 deletions src/struct_pack/tests/test_cross_platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <cstdint>
#include <fstream>
#include <ylt/struct_pack.hpp>

#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<complicated_object>(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<complicated_object>(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<char>{ifs},
std::istreambuf_iterator<char>{});
auto object = create_complicated_object();
auto buffer =
struct_pack::serialize<std::string,
struct_pack::type_info_config::enable>(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<char>{ifs},
std::istreambuf_iterator<char>{});
auto object = create_complicated_object();
auto buffer =
struct_pack::serialize<std::string,
struct_pack::type_info_config::disable>(object);
CHECK(buffer == content);
}
16 changes: 1 addition & 15 deletions src/struct_pack/tests/test_data_struct2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
20 changes: 20 additions & 0 deletions src/struct_pack/tests/test_struct.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <array>
#include <bitset>
#include <deque>
#include <list>
#include <map>
Expand All @@ -7,6 +8,7 @@
#include <span>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 44ea4e4

Please sign in to comment.