diff --git a/tests/urchin/types_test.cc b/tests/urchin/types_test.cc index fa94ddce3e..de0c63f398 100644 --- a/tests/urchin/types_test.cc +++ b/tests/urchin/types_test.cc @@ -7,6 +7,7 @@ #include #include "types.hh" +#include "tuple.hh" BOOST_AUTO_TEST_CASE(test_int32_type_string_conversions) { BOOST_REQUIRE(int32_type->equal(int32_type->from_string("1234567890"), int32_type->decompose(1234567890))); @@ -46,3 +47,27 @@ BOOST_AUTO_TEST_CASE(test_int32_type_string_conversions) { BOOST_REQUIRE_EQUAL(int32_type->to_string(bytes()), ""); } + +BOOST_AUTO_TEST_CASE(test_tuple_is_prefix_of) { + tuple_type<> type({utf8_type, utf8_type, utf8_type}); + auto prefix_type = type.as_prefix(); + + auto val = type.serialize_value({{bytes("a")}, {bytes("b")}, {bytes("c")}}); + + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({}), val)); + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}}), val)); + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("b")}}), val)); + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("b")}, {bytes("c")}}), val)); + + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{}}), val)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes()}}), val)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("b")}, {bytes("c")}}), val)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("c")}, {bytes("b")}}), val)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("abc")}}), val)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("ab")}}), val)); + + auto val2 = type.serialize_value({{bytes("a")}, {bytes("b")}, {}}); + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("b")}}), val2)); + BOOST_REQUIRE(prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("b")}, {}}), val2)); + BOOST_REQUIRE(!prefix_type.is_prefix_of(prefix_type.serialize_value({{bytes("a")}, {bytes("b")}, {bytes()}}), val2)); +} diff --git a/tuple.hh b/tuple.hh index 5eecd238c1..33e4c7444f 100644 --- a/tuple.hh +++ b/tuple.hh @@ -18,6 +18,7 @@ private: const std::vector> types; const bool _byte_order_equal; public: + using prefix_type = tuple_type; using value_type = std::vector; tuple_type(std::vector> types) @@ -27,6 +28,11 @@ public: return t->is_byte_order_equal(); })) { } + + prefix_type as_prefix() { + return prefix_type(types); + } + /* * Format: * ... @@ -50,6 +56,9 @@ public: } } } + bytes serialize_value(const value_type& values) { + return ::serialize_value(*this, values); + } bytes decompose_value(const value_type& values) { return ::serialize_value(*this, values); }