xrpld
Loading...
Searching...
No Matches
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
9using namespace xrpl;
10
11static 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
16TEST(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
70TEST(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
78TEST(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}
An immutable linear range of bytes.
Definition Slice.h:28
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:88
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:70
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TEST(FileUtilitiesTest, get_file_contents)
Slice makeSlice(std::array< T, N > const &a)
Definition Slice.h:228