2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // Official repository: https://github.com/boostorg/beast
10 // Test that header file is self-contained.
11 #include <boost/beast/core/flat_buffer.hpp>
13 #include "test_buffer.hpp"
15 #include <boost/beast/core/ostream.hpp>
16 #include <boost/beast/core/read_size.hpp>
17 #include <boost/beast/core/string.hpp>
18 #include <boost/beast/test/test_allocator.hpp>
19 #include <boost/beast/_experimental/unit_test/suite.hpp>
26 class flat_buffer_test
: public beast::unit_test::suite
30 is_mutable_dynamic_buffer
<flat_buffer
>::value
);
36 BEAST_EXPECT(b
.max_size() == 30);
37 test_dynamic_buffer(b
);
45 using a_t
= test::test_allocator
<char,
46 true, true, true, true, true>;
49 using a_neq_t
= test::test_allocator
<char,
50 false, true, true, true, true>;
56 BEAST_EXPECT(b
.capacity() == 0);
60 BEAST_EXPECT(b
.capacity() == 0);
61 BEAST_EXPECT(b
.max_size() == 500);
65 basic_flat_buffer
<a_neq_t
> b
{a1
};
66 BEAST_EXPECT(b
.get_allocator() == a1
);
68 BEAST_EXPECT(b
.get_allocator() != a2
);
72 basic_flat_buffer
<a_neq_t
> b
{500, a
};
73 BEAST_EXPECT(b
.capacity() == 0);
74 BEAST_EXPECT(b
.max_size() == 500);
81 basic_flat_buffer
<a_t
> b1
{30};
82 BEAST_EXPECT(b1
.get_allocator()->nmove
== 0);
83 ostream(b1
) << "Hello";
84 basic_flat_buffer
<a_t
> b2
{std::move(b1
)};
85 BEAST_EXPECT(b2
.get_allocator()->nmove
== 1);
86 BEAST_EXPECT(b1
.size() == 0);
87 BEAST_EXPECT(b1
.capacity() == 0);
88 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
89 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
92 basic_flat_buffer
<a_t
> b1
{30};
93 ostream(b1
) << "Hello";
95 basic_flat_buffer
<a_t
> b2
{std::move(b1
), a
};
96 BEAST_EXPECT(b1
.size() == 0);
97 BEAST_EXPECT(b1
.capacity() == 0);
98 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
99 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
102 basic_flat_buffer
<a_neq_t
> b1
{30};
103 ostream(b1
) << "Hello";
105 basic_flat_buffer
<a_neq_t
> b2
{std::move(b1
), a
};
106 BEAST_EXPECT(b1
.size() == 0);
107 BEAST_EXPECT(b1
.capacity() == 0);
108 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
109 BEAST_EXPECT(b1
.max_size() == b2
.max_size());
115 basic_flat_buffer
<a_t
> b1
;
116 ostream(b1
) << "Hello";
117 basic_flat_buffer
<a_t
> b2(b1
);
118 BEAST_EXPECT(b1
.get_allocator() == b2
.get_allocator());
119 BEAST_EXPECT(buffers_to_string(b1
.data()) == "Hello");
120 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
123 basic_flat_buffer
<a_neq_t
> b1
;
124 ostream(b1
) << "Hello";
126 basic_flat_buffer
<a_neq_t
> b2(b1
, a
);
127 BEAST_EXPECT(b1
.get_allocator() != b2
.get_allocator());
128 BEAST_EXPECT(buffers_to_string(b1
.data()) == "Hello");
129 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
132 basic_flat_buffer
<a_t
> b1
;
133 ostream(b1
) << "Hello";
134 basic_flat_buffer
<a_neq_t
> b2(b1
);
135 BEAST_EXPECT(buffers_to_string(b1
.data()) == "Hello");
136 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
139 basic_flat_buffer
<a_neq_t
> b1
;
140 ostream(b1
) << "Hello";
142 basic_flat_buffer
<a_t
> b2(b1
, a
);
143 BEAST_EXPECT(b2
.get_allocator() == a
);
144 BEAST_EXPECT(buffers_to_string(b1
.data()) == "Hello");
145 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
149 ostream(b1
) << "Hello";
150 basic_flat_buffer
<a_t
> b2
;
152 BEAST_EXPECT(b2
.capacity() == 1);
154 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
155 BEAST_EXPECT(b2
.capacity() == b2
.size());
162 ostream(b1
) << "Hello";
165 BEAST_EXPECT(b1
.size() == 0);
166 BEAST_EXPECT(b1
.capacity() == 0);
167 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
170 using na_t
= test::test_allocator
<char,
171 true, true, false, true, true>;
172 basic_flat_buffer
<na_t
> b1
;
173 ostream(b1
) << "Hello";
174 basic_flat_buffer
<na_t
> b2
;
176 BEAST_EXPECT(b1
.get_allocator() == b2
.get_allocator());
177 BEAST_EXPECT(b1
.size() == 0);
178 BEAST_EXPECT(b1
.capacity() == 0);
179 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
182 using na_t
= test::test_allocator
<char,
183 false, true, false, true, true>;
184 basic_flat_buffer
<na_t
> b1
;
185 ostream(b1
) << "Hello";
186 basic_flat_buffer
<na_t
> b2
;
188 BEAST_EXPECT(b1
.get_allocator() != b2
.get_allocator());
189 BEAST_EXPECT(b1
.size() == 0);
190 BEAST_EXPECT(b1
.capacity() == 0);
191 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
194 // propagate_on_container_move_assignment : true
195 using pocma_t
= test::test_allocator
<char,
196 true, true, true, true, true>;
197 basic_flat_buffer
<pocma_t
> b1
;
198 ostream(b1
) << "Hello";
199 basic_flat_buffer
<pocma_t
> b2
;
201 BEAST_EXPECT(b1
.size() == 0);
202 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
205 // propagate_on_container_move_assignment : false
206 using pocma_t
= test::test_allocator
<char,
207 true, true, false, true, true>;
208 basic_flat_buffer
<pocma_t
> b1
;
209 ostream(b1
) << "Hello";
210 basic_flat_buffer
<pocma_t
> b2
;
212 BEAST_EXPECT(b1
.size() == 0);
213 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
221 ostream(b1
) << "Hello";
224 BEAST_EXPECT(buffers_to_string(b1
.data()) == "Hello");
225 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
226 basic_flat_buffer
<a_t
> b3
;
228 BEAST_EXPECT(buffers_to_string(b3
.data()) == "Hello");
231 // propagate_on_container_copy_assignment : true
232 using pocca_t
= test::test_allocator
<char,
233 true, true, true, true, true>;
234 basic_flat_buffer
<pocca_t
> b1
;
235 ostream(b1
) << "Hello";
236 basic_flat_buffer
<pocca_t
> b2
;
238 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
241 // propagate_on_container_copy_assignment : false
242 using pocca_t
= test::test_allocator
<char,
243 true, false, true, true, true>;
244 basic_flat_buffer
<pocca_t
> b1
;
245 ostream(b1
) << "Hello";
246 basic_flat_buffer
<pocca_t
> b2
;
248 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
254 string_view
const s
= "Hello, world!";
256 BEAST_EXPECT(b1
.size() == 0);
257 BEAST_EXPECT(b1
.max_size() == 64);
258 BEAST_EXPECT(b1
.capacity() == 0);
260 BEAST_EXPECT(buffers_to_string(b1
.data()) == s
);
263 BEAST_EXPECT(buffers_to_string(b2
.data()) == s
);
265 BEAST_EXPECT(buffers_to_string(b2
.data()) == s
.substr(7));
269 BEAST_EXPECT(b2
.max_size() == 32);
271 BEAST_EXPECT(b2
.max_size() == b1
.max_size());
272 BEAST_EXPECT(buffers_to_string(b2
.data()) == s
);
274 BEAST_EXPECT(buffers_to_string(b2
.data()) == s
.substr(7));
281 ostream(b
) << "12345";
283 ostream(b
) << "67890123";
284 BEAST_EXPECT(buffers_to_string(b
.data()) == "4567890123");
290 BEAST_EXPECT(b
.max_size() == 10);
292 BEAST_EXPECT(b
.max_size() == 32);
295 // allocator max_size
297 basic_flat_buffer
<a_t
> b
;
298 auto a
= b
.get_allocator();
300 ! std::is_const
<decltype(a
)>::value
);
305 fail("", __FILE__
, __LINE__
);
307 catch(std::length_error
const&)
316 BEAST_EXPECT(read_size(b
, 512) == 10);
319 BEAST_EXPECT(read_size(b
, 512) == 6);
321 BEAST_EXPECT(read_size(b
, 512) == 8);
324 BEAST_EXPECT(read_size(b
, 512) == 0);
330 basic_flat_buffer
<a_neq_t
> b1
;
331 ostream(b1
) << "Hello";
332 basic_flat_buffer
<a_neq_t
> b2
;
333 BEAST_EXPECT(b1
.get_allocator() != b2
.get_allocator());
335 BEAST_EXPECT(b1
.get_allocator() != b2
.get_allocator());
336 BEAST_EXPECT(b1
.size() == 0);
337 BEAST_EXPECT(b1
.capacity() == 0);
338 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
341 using na_t
= test::test_allocator
<char,
342 true, true, true, false, true>;
344 basic_flat_buffer
<na_t
> b1
{a1
};
346 ostream(b1
) << "Hello";
347 basic_flat_buffer
<na_t
> b2
{a2
};
348 BEAST_EXPECT(b1
.get_allocator() == a1
);
349 BEAST_EXPECT(b2
.get_allocator() == a2
);
351 BEAST_EXPECT(b1
.get_allocator() == b2
.get_allocator());
352 BEAST_EXPECT(b1
.size() == 0);
353 BEAST_EXPECT(b1
.capacity() == 0);
354 BEAST_EXPECT(buffers_to_string(b2
.data()) == "Hello");
364 BEAST_EXPECT(b
.capacity() >= 5);
368 fail("", __FILE__
, __LINE__
);
370 catch(std::length_error
const&)
379 BEAST_EXPECT(b
.capacity() == 0);
381 BEAST_EXPECT(b
.capacity() == 50);
385 BEAST_EXPECT(b
.capacity() == 50);
387 b
.max_size(b
.capacity());
388 b
.reserve(b
.max_size() + 20);
389 BEAST_EXPECT(b
.capacity() == 70);
390 BEAST_EXPECT(b
.max_size() == 70);
396 BEAST_EXPECT(b
.capacity() == 0);
398 BEAST_EXPECT(b
.capacity() == 50);
400 BEAST_EXPECT(b
.capacity() == 50);
402 BEAST_EXPECT(b
.capacity() >= 125);
404 BEAST_EXPECT(b
.capacity() == b
.size());
406 BEAST_EXPECT(b
.capacity() == b
.size());
408 BEAST_EXPECT(b
.size() == 0);
410 BEAST_EXPECT(b
.capacity() == 0);
416 BEAST_EXPECT(b
.capacity() == 0);
419 BEAST_EXPECT(b
.size() == 50);
420 BEAST_EXPECT(b
.capacity() == 50);
422 BEAST_EXPECT(b
.size() == 0);
423 BEAST_EXPECT(b
.capacity() == 50);
431 testSpecialMembers();
435 BEAST_DEFINE_TESTSUITE(beast
,core
,flat_buffer
);