From be8da7419dbc620caa5a3555a9385422a9bdeed5 Mon Sep 17 00:00:00 2001 From: saipubw Date: Wed, 11 Sep 2024 15:55:43 +0800 Subject: [PATCH] =?UTF-8?q?[struct=5Fpack][fix]=20fix=20mod=20zero=20error?= =?UTF-8?q?=20when=20struct=20only=20contain=20compat=E2=80=A6=20(#769)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [struct_pack][fix] fix mod zero error when struct only contain compatible * fix test --- include/ylt/struct_pack/alignment.hpp | 2 +- src/struct_pack/tests/test_compatible.cpp | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/ylt/struct_pack/alignment.hpp b/include/ylt/struct_pack/alignment.hpp index df0c801b3..432cdfcb9 100644 --- a/include/ylt/struct_pack/alignment.hpp +++ b/include/ylt/struct_pack/alignment.hpp @@ -160,7 +160,7 @@ constexpr auto calculate_padding_size() { std::array + 1> padding_size{}; std::size_t offset = 0; for_each(offset, padding_size); - if (offset % align::alignment_v) { + if (align::alignment_v < T >> 0 && offset % align::alignment_v) { padding_size[struct_pack::members_count] = align::alignment_v - offset % align::alignment_v; } diff --git a/src/struct_pack/tests/test_compatible.cpp b/src/struct_pack/tests/test_compatible.cpp index 59ae1abe9..d9758b671 100644 --- a/src/struct_pack/tests/test_compatible.cpp +++ b/src/struct_pack/tests/test_compatible.cpp @@ -3,6 +3,7 @@ #include "doctest.h" #include "test_struct.hpp" +#include "ylt/struct_pack/compatible.hpp" #include "ylt/struct_pack/endian_wrapper.hpp" using namespace struct_pack; @@ -1307,4 +1308,15 @@ TEST_CASE("test nested trival_serialzable_obj_with_compatible") { CHECK(result.has_value()); CHECK(test_equal(result.value(), a_v1)); } -} \ No newline at end of file +} + +struct only_compatible { + struct_pack::compatible hi; +}; +TEST_CASE("test only_compatible") { + only_compatible o{0}; + auto buffer = struct_pack::serialize(o); + auto result = struct_pack::deserialize(buffer); + CHECK(result.has_value()); + CHECK(result->hi == o.hi); +}