1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
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"
15 using namespace boost::container::pmr
;
17 void test_default_constructor()
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);
24 void test_copy_constructor()
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);
34 void test_move_constructor()
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);
44 void test_lvalue_alloc_constructor()
46 typedef propagation_test_allocator
<char, 0> alloc_t
;
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);
53 void test_rvalue_alloc_constructor()
55 typedef propagation_test_allocator
<char, 0> alloc_t
;
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);
62 void test_copy_assign()
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);
70 BOOST_TEST(rb
.get_allocator().m_move_contructed
== false);
71 BOOST_TEST(rb
.get_allocator().m_move_assigned
== false);
74 void test_move_assign()
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);
89 typedef char value_type
;
93 { typedef stateful other
; };
99 char *allocate(std::size_t n
)
100 { allocate_size
= n
; return allocate_return
; }
102 void deallocate(char *p
, std::size_t n
)
103 { deallocate_p
= p
; deallocate_size
= n
; }
105 friend bool operator==(const stateful
&l
, const stateful
&r
)
106 { return l
.m_u
== r
.m_u
; }
108 friend bool operator!=(const stateful
&l
, const stateful
&r
)
109 { return l
.m_u
!= r
.m_u
; }
113 std::size_t allocate_size
;
114 char *allocate_return
;
115 std::size_t deallocate_size
;
119 void test_get_allocator()
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);
129 typedef resource_adaptor
<stateful
> stateful_resource_adaptor_t
;
131 struct derived_from_resource_adaptor_stateful
132 : public stateful_resource_adaptor_t
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
;
141 void test_do_allocate()
143 derived_from_resource_adaptor_stateful dra
;
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);
151 void test_do_deallocate()
153 derived_from_resource_adaptor_stateful dra
;
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);
160 void test_do_is_equal()
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);
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);
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);
178 test_default_constructor();
179 test_copy_constructor();
180 test_move_constructor();
181 test_lvalue_alloc_constructor();
182 test_rvalue_alloc_constructor();
185 test_get_allocator();
187 test_do_deallocate();
189 return ::boost::report_errors();