]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/core/static_buffer.cpp
9a8b55a7228cb90d4f67a0b0ddff7268c81866dd
[ceph.git] / ceph / src / boost / libs / beast / test / beast / core / static_buffer.cpp
1 //
2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/beast/core/static_buffer.hpp>
12
13 #include "test_buffer.hpp"
14
15 #include <boost/beast/core/buffer_traits.hpp>
16 #include <boost/beast/core/ostream.hpp>
17 #include <boost/beast/core/read_size.hpp>
18 #include <boost/beast/core/string.hpp>
19 #include <boost/beast/_experimental/unit_test/suite.hpp>
20 #include <boost/asio/buffers_iterator.hpp>
21 #include <algorithm>
22 #include <cctype>
23 #include <string>
24
25 namespace boost {
26 namespace beast {
27
28 class static_buffer_test : public beast::unit_test::suite
29 {
30 public:
31 BOOST_STATIC_ASSERT(
32 is_mutable_dynamic_buffer<
33 static_buffer<13>>::value);
34
35 BOOST_STATIC_ASSERT(
36 is_mutable_dynamic_buffer<
37 static_buffer_base>::value);
38
39 void
40 testDynamicBuffer()
41 {
42 test_dynamic_buffer(static_buffer<13>{});
43 }
44
45 void
46 testMembers()
47 {
48 string_view const s = "Hello, world!";
49
50 // static_buffer_base
51 {
52 char buf[64];
53 static_buffer_base b{buf, sizeof(buf)};
54 ostream(b) << s;
55 BEAST_EXPECT(buffers_to_string(b.data()) == s);
56 b.clear();
57 BEAST_EXPECT(b.size() == 0);
58 BEAST_EXPECT(buffer_bytes(b.data()) == 0);
59 }
60
61 // static_buffer
62 {
63 static_buffer<64> b1;
64 BEAST_EXPECT(b1.size() == 0);
65 BEAST_EXPECT(b1.max_size() == 64);
66 BEAST_EXPECT(b1.capacity() == 64);
67 ostream(b1) << s;
68 BEAST_EXPECT(buffers_to_string(b1.data()) == s);
69 {
70 static_buffer<64> b2{b1};
71 BEAST_EXPECT(buffers_to_string(b2.data()) == s);
72 b2.consume(7);
73 BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
74 }
75 {
76 static_buffer<64> b2;
77 b2 = b1;
78 BEAST_EXPECT(buffers_to_string(b2.data()) == s);
79 b2.consume(7);
80 BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7));
81 }
82 }
83
84 // cause memmove
85 {
86 static_buffer<10> b;
87 ostream(b) << "12345";
88 b.consume(3);
89 ostream(b) << "67890123";
90 BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123");
91 try
92 {
93 b.prepare(1);
94 fail("", __FILE__, __LINE__);
95 }
96 catch(std::length_error const&)
97 {
98 pass();
99 }
100 }
101
102 // read_size
103 {
104 static_buffer<10> b;
105 BEAST_EXPECT(read_size(b, 512) == 10);
106 b.prepare(4);
107 b.commit(4);
108 BEAST_EXPECT(read_size(b, 512) == 6);
109 b.consume(2);
110 BEAST_EXPECT(read_size(b, 512) == 8);
111 b.prepare(8);
112 b.commit(8);
113 BEAST_EXPECT(read_size(b, 512) == 0);
114 }
115
116 // base
117 {
118 static_buffer<10> b;
119 [&](static_buffer_base& base)
120 {
121 BEAST_EXPECT(base.max_size() == b.capacity());
122 }
123 (b.base());
124
125 [&](static_buffer_base const& base)
126 {
127 BEAST_EXPECT(base.max_size() == b.capacity());
128 }
129 (b.base());
130 }
131
132 // This exercises the wrap-around cases
133 // for the circular buffer representation
134 {
135 static_buffer<5> b;
136 {
137 auto const mb = b.prepare(5);
138 BEAST_EXPECT(buffers_length(mb) == 1);
139 }
140 b.commit(4);
141 BEAST_EXPECT(buffers_length(b.data()) == 1);
142 BEAST_EXPECT(buffers_length(b.cdata()) == 1);
143 b.consume(3);
144 {
145 auto const mb = b.prepare(3);
146 BEAST_EXPECT(buffers_length(mb) == 2);
147 auto it1 = mb.begin();
148 auto it2 = std::next(it1);
149 BEAST_EXPECT(
150 net::const_buffer(*it1).data() >
151 net::const_buffer(*it2).data());
152 }
153 b.commit(2);
154 {
155 auto const mb = b.data();
156 auto it1 = mb.begin();
157 auto it2 = std::next(it1);
158 BEAST_EXPECT(
159 net::const_buffer(*it1).data() >
160 net::const_buffer(*it2).data());
161 }
162 {
163 auto const cb = b.cdata();
164 auto it1 = cb.begin();
165 auto it2 = std::next(it1);
166 BEAST_EXPECT(
167 net::const_buffer(*it1).data() >
168 net::const_buffer(*it2).data());
169 }
170 }
171 }
172
173 void
174 run() override
175 {
176 testDynamicBuffer();
177 testMembers();
178 }
179 };
180
181 BEAST_DEFINE_TESTSUITE(beast,core,static_buffer);
182
183 } // beast
184 } // boost