Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Buffer.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Buffer.hpp"
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std;
22using namespace toolbox;
23
24namespace {
25
26void write(Buffer& buf, const char* data)
27{
28 const auto len = strlen(data);
29 const auto out = buf.prepare(len);
31 buf.commit(len);
32}
33
34string read(Buffer& buf)
35{
36 const auto in = buf.data();
37 const string s{buffer_cast<const char*>(in), buffer_size(in)};
38 buf.consume(buffer_size(in));
39 return s;
40}
41
42string read(Buffer& buf, std::size_t limit)
43{
44 const auto in = buf.data(limit);
45 const string s{buffer_cast<const char*>(in), buffer_size(in)};
46 buf.consume(buffer_size(in));
47 return s;
48}
49
50} // namespace
51
53
55{
56 Buffer buf;
57 BOOST_CHECK(buf.empty());
58 BOOST_CHECK_EQUAL(buf.size(), 0U);
59 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 0U);
60
61 write(buf, "foo");
62 BOOST_CHECK(!buf.empty());
63 BOOST_CHECK_EQUAL(buf.size(), 3U);
64 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 3U);
66
67 write(buf, "bar");
68 BOOST_CHECK(!buf.empty());
69 BOOST_CHECK_EQUAL(buf.size(), 6U);
70 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 6U);
71 BOOST_CHECK_EQUAL(memcmp(buffer_cast<const char*>(buf.data()), "foobar", 6), 0);
72
73 BOOST_CHECK_EQUAL(read(buf, 4), "foob");
74 BOOST_CHECK(!buf.empty());
75 BOOST_CHECK_EQUAL(buf.size(), 2U);
76 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 2U);
78
79 write(buf, "baz");
80 BOOST_CHECK(!buf.empty());
81 BOOST_CHECK_EQUAL(buf.size(), 5U);
82 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 5U);
84
85 BOOST_CHECK_EQUAL(read(buf), "arbaz");
86 BOOST_CHECK(buf.empty());
87 BOOST_CHECK_EQUAL(buf.size(), 0U);
88 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 0U);
89}
90
92{
93 Buffer buf;
94
95 // Avoid vector reallocation.
96 buf.reserve(4096);
97 // Base address.
98 const auto* base = buffer_cast<const char*>(buf.data());
99
100 // Simulated write.
101 buf.prepare(16);
102 buf.commit(16);
103 BOOST_CHECK(!buf.empty());
104 BOOST_CHECK_EQUAL(buf.size(), 16U);
105 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 16U);
107
108 // Consume less than half.
109 buf.consume(7);
110 BOOST_CHECK(!buf.empty());
111 BOOST_CHECK_EQUAL(buf.size(), 9U);
112 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 9U);
114
115 // Consuming one more should not trigger shrink, because the buffer size is less than the shrink
116 // threshold.
117 buf.consume(1);
118 BOOST_CHECK(!buf.empty());
119 BOOST_CHECK_EQUAL(buf.size(), 8U);
120 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 8U);
122}
123
125{
126 Buffer buf;
127
128 // Avoid vector reallocation.
129 buf.reserve(4096);
130 // Base address.
131 const auto* base = buffer_cast<const char*>(buf.data());
132
133 // Simulated write.
134 buf.prepare(2048);
135 buf.commit(2048);
136 BOOST_CHECK(!buf.empty());
137 BOOST_CHECK_EQUAL(buf.size(), 2048U);
138 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 2048U);
140
141 // Consume less than half.
142 buf.consume(1023);
143 BOOST_CHECK(!buf.empty());
144 BOOST_CHECK_EQUAL(buf.size(), 1025U);
145 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 1025U);
147
148 // Consuming one more should not trigger shrink, because the buffer size is less than the shrink
149 // threshold.
150 buf.consume(1);
151 BOOST_CHECK(!buf.empty());
152 BOOST_CHECK_EQUAL(buf.size(), 1024U);
153 BOOST_CHECK_EQUAL(buffer_size(buf.data()), 1024U);
155}
156
BOOST_AUTO_TEST_CASE(ReadWriteCase)
Definition Buffer.ut.cpp:54
BOOST_CHECK_EQUAL(v.size(), 10U)
void reserve(std::size_t capacity)
Reserve storage.
Definition Buffer.hpp:98
STL namespace.
const DataT & data(const MsgEvent &ev) noexcept
Definition Event.hpp:54
ssize_t read(int fd, void *buf, std::size_t len, std::error_code &ec) noexcept
Read from a file descriptor.
Definition File.hpp:146
ssize_t write(int fd, const void *buf, std::size_t len, std::error_code &ec) noexcept
Write to a file descriptor.
Definition File.hpp:178
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))