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