From 5f9c9278ffabd96082d46e025207732d8c4e6751 Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Wed, 11 Sep 2024 14:44:57 +0800 Subject: [PATCH] [struct_pack][fix] fix mod zero error when struct only contain compatible --- include/ylt/struct_pack/alignment.hpp | 2 +- src/struct_pack/tests/test_compatible.cpp | 15 ++++++++++++++- 2 files changed, 15 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..1923e663d 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,16 @@ 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 { + int i; + 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); +}