text
stringlengths
0
2.2M
return buf;
}
template <class... Ts>
void deserialize(const byte_buffer& buf, Ts&... xs) {
binary_deserializer source{sys, buf};
if (!(source.apply(xs) && ...))
CAF_FAIL("deserialization failed: " << source.get_error());
}
template <class... Ts>
std::string serialize_json(const Ts&... xs) {
jwriter.reset();
if (!(jwriter.apply(xs) && ...))
CAF_FAIL("JSON serialization failed: "
<< jwriter.get_error()
<< ", data: " << deep_to_string(std::forward_as_tuple(xs...)));
auto str = jwriter.str();
return std::string{str.begin(), str.end()};
}
template <class... Ts>
void deserialize_json(const std::string& str, Ts&... xs) {
if (!jreader.load(str))
CAF_FAIL("JSON loading failed: " << jreader.get_error()
<< "\n input: " << str);
if (!(jreader.apply(xs) && ...))
CAF_FAIL("JSON deserialization failed: " << jreader.get_error()
<< "\n input: " << str);
}
// serializes `x` and then deserializes and returns the serialized value
template <class T>
T roundtrip(T x, bool enable_json = true) {
auto r1 = T{};
deserialize(serialize(x), r1);
if (enable_json) {
auto r2 = T{};
deserialize_json(serialize_json(x), r2);
if (!CHECK_EQ(r1, r2))
MESSAGE("generated JSON: " << serialize_json(x));
}
return r1;
}
// 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);
auto buf = serialize(tmp);
MESSAGE("serialized " << to_string(tmp) << " into " << buf.size()
<< " bytes");
deserialize(buf, result);
if (!result.match_elements<T>())
CAF_FAIL("expected: " << x << ", got: " << result);
return result.get_as<T>(0);
}
fixture() : jwriter(sys), jreader(sys) {
rs.str.assign(std::string(str.rbegin(), str.rend()));
msg = make_message(i32, i64, ts, te, str, rs);
config_value::dictionary dict;
put(dict, "scheduler.policy", "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)});
}
};
struct is_message {
explicit is_message(message& msgref) : msg(msgref) {
// nop
}
message& msg;
template <class T, class... Ts>
bool equal(T&& v, Ts&&... vs) {
bool ok = false;
// work around for gcc 4.8.4 bug
auto tup = std::tie(v, vs...);
message_handler impl{
[&](T const& u, Ts const&... us) { ok = tup == std::tie(u, us...); }};
impl(msg);
return ok;
}
};
} // namespace
#define CHECK_RT(val) \
do { \
MESSAGE(#val); \
CHECK_EQ(val, roundtrip(val)); \
} while (false)
#define CHECK_PRED_RT(pred, value) \