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[] = {0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a,
11 0x18, 0xb4, 0x70, 0xcb, 0xf5, 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c,
12 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 EXPECT_EQ(s1, s0);
32 else
33 EXPECT_NE(s1, s0);
34
35 for (std::size_t j = 0; j != sizeof(data); ++j)
36 {
37 Slice const s2{data, j};
38
39 if (i == j)
40 EXPECT_EQ(s1, s2);
41 else
42 EXPECT_NE(s1, s2);
43 }
44 }
45
46 // Test slices of equal size but pointing to different data:
47 std::array<std::uint8_t, sizeof(data)> a;
48 std::array<std::uint8_t, sizeof(data)> b;
49
50 for (std::size_t i = 0; i != sizeof(data); ++i)
51 a[i] = b[i] = data[i];
52
53 EXPECT_EQ(makeSlice(a), makeSlice(b));
54 b[7]++;
55 EXPECT_NE(makeSlice(a), makeSlice(b));
56 a[7]++;
57 EXPECT_EQ(makeSlice(a), makeSlice(b));
58}
59
60TEST(Slice, indexing)
61{
62 Slice const s{data, sizeof(data)};
63
64 for (std::size_t i = 0; i != sizeof(data); ++i)
65 EXPECT_EQ(s[i], data[i]);
66}
67
68TEST(Slice, advancing)
69{
70 for (std::size_t i = 0; i < sizeof(data); ++i)
71 {
72 for (std::size_t j = 0; i + j < sizeof(data); ++j)
73 {
74 Slice s(data + i, sizeof(data) - i);
75 s += j;
76
77 EXPECT_EQ(s.data(), data + i + j);
78 EXPECT_EQ(s.size(), sizeof(data) - i - j);
79 }
80 }
81}
An immutable linear range of bytes.
Definition Slice.h:27
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:6
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:214