18 constexpr std::size_t at_align =
alignof(T);
19 const std::size_t at_misalign = headroom;
20 const std::size_t at_align_ex = at_misalign & ~(at_align - 1);
24 EXPECT_EQ(buf.
offset(), at_misalign);
27 std::memcpy(buf.
write_alloc(
sizeof(at)), &at,
sizeof(at));
28 EXPECT_EQ(buf.
offset(), at_misalign);
30 auto ptr = align_as<align_test>(buf);
32 EXPECT_EQ(ptr->i, 42);
33 EXPECT_EQ(buf.
offset(), at_align_ex);
35 std::cout <<
"Aligning buffer: " << at_misalign <<
" -> " << at_align_ex << std::endl;
38TEST(buffer, buffer_alignas)
40 constexpr std::size_t test_lim = std::numeric_limits<std::size_t>::digits;
41 for (
auto i = std::size_t(0); i < test_lim; ++i)
44 realign_test<align_test>(buf, i);
49TEST(buffer, const_buffer_ref_1)
51 static unsigned char hello[] =
"hello world";
52 Buffer buf(hello,
sizeof(hello) - 1,
true);
54 EXPECT_EQ(cbuf.
size(), 11u);
59TEST(buffer, const_buffer_ref_2)
64 EXPECT_EQ(cbuf.
size(), 11u);
69TEST(buffer, my_const_buffer_1)
72 static const char hello[] =
"hello world";
73 MyConstBuffer cbuf(hello,
sizeof(hello) - 1,
true);
74 EXPECT_EQ(cbuf.size(), 11u);
75 EXPECT_EQ(std::string(cbuf.c_data(), cbuf.size()),
"hello world");
79TEST(buffer, const_buffer_access1)
81 constexpr char data[] =
"hello world";
83 EXPECT_EQ(cbuf[0],
'h');
84 EXPECT_EQ(cbuf[10],
'd');
89TEST(buffer, const_buffer_access2)
91 constexpr char data[] =
"hello world";
94 while (cbuf.
empty() ==
false)
96 auto back = cbuf[cbuf.
size() - 1];
108TEST(buffer, const_buffer_access3)
110 constexpr char data[] =
"hello world";
113 while (cbuf.
empty() ==
false)
115 auto front = cbuf[0];
129 char data[] =
"hello world";
131 EXPECT_EQ(buf[0],
'h');
132 EXPECT_EQ(buf[10],
'd');
139 char data[] =
"hello world";
142 EXPECT_EQ(buf[0],
'j');
143 EXPECT_EQ(buf[4],
'o');
151 char data1[] =
"hello world";
152 char data2[
sizeof(data1)];
156 auto items = buf1.size();
163 EXPECT_EQ(buf2[0],
'j');
164 EXPECT_EQ(buf2[4],
'o');
172 char data1[] =
"hello world";
173 char data2[
sizeof(data1)];
177 auto items = buf1.size();
190 EXPECT_EQ(buf1[0],
'j');
191 EXPECT_EQ(buf1[4],
'o');
197TEST(buffer, alloc_buffer_access1)
201 EXPECT_EQ(buf[0],
'h');
202 EXPECT_EQ(buf[10],
'd');
207TEST(buffer, alloc_buffer_access2)
213 EXPECT_EQ(buf[0],
'j');
214 EXPECT_EQ(buf[4],
'o');
220TEST(buffer, alloc_buffer_access3)
222 char data[] =
"hello world";
226 auto items = buf1.
size();
233 EXPECT_EQ(buf[0],
'j');
234 EXPECT_EQ(buf[4],
'o');
240TEST(buffer, alloc_buffer_pop_front)
251TEST(buffer, alloc_buffer_advance1)
259 }
while (buf.
front() !=
'd');
261 EXPECT_EQ(buf[0],
'd');
262 EXPECT_EQ(buf.
back(),
'd');
268TEST(buffer, alloc_buffer_advance2)
270 constexpr char data[] =
"hello world";
278TEST(buffer, alloc_buffer_advance3)
280 constexpr char data[] =
"hello world";
286 EXPECT_EQ(buf[0],
'd');
287 EXPECT_EQ(buf.
back(),
'd');
293TEST(buffer, alloc_buffer_remaining)
302 EXPECT_EQ(remaining - 1, buf.
remaining());
303 EXPECT_EQ(buf.
back(),
'X');
309TEST(buffer, alloc_buffer_init_headroom)
322 EXPECT_EQ(remaining - 1, buf.
remaining());
323 EXPECT_EQ(buf.
back(),
'X');
329TEST(buffer, alloc_buffer_reset_offset)
340 EXPECT_EQ(remaining - 1, buf.
remaining());
341 EXPECT_EQ(buf.
back(),
'X');
353TEST(buffer, alloc_buffer_reset_size)
364 EXPECT_EQ(remaining - 1, buf.
remaining());
365 EXPECT_EQ(buf.
back(),
'X');
371 EXPECT_EQ(buf.
back(),
'X');
375TEST(buffer, alloc_buffer_read1)
377 constexpr char data[] =
"hello world";
381 char raw[
sizeof(data) - 1];
383 buf.
read(raw,
sizeof(raw));
385 EXPECT_EQ(memcmp(raw, data,
sizeof(raw)), 0);
392 EXPECT_EQ(buf.
offset(), 0u);
395 EXPECT_EQ(buf.
size(), 16u);
403 EXPECT_EQ(buf.
offset(), 0u);
405 EXPECT_EQ(buf.
offset(), 2u);
407 EXPECT_EQ(buf.
offset(), 2u);
410 EXPECT_EQ(buf.
offset(), 0u);
411 EXPECT_EQ(buf.
size(), 16u);
416TEST(buffer, prepend_alloc_fits)
419 EXPECT_EQ(buf.
offset(), 0u);
421 EXPECT_EQ(buf.
offset(), 5u);
423 EXPECT_EQ(buf.
offset(), 5u);
426 EXPECT_EQ(buf.
offset(), 0u);
427 EXPECT_EQ(buf.
size(), 16u);
431TEST(buffer, prepend_alloc_fail)
437 EXPECT_EQ(buf.
size(), 11u);
441TEST(buffer, prepend_alloc_fail2)
447 EXPECT_EQ(buf.
size(), 11u);
461 EXPECT_EQ(buf[0],
' ');
462 EXPECT_EQ(buf[5],
'd');
464 EXPECT_EQ(buf.
size(), 6u);
478 EXPECT_EQ(buf[0],
'h');
479 EXPECT_EQ(buf.
size(), 11u);
492 EXPECT_EQ(buf[0],
'h');
493 EXPECT_EQ(buf.
size(), 11u);
494 EXPECT_EQ(buf.
offset(), 5u);
502 EXPECT_EQ(buf.
offset(), 7u);
506 EXPECT_EQ(buf[0],
'h');
507 EXPECT_EQ(buf.
offset(), 0);
531TEST(buffer, invariants_after_move_safe)
539 EXPECT_EQ(buf.
size(), 0u);
545 EXPECT_EQ(buf.
c_data(),
nullptr);
549 EXPECT_EQ(buf.
data(),
nullptr);
553 EXPECT_EQ(buf.
offset(), 0u);
558TEST(buffer, push_back_after_move_safe)
568 EXPECT_EQ(buf2.
size(), 11u);
570 EXPECT_EQ(buf2[0],
'h');
572 EXPECT_EQ(buf2[10],
'd');
574 EXPECT_EQ(buf[0],
'X');
577TEST(buffer, append_after_move_safe)
588 EXPECT_EQ(buf2.
size(), 11u);
590 EXPECT_EQ(buf2[0],
'h');
592 EXPECT_EQ(buf2[10],
'd');
594 EXPECT_EQ(buf, buf3);
void realloc(const size_t newcap)
Reallocates the buffer to the specified new capacity.
BufferAllocatedType & realign(const size_t headroom)
Realign the buffer with the specified headroom.
report various types of exceptions or errors that may occur when working with buffers
const T * c_data() const
Returns a const pointer to the start of the buffer.
T * prepend_alloc(const size_t size)
Allocate space for prepending data to the buffer.
void init_headroom(const size_t headroom)
Initializes the headroom (offset) of the buffer.
T front() const
Returns the first element of the buffer.
void push_back(const T &value)
Append a T object to the end of the array, resizing the array if necessary.
T back() const
Returns the last element of the buffer.
T * write_alloc(const size_t size)
Allocate space for writing data to the buffer.
size_t capacity() const
Returns the capacity (raw size) of the allocated buffer in T objects.
size_t size() const
Returns the size of the buffer in T objects.
T * data()
Get a mutable pointer to the start of the array.
T pop_back()
Removes and returns the last element from the buffer.
void advance(const size_t delta)
Advances the buffer by the specified delta.
bool empty() const
Returns true if the buffer is empty.
const T * c_data_raw() const
Returns a const pointer to the start of the raw data in the buffer.
auto * read_alloc(const size_t size)
Allocate memory and read data from the buffer into the allocated memory.
T pop_front()
Removes and returns the first element from the buffer.
size_t remaining(const size_t tailroom=0) const
Return the number of additional T objects that can be added before capacity is reached (without consi...
T * data_raw()
Get a mutable pointer to the start of the raw data.
size_t offset() const
Returns the current offset (headroom) into the buffer.
void reset_offset(const size_t offset)
Resets the offset of the buffer.
void reset_size()
Resets the size of the buffer to zero.
void read(NCT *data, const size_t size)
Read data from the buffer into the specified memory location.
constexpr BufferFlags CONSTRUCT_ZERO(1u<< 0)
if enabled, constructors/init will zero allocated space
constexpr BufferFlags DESTRUCT_ZERO(1u<< 1)
if enabled, destructor will zero data before deletion
ConstBufferType< T > & const_buffer_ref(BufferType< T > &src)
BufferAllocatedType< unsigned char > BufferAllocated
void buf_append_string(Buffer &buf, const std::string &str)
std::string buf_to_string(const Buffer &buf)
TEST(buffer, buffer_alignas)
void realign_test(BufferAllocated &buf, std::size_t headroom)