tests: Add test for tuple_type::is_prefix_of()

This commit is contained in:
Tomasz Grabiec
2015-02-07 22:39:41 +01:00
parent 0a5bf555ea
commit 138ed6faac
2 changed files with 34 additions and 0 deletions

View File

@@ -7,6 +7,7 @@
#include <boost/test/included/unit_test.hpp>
#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));
}

View File

@@ -18,6 +18,7 @@ private:
const std::vector<shared_ptr<abstract_type>> types;
const bool _byte_order_equal;
public:
using prefix_type = tuple_type<true>;
using value_type = std::vector<bytes_opt>;
tuple_type(std::vector<shared_ptr<abstract_type>> types)
@@ -27,6 +28,11 @@ public:
return t->is_byte_order_equal();
}))
{ }
prefix_type as_prefix() {
return prefix_type(types);
}
/*
* Format:
* <len(value1)><value1><len(value2)><value2>...
@@ -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);
}