1/*
2Copyright 2019 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#include <boost/config.hpp>
9#if (!defined(BOOST_LIBSTDCXX_VERSION) || \
10 BOOST_LIBSTDCXX_VERSION >= 48000) && \
11 !defined(BOOST_NO_CXX11_SMART_PTR) && \
12 !defined(BOOST_NO_CXX11_ALLOCATOR)
13#include <boost/smart_ptr/allocate_unique.hpp>
14#include <boost/core/lightweight_test.hpp>
15
16struct allow { };
17
18template<class T = void>
19struct creator {
20 typedef T value_type;
21
22 template<class U>
23 struct rebind {
24 typedef creator<U> other;
25 };
26
27 creator() { }
28
29 template<class U>
30 creator(const creator<U>&) { }
31
32 T* allocate(std::size_t size) {
33 return static_cast<T*>(::operator new(sizeof(T) * size));
34 }
35
36 void deallocate(T* ptr, std::size_t) {
37 ::operator delete(ptr);
38 }
39
40 template<class U>
41 void construct(U* ptr) {
42 ::new(static_cast<void*>(ptr)) U(allow());
43 }
44
45 template<class U>
46 void destroy(U* ptr) {
47 ptr->~U();
48 }
49
50};
51
52template<class T, class U>
53inline bool
54operator==(const creator<T>&, const creator<U>&)
55{
56 return true;
57}
58
59template<class T, class U>
60inline bool
61operator!=(const creator<T>&, const creator<U>&)
62{
63 return false;
64}
65
66class type {
67public:
68 static unsigned instances;
69
70 explicit type(allow) {
71 ++instances;
72 }
73
74 ~type() {
75 --instances;
76 }
77
78private:
79 type(const type&);
80 type& operator=(const type&);
81};
82
83unsigned type::instances = 0;
84
85int main()
86{
87 {
88 std::unique_ptr<type[],
89 boost::alloc_deleter<type[], creator<type> > > result =
90 boost::allocate_unique<type[]>(alloc: creator<type>(), size: 3);
91 BOOST_TEST(result.get() != 0);
92 BOOST_TEST(type::instances == 3);
93 result.reset();
94 BOOST_TEST(type::instances == 0);
95 }
96 {
97 std::unique_ptr<type[],
98 boost::alloc_deleter<type[3], creator<type> > > result =
99 boost::allocate_unique<type[3]>(alloc: creator<type>());
100 BOOST_TEST(result.get() != 0);
101 BOOST_TEST(type::instances == 3);
102 result.reset();
103 BOOST_TEST(type::instances == 0);
104 }
105 {
106 std::unique_ptr<type[][2],
107 boost::alloc_deleter<type[][2], creator<> > > result =
108 boost::allocate_unique<type[][2]>(alloc: creator<>(), size: 2);
109 BOOST_TEST(result.get() != 0);
110 BOOST_TEST(type::instances == 4);
111 result.reset();
112 BOOST_TEST(type::instances == 0);
113 }
114 {
115 std::unique_ptr<type[][2],
116 boost::alloc_deleter<type[2][2], creator<> > > result =
117 boost::allocate_unique<type[2][2]>(alloc: creator<>());
118 BOOST_TEST(result.get() != 0);
119 BOOST_TEST(type::instances == 4);
120 result.reset();
121 BOOST_TEST(type::instances == 0);
122 }
123 {
124 std::unique_ptr<const type[],
125 boost::alloc_deleter<const type[], creator<> > > result =
126 boost::allocate_unique<const type[]>(alloc: creator<>(), size: 3);
127 BOOST_TEST(result.get() != 0);
128 BOOST_TEST(type::instances == 3);
129 result.reset();
130 BOOST_TEST(type::instances == 0);
131 }
132 {
133 std::unique_ptr<const type[],
134 boost::alloc_deleter<const type[3], creator<> > > result =
135 boost::allocate_unique<const type[3]>(alloc: creator<>());
136 BOOST_TEST(result.get() != 0);
137 BOOST_TEST(type::instances == 3);
138 result.reset();
139 BOOST_TEST(type::instances == 0);
140 }
141 {
142 std::unique_ptr<const type[][2],
143 boost::alloc_deleter<const type[][2], creator<> > > result =
144 boost::allocate_unique<const type[][2]>(alloc: creator<>(), size: 2);
145 BOOST_TEST(result.get() != 0);
146 BOOST_TEST(type::instances == 4);
147 result.reset();
148 BOOST_TEST(type::instances == 0);
149 }
150 {
151 std::unique_ptr<const type[][2],
152 boost::alloc_deleter<const type[2][2], creator<> > > result =
153 boost::allocate_unique<const type[2][2]>(alloc: creator<>());
154 BOOST_TEST(result.get() != 0);
155 BOOST_TEST(type::instances == 4);
156 result.reset();
157 BOOST_TEST(type::instances == 0);
158 }
159 return boost::report_errors();
160}
161#else
162int main()
163{
164 return 0;
165}
166#endif
167

source code of boost/libs/smart_ptr/test/allocate_unique_array_construct_test.cpp