|
| 1 | +#include <boost/archive/text_iarchive.hpp> |
| 2 | +#include <boost/archive/text_oarchive.hpp> |
| 3 | +#include <rfl/NoFieldNames.hpp> |
| 4 | +#include <rfl/boost_serialization.hpp> |
| 5 | +#include <rfl/parsing/ParserArray.hpp> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +#include "../check_numeric_array_io.hpp" |
| 9 | + |
| 10 | +namespace rfl::parsing { |
| 11 | +template <class T, std::size_t N, class... Ps> |
| 12 | +struct Parser<boost_serialization::Reader<::boost::archive::binary_iarchive>, |
| 13 | + boost_serialization::Writer<::boost::archive::binary_oarchive>, |
| 14 | + std::array<T, N>, |
| 15 | + Processors<test_numeric_array_io::GenericArrays, Ps...>> |
| 16 | + : test_numeric_array_io::GenericArrayParser< |
| 17 | + boost_serialization::Reader<::boost::archive::binary_iarchive>, |
| 18 | + boost_serialization::Writer<::boost::archive::binary_oarchive>, T, N, |
| 19 | + Processors<test_numeric_array_io::GenericArrays, Ps...>> {}; |
| 20 | +} // namespace rfl::parsing |
| 21 | + |
| 22 | +namespace test_numeric_array_io { |
| 23 | +namespace { |
| 24 | +template <class... Ps> |
| 25 | +constexpr auto write = [](const auto& input) { |
| 26 | + return rfl::boost_serialization::write<Ps...>(input); |
| 27 | +}; |
| 28 | + |
| 29 | +template <class... Ps> |
| 30 | +constexpr auto read = []<class T>(const auto& bytes) { |
| 31 | + return rfl::boost_serialization::read<T, Ps...>(bytes); |
| 32 | +}; |
| 33 | + |
| 34 | +template <class... Ps> |
| 35 | +void check(const auto& input) { |
| 36 | + check_compatible(input, write<Ps...>, read<Ps...>, |
| 37 | + write<GenericArrays, Ps...>, read<GenericArrays, Ps...>); |
| 38 | +} |
| 39 | +} // namespace |
| 40 | + |
| 41 | +TEST(boost_serialization, numeric_array_wire_compatibility) { |
| 42 | + // bool deliberately retains the production generic parser, whose N == 1 |
| 43 | + // raw buffer triggers GCC 13's -Wmaybe-uninitialized false positive. Keep |
| 44 | + // bool coverage at N == 0, 4, and 65; all optimized types also test N == 1. |
| 45 | + check_types<false>(write<>, read<>, write<GenericArrays>, |
| 46 | + read<GenericArrays>); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(boost_serialization, numeric_array_parents) { |
| 50 | + check_parents([](const auto& input) { check(input); }); |
| 51 | + check_parents([](const auto& input) { check<rfl::NoFieldNames>(input); }); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(boost_serialization, numeric_array_non_arithmetic_fallback) { |
| 55 | + check_non_arithmetic([](const auto& input) { check(input); }); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(boost_serialization, numeric_array_truncated_input) { |
| 59 | + check_truncated(write<>, read<>); |
| 60 | + check_truncated(write<>, read<GenericArrays>); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(boost_serialization, numeric_array_text_archive_fallback) { |
| 64 | + using I = boost::archive::text_iarchive; |
| 65 | + using O = boost::archive::text_oarchive; |
| 66 | + using R = rfl::boost_serialization::Reader<I>; |
| 67 | + using W = rfl::boost_serialization::Writer<O>; |
| 68 | + const std::array<double, 3> input{1.25, -2.5, 0.0}; |
| 69 | + std::ostringstream output; |
| 70 | + { |
| 71 | + O archive(output); |
| 72 | + rfl::boost_serialization::write<I>(archive, input); |
| 73 | + } |
| 74 | + std::ostringstream generic; |
| 75 | + { |
| 76 | + O archive(generic); |
| 77 | + const W writer(&archive); |
| 78 | + rfl::parsing::ParserArray<R, W, double, 3, rfl::Processors<>>::write( |
| 79 | + writer, input, rfl::parsing::Parent<W>::Root{}); |
| 80 | + } |
| 81 | + EXPECT_EQ(output.str(), generic.str()); |
| 82 | + std::istringstream stream(output.str()); |
| 83 | + I archive(stream); |
| 84 | + const auto decoded = |
| 85 | + rfl::boost_serialization::read_from_archive<std::array<double, 3>, I, O>( |
| 86 | + archive); |
| 87 | + ASSERT_TRUE(decoded) << decoded.error().what(); |
| 88 | + EXPECT_EQ(*decoded, input); |
| 89 | +} |
| 90 | + |
| 91 | +TEST(boost_serialization, numeric_array_binary_archive_options) { |
| 92 | + using I = boost::archive::binary_iarchive; |
| 93 | + using O = boost::archive::binary_oarchive; |
| 94 | + const std::array<long double, 3> input{1.25L, -2.5L, 3.75L}; |
| 95 | + for (const auto flags : {0u, unsigned(boost::archive::no_header | |
| 96 | + boost::archive::no_tracking)}) { |
| 97 | + const auto serialize = [&]<class... Ps>(rfl::Processors<Ps...>) { |
| 98 | + std::ostringstream stream; |
| 99 | + { |
| 100 | + O archive(stream, flags); |
| 101 | + rfl::boost_serialization::write<I, O, Ps...>(archive, input); |
| 102 | + } |
| 103 | + return stream.str(); |
| 104 | + }; |
| 105 | + const auto bytes = serialize(rfl::Processors<>{}); |
| 106 | + EXPECT_EQ(bytes, serialize(rfl::Processors<GenericArrays>{})); |
| 107 | + std::istringstream stream(bytes); |
| 108 | + I archive(stream, flags); |
| 109 | + const auto decoded = |
| 110 | + rfl::boost_serialization::read_from_archive<std::array<long double, 3>, |
| 111 | + I, O>(archive); |
| 112 | + ASSERT_TRUE(decoded) << decoded.error().what(); |
| 113 | + EXPECT_EQ(*decoded, input); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace test_numeric_array_io |
0 commit comments