Brian Silverman | fad8f55 | 2018-08-04 23:36:19 -0700 | [diff] [blame^] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost |
| 4 | // Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // See http://www.boost.org/libs/container for documentation. |
| 8 | // |
| 9 | ////////////////////////////////////////////////////////////////////////////// |
| 10 | #include <boost/container/pmr/resource_adaptor.hpp> |
| 11 | #include <boost/core/lightweight_test.hpp> |
| 12 | #include "propagation_test_allocator.hpp" |
| 13 | #include "derived_from_memory_resource.hpp" |
| 14 | |
| 15 | using namespace boost::container::pmr; |
| 16 | |
| 17 | void test_default_constructor() |
| 18 | { |
| 19 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 20 | resource_adaptor<alloc_t> ra; |
| 21 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 22 | } |
| 23 | |
| 24 | void test_copy_constructor() |
| 25 | { |
| 26 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 27 | resource_adaptor<alloc_t> ra; |
| 28 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 29 | resource_adaptor<alloc_t> rb(ra); |
| 30 | BOOST_TEST(rb.get_allocator().m_default_contructed == false); |
| 31 | BOOST_TEST(rb.get_allocator().m_move_contructed == false); |
| 32 | } |
| 33 | |
| 34 | void test_move_constructor() |
| 35 | { |
| 36 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 37 | resource_adaptor<alloc_t> ra; |
| 38 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 39 | resource_adaptor<alloc_t> rb(::boost::move(ra)); |
| 40 | BOOST_TEST(rb.get_allocator().m_default_contructed == false); |
| 41 | BOOST_TEST(rb.get_allocator().m_move_contructed == true); |
| 42 | } |
| 43 | |
| 44 | void test_lvalue_alloc_constructor() |
| 45 | { |
| 46 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 47 | alloc_t a; |
| 48 | resource_adaptor<alloc_t> ra(a); |
| 49 | BOOST_TEST(ra.get_allocator().m_default_contructed == false); |
| 50 | BOOST_TEST(ra.get_allocator().m_move_contructed == false); |
| 51 | } |
| 52 | |
| 53 | void test_rvalue_alloc_constructor() |
| 54 | { |
| 55 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 56 | alloc_t a; |
| 57 | resource_adaptor<alloc_t> ra(::boost::move(a)); |
| 58 | BOOST_TEST(ra.get_allocator().m_default_contructed == false); |
| 59 | BOOST_TEST(ra.get_allocator().m_move_contructed == true); |
| 60 | } |
| 61 | |
| 62 | void test_copy_assign() |
| 63 | { |
| 64 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 65 | resource_adaptor<alloc_t> ra; |
| 66 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 67 | resource_adaptor<alloc_t> rb; |
| 68 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 69 | rb = ra; |
| 70 | BOOST_TEST(rb.get_allocator().m_move_contructed == false); |
| 71 | BOOST_TEST(rb.get_allocator().m_move_assigned == false); |
| 72 | } |
| 73 | |
| 74 | void test_move_assign() |
| 75 | { |
| 76 | typedef propagation_test_allocator<char, 0> alloc_t; |
| 77 | resource_adaptor<alloc_t> ra; |
| 78 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 79 | resource_adaptor<alloc_t> rb; |
| 80 | BOOST_TEST(ra.get_allocator().m_default_contructed == true); |
| 81 | rb = ::boost::move(ra); |
| 82 | BOOST_TEST(rb.get_allocator().m_move_contructed == false); |
| 83 | BOOST_TEST(rb.get_allocator().m_move_assigned == true); |
| 84 | } |
| 85 | |
| 86 | struct stateful |
| 87 | { |
| 88 | public: |
| 89 | typedef char value_type; |
| 90 | |
| 91 | template<class U> |
| 92 | struct rebind |
| 93 | { typedef stateful other; }; |
| 94 | |
| 95 | stateful() |
| 96 | : m_u(0u) |
| 97 | {} |
| 98 | |
| 99 | char *allocate(std::size_t n) |
| 100 | { allocate_size = n; return allocate_return; } |
| 101 | |
| 102 | void deallocate(char *p, std::size_t n) |
| 103 | { deallocate_p = p; deallocate_size = n; } |
| 104 | |
| 105 | friend bool operator==(const stateful &l, const stateful &r) |
| 106 | { return l.m_u == r.m_u; } |
| 107 | |
| 108 | friend bool operator!=(const stateful &l, const stateful &r) |
| 109 | { return l.m_u != r.m_u; } |
| 110 | |
| 111 | public: |
| 112 | unsigned m_u; |
| 113 | std::size_t allocate_size; |
| 114 | char *allocate_return; |
| 115 | std::size_t deallocate_size; |
| 116 | char *deallocate_p; |
| 117 | }; |
| 118 | |
| 119 | void test_get_allocator() |
| 120 | { |
| 121 | stateful a; |
| 122 | a.m_u = 999; |
| 123 | resource_adaptor<stateful> ra(a); |
| 124 | const resource_adaptor<stateful> & cra = ra; |
| 125 | BOOST_TEST( ra.get_allocator().m_u == 999); |
| 126 | BOOST_TEST(cra.get_allocator().m_u == 999); |
| 127 | } |
| 128 | |
| 129 | typedef resource_adaptor<stateful> stateful_resource_adaptor_t; |
| 130 | |
| 131 | struct derived_from_resource_adaptor_stateful |
| 132 | : public stateful_resource_adaptor_t |
| 133 | { |
| 134 | public: |
| 135 | typedef stateful_resource_adaptor_t base_t; |
| 136 | using base_t::do_allocate; |
| 137 | using base_t::do_deallocate; |
| 138 | using base_t::do_is_equal; |
| 139 | }; |
| 140 | |
| 141 | void test_do_allocate() |
| 142 | { |
| 143 | derived_from_resource_adaptor_stateful dra; |
| 144 | char dummy = 0; |
| 145 | dra.get_allocator().allocate_return = &dummy; |
| 146 | void *allocate_ret = dra.do_allocate(998, 1234); |
| 147 | BOOST_TEST(allocate_ret == &dummy); |
| 148 | BOOST_TEST(dra.get_allocator().allocate_size == 998); |
| 149 | } |
| 150 | |
| 151 | void test_do_deallocate() |
| 152 | { |
| 153 | derived_from_resource_adaptor_stateful dra; |
| 154 | char dummy = 0; |
| 155 | dra.do_deallocate(&dummy, 1234, 753); |
| 156 | BOOST_TEST(dra.get_allocator().deallocate_p == &dummy); |
| 157 | BOOST_TEST(dra.get_allocator().deallocate_size == 1234); |
| 158 | } |
| 159 | |
| 160 | void test_do_is_equal() |
| 161 | { |
| 162 | derived_from_resource_adaptor_stateful dra; |
| 163 | derived_from_memory_resource dmr; |
| 164 | //Different dynamic type must return false |
| 165 | BOOST_TEST(dra.do_is_equal(dmr) == false); |
| 166 | |
| 167 | //Same dynamic type with same state must return true |
| 168 | derived_from_resource_adaptor_stateful dra2; |
| 169 | BOOST_TEST(dra.do_is_equal(dra2) == true); |
| 170 | |
| 171 | //Same dynamic type with different state must return false |
| 172 | dra2.get_allocator().m_u = 1234; |
| 173 | BOOST_TEST(dra.do_is_equal(dra2) == false); |
| 174 | } |
| 175 | |
| 176 | int main() |
| 177 | { |
| 178 | test_default_constructor(); |
| 179 | test_copy_constructor(); |
| 180 | test_move_constructor(); |
| 181 | test_lvalue_alloc_constructor(); |
| 182 | test_rvalue_alloc_constructor(); |
| 183 | test_copy_assign(); |
| 184 | test_move_assign(); |
| 185 | test_get_allocator(); |
| 186 | test_do_allocate(); |
| 187 | test_do_deallocate(); |
| 188 | test_do_is_equal(); |
| 189 | return ::boost::report_errors(); |
| 190 | } |