From 4a544b3655574cd3dfef0763ccd2ba0ca5d7a712 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 15 Apr 2015 14:56:34 +0300 Subject: [PATCH] tests: add type test for tuples --- tests/urchin/types_test.cc | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/urchin/types_test.cc b/tests/urchin/types_test.cc index 4c76eab6a7..12b5ba5f93 100644 --- a/tests/urchin/types_test.cc +++ b/tests/urchin/types_test.cc @@ -75,3 +75,61 @@ BOOST_AUTO_TEST_CASE(test_tuple_type_compare) { type.serialize_value({bytes("a"), bytes("d"), bytes("c")}), type.serialize_value({bytes("c"), bytes("b"), bytes("c")})) < 0); } + +template +std::experimental::optional +extract(boost::any a) { + if (a.empty()) { + return std::experimental::nullopt; + } else { + return std::experimental::make_optional(boost::any_cast(a)); + } +} + +template +boost::any +unextract(std::experimental::optional v) { + if (v) { + return boost::any(*v); + } else { + return boost::any(); + } +} + +template +using opt = std::experimental::optional; + +BOOST_AUTO_TEST_CASE(test_the_real_tuple) { + auto t = tuple_type_impl::get_instance({int32_type, long_type, utf8_type}); + using native_type = tuple_type_impl::native_type; + using c_type = std::tuple, opt, opt>; + auto native_to_c = [] (native_type v) { + return std::make_tuple(extract(v[0]), extract(v[1]), extract(v[2])); + }; + auto c_to_native = [] (std::tuple, opt, opt> v) { + return native_type({unextract(std::get<0>(v)), unextract(std::get<1>(v)), unextract(std::get<2>(v))}); + }; + auto native_to_bytes = [t] (native_type v) { + return t->decompose(v); + }; + auto bytes_to_native = [t] (bytes v) { + return boost::any_cast(t->compose(v)); + }; + auto c_to_bytes = [=] (c_type v) { + return native_to_bytes(c_to_native(v)); + }; + auto bytes_to_c = [=] (bytes v) { + return native_to_c(bytes_to_native(v)); + }; + auto round_trip = [=] (c_type v) { + return bytes_to_c(c_to_bytes(v)); + }; + auto v1 = c_type(int32_t(1), int64_t(2), sstring("abc")); + BOOST_REQUIRE(v1 == round_trip(v1)); + auto v2 = c_type(int32_t(1), int64_t(2), std::experimental::nullopt); + BOOST_REQUIRE(v2 == round_trip(v2)); + auto b1 = c_to_bytes(v1); + auto b2 = c_to_bytes(v2); + BOOST_REQUIRE(t->compare(b1, b2) > 0); + BOOST_REQUIRE(t->compare(b2, b2) == 0); +}