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 <<
'\n';
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);
51 static unsigned char hello[] =
"hello world";
52 Buffer buf(hello,
sizeof(hello) - 1,
true);
54 EXPECT_EQ(cbuf.
size(), 11U);
64 EXPECT_EQ(cbuf.
size(), 11U);
72 static const char hello[] =
"hello world";
73 const 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");
81 constexpr char data[] =
"hello world";
83 EXPECT_EQ(cbuf[0],
'h');
84 EXPECT_EQ(cbuf[10],
'd');
91 constexpr char data[] =
"hello world";
94 while (cbuf.
empty() ==
false)
96 auto back = cbuf[cbuf.
size() - 1];
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');
201 EXPECT_EQ(buf[0],
'h');
202 EXPECT_EQ(buf[10],
'd');
213 EXPECT_EQ(buf[0],
'j');
214 EXPECT_EQ(buf[4],
'o');
222 char data[] =
"hello world";
226 auto items = buf1.
size();
233 EXPECT_EQ(buf[0],
'j');
234 EXPECT_EQ(buf[4],
'o');
259 }
while (buf.
front() !=
'd');
261 EXPECT_EQ(buf[0],
'd');
262 EXPECT_EQ(buf.
back(),
'd');
270 constexpr char data[] =
"hello world";
280 constexpr char data[] =
"hello world";
286 EXPECT_EQ(buf[0],
'd');
287 EXPECT_EQ(buf.
back(),
'd');
302 EXPECT_EQ(remaining - 1, buf.
remaining());
303 EXPECT_EQ(buf.
back(),
'X');
322 EXPECT_EQ(remaining - 1, buf.
remaining());
323 EXPECT_EQ(buf.
back(),
'X');
340 EXPECT_EQ(remaining - 1, buf.
remaining());
341 EXPECT_EQ(buf.
back(),
'X');
364 EXPECT_EQ(remaining - 1, buf.
remaining());
365 EXPECT_EQ(buf.
back(),
'X');
371 EXPECT_EQ(buf.
back(),
'X');
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);
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);
437 EXPECT_EQ(buf.
size(), 11U);
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);
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);
568 EXPECT_EQ(buf2.
size(), 11U);
570 EXPECT_EQ(buf2[0],
'h');
572 EXPECT_EQ(buf2[10],
'd');
574 EXPECT_EQ(buf[0],
'X');
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 DESTRUCT_ZERO(1U<< 1)
if enabled, destructor will zero data before deletion
constexpr BufferFlags CONSTRUCT_ZERO(1U<< 0)
if enabled, constructors/init will zero allocated space
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, BufferAlignas)
void realign_test(BufferAllocated &buf, std::size_t headroom)