8000 Enhancements to span and other functionality, changed byte compares t… · connectivecpp/shared-buffer@c4009eb · GitHub
[go: up one dir, main page]

Skip to content

Commit c4009eb

Browse files
Enhancements to span and other functionality, changed byte compares to integer compares for Catch2 stringify weirdness on MSVC and macos
1 parent 717fb20 commit c4009eb

File tree

1 file changed

+104
-45
lines changed

1 file changed

+104
-45
lines changed

test/shared_buffer_test.cpp

Lines changed: 104 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,35 @@
2020
#include <list>
2121
#include <string_view>
2222
#include <span>
23+
#include <array>
24+
#include <algorithm> // std::copy
2325
#include <bit> // std::bit_cast
2426

2527
#include "buffer/shared_buffer.hpp"
2628

2729
#include "utility/repeat.hpp"
28-
#include "utility/make_byte_array.hpp"
30+
#include "utility/byte_array.hpp"
2931

30-
constexpr std::byte Harhar { 42 };
31-
constexpr int N = 11;
32+
constexpr std::size_t test_data_size { 12u };
33+
using test_data_type = std::array<std::byte, test_data_size>;
34+
constexpr test_data_type test_data { chops::make_byte_array( 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 ) };
35+
char test_data_char[test_data_size] { 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 };
36+
const char* test_data_char_ptr {test_data_char};
3237

38+
template <typename SB>
39+
bool check_sb_against_test_data(SB sb) {
40+
REQUIRE (sb.size() == test_data_size);
41+
test_data_type buf;
42+
std::copy(sb.data(), sb.data()+sb.size(), buf.begin());
43+
return chops::compare_byte_arrays(buf, test_data);
44+
}
3345

3446
template <typename SB, typename PT>
3547
SB generic_pointer_construction_test() {
36-
auto arr = chops::make_byte_array( 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 );
37-
auto ptr = std::bit_cast<const PT *>(arr.data());
38-
SB sb(ptr, arr.size());
48+
auto ptr { std::bit_cast<const PT *>(test_data.data()) };
49+
SB sb(ptr, test_data_size);
3950
REQUIRE_FALSE (sb.empty());
40-
chops::repeat(static_cast<int>(arr.size()), [&sb, arr] (int i) { REQUIRE(*(sb.data()+i) == arr[i]); } );
51+
REQUIRE (check_sb_against_test_data(sb));
4152
return sb;
4253
}
4354

@@ -49,48 +60,85 @@ void generic_pointer_append_test() {
4960
const PT* ptr_arr { arr };
5061
sb.append (ptr_arr, 3);
5162
REQUIRE (sb.size() == (sav_sz + 3));
52-
std::span<const PT> sp { arr };
63+
std::span<const PT, 3> sp { arr };
5364
sb.append (sp);
5465
REQUIRE (sb.size() == (sav_sz + 6));
5566
}
5667

5768
template <typename SB>
58-
void common_methods_test(const std::byte* buf, typename SB::size_type sz) {
69+
void check_sb(SB sb) {
70+
REQUIRE_FALSE (sb.empty());
71+
REQUIRE (sb.size() == test_data_size);
72+
REQUIRE (check_sb_against_test_data(sb));
73+
}
5974

60-
REQUIRE (sz > 2);
75+
template <typename SB>
76+
void common_ctor_test() {
6177

62-
SB sb(buf, sz);
63-
REQUIRE_FALSE (sb.empty());
6478
{
65-
SB sb2(buf, sz);
66-
REQUIRE_FALSE (sb2.empty());
67-
REQUIRE (sb == sb2);
79+
std::span<const std::byte, test_data_size> sp { test_data };
80+
SB sb{sp};
81+
check_sb(sb);
6882
}
6983
{
70-
std::list<std::byte> lst (buf, buf+sz);
71-
SB sb2(lst.cbegin(), lst.cend());
72-
REQUIRE_FALSE (sb2.empty());
73-
REQUIRE (sb == sb2);
84+
std::span<const std::byte> sp { test_data.data(), test_data.size() };
85+
SB sb{sp};
86+
check_sb(sb);
7487
}
7588
{
76-
auto ba = chops::make_byte_array(buf[0], buf[1]);
77-
SB sb2(ba.cbegin(), ba.cend());
78-
REQUIRE_FALSE (sb2.empty());
79-
REQUIRE (((sb2 < sb) != 0)); // uses spaceship operator
80-
REQUIRE (sb2 != sb);
89+
SB sb{test_data.data(), test_data.size()};
90+
check_sb(sb);
8191
}
8292
{
83-
auto ba = chops::make_byte_array(0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
84-
SB sb2(ba.cbegin(), ba.cend());
85-
REQUIRE_FALSE (sb2.empty());
86-
REQUIRE (sb2 != sb);
93+
std::span<const char, test_data_size> sp { test_data_char };
94+
SB sb{sp};
95+
check_sb(sb);
96+
}
97+
{
98+
std::span<const char> sp { test_data_char, test_data_char+test_data_size };
99+
SB sb{sp};
100+
check_sb(sb);
101+
}
102+
{
103+
SB sb{test_data_char_ptr, test_data_size};
104+
check_sb(sb);
105+
}
106+
107+
{
108+
std::list<std::byte> lst {test_data.cbegin(), test_data.cend()};
109+
SB sb {lst.cbegin(), lst.cend()};
110+
check_sb(sb);
87111
}
112+
{
113+
SB sb1{test_data.data(), test_data.size()};
114+
SB sb2{test_data.data(), test_data.size()};
115+
REQUIRE (sb1 == sb2);
116+
}
117+
{
118+
SB sb1{test_data.data(), test_data.size()};
119+
SB sb2{sb1};
120+
REQUIRE (sb1 == sb2);
121+
}
122+
123+
}
124+
125+
template <typename SB>
126+
void common_comparison_test() {
127+
auto ba1 { chops::make_byte_array(0x00, 0x00, 0x00) };
128+
auto ba2 { chops::make_byte_array(0x00, 0x22, 0x33) };
129+
130+
SB sb1(ba1.cbegin(), ba1.cend());
131+
SB sb2(ba2.cbegin(), ba2.cend());
132+
REQUIRE_FALSE (sb1.empty());
133+
REQUIRE_FALSE (sb2.empty());
134+
REQUIRE_FALSE (sb1 == sb2);
135+
REQUIRE (((sb1 < sb2) != 0)); // uses spaceship operator
88136
}
89137

90138
template <typename SB>
91139
void byte_vector_move_test() {
92140

93-
auto arr = chops::make_byte_array (0x01, 0x02, 0x03, 0x04, 0x05);
141+
auto arr { chops::make_byte_array (0x01, 0x02, 0x03, 0x04, 0x05) };
94142

95143
std::vector<std::byte> bv { arr.cbegin(), arr.cend() };
96144
SB sb(std::move(bv));
@@ -106,17 +154,24 @@ TEMPLATE_TEST_CASE ( "Generic pointer construction",
106154
}
107155

108156

109-
TEMPLATE_TEST_CASE ( "Shared buffer common methods",
110-
"[const_shared_buffer] [common]",
157+
TEMPLATE_TEST_CASE ( "Shared buffer common ctor methods",
158+
"[const_shared_buffer] [mutable_shared_buffer] [common]",
159+
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
160+
common_ctor_test<TestType>();
161+
}
162+
163+
TEMPLATE_TEST_CASE ( "Shared buffer common comparison methods",
164+
"[const_shared_buffer] [mutable_shared_buffer] [common]",
111165
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
112-
auto arr = chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 );
113-
common_methods_test<TestType>(arr.data(), arr.size());
166+
common_comparison_test<TestType>();
114167
}
115168

116169
TEST_CASE ( "Mutable shared buffer copy construction and assignment",
117170
"[mutable_shared_buffer] [copy]" ) {
118171

119-
auto arr = chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 );
172+
constexpr std::byte Harhar { 42 };
173+
174+
auto arr { chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 ) };
120175

121176
chops::mutable_shared_buffer sb;
122177
REQUIRE (sb.empty());
@@ -140,18 +195,22 @@ TEST_CASE ( "Mutable shared buffer copy construction and assignment",
140195
TEST_CASE ( "Mutable shared buffer resize and clear",
141196
"[mutable_shared_buffer] [resize_and_clear]" ) {
142197

198+
constexpr int N = 11;
199+
143200
chops::mutable_shared_buffer sb;
201+
REQUIRE (sb.empty());
202+
REQUIRE (sb.size() == 0);
144203

145204
sb.resize(N);
146205
REQUIRE (sb.size() == N);
147-
chops::repeat(N, [&sb] (int i) { REQUIRE (*(sb.data() + i) == std::byte{0} ); } );
206+
chops::repeat(N, [&sb] (int i) { REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 ); } );
148207

149208
SECTION ( "Compare two resized mutable shared buffer with same size" ) {
150209
chops::mutable_shared_buffer sb2(N);
151210
REQUIRE (sb == sb2);
152211
chops::repeat(N, [&sb, &sb2] (int i) {
153-
REQUIRE (*(sb.data() + i) == std::byte{0} );
154-
REQUIRE (*(sb2.data() + i) == std::byte{0} );
212+
REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 );
213+
REQUIRE (std::to_integer<int>(*(sb2.data() + i)) == 0 );
155214
} );
156215
}
157216
SECTION ( "Clear, check size" ) {
@@ -174,15 +233,15 @@ TEST_CASE ( "Mutable shared buffer swap",
174233
REQUIRE (sb1.size() == arr2.size());
175234
REQUIRE (sb2.size() == arr1.size());
176235

177-
REQUIRE (*(sb1.data()+0) == *(arr2.data()+0));
178-
REQUIRE (*(sb1.data()+1) == *(arr2.data()+1));
179-
REQUIRE (*(sb1.data()+2) == *(arr2.data()+2));
180-
REQUIRE (*(sb1.data()+3) == *(arr2.data()+3));
181-
REQUIRE (*(sb1.data()+4) == *(arr2.data()+4));
236+
REQUIRE (std::to_integer<int>(*(sb1.data()+0)) == std::to_integer<int>(*(arr2.data()+0)));
237+
REQUIRE (std::to_integer<int>(*(sb1.data()+1)) == std::to_integer<int>(*(arr2.data()+1)));
238+
REQUIRE (std::to_integer<int>(*(sb1.data()+2)) == std::to_integer<int>(*(arr2.data()+2)));
239+
REQUIRE (std::to_integer<int>(*(sb1.data()+3)) == std::to_integer<int>(*(arr2.data()+3)));
240+
REQUIRE (std::to_integer<int>(*(sb1.data()+4)) == std::to_integer<int>(*(arr2.data()+4)));
182241

183-
REQUIRE (*(sb2.data()+0) == *(arr1.data()+0));
184-
REQUIRE (*(sb2.data()+1) == *(arr1.data()+1));
185-
REQUIRE (*(sb2.data()+2) == *(arr1.data()+2));
242+
REQUIRE (std::to_integer<int>(*(sb2.data()+0)) == std::to_integer<int>(*(arr1.data()+0)));
243+
REQUIRE (std::to_integer<int>(*(sb2.data()+1)) == std::to_integer<int>(*(arr1.data()+1)));
244+
REQUIRE (std::to_integer<int>(*(sb2.data()+2)) == std::to_integer<int>(*(arr1.data()+2)));
186245
}
187246

188247
TEST_CASE ( "Mutable shared buffer append",

0 commit comments

Comments
 (0)
0