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/polymorphic_allocator.hpp>
11 #include <boost/container/pmr/global_resource.hpp>
12 #include <boost/core/lightweight_test.hpp>
14 #include "derived_from_memory_resource.hpp"
15 #include "propagation_test_allocator.hpp"
17 using namespace boost::container::pmr
;
18 using namespace boost::container
;
20 void test_default_constructor()
22 polymorphic_allocator
<int> a
;
23 BOOST_TEST(a
.resource() == get_default_resource());
26 void test_resource_constructor()
28 polymorphic_allocator
<int> a(0);
29 BOOST_TEST(a
.resource() == get_default_resource());
31 derived_from_memory_resource d
;
32 polymorphic_allocator
<int> b(&d
);
33 BOOST_TEST(&d
== b
.resource());
36 void test_copy_constructor()
38 derived_from_memory_resource d
;
39 polymorphic_allocator
<int> b(&d
);
40 polymorphic_allocator
<int> c(b
);
41 BOOST_TEST(b
.resource() == c
.resource());
44 void test_copy_assignment()
46 derived_from_memory_resource d
;
47 polymorphic_allocator
<int> b(&d
);
48 polymorphic_allocator
<int> c
;
49 BOOST_TEST(c
.resource() == get_default_resource());
51 BOOST_TEST(c
.resource() == b
.resource());
57 derived_from_memory_resource d
;
58 polymorphic_allocator
<int> p(&d
);
60 d
.do_allocate_return
= &dummy
;
62 BOOST_TEST(d
.do_allocate_called
== true);
63 BOOST_TEST(d
.do_allocate_return
== &dummy
);
64 //It shall allocate 2*sizeof(int), alignment_of<int>
65 BOOST_TEST(d
.do_allocate_bytes
== 2*sizeof(int));
66 BOOST_TEST(d
.do_allocate_alignment
== container_detail::alignment_of
<int>::value
);
69 void test_deallocate()
72 derived_from_memory_resource d
;
73 polymorphic_allocator
<int> p(&d
);
75 p
.deallocate(&dummy
, 3);
76 BOOST_TEST(d
.do_deallocate_called
== true);
77 //It shall deallocate 2*sizeof(int), alignment_of<int>
78 BOOST_TEST(d
.do_deallocate_p
== &dummy
);
79 BOOST_TEST(d
.do_deallocate_bytes
== 3*sizeof(int));
80 BOOST_TEST(d
.do_deallocate_alignment
== container_detail::alignment_of
<int>::value
);
87 typedef allocator_argument_tester
<NotUsesAllocator
, 0> value_type
;
90 polymorphic_allocator
<int> pa
;
92 BOOST_TEST(value
.construction_type
== NotUsesAllocator
);
93 BOOST_TEST(value
.value
== 0);
97 typedef allocator_argument_tester
<ErasedTypePrefix
, 0> value_type
;
100 polymorphic_allocator
<int> pa
;
101 pa
.construct(&value
);
102 BOOST_TEST(value
.construction_type
== ConstructiblePrefix
);
103 BOOST_TEST(value
.value
== 0);
107 typedef allocator_argument_tester
<ErasedTypeSuffix
, 0> value_type
;
110 polymorphic_allocator
<int> pa
;
111 pa
.construct(&value
);
112 BOOST_TEST(value
.construction_type
== ConstructibleSuffix
);
113 BOOST_TEST(value
.value
== 0);
118 typedef allocator_argument_tester
<NotUsesAllocator
, 0> value_type
;
121 polymorphic_allocator
<int> pa
;
122 pa
.construct(&value
, 2);
123 BOOST_TEST(value
.construction_type
== NotUsesAllocator
);
124 BOOST_TEST(value
.value
== 2);
128 typedef allocator_argument_tester
<ErasedTypePrefix
, 0> value_type
;
131 polymorphic_allocator
<int> pa
;
132 pa
.construct(&value
, 3);
133 BOOST_TEST(value
.construction_type
== ConstructiblePrefix
);
134 BOOST_TEST(value
.value
== 3);
138 typedef allocator_argument_tester
<ErasedTypeSuffix
, 0> value_type
;
141 polymorphic_allocator
<int> pa
;
142 pa
.construct(&value
, 4);
143 BOOST_TEST(value
.construction_type
== ConstructibleSuffix
);
144 BOOST_TEST(value
.value
== 4);
153 { destructor_called
= true; }
154 static bool destructor_called
;
157 bool char_holder::destructor_called
= false;
162 polymorphic_allocator
<int> p
;
163 BOOST_TEST(char_holder::destructor_called
== false);
165 BOOST_TEST(char_holder::destructor_called
== true);
168 void test_select_on_container_copy_construction()
170 //select_on_container_copy_construction shall return
171 //a default constructed polymorphic_allocator
172 //which uses the default resource.
173 derived_from_memory_resource d
;
174 polymorphic_allocator
<int> p(&d
);
175 BOOST_TEST(get_default_resource() == p
.select_on_container_copy_construction().resource());
180 derived_from_memory_resource d
;
181 polymorphic_allocator
<int> p(&d
);
182 BOOST_TEST(&d
== p
.resource());
187 test_default_constructor();
188 test_resource_constructor();
189 test_copy_constructor();
190 test_copy_assignment();
195 test_select_on_container_copy_construction();
197 return ::boost::report_errors();