20
20
#include < list>
21
21
#include < string_view>
22
22
#include < span>
23
+ #include < array>
24
+ #include < algorithm> // std::copy
23
25
#include < bit> // std::bit_cast
24
26
25
27
#include " buffer/shared_buffer.hpp"
26
28
27
29
#include " utility/repeat.hpp"
28
- #include " utility/make_byte_array .hpp"
30
+ #include " utility/byte_array .hpp"
29
31
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};
32
37
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
+ }
33
45
34
46
template <typename SB, typename PT>
35
47
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);
39
50
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) );
41
52
return sb;
42
53
}
43
54
@@ -49,48 +60,85 @@ void generic_pointer_append_test() {
49
60
const PT* ptr_arr { arr };
50
61
sb.append (ptr_arr, 3 );
51
62
REQUIRE (sb.size () == (sav_sz + 3 ));
52
- std::span<const PT> sp { arr };
63
+ std::span<const PT, 3 > sp { arr };
53
64
sb.append (sp);
54
65
REQUIRE (sb.size () == (sav_sz + 6 ));
55
66
}
56
67
57
68
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
+ }
59
74
60
- REQUIRE (sz > 2 );
75
+ template <typename SB>
76
+ void common_ctor_test () {
61
77
62
- SB sb (buf, sz);
63
- REQUIRE_FALSE (sb.empty ());
64
78
{
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);
68
82
}
69
83
{
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);
74
87
}
75
88
{
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);
81
91
}
82
92
{
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);
87
111
}
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
88
136
}
89
137
90
138
template <typename SB>
91
139
void byte_vector_move_test () {
92
140
93
- auto arr = chops::make_byte_array (0x01 , 0x02 , 0x03 , 0x04 , 0x05 );
141
+ auto arr { chops::make_byte_array (0x01 , 0x02 , 0x03 , 0x04 , 0x05 ) } ;
94
142
95
143
std::vector<std::byte> bv { arr.cbegin (), arr.cend () };
96
144
SB sb (std::move (bv));
@@ -106,17 +154,24 @@ TEMPLATE_TEST_CASE ( "Generic pointer construction",
106
154
}
107
155
108
156
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]" ,
111
165
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>();
114
167
}
115
168
116
169
TEST_CASE ( " Mutable shared buffer copy construction and assignment" ,
117
170
" [mutable_shared_buffer] [copy]" ) {
118
171
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 ) };
120
175
121
176
chops::mutable_shared_buffer sb;
122
177
REQUIRE (sb.empty ());
@@ -140,18 +195,22 @@ TEST_CASE ( "Mutable shared buffer copy construction and assignment",
140
195
TEST_CASE ( " Mutable shared buffer resize and clear" ,
141
196
" [mutable_shared_buffer] [resize_and_clear]" ) {
142
197
198
+ constexpr int N = 11 ;
199
+
143
200
chops::mutable_shared_buffer sb;
201
+ REQUIRE (sb.empty ());
202
+ REQUIRE (sb.size () == 0 );
144
203
145
204
sb.resize (N);
146
205
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 ); } );
148
207
149
208
SECTION ( " Compare two resized mutable shared buffer with same size" ) {
150
209
chops::mutable_shared_buffer sb2 (N);
151
210
REQUIRE (sb == sb2);
152
211
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 );
155
214
} );
156
215
}
157
216
SECTION ( " Clear, check size" ) {
@@ -174,15 +233,15 @@ TEST_CASE ( "Mutable shared buffer swap",
174
233
REQUIRE (sb1.size () == arr2.size ());
175
234
REQUIRE (sb2.size () == arr1.size ());
176
235
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 ) ));
182
241
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 ) ));
186
245
}
187
246
188
247
TEST_CASE ( " Mutable shared buffer append" ,
0 commit comments