From 26a42fe54ae0a7e6f348c205cfc7d7b4bf3c012b Mon Sep 17 00:00:00 2001 From: Lee Clagett Date: Wed, 1 Aug 2018 23:08:59 -0400 Subject: Added features to epee::span : - Support for classes - Added `remove_prefix` function - Added `to_mut_span` and `as_mut_byte_span` --- tests/unit_tests/epee_utils.cpp | 57 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index 3474000d8..c2b0b7647 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -166,12 +166,17 @@ TEST(Span, Traits) TEST(Span, MutableConstruction) { struct no_conversion{}; + struct inherited : no_conversion {}; EXPECT_TRUE(std::is_constructible>()); EXPECT_TRUE((std::is_constructible, char*, std::size_t>())); EXPECT_FALSE((std::is_constructible, const char*, std::size_t>())); EXPECT_FALSE((std::is_constructible, unsigned char*, std::size_t>())); + EXPECT_TRUE(std::is_constructible>()); + EXPECT_TRUE((std::is_constructible, no_conversion*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, inherited*, std::size_t>())); + EXPECT_TRUE((can_construct, std::nullptr_t>())); EXPECT_TRUE((can_construct, char(&)[1]>())); @@ -193,12 +198,19 @@ TEST(Span, MutableConstruction) TEST(Span, ImmutableConstruction) { struct no_conversion{}; + struct inherited : no_conversion {}; EXPECT_TRUE(std::is_constructible>()); EXPECT_TRUE((std::is_constructible, char*, std::size_t>())); EXPECT_TRUE((std::is_constructible, const char*, std::size_t>())); EXPECT_FALSE((std::is_constructible, unsigned char*, std::size_t>())); + EXPECT_TRUE(std::is_constructible>()); + EXPECT_TRUE((std::is_constructible, const no_conversion*, std::size_t>())); + EXPECT_TRUE((std::is_constructible, no_conversion*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, const inherited*, std::size_t>())); + EXPECT_FALSE((std::is_constructible, inherited*, std::size_t>())); + EXPECT_FALSE((can_construct, std::string>())); EXPECT_FALSE((can_construct, std::vector>())); EXPECT_FALSE((can_construct, const std::vector>())); @@ -231,7 +243,6 @@ TEST(Span, NoExcept) const epee::span clvalue(data); EXPECT_TRUE(noexcept(epee::span())); EXPECT_TRUE(noexcept(epee::span(nullptr))); - EXPECT_TRUE(noexcept(epee::span(nullptr, 0))); EXPECT_TRUE(noexcept(epee::span(data))); EXPECT_TRUE(noexcept(epee::span(lvalue))); EXPECT_TRUE(noexcept(epee::span(clvalue))); @@ -284,6 +295,25 @@ TEST(Span, Writing) EXPECT_TRUE(boost::range::equal(expected, span)); } +TEST(Span, RemovePrefix) +{ + const std::array expected{0, 1, 2, 3}; + auto span = epee::to_span(expected); + + EXPECT_EQ(expected.begin(), span.begin()); + EXPECT_EQ(expected.end(), span.end()); + + EXPECT_EQ(2u, span.remove_prefix(2)); + EXPECT_EQ(expected.begin() + 2, span.begin()); + EXPECT_EQ(expected.end(), span.end()); + + EXPECT_EQ(2u, span.remove_prefix(3)); + EXPECT_EQ(span.begin(), span.end()); + EXPECT_EQ(expected.end(), span.begin()); + + EXPECT_EQ(0u, span.remove_prefix(100)); +} + TEST(Span, ToByteSpan) { const char expected[] = {56, 44, 11, 5}; @@ -318,6 +348,30 @@ TEST(Span, AsByteSpan) ); } +TEST(Span, AsMutByteSpan) +{ + struct some_pod { char value[4]; }; + some_pod actual {}; + + auto span = epee::as_mut_byte_span(actual); + boost::range::iota(span, 1); + EXPECT_TRUE( + boost::range::equal( + std::array{{1, 2, 3, 4}}, actual.value + ) + ); +} + +TEST(Span, ToMutSpan) +{ + std::vector mut; + mut.resize(4); + + auto span = epee::to_mut_span(mut); + boost::range::iota(span, 1); + EXPECT_EQ((std::vector{1, 2, 3, 4}), mut); +} + TEST(ToHex, String) { EXPECT_TRUE(epee::to_hex::string(nullptr).empty()); @@ -330,6 +384,7 @@ TEST(ToHex, String) EXPECT_EQ( std_to_hex(all_bytes), epee::to_hex::string(epee::to_span(all_bytes)) ); + } TEST(ToHex, Array) -- cgit v1.2.3