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