rippled
Loading...
Searching...
No Matches
Slice.cpp
1#include <xrpl/basics/Slice.h>
2
3#include <doctest/doctest.h>
4
5#include <array>
6#include <cstdint>
7
8using namespace ripple;
9
10static std::uint8_t const data[] = {
11 0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a,
12 0x18, 0xb4, 0x70, 0xcb, 0xf5, 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c,
13 0xf0, 0x2c, 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3};
14
15TEST_SUITE_BEGIN("Slice");
16
17TEST_CASE("equality & inequality")
18{
19 Slice const s0{};
20
21 CHECK(s0.size() == 0);
22 CHECK(s0.data() == nullptr);
23 CHECK(s0 == s0);
24
25 // Test slices of equal and unequal size pointing to same data:
26 for (std::size_t i = 0; i != sizeof(data); ++i)
27 {
28 Slice const s1{data, i};
29
30 CHECK(s1.size() == i);
31 CHECK(s1.data() != nullptr);
32
33 if (i == 0)
34 CHECK(s1 == s0);
35 else
36 CHECK(s1 != s0);
37
38 for (std::size_t j = 0; j != sizeof(data); ++j)
39 {
40 Slice const s2{data, j};
41
42 if (i == j)
43 CHECK(s1 == s2);
44 else
45 CHECK(s1 != s2);
46 }
47 }
48
49 // Test slices of equal size but pointing to different data:
50 std::array<std::uint8_t, sizeof(data)> a;
51 std::array<std::uint8_t, sizeof(data)> b;
52
53 for (std::size_t i = 0; i != sizeof(data); ++i)
54 a[i] = b[i] = data[i];
55
56 CHECK(makeSlice(a) == makeSlice(b));
57 b[7]++;
58 CHECK(makeSlice(a) != makeSlice(b));
59 a[7]++;
60 CHECK(makeSlice(a) == makeSlice(b));
61}
62
63TEST_CASE("indexing")
64{
65 Slice const s{data, sizeof(data)};
66
67 for (std::size_t i = 0; i != sizeof(data); ++i)
68 CHECK(s[i] == data[i]);
69}
70
71TEST_CASE("advancing")
72{
73 for (std::size_t i = 0; i < sizeof(data); ++i)
74 {
75 for (std::size_t j = 0; i + j < sizeof(data); ++j)
76 {
77 Slice s(data + i, sizeof(data) - i);
78 s += j;
79
80 CHECK(s.data() == data + i + j);
81 CHECK(s.size() == sizeof(data) - i - j);
82 }
83 }
84}
85
86TEST_SUITE_END();
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:225