]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
92f5a8d4 | 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
7c673cae FG |
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 | // | |
b32b8144 FG |
7 | // Official repository: https://github.com/boostorg/beast |
8 | // | |
7c673cae FG |
9 | |
10 | // Test that header file is self-contained. | |
b32b8144 FG |
11 | #include <boost/beast/core/flat_static_buffer.hpp> |
12 | ||
92f5a8d4 | 13 | #include "test_buffer.hpp" |
7c673cae | 14 | |
92f5a8d4 | 15 | #include <boost/beast/core/buffer_traits.hpp> |
b32b8144 | 16 | #include <boost/beast/core/ostream.hpp> |
92f5a8d4 | 17 | #include <boost/beast/core/read_size.hpp> |
b32b8144 | 18 | #include <boost/beast/core/string.hpp> |
92f5a8d4 TL |
19 | #include <boost/beast/_experimental/unit_test/suite.hpp> |
20 | #include <boost/asio/buffers_iterator.hpp> | |
21 | #include <algorithm> | |
22 | #include <cctype> | |
7c673cae FG |
23 | #include <string> |
24 | ||
b32b8144 | 25 | namespace boost { |
7c673cae FG |
26 | namespace beast { |
27 | ||
b32b8144 | 28 | class flat_static_buffer_test : public beast::unit_test::suite |
7c673cae FG |
29 | { |
30 | public: | |
92f5a8d4 TL |
31 | BOOST_STATIC_ASSERT( |
32 | is_mutable_dynamic_buffer< | |
33 | flat_static_buffer<13>>::value); | |
34 | ||
b32b8144 | 35 | void |
92f5a8d4 | 36 | testDynamicBuffer() |
7c673cae | 37 | { |
92f5a8d4 | 38 | test_dynamic_buffer(flat_static_buffer<13>{}); |
7c673cae FG |
39 | } |
40 | ||
b32b8144 | 41 | void |
92f5a8d4 | 42 | testMembers() |
7c673cae | 43 | { |
b32b8144 FG |
44 | string_view const s = "Hello, world!"; |
45 | ||
46 | // flat_static_buffer_base | |
47 | { | |
48 | char buf[64]; | |
49 | flat_static_buffer_base b{buf, sizeof(buf)}; | |
50 | ostream(b) << s; | |
92f5a8d4 TL |
51 | BEAST_EXPECT(buffers_to_string(b.data()) == s); |
52 | b.clear(); | |
53 | BEAST_EXPECT(b.size() == 0); | |
54 | BEAST_EXPECT(buffer_bytes(b.data()) == 0); | |
b32b8144 FG |
55 | } |
56 | ||
57 | // flat_static_buffer | |
58 | { | |
59 | flat_static_buffer<64> b1; | |
60 | BEAST_EXPECT(b1.size() == 0); | |
61 | BEAST_EXPECT(b1.max_size() == 64); | |
62 | BEAST_EXPECT(b1.capacity() == 64); | |
63 | ostream(b1) << s; | |
92f5a8d4 | 64 | BEAST_EXPECT(buffers_to_string(b1.data()) == s); |
b32b8144 FG |
65 | { |
66 | flat_static_buffer<64> b2{b1}; | |
92f5a8d4 | 67 | BEAST_EXPECT(buffers_to_string(b2.data()) == s); |
b32b8144 | 68 | b2.consume(7); |
92f5a8d4 | 69 | BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7)); |
b32b8144 FG |
70 | } |
71 | { | |
72 | flat_static_buffer<64> b2; | |
73 | b2 = b1; | |
92f5a8d4 | 74 | BEAST_EXPECT(buffers_to_string(b2.data()) == s); |
b32b8144 | 75 | b2.consume(7); |
92f5a8d4 | 76 | BEAST_EXPECT(buffers_to_string(b2.data()) == s.substr(7)); |
b32b8144 FG |
77 | } |
78 | } | |
79 | ||
80 | // cause memmove | |
7c673cae | 81 | { |
b32b8144 | 82 | flat_static_buffer<10> b; |
92f5a8d4 | 83 | ostream(b) << "12345"; |
b32b8144 | 84 | b.consume(3); |
92f5a8d4 TL |
85 | ostream(b) << "67890123"; |
86 | BEAST_EXPECT(buffers_to_string(b.data()) == "4567890123"); | |
b32b8144 FG |
87 | try |
88 | { | |
89 | b.prepare(1); | |
90 | fail("", __FILE__, __LINE__); | |
91 | } | |
92 | catch(std::length_error const&) | |
93 | { | |
94 | pass(); | |
95 | } | |
96 | } | |
97 | ||
98 | // read_size | |
99 | { | |
100 | flat_static_buffer<10> b; | |
101 | BEAST_EXPECT(read_size(b, 512) == 10); | |
102 | b.prepare(4); | |
103 | b.commit(4); | |
104 | BEAST_EXPECT(read_size(b, 512) == 6); | |
105 | b.consume(2); | |
106 | BEAST_EXPECT(read_size(b, 512) == 8); | |
107 | b.prepare(8); | |
108 | b.commit(8); | |
109 | BEAST_EXPECT(read_size(b, 512) == 0); | |
110 | } | |
111 | ||
112 | // base | |
113 | { | |
114 | flat_static_buffer<10> b; | |
115 | [&](flat_static_buffer_base& base) | |
116 | { | |
117 | BEAST_EXPECT(base.max_size() == b.capacity()); | |
118 | } | |
119 | (b.base()); | |
120 | ||
121 | [&](flat_static_buffer_base const& base) | |
122 | { | |
123 | BEAST_EXPECT(base.max_size() == b.capacity()); | |
124 | } | |
125 | (b.base()); | |
7c673cae | 126 | } |
7c673cae FG |
127 | } |
128 | ||
129 | void run() override | |
130 | { | |
92f5a8d4 TL |
131 | testDynamicBuffer(); |
132 | testMembers(); | |
7c673cae FG |
133 | } |
134 | }; | |
135 | ||
b32b8144 | 136 | BEAST_DEFINE_TESTSUITE(beast,core,flat_static_buffer); |
7c673cae | 137 | |
b32b8144 FG |
138 | } // beast |
139 | } // boost |