| 1 | // Copyright (c) 2022 Klemens D. Morgenstern |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/beast/core/buffer_ref.hpp> |
| 7 | #include <boost/beast/core/flat_buffer.hpp> |
| 8 | #include <boost/beast/core/flat_static_buffer.hpp> |
| 9 | #include <boost/beast/core/multi_buffer.hpp> |
| 10 | #include <boost/beast/core/static_buffer.hpp> |
| 11 | #include <boost/beast/_experimental/unit_test/suite.hpp> |
| 12 | #include <boost/asio/io_context.hpp> |
| 13 | #include <boost/asio/readable_pipe.hpp> |
| 14 | #include <boost/asio/writable_pipe.hpp> |
| 15 | #include <boost/asio/connect_pipe.hpp> |
| 16 | #include <boost/asio/write.hpp> |
| 17 | #include <boost/asio/detached.hpp> |
| 18 | #include <boost/asio/read_until.hpp> |
| 19 | #include "test_buffer.hpp" |
| 20 | |
| 21 | namespace boost { |
| 22 | namespace beast { |
| 23 | |
| 24 | template struct buffer_ref<flat_buffer>; |
| 25 | template struct buffer_ref<flat_static_buffer<2u>>; |
| 26 | template struct buffer_ref<multi_buffer>; |
| 27 | template struct buffer_ref<static_buffer<2u>>; |
| 28 | |
| 29 | class buffer_ref_test : public beast::unit_test::suite |
| 30 | { |
| 31 | |
| 32 | template<typename Buffer> |
| 33 | void testBuffer() |
| 34 | { |
| 35 | net::io_context ioc; |
| 36 | net::readable_pipe rp{ioc}; |
| 37 | net::writable_pipe wp{ioc}; |
| 38 | net::connect_pipe(read_end&: rp, write_end&: wp); |
| 39 | |
| 40 | const char msg[] = "Hello, world!\n" ; |
| 41 | |
| 42 | net::async_write(s&: wp, buffers: net::buffer(data: msg), token: asio::detached); |
| 43 | |
| 44 | Buffer buf; |
| 45 | |
| 46 | net::async_read_until(rp, ref(buf), '\n', asio::detached); |
| 47 | ioc.run(); |
| 48 | // writable, not commited yet! |
| 49 | std::string cmp; |
| 50 | cmp.resize(n: sizeof(msg) -1); |
| 51 | const auto n = net::buffer_copy(net::buffer(data&: cmp), buf.data()); |
| 52 | BEAST_EXPECT(n >= std::strlen(msg)); |
| 53 | cmp.resize(n: 13); |
| 54 | BEAST_EXPECT(cmp == "Hello, world!" ); |
| 55 | |
| 56 | |
| 57 | buf = Buffer(); |
| 58 | test_dynamic_buffer_ref(ref(buf)); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | run() override |
| 63 | { |
| 64 | testBuffer<flat_buffer>(); |
| 65 | testBuffer<flat_static_buffer<1024u>>(); |
| 66 | testBuffer<multi_buffer>(); |
| 67 | testBuffer<static_buffer<1024u>>(); |
| 68 | |
| 69 | { |
| 70 | std::string buf; |
| 71 | test_dynamic_buffer_ref(b0: asio::dynamic_buffer(data&: buf)); |
| 72 | } |
| 73 | |
| 74 | { |
| 75 | std::vector<unsigned char> buf; |
| 76 | test_dynamic_buffer_ref(b0: asio::dynamic_buffer(data&: buf)); |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | BEAST_DEFINE_TESTSUITE(beast,core, buffer_ref); |
| 84 | |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |