58 lines
2.3 KiB
C++
58 lines
2.3 KiB
C++
//-----------------------------------------------------------------------------
|
|
// ___ __ _ _
|
|
// / _ \__ _ _ __ ___ ___ / /(_)_ __ | | __
|
|
// / /_)/ _` | '__/ __|/ _ \/ / | | '_ \| |/ /
|
|
// / ___/ (_| | | \__ \ __/ /__| | | | | <
|
|
// \/ \__,_|_| |___/\___\____/_|_| |_|_|\_\ .
|
|
//
|
|
//-----------------------------------------------------------------------------
|
|
// Author: Kurt Sassenrath
|
|
// Module: msgpack
|
|
//
|
|
// Unpacker tests, common code.
|
|
//
|
|
// Copyright (c) 2023 Kurt Sassenrath.
|
|
//
|
|
// License TBD.
|
|
//-----------------------------------------------------------------------------
|
|
#ifndef tests_msgpack_unpacker_25b118dd14e2b1b4
|
|
#define tests_msgpack_unpacker_25b118dd14e2b1b4
|
|
|
|
#include "parselink/msgpack/core/unpacker.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
// Helper template to check that a type is either equal to itself, or verify
|
|
// that it won't unpack correctly.
|
|
template <typename T, typename U>
|
|
constexpr bool unpack_type_check(msgpack::unpacker unpacker) noexcept {
|
|
auto wrong_type = tl::make_unexpected(msgpack::error::wrong_type);
|
|
auto compatible_type = std::same_as<T, U>
|
|
|| (std::is_unsigned_v<T> && std::is_unsigned_v<U>)
|
|
|| (std::is_signed_v<T> && std::is_signed_v<U>);
|
|
auto result = compatible_type || unpacker.unpack<U>() == wrong_type;
|
|
if (!result) {
|
|
fmt::print("Unpack_type_check failed: {}\n",
|
|
std::source_location::current().function_name());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// Validate the wrong types do not unpack correctly, or fail.
|
|
template <typename Expected, typename... OtherTypes>
|
|
constexpr bool try_unpacking_types(msgpack::unpacker unpacker) noexcept {
|
|
return ((unpack_type_check<Expected, OtherTypes>(unpacker)) && ...);
|
|
}
|
|
|
|
#define SUPPORTED_TYPES \
|
|
msgpack::nil, msgpack::invalid, bool, std::uint8_t, std::uint16_t, \
|
|
std::uint32_t, std::uint64_t, std::int8_t, std::int16_t, \
|
|
std::int32_t, std::int64_t, std::string_view
|
|
|
|
template <typename Expected>
|
|
constexpr bool type_check_unpacker(msgpack::unpacker unpacker) noexcept {
|
|
return try_unpacking_types<Expected, SUPPORTED_TYPES>(unpacker);
|
|
}
|
|
|
|
#endif // tests_msgpack_unpacker_25b118dd14e2b1b4
|