22 { std::array<boost::asio::const_buffer, 3u> s; buffer.
copy_from(s); }
23 { std::array<boost::asio::mutable_buffer, 3u> s; buffer.
copy_from(s); }
24 { std::vector<boost::asio::const_buffer> s; buffer.
copy_from(s); }
25 { std::vector<boost::asio::mutable_buffer> s; buffer.
copy_from(s); }
26 { std::list<boost::asio::const_buffer> s; buffer.
copy_from(s); }
27 { std::list<boost::asio::mutable_buffer> s; buffer.
copy_from(s); }
28 { std::set<boost::asio::const_buffer> s; buffer.
copy_from(s); }
29 { std::set<boost::asio::mutable_buffer> s; buffer.
copy_from(s); }
31 { boost::asio::const_buffer v; buffer.
copy_from(v); }
32 { boost::asio::mutable_buffer v; buffer.
copy_from(v); }
34 { std::vector<int> v; buffer.
copy_from(v); }
37 {
struct C {
int x = 0; } v[3u]; buffer.
copy_from(v); }
38 {
struct C {
int x = 0; }; std::array<C, 3u> v; buffer.
copy_from(v); }
39 {
struct C {
int x = 0; }; std::vector<C> v; buffer.
copy_from(v); }
42 TEST(buffer, copy_buffer_sequence) {
43 constexpr
auto number_of_buffers = 15u;
44 const std::string str =
"WXdI<x->+<If$ua>$pu1AUBmS]?_PT{3z$B7L(E|?$]";
46 std::array<Buffer, number_of_buffers> buffers;
47 std::array<boost::asio::const_buffer, number_of_buffers> sequence;
48 for (
auto i = 0u; i < number_of_buffers; ++i) {
50 buffers[i].copy_from(str);
51 sequence[i] = buffers[i].buffer();
53 auto result =
Buffer(sequence);
54 ASSERT_EQ(result.size(), message.size());
56 ASSERT_EQ(result_str, message);
59 TEST(buffer, to_from_string) {
60 const std::string str =
"The quick brown fox jumps over the lazy dog";
62 ASSERT_EQ(buffer.
size(), str.size());
63 const std::string result =
as_string(buffer);
64 ASSERT_EQ(result, str);
67 TEST(buffer, to_from_vector) {
68 constexpr
auto size = 1000u;
72 for (
auto i = 0u; i < size; ++i) {
76 ASSERT_EQ(buffer.
size(),
sizeof(T) * size);
77 auto begin =
reinterpret_cast<const T *
>(buffer.
data());
78 std::vector<T> result(begin, begin + size);
86 ASSERT_EQ(msg->size(), cpy->size());
87 ASSERT_EQ(*cpy, *msg);
90 TEST(buffer, copy_with_offset) {
91 const char str0[] =
"Hello";
92 const char str1[] =
"buffer!";
97 reinterpret_cast<const unsigned char *>(&str1),
99 std::memcpy(buffer.
data(), str0, std::strlen(str0));
100 buffer[std::strlen(str0)] =
' ';
101 auto str = std::string(str0) +
" " + str1;
102 ASSERT_EQ(buffer.size(), str.size());
103 ASSERT_EQ(
as_string(buffer), str.c_str());
109 ASSERT_EQ(msg->size(), cpy->size());
110 auto buffer = cpy->buffer();
111 std::memcpy(buffer.data(), msg->data(), buffer.size());
112 ASSERT_EQ(*cpy, *msg);
115 #ifndef LIBCARLA_NO_EXCEPTIONS 116 TEST(buffer, message_too_big) {
117 ASSERT_THROW(
Buffer(4294967296ul), std::invalid_argument);
119 ASSERT_THROW(buf.
reset(4294967296ul), std::invalid_argument);
121 #endif // LIBCARLA_NO_EXCEPTIONS 124 const std::string str =
"Hello buffer!";
125 auto pool = std::make_shared<carla::BufferPool>();
127 auto buff = pool->Pop();
130 auto buff1 = pool->Pop();
132 auto buff2 = pool->Pop();
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
static shared_buffer make_empty(size_t size=0u)
static std::string as_string(const Buffer &buf)
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
void reset(size_type size)
Reset the size of this buffer.
shared_buffer make_random(size_t size)
size_type size() const noexcept
void copy_from(const T &source)
Copy source into this buffer. Allocates memory if necessary.