xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
tests
libxrpl
basics
Slice.cpp
1
#include <xrpl/basics/Slice.h>
2
3
#include <gtest/gtest.h>
4
5
#include <
array
>
6
#include <
cstddef
>
7
#include <
cstdint
>
8
9
using namespace
xrpl
;
10
11
static
std::uint8_t
const
kData[] = {
12
0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a, 0x18, 0xb4, 0x70, 0xcb, 0xf5,
13
0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c, 0xf0, 0x2c, 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3,
14
};
15
16
TEST
(
Slice
, equality_and_inequality)
17
{
18
Slice
const
s0{};
19
20
EXPECT_EQ(s0.
size
(), 0);
21
EXPECT_EQ(s0.
data
(),
nullptr
);
22
EXPECT_EQ(s0, s0);
23
24
// Test slices of equal and unequal size pointing to same data:
25
for
(
std::size_t
i = 0; i !=
sizeof
(kData); ++i)
26
{
27
Slice
const
s1{kData, i};
28
29
EXPECT_EQ(s1.
size
(), i);
30
EXPECT_NE(s1.
data
(),
nullptr
);
31
32
if
(i == 0)
33
{
34
EXPECT_EQ(s1, s0);
35
}
36
else
37
{
38
EXPECT_NE(s1, s0);
39
}
40
41
for
(
std::size_t
j = 0; j !=
sizeof
(kData); ++j)
42
{
43
Slice
const
s2{kData, j};
44
45
if
(i == j)
46
{
47
EXPECT_EQ(s1, s2);
48
}
49
else
50
{
51
EXPECT_NE(s1, s2);
52
}
53
}
54
}
55
56
// Test slices of equal size but pointing to different data:
57
std::array
<
std::uint8_t
,
sizeof
(kData)> a{};
58
std::array
<
std::uint8_t
,
sizeof
(kData)> b{};
59
60
for
(
std::size_t
i = 0; i !=
sizeof
(kData); ++i)
61
a[i] = b[i] = kData[i];
62
63
EXPECT_EQ(
makeSlice
(a),
makeSlice
(b));
64
b[7]++;
65
EXPECT_NE(
makeSlice
(a),
makeSlice
(b));
66
a[7]++;
67
EXPECT_EQ(
makeSlice
(a),
makeSlice
(b));
68
}
69
70
TEST
(
Slice
, indexing)
71
{
72
Slice
const
s{kData,
sizeof
(kData)};
73
74
for
(
std::size_t
i = 0; i !=
sizeof
(kData); ++i)
75
EXPECT_EQ(s[i], kData[i]);
76
}
77
78
TEST
(
Slice
, advancing)
79
{
80
for
(
std::size_t
i = 0; i <
sizeof
(kData); ++i)
81
{
82
for
(
std::size_t
j = 0; i + j <
sizeof
(kData); ++j)
83
{
84
Slice
s(kData + i,
sizeof
(kData) - i);
85
s += j;
86
87
EXPECT_EQ(s.
data
(), kData + i + j);
88
EXPECT_EQ(s.
size
(),
sizeof
(kData) - i - j);
89
}
90
}
91
}
array
xrpl::Slice
An immutable linear range of bytes.
Definition
Slice.h:28
xrpl::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition
Slice.h:88
xrpl::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition
Slice.h:70
cstddef
cstdint
std::uint8_t
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::TEST
TEST(FileUtilitiesTest, get_file_contents)
Definition
tests/libxrpl/basics/FileUtilities.cpp:60
xrpl::makeSlice
Slice makeSlice(std::array< T, N > const &a)
Definition
Slice.h:228
std::size_t
Generated by
1.17.0