diff --git a/CHANGELOG.md b/CHANGELOG.md index 39378647..05752ea1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Use static_string for WebSocket handshakes * Simplify get_lowest_layer test * Add test_allocator to extras/test +* More flat_streambuf tests -------------------------------------------------------------------------------- diff --git a/extras/beast/test/test_allocator.hpp b/extras/beast/test/test_allocator.hpp index 851b0712..13bd7019 100644 --- a/extras/beast/test/test_allocator.hpp +++ b/extras/beast/test/test_allocator.hpp @@ -23,45 +23,50 @@ struct test_allocator_info }; template + bool Equal, bool Assign, bool Move, bool Swap, bool Select> class test_allocator; template + bool Equal, bool Assign, bool Move, bool Swap, bool Select> struct test_allocator_base { }; template -struct test_allocator_base + bool Equal, bool Assign, bool Move, bool Swap> +struct test_allocator_base< + T, Equal, Assign, Move, Swap, true> { static - test_allocator - select_on_container_copy_construction( - test_allocator const& a) + test_allocator + select_on_container_copy_construction(test_allocator< + T, Equal, Assign, Move, Swap, true> const& a) { - return test_allocator{}; + return test_allocator{}; } }; template + bool Equal, bool Assign, bool Move, bool Swap, bool Select> class test_allocator : public test_allocator_base< - T, Assign, Move, Swap, Select> + T, Equal, Assign, Move, Swap, Select> { std::size_t id_; std::shared_ptr info_; - template + template friend class test_allocator; public: using value_type = T; + using propagate_on_container_copy_assignment = std::integral_constant; + using propagate_on_container_move_assignment = std::integral_constant; + using propagate_on_container_swap = std::integral_constant; @@ -69,7 +74,7 @@ public: struct rebind { using other = test_allocator< - U, Assign, Move, Swap, Select>; + U, Equal, Assign, Move, Swap, Select>; }; test_allocator() @@ -91,8 +96,8 @@ public: } template - test_allocator(test_allocator< - U, Assign, Move, Swap, Select> const& u) noexcept + test_allocator(test_allocator const& u) noexcept : id_(u.id_) , info_(u.info_) { @@ -119,6 +124,18 @@ public: ::operator delete(p); } + bool + operator==(test_allocator const& other) const + { + return id_ == other.id_ || Equal; + } + + bool + operator!=(test_allocator const& other) const + { + return ! this->operator==(other); + } + std::size_t id() const { diff --git a/test/core/flat_streambuf.cpp b/test/core/flat_streambuf.cpp index c6c034aa..77d79000 100644 --- a/test/core/flat_streambuf.cpp +++ b/test/core/flat_streambuf.cpp @@ -10,7 +10,9 @@ #include "buffer_test.hpp" #include +#include #include +#include namespace beast { @@ -29,6 +31,121 @@ public: return to_string(sb1.data()) == to_string(sb2.data()); } + template< + bool Equal, bool Assign, bool Move, bool Swap, bool Select> + void + testCtor() + { + using allocator = test::test_allocator; + { + using boost::asio::buffer_size; + basic_flat_streambuf b1{10}; + BEAST_EXPECT(b1.size() == 0); + BEAST_EXPECT(b1.capacity() == 0); + BEAST_EXPECT(b1.max_size() == 10); + b1.prepare(1); + b1.commit(1); + basic_flat_streambuf b2{std::move(b1)}; + BEAST_EXPECT(b1.capacity() == 0); + BEAST_EXPECT(b1.max_size() == 10); + BEAST_EXPECT(b2.size() == 1); + BEAST_EXPECT(b2.max_size() == 10); + BEAST_EXPECT(buffer_size(b1.data()) == 0); + BEAST_EXPECT(buffer_size(b1.prepare(1)) == 1); + } + { + basic_flat_streambuf b1{10}; + basic_flat_streambuf b2{std::move(b1), allocator{}}; + } + { + basic_flat_streambuf b1{10}; + basic_flat_streambuf b2{b1}; + } + { + basic_flat_streambuf b1{10}; + basic_flat_streambuf b2{b1, allocator{}}; + } + { + flat_streambuf b1{10}; + b1.prepare(1); + b1.commit(1); + basic_flat_streambuf b2{b1}; + BEAST_EXPECT(b2.size() == 1); + } + { + basic_flat_streambuf b1{10}; + } + { + basic_flat_streambuf b1{allocator{}, 10}; + } + } + + void + testCtors() + { + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor(); + testCtor< true, false, false, false, false>(); + testCtor< true, false, false, false, true>(); + testCtor< true, false, false, true, false>(); + testCtor< true, false, false, true, true>(); + testCtor< true, false, true, false, false>(); + testCtor< true, false, true, false, true>(); + testCtor< true, false, true, true, false>(); + testCtor< true, false, true, true, true>(); + testCtor< true, true, false, false, false>(); + testCtor< true, true, false, false, true>(); + testCtor< true, true, false, true, false>(); + testCtor< true, true, false, true, true>(); + testCtor< true, true, true, false, false>(); + testCtor< true, true, true, false, true>(); + testCtor< true, true, true, true, false>(); + testCtor< true, true, true, true, true>(); + } + + void + testOperations() + { + // + // reserve + // + + { + flat_streambuf b{10}; + b.prepare(1); + b.commit(1); + b.reserve(2); + BEAST_EXPECT(b.size() == 1); + } + { + flat_streambuf b{10}; + try + { + b.reserve(11); + fail("", __FILE__, __LINE__); + } + catch(std::length_error const&) + { + pass(); + } + } + } + void testSpecialMembers() { @@ -159,6 +276,9 @@ public: void run() override { + testCtors(); + testOperations(); + testSpecialMembers(); testStream(); testPrepare(); diff --git a/test/core/streambuf.cpp b/test/core/streambuf.cpp index 6d110cbc..2d3440de 100644 --- a/test/core/streambuf.cpp +++ b/test/core/streambuf.cpp @@ -109,14 +109,14 @@ public: // VFALCO This needs work { using alloc_type = - test_allocator; + test_allocator; using sb_type = basic_streambuf; sb_type sb; BEAST_EXPECT(sb.get_allocator().id() == 1); } { using alloc_type = - test_allocator; + test_allocator; using sb_type = basic_streambuf; sb_type sb; BEAST_EXPECT(sb.get_allocator().id() == 2);