//----------------------------------------------------------------------------- // ___ __ _ _ // / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __ // / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ / // / ___/ (_| | | \__ \ __/ /__| | | | | < // \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ . // //----------------------------------------------------------------------------- // Author: Kurt Sassenrath // Module: msgpack // // Unpacker tests for signed integer values // // Copyright (c) 2023 Kurt Sassenrath. // // License TBD. //----------------------------------------------------------------------------- #include "test_unpacker.h" #include using namespace boost::ut; namespace { template struct get_marker; template <> struct get_marker { static constexpr std::byte marker = msgpack::format::int8::marker; }; template <> struct get_marker { static constexpr std::byte marker = msgpack::format::int16::marker; }; template <> struct get_marker { static constexpr std::byte marker = msgpack::format::int32::marker; }; template <> struct get_marker { static constexpr std::byte marker = msgpack::format::int64::marker; }; // Utilize rapidcheck to generate random packed payloads, and unpacking the // correct value template auto check_unpack_integer() noexcept { auto result = rc::check([](PackType value) { std::vector payload = {get_marker::marker}; auto bytes = host_to_be( std::bit_cast>(value)); std::copy(bytes.begin(), bytes.end(), std::back_inserter(payload)); msgpack::unpacker unpacker(payload); auto unpacked_value = unpacker.unpack(); if (std::numeric_limits::max() < value || std::numeric_limits::min() > value) { return unpacked_value == tl::make_unexpected(msgpack::error::will_truncate); } else { return unpacked_value.has_value() && unpacked_value == value; } }); if (!result) { fmt::print("Failed on {}\n", std::source_location::current().function_name()); } return result; } } // anonymous namespace suite unpack_signed_types = [] { "unpacker::unpack"_test = [] { expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); }; "unpacker::unpack"_test = [] { expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); }; "unpacker::unpack"_test = [] { expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); }; "unpacker::unpack"_test = [] { expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); expect(check_unpack_integer()); }; };