CARLA
 
载入中...
搜索中...
未找到
test_buffer.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
9#include <carla/Buffer.h>
10#include <carla/BufferPool.h>
11
12#include <array>
13#include <list>
14#include <set>
15#include <string>
16#include <vector>
17
18using namespace util::buffer;
19
20TEST(buffer, compile) {
21 carla::Buffer buffer;
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); }
30
31 { boost::asio::const_buffer v; buffer.copy_from(v); }
32 { boost::asio::mutable_buffer v; buffer.copy_from(v); }
33 { int v[3u]; buffer.copy_from(v); }
34 { std::vector<int> v; buffer.copy_from(v); }
35 { std::string v; buffer.copy_from(v); }
36 { std::wstring 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); }
40}
41
42TEST(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|?$]";
45 std::string message;
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) {
49 message += str;
50 buffers[i].copy_from(str);
51 sequence[i] = buffers[i].buffer();
52 }
53 auto result = Buffer(sequence);
54 ASSERT_EQ(result.size(), message.size());
55 auto result_str = as_string(result);
56 ASSERT_EQ(result_str, message);
57}
58
59TEST(buffer, to_from_string) {
60 const std::string str = "The quick brown fox jumps over the lazy dog";
61 Buffer buffer(str);
62 ASSERT_EQ(buffer.size(), str.size());
63 const std::string result = as_string(buffer);
64 ASSERT_EQ(result, str);
65}
66
67TEST(buffer, to_from_vector) {
68 constexpr auto size = 1000u;
69 using T = size_t;
70 std::vector<T> v;
71 v.reserve(size);
72 for (auto i = 0u; i < size; ++i) {
73 v.emplace_back(i);
74 }
75 Buffer buffer(v);
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);
79 ASSERT_EQ(result, v);
80}
81
82TEST(buffer, copy) {
83 auto msg = make_random(1024u);
84 auto cpy = make_empty();
85 cpy->copy_from(*msg);
86 ASSERT_EQ(msg->size(), cpy->size());
87 ASSERT_EQ(*cpy, *msg);
88}
89
90TEST(buffer, copy_with_offset) {
91 const char str0[] = "Hello";
92 const char str1[] = "buffer!";
93 Buffer buffer;
94 auto offset = static_cast<Buffer::size_type>(sizeof(str0));
95 buffer.copy_from(
96 offset,
97 reinterpret_cast<const unsigned char *>(&str1),
98 std::strlen(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());
104}
105
106TEST(buffer, memcpy) {
107 auto msg = make_random(1024u);
108 auto cpy = make_empty(msg->size());
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);
113}
114
115#ifndef LIBCARLA_NO_EXCEPTIONS
116TEST(buffer, message_too_big) {
117 ASSERT_THROW(Buffer(4294967296ul), std::invalid_argument);
118 Buffer buf;
119 ASSERT_THROW(buf.reset(4294967296ul), std::invalid_argument);
120}
121#endif // LIBCARLA_NO_EXCEPTIONS
122
123TEST(buffer, buffer_pool) {
124 const std::string str = "Hello buffer!";
125 auto pool = std::make_shared<carla::BufferPool>();
126 {
127 auto buff = pool->Pop();
128 buff.copy_from(str);
129 }
130 auto buff1 = pool->Pop();
131 ASSERT_EQ(as_string(buff1), str);
132 auto buff2 = pool->Pop();
133 ASSERT_NE(as_string(buff2), str);
134 // Now delete the pool to test the weak reference inside the buffers.
135 pool.reset();
136}
A piece of raw data.
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
void reset(size_type size)
Reset the size of this buffer.
size_type size() const noexcept
void copy_from(const T &source)
Copy source into this buffer. Allocates memory if necessary.
uint32_t size_type
static std::string as_string(const Buffer &buf)
Definition test/Buffer.h:37
static shared_buffer make_empty(size_t size=0u)
Definition test/Buffer.h:24
shared_buffer make_random(size_t size)
TEST(buffer, compile)