2
0
mirror of https://github.com/boostorg/mysql.git synced 2026-01-29 19:52:11 +00:00

Split serialization test utils into a separate header

This commit is contained in:
ruben
2019-10-05 11:56:19 +01:00
parent b546bd01aa
commit d1fec94d99
2 changed files with 115 additions and 98 deletions

View File

@@ -5,113 +5,16 @@
* Author: ruben
*/
#include "mysql/impl/basic_serialization.hpp"
#include "mysql/impl/messages.hpp"
#include "mysql/impl/constants.hpp"
#include <gtest/gtest.h>
#include <string>
#include <boost/type_index.hpp>
#include "serialization_test_common.hpp"
using namespace testing;
using namespace std;
using namespace mysql;
using namespace mysql::detail;
// Operator == for structs
template <std::size_t index, typename T>
bool equals_struct(const T& lhs, const T& rhs)
{
if constexpr (index == std::tuple_size<std::decay_t<decltype(T::fields)>>::value)
{
return true;
}
else
{
constexpr auto pmem = std::get<index>(T::fields);
return (rhs.*pmem == lhs.*pmem) && equals_struct<index+1>(lhs, rhs);
}
}
template <typename T>
enable_if_t<is_struct_with_fields<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<TypeErasedValue> default_construct() const = 0;
virtual bool equals(const TypeErasedValue& rhs) const = 0;
bool operator==(const TypeErasedValue& rhs) const { return equals(rhs); }
};
template <typename T>
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<T>().pretty_name();
}
shared_ptr<TypeErasedValue> default_construct() const override
{
T res; // intentionally not value-initializing it
return make_shared<TypeErasedValueImpl<T>>(res);
}
bool equals(const TypeErasedValue& rhs) const
{
auto typed_value = dynamic_cast<const TypeErasedValueImpl<T>*>(&rhs);
return typed_value && (typed_value->value_ == value_);
}
};
struct SerializeParams
{
shared_ptr<TypeErasedValue> value;
vector<uint8_t> expected_buffer;
string test_name;
bool is_space_sensitive;
template <typename T>
SerializeParams(const T& v, vector<uint8_t>&& buff, string&& name="default", bool is_space_sensitive=true):
value(std::make_shared<TypeErasedValueImpl<T>>(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<uint8_t> concat(vector<uint8_t>&& lhs, const vector<uint8_t>& rhs)
{
size_t lhs_size = lhs.size();
vector<uint8_t> 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<SerializeParams>
{
static string_view make_buffer_view(const uint8_t* buff, size_t sz)