Commit 58607cf0 authored by Jakob Otto's avatar Jakob Otto

Finish simple tests for serializer_impl

parent 324644b6
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#pragma once #pragma once
#include <caf/detail/network_order.hpp>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <iomanip> #include <iomanip>
...@@ -104,7 +105,7 @@ public: ...@@ -104,7 +105,7 @@ public:
error apply_raw(size_t num_bytes, void* data) override { error apply_raw(size_t num_bytes, void* data) override {
CAF_ASSERT(write_pos_ <= buf_.size()); CAF_ASSERT(write_pos_ <= buf_.size());
auto ptr = reinterpret_cast<char*>(data); auto ptr = reinterpret_cast<typename container_type::value_type*>(data);
auto buf_size = buf_.size(); auto buf_size = buf_.size();
if (write_pos_ == buf_size) { if (write_pos_ == buf_size) {
buf_.insert(buf_.end(), ptr, ptr + num_bytes); buf_.insert(buf_.end(), ptr, ptr + num_bytes);
...@@ -221,6 +222,12 @@ protected: ...@@ -221,6 +222,12 @@ protected:
} }
private: private:
template <class T>
error apply_int(serializer_impl<Container>& bs, T x) {
auto y = detail::to_network_order(x);
return bs.apply_raw(sizeof(T), &y);
}
container_type& buf_; container_type& buf_;
size_t write_pos_; size_t write_pos_;
}; };
......
...@@ -15,13 +15,15 @@ ...@@ -15,13 +15,15 @@
* http://opensource.org/licenses/BSD-3-Clause and * * http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. * * http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/ ******************************************************************************/
/*
#include "caf/config.hpp" #include "caf/config.hpp"
#define CAF_SUITE serialization #define CAF_SUITE serializer_impl
#include "caf/test/unit_test.hpp" #include "caf/test/unit_test.hpp"
#include <algorithm> #include <algorithm>
#include <caf/byte.hpp>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
...@@ -57,6 +59,7 @@ ...@@ -57,6 +59,7 @@
#include "caf/proxy_registry.hpp" #include "caf/proxy_registry.hpp"
#include "caf/ref_counted.hpp" #include "caf/ref_counted.hpp"
#include "caf/serializer.hpp" #include "caf/serializer.hpp"
#include "caf/serializer_impl.hpp"
#include "caf/stream_deserializer.hpp" #include "caf/stream_deserializer.hpp"
#include "caf/stream_serializer.hpp" #include "caf/stream_serializer.hpp"
#include "caf/streambuf.hpp" #include "caf/streambuf.hpp"
...@@ -75,6 +78,12 @@ using caf::detail::type_erased_value_impl; ...@@ -75,6 +78,12 @@ using caf::detail::type_erased_value_impl;
namespace { namespace {
enum class test_enum {
a,
b,
c,
};
struct test_data { struct test_data {
int32_t i32 = -345; int32_t i32 = -345;
int64_t i64 = -1234567890123456789ll; int64_t i64 = -1234567890123456789ll;
...@@ -84,136 +93,62 @@ struct test_data { ...@@ -84,136 +93,62 @@ struct test_data {
timestamp ts = timestamp{timestamp::duration{1478715821 * 1000000000ll}}; timestamp ts = timestamp{timestamp::duration{1478715821 * 1000000000ll}};
test_enum te = test_enum::b; test_enum te = test_enum::b;
string str = "Lorem ipsum dolor sit amet."; string str = "Lorem ipsum dolor sit amet.";
raw_struct rs;
test_array ta{
{0, 1, 2, 3},
{{0, 1, 2, 3}, {4, 5, 6, 7}},
};
}; };
template <class Inspector> template <class Inspector>
typename Inspector::result_type inspect(Inspector& f, test_data& x) { typename Inspector::result_type inspect(Inspector& f, test_data& x) {
return f(x.value, x.value2); return f(meta::type_name("test_data"), x.i32, x.i64, x.f32, x.f64, x.dur,
x.ts, x.te, x.str);
} }
template <class Serializer, class Deserializer>
struct fixture {
int32_t i32 = -345;
int64_t i64 = -1234567890123456789ll;
float f32 = 3.45f;
double f64 = 54.3;
duration dur = duration{time_unit::seconds, 123};
timestamp ts = timestamp{timestamp::duration{1478715821 * 1000000000ll}};
test_enum te = test_enum::b;
string str = "Lorem ipsum dolor sit amet.";
raw_struct rs;
test_array ta{
{0, 1, 2, 3},
{{0, 1, 2, 3}, {4, 5, 6, 7}},
};
int ra[3] = {1, 2, 3};
test_data testData{
};
config cfg;
actor_system system;
message msg;
message recursive;
template <class T, class... Ts>
vector<char> serialize(T& x, Ts&... xs) {
vector<char> buf;
binary_serializer sink{system, buf};
if (auto err = sink(x, xs...))
CAF_FAIL("serialization failed: "
<< system.render(err) << ", data: "
<< deep_to_string(std::forward_as_tuple(x, xs...)));
return buf;
}
template <class T, class... Ts>
void deserialize(const vector<char>& buf, T& x, Ts&... xs) {
binary_deserializer source{system, buf};
if (auto err = source(x, xs...))
CAF_FAIL("deserialization failed: " << system.render(err));
}
// serializes `x` and then deserializes and returns the serialized value
template <class T>
T roundtrip(T x) {
T result;
deserialize(serialize(x), result);
return result;
}
// converts `x` to a message, serialize it, then deserializes it, and
// finally returns unboxed value
template <class T>
T msg_roundtrip(const T& x) {
message result;
auto tmp = make_message(x);
deserialize(serialize(tmp), result);
if (!result.match_elements<T>())
CAF_FAIL("expected: " << x << ", got: " << result);
return result.get_as<T>(0);
}
fixture() : system(cfg) {
rs.str.assign(string(str.rbegin(), str.rend()));
msg = make_message(i32, i64, dur, ts, te, str, rs);
config_value::dictionary dict;
put(dict, "scheduler.policy", atom("none"));
put(dict, "scheduler.max-threads", 42);
put(dict, "nodes.preload",
make_config_value_list("sun", "venus", "mercury", "earth", "mars"));
recursive = make_message(config_value{std::move(dict)});
}
};
} // namespace } // namespace
#define SERIALIZATION_TEST(name) \ CAF_TEST("serialize to std::vector<char>") {
template <class Serializer, class Deserializer> \ using container_type = std::vector<char>;
struct name##_tpl : fixture<Serializer, Deserializer> { \ actor_system_config cfg;
using super = fixture<Serializer, Deserializer>; \ actor_system sys{cfg};
using super::i32; \ test_data data;
using super::i64; \ std::vector<char> binary_serializer_buffer;
using super::f32; \ container_type serializer_impl_buffer;
using super::f64; \ binary_serializer binarySerializer{sys, binary_serializer_buffer};
using super::dur; \ serializer_impl<container_type> serializerImpl{sys, serializer_impl_buffer};
using super::ts; \ binarySerializer(data);
using super::te; \ serializerImpl(data);
using super::str; \ CAF_CHECK_EQUAL(memcmp(binary_serializer_buffer.data(),
using super::rs; \ serializer_impl_buffer.data(),
using super::ta; \ binary_serializer_buffer.size()),
using super::ra; \ 0);
using super::system; \ }
using super::msg; \
using super::recursive; \ CAF_TEST("serialize to std::vector<byte>") {
using super::serialize; \ using container_type = std::vector<byte>;
using super::deserialize; \ actor_system_config cfg;
using super::roundtrip; \ actor_system sys{cfg};
using super::msg_roundtrip; \ test_data data;
void run_test_impl(); \ std::vector<char> binary_serializer_buffer;
}; \ container_type serializer_impl_buffer;
namespace { \ binary_serializer binarySerializer{sys, binary_serializer_buffer};
using name##_binary = name##_tpl<binary_serializer, binary_deserializer>; \ serializer_impl<container_type> serializerImpl{sys, serializer_impl_buffer};
using name##_stream = name##_tpl<stream_serializer<vectorbuf>, \ binarySerializer(data);
stream_deserializer<charbuf>>; \ serializerImpl(data);
::caf::test::detail::adder<::caf::test::test_impl<name##_binary>> \ CAF_CHECK_EQUAL(memcmp(binary_serializer_buffer.data(),
CAF_UNIQUE(a_binary){CAF_XSTR(CAF_SUITE), CAF_XSTR(name##_binary), false}; \ serializer_impl_buffer.data(),
::caf::test::detail::adder<::caf::test::test_impl<name##_stream>> \ binary_serializer_buffer.size()),
CAF_UNIQUE(a_stream){CAF_XSTR(CAF_SUITE), CAF_XSTR(name##_stream), false}; \ 0);
} \ }
template <class Serializer, class Deserializer> \
void name##_tpl<Serializer, Deserializer>::run_test_impl()
SERIALIZATION_TEST(i32_values) { CAF_TEST("serialize to std::vector<uint8_t>") {
auto buf = serialize(i32); using container_type = std::vector<uint8_t>;
int32_t x; actor_system_config cfg;
deserialize(buf, x); actor_system sys{cfg};
CAF_CHECK_EQUAL(i32, x); test_data data;
std::vector<char> binary_serializer_buffer;
container_type serializer_impl_buffer;
binary_serializer binarySerializer{sys, binary_serializer_buffer};
serializer_impl<container_type> serializerImpl{sys, serializer_impl_buffer};
binarySerializer(data);
serializerImpl(data);
CAF_CHECK_EQUAL(memcmp(binary_serializer_buffer.data(),
serializer_impl_buffer.data(),
binary_serializer_buffer.size()),
0);
} }
*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment