rippled
Loading...
Searching...
No Matches
Slice_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github0.com/ripple/rippled
4 Copyright (c) 2012-2016 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Slice.h>
21#include <xrpl/beast/unit_test.h>
22
23#include <array>
24#include <cstdint>
25
26namespace ripple {
27namespace test {
28
30{
31 void
32 run() override
33 {
34 std::uint8_t const data[] = {
35 0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23, 0x71, 0x6d, 0x2a,
36 0x18, 0xb4, 0x70, 0xcb, 0xf5, 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c,
37 0xf0, 0x2c, 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3};
38
39 {
40 testcase("Equality & Inequality");
41
42 Slice const s0{};
43
44 BEAST_EXPECT(s0.size() == 0);
45 BEAST_EXPECT(s0.data() == nullptr);
46 BEAST_EXPECT(s0 == s0);
47
48 // Test slices of equal and unequal size pointing to same data:
49 for (std::size_t i = 0; i != sizeof(data); ++i)
50 {
51 Slice const s1{data, i};
52
53 BEAST_EXPECT(s1.size() == i);
54 BEAST_EXPECT(s1.data() != nullptr);
55
56 if (i == 0)
57 BEAST_EXPECT(s1 == s0);
58 else
59 BEAST_EXPECT(s1 != s0);
60
61 for (std::size_t j = 0; j != sizeof(data); ++j)
62 {
63 Slice const s2{data, j};
64
65 if (i == j)
66 BEAST_EXPECT(s1 == s2);
67 else
68 BEAST_EXPECT(s1 != s2);
69 }
70 }
71
72 // Test slices of equal size but pointing to different data:
73 std::array<std::uint8_t, sizeof(data)> a;
74 std::array<std::uint8_t, sizeof(data)> b;
75
76 for (std::size_t i = 0; i != sizeof(data); ++i)
77 a[i] = b[i] = data[i];
78
79 BEAST_EXPECT(makeSlice(a) == makeSlice(b));
80 b[7]++;
81 BEAST_EXPECT(makeSlice(a) != makeSlice(b));
82 a[7]++;
83 BEAST_EXPECT(makeSlice(a) == makeSlice(b));
84 }
85
86 {
87 testcase("Indexing");
88
89 Slice const s{data, sizeof(data)};
90
91 for (std::size_t i = 0; i != sizeof(data); ++i)
92 BEAST_EXPECT(s[i] == data[i]);
93 }
94
95 {
96 testcase("Advancing");
97
98 for (std::size_t i = 0; i < sizeof(data); ++i)
99 {
100 for (std::size_t j = 0; i + j < sizeof(data); ++j)
101 {
102 Slice s(data + i, sizeof(data) - i);
103 s += j;
104
105 BEAST_EXPECT(s.data() == data + i + j);
106 BEAST_EXPECT(s.size() == sizeof(data) - i - j);
107 }
108 }
109 }
110 }
111};
112
113BEAST_DEFINE_TESTSUITE(Slice, ripple_basics, ripple);
114
115} // namespace test
116} // namespace ripple
A testsuite class.
Definition: suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:155
An immutable linear range of bytes.
Definition: Slice.h:46
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:98
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:81
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:244
void run() override
Runs the suite.
Definition: Slice_test.cpp:32