From 4792ecd8f402bfadf4f0515ac11c5e90a2771b6f Mon Sep 17 00:00:00 2001 From: ykiko Date: Mon, 24 Mar 2025 08:48:39 +0800 Subject: [PATCH] Add tests for binary (#111) --- unittests/Support/Binary.cpp | 209 +++++++++++++++++++++++++---------- 1 file changed, 153 insertions(+), 56 deletions(-) diff --git a/unittests/Support/Binary.cpp b/unittests/Support/Binary.cpp index a50bd7ad..4ee93d89 100644 --- a/unittests/Support/Binary.cpp +++ b/unittests/Support/Binary.cpp @@ -10,47 +10,104 @@ constexpr inline bool check_sections = std::is_same_v, std::tuple...>>; TEST(Binary, String) { - std::string s1 = "123"; static_assert(check_sections); - auto [buffer, proxy] = binary::serialize(s1); - std::string s2 = binary::deserialize(proxy); - EXPECT_EQ(s1, s2); + + { + std::string s1 = ""; + auto [buffer, proxy] = binary::serialize(s1); + EXPECT_EQ(s1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(s1[i], proxy[i].value()); + } + + EXPECT_EQ(s1, proxy.as_string()); + std::string s2 = binary::deserialize(proxy); + EXPECT_EQ(s1, s2); + } + + { + std::string s1 = "123"; + auto [buffer, proxy] = binary::serialize(s1); + EXPECT_EQ(s1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(s1[i], proxy[i].value()); + } + + EXPECT_EQ(s1, proxy.as_string()); + std::string s2 = binary::deserialize(proxy); + EXPECT_EQ(s1, s2); + } + + { + std::string s1 = "11111111111111111111111111111111111111111111111111111111111111111"; + auto [buffer, proxy] = binary::serialize(s1); + EXPECT_EQ(s1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(s1[i], proxy[i].value()); + } + + EXPECT_EQ(s1, proxy.as_string()); + std::string s2 = binary::deserialize(proxy); + EXPECT_EQ(s1, s2); + } } -TEST(Binary, Binarify) { - static_assert(binary::is_directly_binarizable_v); - static_assert(std::same_as, int>); +TEST(Binary, Array) { + static_assert(check_sections, int>); - struct Point { - uint32_t x; - uint32_t y; - }; + { + std::vector vec1 = {}; + auto [buffer, proxy] = binary::serialize(vec1); + EXPECT_EQ(vec1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(vec1[i], proxy[i].value()); + } - static_assert(binary::is_directly_binarizable_v); - static_assert(std::same_as, Point>); + EXPECT_EQ(vec1, proxy.as_array().vec()); + std::vector vec2 = binary::deserialize(proxy); + EXPECT_EQ(vec1, vec2); + } - struct Person { - std::string x; - uint32_t age; - }; + { + std::vector vec1 = {1, 2, 3}; + auto [buffer, proxy] = binary::serialize(vec1); + EXPECT_EQ(vec1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(vec1[i], proxy[i].value()); + } - static_assert(!binary::is_directly_binarizable_v); - static_assert(std::same_as, std::tuple>); + EXPECT_EQ(vec1, proxy.as_array().vec()); + std::vector vec2 = binary::deserialize(proxy); + EXPECT_EQ(vec1, vec2); + } - struct Foo { - std::vector scores; - }; + { + std::vector vec1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + auto [buffer, proxy] = binary::serialize(vec1); + EXPECT_EQ(vec1.size(), proxy.size()); + for(auto i = 0; i < proxy.size(); i++) { + EXPECT_EQ(vec1[i], proxy[i].value()); + } - static_assert(!binary::is_directly_binarizable_v); - static_assert(std::same_as, std::tuple>>); + EXPECT_EQ(vec1, proxy.as_array().vec()); + std::vector vec2 = binary::deserialize(proxy); + EXPECT_EQ(vec1, vec2); + } +} - struct Bar { - Foo foo; - }; +TEST(Binary, StringArray) { + static_assert(check_sections, std::string, char>); - static_assert(!binary::is_directly_binarizable_v); - static_assert( - std::same_as, std::tuple>>>); + std::vector sv = {"1", "22", "333", "444"}; + auto [buffer, proxy] = binary::serialize(sv); + EXPECT_EQ(sv.size(), proxy.size()); + + for(auto i = 0; i < sv.size(); i++) { + EXPECT_EQ(sv[i], proxy[i].as_string()); + } + + std::vector sv2 = binary::deserialize(proxy); + EXPECT_EQ(sv, sv2); } struct Point { @@ -58,29 +115,66 @@ struct Point { uint32_t y; }; -TEST(Binary, Simple) { - using namespace clice::binary; - auto [buffer, proxy] = binary::serialize(Point{1, 2}); +TEST(Binary, Struct) { + { + static_assert(binary::is_directly_binarizable_v); + static_assert(std::same_as, Point>); + static_assert(check_sections); - EXPECT_EQ(proxy.value().x, 1); - EXPECT_EQ(proxy.value().y, 2); -} + Point p = {1, 2}; + auto [buffer, proxy] = binary::serialize(p); + EXPECT_EQ(proxy->x, 1); + EXPECT_EQ(proxy->y, 2); + EXPECT_EQ(p, proxy.value()); + auto p2 = binary::deserialize(proxy); + EXPECT_EQ(p, p2); + } -struct Points { - std::vector points; -}; - -TEST(Binary, Nested) { - Points points{ - {Point{1, 2}, Point{3, 4}} + struct Foo { + uint32_t age; + std::string name; + std::vector scores; }; - auto [buffer, proxy] = binary::serialize(points); + { + static_assert(!binary::is_directly_binarizable_v); + static_assert(check_sections); - auto points2 = proxy.get<"points">(); + Foo foo = { + 0, + "123", + {1, 2, 3}, + }; - EXPECT_EQ(points2[0].value(), Point{1, 2}); - EXPECT_EQ(points2[1].value(), Point{3, 4}); + auto [buffer, proxy] = binary::serialize(foo); + EXPECT_EQ(proxy.get<"age">().value(), 0); + EXPECT_EQ(proxy.get<"name">().as_string(), "123"); + EXPECT_EQ(proxy.get<"scores">().as_array().vec(), std::vector{1, 2, 3}); + auto foo2 = binary::deserialize(proxy); + EXPECT_EQ(foo, foo2); + }; + + struct Points { + std::vector points; + }; + + { + static_assert(!binary::is_directly_binarizable_v); + static_assert(check_sections); + + Points points{ + { + Point{1, 2}, + Point{3, 4}, + }, + }; + auto [buffer, proxy] = binary::serialize(points); + auto points2 = proxy.get<"points">(); + EXPECT_EQ(points2[0].value(), Point{1, 2}); + EXPECT_EQ(points2[1].value(), Point{3, 4}); + auto points3 = binary::deserialize(proxy); + EXPECT_EQ(points, points3); + } } struct Node { @@ -91,24 +185,27 @@ struct Node { TEST(Binary, Recursively) { Node node = { 1, - { - {3}, + {{3}, {4}, { - 5, - { - {3}, - {4}, - {5}, - }, - }, }, + 5, + { + {3}, + {4}, + {5}, + }, + }}, }; + static_assert(!binary::is_directly_binarizable_v); + static_assert(check_sections); + auto [buffer, proxy] = binary::serialize(node); auto node2 = binary::deserialize(proxy); EXPECT_EQ(node, node2); } } // namespace + } // namespace clice::testing