rippled
Loading...
Searching...
No Matches
Buffer_test.cpp
1#include <xrpl/basics/Buffer.h>
2#include <xrpl/beast/unit_test.h>
3
4#include <cstdint>
5#include <type_traits>
6
7namespace xrpl {
8namespace test {
9
11{
12 bool
13 sane(Buffer const& b) const
14 {
15 if (b.size() == 0)
16 return b.data() == nullptr;
17
18 return b.data() != nullptr;
19 }
20
21 void
22 run() override
23 {
24 std::uint8_t const data[] = {0xa8, 0xa1, 0x38, 0x45, 0x23, 0xec, 0xe4, 0x23,
25 0x71, 0x6d, 0x2a, 0x18, 0xb4, 0x70, 0xcb, 0xf5,
26 0xac, 0x2d, 0x89, 0x4d, 0x19, 0x9c, 0xf0, 0x2c,
27 0x15, 0xd1, 0xf9, 0x9b, 0x66, 0xd2, 0x30, 0xd3};
28
29 Buffer b0;
30 BEAST_EXPECT(sane(b0));
31 BEAST_EXPECT(b0.empty());
32
33 Buffer b1{0};
34 BEAST_EXPECT(sane(b1));
35 BEAST_EXPECT(b1.empty());
36 std::memcpy(b1.alloc(16), data, 16);
37 BEAST_EXPECT(sane(b1));
38 BEAST_EXPECT(!b1.empty());
39 BEAST_EXPECT(b1.size() == 16);
40
41 Buffer b2{b1.size()};
42 BEAST_EXPECT(sane(b2));
43 BEAST_EXPECT(!b2.empty());
44 BEAST_EXPECT(b2.size() == b1.size());
45 std::memcpy(b2.data(), data + 16, 16);
46
47 Buffer b3{data, sizeof(data)};
48 BEAST_EXPECT(sane(b3));
49 BEAST_EXPECT(!b3.empty());
50 BEAST_EXPECT(b3.size() == sizeof(data));
51 BEAST_EXPECT(std::memcmp(b3.data(), data, b3.size()) == 0);
52
53 // Check equality and inequality comparisons
54 BEAST_EXPECT(b0 == b0);
55 BEAST_EXPECT(b0 != b1);
56 BEAST_EXPECT(b1 == b1);
57 BEAST_EXPECT(b1 != b2);
58 BEAST_EXPECT(b2 != b3);
59
60 // Check copy constructors and copy assignments:
61 {
62 testcase("Copy Construction / Assignment");
63
64 Buffer x{b0};
65 BEAST_EXPECT(x == b0);
66 BEAST_EXPECT(sane(x));
67 Buffer y{b1};
68 BEAST_EXPECT(y == b1);
69 BEAST_EXPECT(sane(y));
70 x = b2;
71 BEAST_EXPECT(x == b2);
72 BEAST_EXPECT(sane(x));
73 x = y;
74 BEAST_EXPECT(x == y);
75 BEAST_EXPECT(sane(x));
76 y = b3;
77 BEAST_EXPECT(y == b3);
78 BEAST_EXPECT(sane(y));
79 x = b0;
80 BEAST_EXPECT(x == b0);
81 BEAST_EXPECT(sane(x));
82#if defined(__clang__)
83#pragma clang diagnostic push
84#pragma clang diagnostic ignored "-Wself-assign-overloaded"
85#endif
86
87 x = x;
88 BEAST_EXPECT(x == b0);
89 BEAST_EXPECT(sane(x));
90 y = y;
91 BEAST_EXPECT(y == b3);
92 BEAST_EXPECT(sane(y));
93
94#if defined(__clang__)
95#pragma clang diagnostic pop
96#endif
97 }
98
99 // Check move constructor & move assignments:
100 {
101 testcase("Move Construction / Assignment");
102
105
106 { // Move-construct from empty buf
107 Buffer x;
108 Buffer y{std::move(x)};
109 BEAST_EXPECT(sane(x));
110 BEAST_EXPECT(x.empty());
111 BEAST_EXPECT(sane(y));
112 BEAST_EXPECT(y.empty());
113 BEAST_EXPECT(x == y);
114 }
115
116 { // Move-construct from non-empty buf
117 Buffer x{b1};
118 Buffer y{std::move(x)};
119 BEAST_EXPECT(sane(x));
120 BEAST_EXPECT(x.empty());
121 BEAST_EXPECT(sane(y));
122 BEAST_EXPECT(y == b1);
123 }
124
125 { // Move assign empty buf to empty buf
126 Buffer x;
127 Buffer y;
128
129 x = std::move(y);
130 BEAST_EXPECT(sane(x));
131 BEAST_EXPECT(x.empty());
132 BEAST_EXPECT(sane(y));
133 BEAST_EXPECT(y.empty());
134 }
135
136 { // Move assign non-empty buf to empty buf
137 Buffer x;
138 Buffer y{b1};
139
140 x = std::move(y);
141 BEAST_EXPECT(sane(x));
142 BEAST_EXPECT(x == b1);
143 BEAST_EXPECT(sane(y));
144 BEAST_EXPECT(y.empty());
145 }
146
147 { // Move assign empty buf to non-empty buf
148 Buffer x{b1};
149 Buffer y;
150
151 x = std::move(y);
152 BEAST_EXPECT(sane(x));
153 BEAST_EXPECT(x.empty());
154 BEAST_EXPECT(sane(y));
155 BEAST_EXPECT(y.empty());
156 }
157
158 { // Move assign non-empty buf to non-empty buf
159 Buffer x{b1};
160 Buffer y{b2};
161 Buffer z{b3};
162
163 x = std::move(y);
164 BEAST_EXPECT(sane(x));
165 BEAST_EXPECT(!x.empty());
166 BEAST_EXPECT(sane(y));
167 BEAST_EXPECT(y.empty());
168
169 x = std::move(z);
170 BEAST_EXPECT(sane(x));
171 BEAST_EXPECT(!x.empty());
172 BEAST_EXPECT(sane(z));
173 BEAST_EXPECT(z.empty());
174 }
175 }
176
177 {
178 testcase("Slice Conversion / Construction / Assignment");
179
180 Buffer w{static_cast<Slice>(b0)};
181 BEAST_EXPECT(sane(w));
182 BEAST_EXPECT(w == b0);
183
184 Buffer x{static_cast<Slice>(b1)};
185 BEAST_EXPECT(sane(x));
186 BEAST_EXPECT(x == b1);
187
188 Buffer y{static_cast<Slice>(b2)};
189 BEAST_EXPECT(sane(y));
190 BEAST_EXPECT(y == b2);
191
192 Buffer z{static_cast<Slice>(b3)};
193 BEAST_EXPECT(sane(z));
194 BEAST_EXPECT(z == b3);
195
196 // Assign empty slice to empty buffer
197 w = static_cast<Slice>(b0);
198 BEAST_EXPECT(sane(w));
199 BEAST_EXPECT(w == b0);
200
201 // Assign non-empty slice to empty buffer
202 w = static_cast<Slice>(b1);
203 BEAST_EXPECT(sane(w));
204 BEAST_EXPECT(w == b1);
205
206 // Assign non-empty slice to non-empty buffer
207 x = static_cast<Slice>(b2);
208 BEAST_EXPECT(sane(x));
209 BEAST_EXPECT(x == b2);
210
211 // Assign non-empty slice to non-empty buffer
212 y = static_cast<Slice>(z);
213 BEAST_EXPECT(sane(y));
214 BEAST_EXPECT(y == z);
215
216 // Assign empty slice to non-empty buffer:
217 z = static_cast<Slice>(b0);
218 BEAST_EXPECT(sane(z));
219 BEAST_EXPECT(z == b0);
220 }
221
222 {
223 testcase("Allocation, Deallocation and Clearing");
224
225 auto test = [this](Buffer const& b, std::size_t i) {
226 Buffer x{b};
227
228 // Try to allocate some number of bytes, possibly
229 // zero (which means clear) and sanity check
230 x(i);
231 BEAST_EXPECT(sane(x));
232 BEAST_EXPECT(x.size() == i);
233 BEAST_EXPECT((x.data() == nullptr) == (i == 0));
234
235 // Try to allocate some more data (always non-zero)
236 x(i + 1);
237 BEAST_EXPECT(sane(x));
238 BEAST_EXPECT(x.size() == i + 1);
239 BEAST_EXPECT(x.data() != nullptr);
240
241 // Try to clear:
242 x.clear();
243 BEAST_EXPECT(sane(x));
244 BEAST_EXPECT(x.size() == 0);
245 BEAST_EXPECT(x.data() == nullptr);
246
247 // Try to clear again:
248 x.clear();
249 BEAST_EXPECT(sane(x));
250 BEAST_EXPECT(x.size() == 0);
251 BEAST_EXPECT(x.data() == nullptr);
252 };
253
254 for (std::size_t i = 0; i < 16; ++i)
255 {
256 test(b0, i);
257 test(b1, i);
258 }
259 }
260 }
261};
262
263BEAST_DEFINE_TESTSUITE(Buffer, basics, xrpl);
264
265} // namespace test
266} // namespace xrpl
A testsuite class.
Definition suite.h:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:150
Like std::vector<char> but better.
Definition Buffer.h:16
bool empty() const noexcept
Definition Buffer.h:110
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:104
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:128
An immutable linear range of bytes.
Definition Slice.h:26
T memcmp(T... args)
T memcpy(T... args)
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
bool sane(Buffer const &b) const
void run() override
Runs the suite.