diff --git a/test/serialization.cpp b/test/serialization.cpp index 354a3088..94aff5c3 100644 --- a/test/serialization.cpp +++ b/test/serialization.cpp @@ -5,113 +5,16 @@ * Author: ruben */ -#include "mysql/impl/basic_serialization.hpp" -#include "mysql/impl/messages.hpp" -#include "mysql/impl/constants.hpp" -#include -#include -#include +#include "serialization_test_common.hpp" using namespace testing; using namespace std; using namespace mysql; using namespace mysql::detail; -// Operator == for structs -template -bool equals_struct(const T& lhs, const T& rhs) -{ - if constexpr (index == std::tuple_size>::value) - { - return true; - } - else - { - constexpr auto pmem = std::get(T::fields); - return (rhs.*pmem == lhs.*pmem) && equals_struct(lhs, rhs); - } -} - -template -enable_if_t::value, bool> -operator==(const T& lhs, const T& rhs) -{ - return equals_struct<0>(lhs, rhs); -} - namespace { -class TypeErasedValue -{ -public: - virtual ~TypeErasedValue() {} - virtual void serialize(SerializationContext& ctx) const = 0; - virtual std::size_t get_size(const SerializationContext& ctx) const = 0; - virtual Error deserialize(DeserializationContext& ctx) = 0; - virtual string get_type_name() const = 0; - virtual shared_ptr default_construct() const = 0; - virtual bool equals(const TypeErasedValue& rhs) const = 0; - - bool operator==(const TypeErasedValue& rhs) const { return equals(rhs); } -}; - -template -class TypeErasedValueImpl : public TypeErasedValue -{ - T value_; -public: - TypeErasedValueImpl(const T& v): value_(v) {}; - void serialize(SerializationContext& ctx) const override { ::mysql::detail::serialize(value_, ctx); } - std::size_t get_size(const SerializationContext& ctx) const override { return ::mysql::detail::get_size(value_, ctx); } - Error deserialize(DeserializationContext& ctx) override { return ::mysql::detail::deserialize(value_, ctx); } - string get_type_name() const override - { - return boost::typeindex::type_id().pretty_name(); - } - shared_ptr default_construct() const override - { - T res; // intentionally not value-initializing it - return make_shared>(res); - } - bool equals(const TypeErasedValue& rhs) const - { - auto typed_value = dynamic_cast*>(&rhs); - return typed_value && (typed_value->value_ == value_); - } -}; - -struct SerializeParams -{ - shared_ptr value; - vector expected_buffer; - string test_name; - bool is_space_sensitive; - - template - SerializeParams(const T& v, vector&& buff, string&& name="default", bool is_space_sensitive=true): - value(std::make_shared>(v)), - expected_buffer(move(buff)), - test_name(move(name)), - is_space_sensitive(is_space_sensitive) - { - } -}; - -ostream& operator<<(ostream& os, const SerializeParams& params) -{ - return os << params.value->get_type_name() << " - " << params.test_name; -} - -vector concat(vector&& lhs, const vector& rhs) -{ - size_t lhs_size = lhs.size(); - vector res (move(lhs)); - res.resize(lhs_size + rhs.size()); - memcpy(res.data() + lhs_size, rhs.data(), rhs.size()); - return res; -} - struct SerializeTest : public testing::TestWithParam { static string_view make_buffer_view(const uint8_t* buff, size_t sz) diff --git a/test/serialization_test_common.hpp b/test/serialization_test_common.hpp new file mode 100644 index 00000000..fcbcb40c --- /dev/null +++ b/test/serialization_test_common.hpp @@ -0,0 +1,114 @@ +#ifndef TEST_SERIALIZATION_TEST_COMMON_HPP_ +#define TEST_SERIALIZATION_TEST_COMMON_HPP_ + +#include "mysql/impl/basic_serialization.hpp" +#include "mysql/impl/messages.hpp" +#include "mysql/impl/constants.hpp" +#include +#include +#include + +namespace mysql +{ +namespace detail +{ + +// Operator == for structs +template +bool equals_struct(const T& lhs, const T& rhs) +{ + if constexpr (index == std::tuple_size>::value) + { + return true; + } + else + { + constexpr auto pmem = std::get(T::fields); + return (rhs.*pmem == lhs.*pmem) && equals_struct(lhs, rhs); + } +} + +template +std::enable_if_t::value, bool> +operator==(const T& lhs, const T& rhs) +{ + return equals_struct<0>(lhs, rhs); +} + + +class TypeErasedValue +{ +public: + virtual ~TypeErasedValue() {} + virtual void serialize(SerializationContext& ctx) const = 0; + virtual std::size_t get_size(const SerializationContext& ctx) const = 0; + virtual Error deserialize(DeserializationContext& ctx) = 0; + virtual std::string get_type_name() const = 0; + virtual std::shared_ptr default_construct() const = 0; + virtual bool equals(const TypeErasedValue& rhs) const = 0; + + bool operator==(const TypeErasedValue& rhs) const { return equals(rhs); } +}; + +template +class TypeErasedValueImpl : public TypeErasedValue +{ + T value_; +public: + TypeErasedValueImpl(const T& v): value_(v) {}; + void serialize(SerializationContext& ctx) const override { ::mysql::detail::serialize(value_, ctx); } + std::size_t get_size(const SerializationContext& ctx) const override { return ::mysql::detail::get_size(value_, ctx); } + Error deserialize(DeserializationContext& ctx) override { return ::mysql::detail::deserialize(value_, ctx); } + std::string get_type_name() const override + { + return boost::typeindex::type_id().pretty_name(); + } + std::shared_ptr default_construct() const override + { + T res; // intentionally not value-initializing it + return std::make_shared>(res); + } + bool equals(const TypeErasedValue& rhs) const + { + auto typed_value = dynamic_cast*>(&rhs); + return typed_value && (typed_value->value_ == value_); + } +}; + +struct SerializeParams +{ + std::shared_ptr value; + std::vector expected_buffer; + std::string test_name; + bool is_space_sensitive; + + template + SerializeParams(const T& v, std::vector&& buff, + std::string&& name="default", bool is_space_sensitive=true): + value(std::make_shared>(v)), + expected_buffer(move(buff)), + test_name(move(name)), + is_space_sensitive(is_space_sensitive) + { + } +}; + +std::ostream& operator<<(std::ostream& os, const SerializeParams& params) +{ + return os << params.value->get_type_name() << " - " << params.test_name; +} + +std::vector concat(std::vector&& lhs, const std::vector& rhs) +{ + size_t lhs_size = lhs.size(); + std::vector res (move(lhs)); + res.resize(lhs_size + rhs.size()); + std::memcpy(res.data() + lhs_size, rhs.data(), rhs.size()); + return res; +} + +} +} + + +#endif /* TEST_SERIALIZATION_TEST_COMMON_HPP_ */