Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / smart_ptr / test / allocate_unique_test.cpp
1 /*
2 Copyright 2019 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4
5 Distributed 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 >= 46000) && \
11     !defined(BOOST_NO_CXX11_SMART_PTR)
12 #include <boost/smart_ptr/allocate_unique.hpp>
13 #include <boost/core/lightweight_test.hpp>
14
15 template<class T = void>
16 struct creator {
17     typedef T value_type;
18     typedef T* pointer;
19
20     template<class U>
21     struct rebind {
22         typedef creator<U> other;
23     };
24
25     creator() { }
26
27     template<class U>
28     creator(const creator<U>&) { }
29
30     T* allocate(std::size_t size) {
31         return static_cast<T*>(::operator new(sizeof(T) * size));
32     }
33
34     void deallocate(T* ptr, std::size_t) {
35         ::operator delete(ptr);
36     }
37 };
38
39 template<class T, class U>
40 inline bool
41 operator==(const creator<T>&, const creator<U>&)
42 {
43     return true;
44 }
45
46 template<class T, class U>
47 inline bool
48 operator!=(const creator<T>&, const creator<U>&)
49 {
50     return false;
51 }
52
53 class type {
54 public:
55     static unsigned instances;
56
57     type() {
58         ++instances;
59     }
60
61     ~type() {
62         --instances;
63     }
64
65 private:
66     type(const type&);
67     type& operator=(const type&);
68 };
69
70 unsigned type::instances = 0;
71
72 int main()
73 {
74     {
75         std::unique_ptr<int,
76             boost::alloc_deleter<int, creator<int> > > result =
77             boost::allocate_unique<int>(creator<int>());
78         BOOST_TEST(result.get() != 0);
79         BOOST_TEST(*result == 0);
80     }
81     {
82         std::unique_ptr<const int,
83             boost::alloc_deleter<const int, creator<> > > result =
84             boost::allocate_unique<const int>(creator<>());
85         BOOST_TEST(result.get() != 0);
86         BOOST_TEST(*result == 0);
87     }
88     BOOST_TEST(type::instances == 0);
89     {
90         std::unique_ptr<type,
91             boost::alloc_deleter<type, creator<type> > > result =
92             boost::allocate_unique<type>(creator<type>());
93         BOOST_TEST(result.get() != 0);
94         BOOST_TEST(type::instances == 1);
95         result.reset();
96         BOOST_TEST(type::instances == 0);
97     }
98     BOOST_TEST(type::instances == 0);
99     {
100         std::unique_ptr<const type,
101             boost::alloc_deleter<const type, creator<> > > result =
102             boost::allocate_unique<const type>(creator<>());
103         BOOST_TEST(result.get() != 0);
104         BOOST_TEST(type::instances == 1);
105         result.reset();
106         BOOST_TEST(type::instances == 0);
107     }
108     return boost::report_errors();
109 }
110 #else
111 int main()
112 {
113     return 0;
114 }
115 #endif