FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
shared-buffer/test/shared_buffer_test.cpp at develop · connectivecpp/shared-buffer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
connectivecpp
/
shared-buffer
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
shared-buffer
/
test
/
shared_buffer_test.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
362 lines (301 loc) · 11.2 KB
Breadcrumbs
shared-buffer
/
test
/
shared_buffer_test.cpp
Copy path
File metadata and controls
362 lines (301 loc) · 11.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* @file
*
* @brief Test scenarios for @c mutable_shared_buffer and
* @c const_shared_buffer classes.
*
* @author Cliff Green
*
* @copyright (c) 2017-2025 by Cliff Green
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#
include
"
catch2/catch_test_macros.hpp
"
#
include
"
catch2/catch_template_test_macros.hpp
"
#
include
<
cstddef
>
//
std::byte
#
include
<
list
>
#
include
<
string_view
>
#
include
<
span
>
#
include
<
array
>
#
include
<
algorithm
>
//
std::copy
#
include
<
bit
>
//
std::bit_cast
#
include
<
ranges
>
//
std::views::iota
#
include
"
buffer/shared_buffer.hpp
"
#
include
"
utility/byte_array.hpp
"
constexpr
std::
size_t
test_data_size {
12u
};
using
test_data_type = std::array<std::byte, test_data_size>;
constexpr
test_data_type test_data {
chops::make_byte_array
(
40
,
41
,
42
,
43
,
44
,
60
,
59
,
58
,
57
,
56
,
42
,
42
) };
char
test_data_char[test_data_size] {
40
,
41
,
42
,
43
,
44
,
60
,
59
,
58
,
57
,
56
,
42
,
42
};
const
char
* test_data_char_ptr {test_data_char};
template
<
typename
SB
>
bool
check_sb_against_test_data
(
SB
sb) {
REQUIRE
(sb.
size
() == test_data_size);
test_data_type buf;
std::copy
(sb.
data
(), sb.
data
()+sb.
size
(), buf.
begin
());
return
chops::compare_byte_arrays
(buf, test_data);
}
template
<
typename
SB
,
typename
PT
>
SB
generic_pointer_construction_test
(
const
PT
* ptr) {
SB
sb
(ptr, test_data_size);
REQUIRE_FALSE
(sb.
empty
());
REQUIRE
(
check_sb_against_test_data
(sb));
return
sb;
}
template
<
typename
PT
>
void
generic_pointer_append_test
(
const
PT
* ptr) {
auto
sb { generic_pointer_construction_test<chops::mutable_shared_buffer>(ptr) };
auto
sav_sz { sb.
size
() };
const
PT
arr[] {
5
,
6
,
7
};
const
PT
* ptr_arr { arr };
sb.
append
(ptr_arr,
3
);
REQUIRE
(sb.
size
() == (sav_sz +
3
));
std::span<
const
PT
,
3
> sp { arr };
sb.
append
(sp);
REQUIRE
(sb.
size
() == (sav_sz +
6
));
}
template
<
typename
SB
>
void
check_sb
(
SB
sb) {
REQUIRE_FALSE
(sb.
empty
());
REQUIRE
(sb.
size
() == test_data_size);
REQUIRE
(
check_sb_against_test_data
(sb));
}
template
<
typename
SB
>
void
common_ctor_test
() {
{
std::span<
const
std::byte, test_data_size> sp { test_data };
SB
sb{sp};
check_sb
(sb);
}
{
std::span<
const
std::byte> sp { test_data.
data
(), test_data.
size
() };
SB
sb{sp};
check_sb
(sb);
}
{
SB
sb{test_data.
data
(), test_data.
size
()};
check_sb
(sb);
}
{
std::span<
const
char
, test_data_size> sp { test_data_char };
SB
sb{sp};
check_sb
(sb);
}
{
std::span<
const
char
> sp { test_data_char, test_data_char+test_data_size };
SB
sb{sp};
check_sb
(sb);
}
{
SB
sb{test_data_char_ptr, test_data_size};
check_sb
(sb);
}
{
std::list<std::byte> lst {test_data.
cbegin
(), test_data.
cend
()};
SB
sb {lst.
cbegin
(), lst.
cend
()};
check_sb
(sb);
}
{
SB
sb1{test_data.
data
(), test_data.
size
()};
SB
sb2{test_data.
data
(), test_data.
size
()};
REQUIRE
(sb1 == sb2);
}
{
SB
sb1{test_data.
data
(), test_data.
size
()};
SB
sb2{sb1};
REQUIRE
(sb1 == sb2);
}
}
template
<
typename
SB
>
void
common_comparison_test
() {
auto
ba1 {
chops::make_byte_array
(
0x00
,
0x00
,
0x00
) };
auto
ba2 {
chops::make_byte_array
(
0x00
,
0x22
,
0x33
) };
SB
sb1
(ba1.
cbegin
(), ba1.
cend
());
SB
sb2
(ba2.
cbegin
(), ba2.
cend
());
REQUIRE_FALSE
(sb1.
empty
());
REQUIRE_FALSE
(sb2.
empty
());
REQUIRE_FALSE
(sb1 == sb2);
REQUIRE
(((sb1 < sb2) !=
0
));
//
uses spaceship operator
}
template
<
typename
SB
>
void
byte_vector_move_test
() {
auto
arr {
chops::make_byte_array
(
0x01
,
0x02
,
0x03
,
0x04
,
0x05
) };
std::vector<std::byte> bv { arr.
cbegin
(), arr.
cend
() };
SB
sb
(
std::move
(bv));
REQUIRE
(sb ==
SB
(arr.
cbegin
(), arr.
cend
()));
REQUIRE_FALSE
(bv.
size
() == sb.
size
());
}
TEMPLATE_TEST_CASE
(
"
Generic pointer construction
"
,
"
[common]
"
,
char
,
unsigned
char
,
signed
char
, std::
uint8_t
) {
const
TestType* ptr { std::bit_cast<
const
TestType *>(test_data.
data
()) };
generic_pointer_construction_test<chops::mutable_shared_buffer>(ptr);
generic_pointer_construction_test<chops::const_shared_buffer>(ptr);
}
TEST_CASE
(
"
Void pointer construction
"
,
"
[common]
"
) {
auto
ptr {
static_cast
<
const
void
*>(test_data.
data
()) };
generic_pointer_construction_test<chops::mutable_shared_buffer>(ptr);
generic_pointer_construction_test<chops::const_shared_buffer>(ptr);
}
TEMPLATE_TEST_CASE
(
"
Shared buffer common ctor methods
"
,
"
[const_shared_buffer] [mutable_shared_buffer] [common]
"
,
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
common_ctor_test<TestType>();
}
TEMPLATE_TEST_CASE
(
"
Shared buffer common comparison methods
"
,
"
[const_shared_buffer] [mutable_shared_buffer] [common]
"
,
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
common_comparison_test<TestType>();
}
TEST_CASE
(
"
Mutable shared buffer copy construction and assignment
"
,
"
[mutable_shared_buffer] [copy]
"
) {
constexpr
std::byte Harhar {
42
};
auto
arr {
chops::make_byte_array
(
80
,
81
,
82
,
83
,
84
,
90
,
91
,
92
) };
chops::mutable_shared_buffer sb;
REQUIRE
(sb.
empty
());
SECTION
(
"
Assign mutable shared buffer into default constructed mutable shared buffer
"
) {
chops::mutable_shared_buffer
sb2
(arr.
cbegin
(), arr.
cend
());
sb = sb2;
REQUIRE
(sb.
size
() == arr.
size
());
REQUIRE
(sb == sb2);
}
SECTION
(
"
Assign mutable shared buffer, then copy construct
"
) {
sb =
chops::mutable_shared_buffer
(arr.
cbegin
(), arr.
cend
());
chops::mutable_shared_buffer
sb2
(sb);
REQUIRE
(sb == sb2);
*(sb.
data
()+
0
) = Harhar;
*(sb.
data
()+
1
) = Harhar;
REQUIRE
(sb == sb2);
}
}
TEST_CASE
(
"
Mutable shared buffer resize and clear
"
,
"
[mutable_shared_buffer] [resize_and_clear]
"
) {
constexpr
int
N =
11
;
chops::mutable_shared_buffer sb;
REQUIRE
(sb.
empty
());
REQUIRE
(sb.
size
() ==
0
);
sb.
resize
(N);
REQUIRE
(sb.
size
() == N);
for
(
int
i :
std::views::iota
(
0
, N)) {
REQUIRE
(std::to_integer<
int
>(*(sb.
data
() + i)) ==
0
);
}
SECTION
(
"
Compare two resized mutable shared buffer with same size
"
) {
chops::mutable_shared_buffer
sb2
(N);
REQUIRE
(sb == sb2);
for
(
int
i :
std::views::iota
(
0
, N)) {
REQUIRE
(std::to_integer<
int
>(*(sb.
data
() + i)) ==
0
);
REQUIRE
(std::to_integer<
int
>(*(sb2.
data
() + i)) ==
0
);
}
}
SECTION
(
"
Clear, check size
"
) {
sb.
clear
();
REQUIRE
(sb.
size
() ==
0
);
REQUIRE
(sb.
empty
());
}
}
TEST_CASE
(
"
Mutable shared buffer swap
"
,
"
[mutable_shared_buffer] [swap]
"
) {
auto
arr1 =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
);
auto
arr2 =
chops::make_byte_array
(
0x01
,
0x02
,
0x03
,
0x04
,
0x05
);
chops::mutable_shared_buffer
sb1
(arr1.
cbegin
(), arr1.
cend
());
chops::mutable_shared_buffer
sb2
(arr2.
cbegin
(), arr2.
cend
());
chops::swap
(sb1, sb2);
REQUIRE
(sb1.
size
() == arr2.
size
());
REQUIRE
(sb2.
size
() == arr1.
size
());
REQUIRE
(std::to_integer<
int
>(*(sb1.
data
()+
0
)) == std::to_integer<
int
>(*(arr2.
data
()+
0
)));
REQUIRE
(std::to_integer<
int
>(*(sb1.
data
()+
1
)) == std::to_integer<
int
>(*(arr2.
data
()+
1
)));
REQUIRE
(std::to_integer<
int
>(*(sb1.
data
()+
2
)) == std::to_integer<
int
>(*(arr2.
data
()+
2
)));
REQUIRE
(std::to_integer<
int
>(*(sb1.
data
()+
3
)) == std::to_integer<
int
>(*(arr2.
data
()+
3
)));
REQUIRE
(std::to_integer<
int
>(*(sb1.
data
()+
4
)) == std::to_integer<
int
>(*(arr2.
data
()+
4
)));
REQUIRE
(std::to_integer<
int
>(*(sb2.
data
()+
0
)) == std::to_integer<
int
>(*(arr1.
data
()+
0
)));
REQUIRE
(std::to_integer<
int
>(*(sb2.
data
()+
1
)) == std::to_integer<
int
>(*(arr1.
data
()+
1
)));
REQUIRE
(std::to_integer<
int
>(*(sb2.
data
()+
2
)) == std::to_integer<
int
>(*(arr1.
data
()+
2
)));
}
TEST_CASE
(
"
Mutable shared buffer append
"
,
"
[mutable_shared_buffer] [append]
"
) {
auto
arr =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
);
auto
arr2 =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
,
0xaa
,
0xbb
,
0xcc
);
chops::mutable_shared_buffer
ta
(arr.
cbegin
(), arr.
cend
());
chops::mutable_shared_buffer
ta2
(arr2.
cbegin
(), arr2.
cend
());
chops::mutable_shared_buffer sb;
REQUIRE
(sb.
empty
());
SECTION
(
"
Append array to default constructed mutable shared_buffer
"
) {
sb.
append
(arr.
data
(), arr.
size
());
REQUIRE
(sb == ta);
}
SECTION
(
"
Append mutable shared buffer
"
) {
sb.
append
(ta);
REQUIRE
(sb == ta);
}
SECTION
(
"
Call append twice
"
) {
sb.
append
(ta);
sb.
append
(ta);
REQUIRE
(sb == ta2);
}
SECTION
(
"
Append with single byte
"
) {
sb.
append
(
std::byte
(
0xaa
));
sb.
append
(
std::byte
(
0xbb
));
sb +=
std::byte
(
0xcc
);
REQUIRE
(sb == ta);
}
SECTION
(
"
Append with templated append
"
) {
std::string_view
sv
(
"
Haha, Bro!
"
);
chops::mutable_shared_buffer
cb
(sv.
data
(), sv.
size
());
sb.
append
(sv.
data
(), sv.
size
());
REQUIRE
(sb == cb);
}
SECTION
(
"
Append with void pointer
"
) {
std::string_view
sv
(
"
Haha, Bro!
"
);
const
void
* ptr {
static_cast
<
const
void
*>(sv.
data
()) };
chops::mutable_shared_buffer
cb
(sv.
data
(), sv.
size
());
sb.
append
(ptr, sv.
size
());
REQUIRE
(sb == cb);
}
}
TEMPLATE_TEST_CASE
(
"
Generic pointer append
"
,
"
[mutable_shared_buffer] [pointer] [append]
"
,
char
,
unsigned
char
,
signed
char
, std::
uint8_t
) {
const
TestType* ptr { std::bit_cast<
const
TestType *>(test_data.
data
()) };
generic_pointer_append_test<TestType>(ptr);
}
TEST_CASE
(
"
Compare a mutable shared_buffer with a const shared buffer
"
,
"
[mutable_shared_buffer] [const_shared_buffer] [compare]
"
) {
auto
arr =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
);
chops::mutable_shared_buffer
msb
(arr.
cbegin
(), arr.
cend
());
chops::const_shared_buffer
csb
(arr.
cbegin
(), arr.
cend
());
REQUIRE
(msb == csb);
REQUIRE
(csb == msb);
}
TEST_CASE
(
"
Mutable shared buffer move into const shared buffer
"
,
"
[mutable_shared_buffer] [const_shared_buffer] [move]
"
) {
auto
arr1 =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
);
auto
arr2 =
chops::make_byte_array
(
0x01
,
0x02
,
0x03
,
0x04
,
0x05
);
chops::mutable_shared_buffer
msb
(arr1.
cbegin
(), arr1.
cend
());
chops::const_shared_buffer
csb
(
std::move
(msb));
REQUIRE
(csb ==
chops::const_shared_buffer
(arr1.
cbegin
(), arr1.
cend
()));
REQUIRE_FALSE
(msb == csb);
msb.
clear
();
msb.
resize
(arr2.
size
());
msb.
append
(arr2.
data
(), arr2.
size
());
REQUIRE_FALSE
(msb == csb);
}
TEMPLATE_TEST_CASE
(
"
Move a vector of bytes into a shared buffer
"
,
"
[common] [move_byte_vec]
"
,
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
byte_vector_move_test<TestType>();
}
TEST_CASE
(
"
Use get_byte_vec for external modification of buffer
"
,
"
[mutable_shared_buffer] [get_byte_vec]
"
) {
auto
arr =
chops::make_byte_array
(
0xaa
,
0xbb
,
0xcc
);
chops::mutable_shared_buffer::byte_vec
bv
(arr.
cbegin
(), arr.
cend
());
chops::mutable_shared_buffer
msb
(bv.
cbegin
(), bv.
cend
());
auto
r = msb.
get_byte_vec
();
//
REQUIRE (r == bv); // Catch2 build problems on MSVC
std::array<std::byte,
3
> arr2 { r[
0
], r[
1
], r[
2
] };
REQUIRE
(
chops::compare_byte_arrays
(arr, arr2));
r[
0
] =
std::byte
(
0xdd
);
//
REQUIRE_FALSE (r == bv); // Catch2 build problems on MSVC
std::array<std::byte,
3
> arr3 { r[
0
], r[
1
], r[
2
] };
REQUIRE_FALSE
(
chops::compare_byte_arrays
(arr, arr3));
}
Back
|
FazBrowse Home
|
New Git URL