aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2018-09-21 20:30:35 +0200
committerRiccardo Spagni <ric@spagni.net>2018-09-21 20:30:36 +0200
commitbb3ff2bb36ed0aa7927690d7f0ee40dc212d2a2b (patch)
treef392a79c4a6024733299e37f5e10c75315c99918 /tests/unit_tests
parentMerge pull request #4391 (diff)
parentAdded features to epee::span<T> : (diff)
downloadmonero-bb3ff2bb36ed0aa7927690d7f0ee40dc212d2a2b.tar.xz
Merge pull request #4209
26a42fe5 Added features to epee::span<T> : - Support for classes - Added `remove_prefix` function - Added `to_mut_span` and `as_mut_byte_span` (Lee Clagett)
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/epee_utils.cpp57
1 files changed, 56 insertions, 1 deletions
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<epee::span<char>>());
EXPECT_TRUE((std::is_constructible<epee::span<char>, char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<char>, const char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<char>, unsigned char*, std::size_t>()));
+ EXPECT_TRUE(std::is_constructible<epee::span<no_conversion>>());
+ EXPECT_TRUE((std::is_constructible<epee::span<no_conversion>, no_conversion*, std::size_t>()));
+ EXPECT_FALSE((std::is_constructible<epee::span<no_conversion>, inherited*, std::size_t>()));
+
EXPECT_TRUE((can_construct<epee::span<char>, std::nullptr_t>()));
EXPECT_TRUE((can_construct<epee::span<char>, char(&)[1]>()));
@@ -193,12 +198,19 @@ TEST(Span, MutableConstruction)
TEST(Span, ImmutableConstruction)
{
struct no_conversion{};
+ struct inherited : no_conversion {};
EXPECT_TRUE(std::is_constructible<epee::span<const char>>());
EXPECT_TRUE((std::is_constructible<epee::span<const char>, char*, std::size_t>()));
EXPECT_TRUE((std::is_constructible<epee::span<const char>, const char*, std::size_t>()));
EXPECT_FALSE((std::is_constructible<epee::span<const char>, unsigned char*, std::size_t>()));
+ EXPECT_TRUE(std::is_constructible<epee::span<const no_conversion>>());
+ EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, const no_conversion*, std::size_t>()));
+ EXPECT_TRUE((std::is_constructible<epee::span<const no_conversion>, no_conversion*, std::size_t>()));
+ EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, const inherited*, std::size_t>()));
+ EXPECT_FALSE((std::is_constructible<epee::span<const no_conversion>, inherited*, std::size_t>()));
+
EXPECT_FALSE((can_construct<epee::span<const char>, std::string>()));
EXPECT_FALSE((can_construct<epee::span<const char>, std::vector<char>>()));
EXPECT_FALSE((can_construct<epee::span<const char>, const std::vector<char>>()));
@@ -231,7 +243,6 @@ TEST(Span, NoExcept)
const epee::span<char> clvalue(data);
EXPECT_TRUE(noexcept(epee::span<char>()));
EXPECT_TRUE(noexcept(epee::span<char>(nullptr)));
- EXPECT_TRUE(noexcept(epee::span<char>(nullptr, 0)));
EXPECT_TRUE(noexcept(epee::span<char>(data)));
EXPECT_TRUE(noexcept(epee::span<char>(lvalue)));
EXPECT_TRUE(noexcept(epee::span<char>(clvalue)));
@@ -284,6 +295,25 @@ TEST(Span, Writing)
EXPECT_TRUE(boost::range::equal(expected, span));
}
+TEST(Span, RemovePrefix)
+{
+ const std::array<unsigned, 4> 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<unsigned char, 4>{{1, 2, 3, 4}}, actual.value
+ )
+ );
+}
+
+TEST(Span, ToMutSpan)
+{
+ std::vector<unsigned> mut;
+ mut.resize(4);
+
+ auto span = epee::to_mut_span(mut);
+ boost::range::iota(span, 1);
+ EXPECT_EQ((std::vector<unsigned>{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)