1 /*
2  * Copyright (c) 2012-2014 Glen Joseph Fernandes
3  * glenfe at live dot com
4  *
5  * Distributed under the Boost Software License,
6  * Version 1.0. (See accompanying file LICENSE_1_0.txt
7  * or copy at http://boost.org/LICENSE_1_0.txt)
8  */
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/smart_ptr/allocate_shared_array.hpp>
11 #include <boost/smart_ptr/weak_ptr.hpp>
12 #include <boost/type_traits/alignment_of.hpp>
13 
14 class type {
15 public:
16     static unsigned int instances;
17 
type()18     explicit type() {
19         instances++;
20     }
21 
~type()22     ~type() {
23         instances--;
24     }
25 
26 private:
27     type(const type&);
28     type& operator=(const type&);
29 };
30 
31 unsigned int type::instances = 0;
32 
main()33 int main() {
34     {
35         boost::shared_ptr<int[]> a1 = boost::allocate_shared_noinit<int[]>(std::allocator<int>(), 3);
36         int* a2 = a1.get();
37         BOOST_TEST(a1.use_count() == 1);
38         BOOST_TEST(a2 != 0);
39         BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
40     }
41 
42     {
43         boost::shared_ptr<int[3]> a1 = boost::allocate_shared_noinit<int[3]>(std::allocator<int>());
44         int* a2 = a1.get();
45         BOOST_TEST(a1.use_count() == 1);
46         BOOST_TEST(a2 != 0);
47         BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
48     }
49 
50     {
51         boost::shared_ptr<int[][2]> a1 = boost::allocate_shared_noinit<int[][2]>(std::allocator<int>(), 2);
52         BOOST_TEST(a1.get() != 0);
53         BOOST_TEST(a1.use_count() == 1);
54     }
55 
56     {
57         boost::shared_ptr<int[2][2]> a1 = boost::allocate_shared_noinit<int[2][2]>(std::allocator<int>());
58         BOOST_TEST(a1.get() != 0);
59         BOOST_TEST(a1.use_count() == 1);
60     }
61 
62     {
63         boost::shared_ptr<const int[]> a1 = boost::allocate_shared_noinit<const int[]>(std::allocator<int>(), 3);
64         const int* a2 = a1.get();
65         BOOST_TEST(a1.use_count() == 1);
66         BOOST_TEST(a2 != 0);
67         BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
68     }
69 
70     {
71         boost::shared_ptr<const int[3]> a1 = boost::allocate_shared_noinit<const int[3]>(std::allocator<int>());
72         const int* a2 = a1.get();
73         BOOST_TEST(a1.use_count() == 1);
74         BOOST_TEST(a2 != 0);
75         BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
76     }
77 
78     {
79         boost::shared_ptr<const int[][2]> a1 = boost::allocate_shared_noinit<const int[][2]>(std::allocator<int>(), 2);
80         BOOST_TEST(a1.get() != 0);
81         BOOST_TEST(a1.use_count() == 1);
82     }
83 
84     {
85         boost::shared_ptr<const int[2][2]> a1 = boost::allocate_shared_noinit<const int[2][2]>(std::allocator<int>());
86         BOOST_TEST(a1.get() != 0);
87         BOOST_TEST(a1.use_count() == 1);
88     }
89 
90     BOOST_TEST(type::instances == 0);
91     {
92         boost::shared_ptr<type[]> a1 = boost::allocate_shared_noinit<type[]>(std::allocator<type>(), 3);
93         type* a2 = a1.get();
94         BOOST_TEST(a1.use_count() == 1);
95         BOOST_TEST(a2 != 0);
96         BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
97         BOOST_TEST(type::instances == 3);
98         boost::weak_ptr<type[]> w1 = a1;
99         a1.reset();
100         BOOST_TEST(type::instances == 0);
101     }
102 
103     BOOST_TEST(type::instances == 0);
104     {
105         boost::shared_ptr<type[3]> a1 = boost::allocate_shared_noinit<type[3]>(std::allocator<type>());
106         type* a2 = a1.get();
107         BOOST_TEST(a1.use_count() == 1);
108         BOOST_TEST(a2 != 0);
109         BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
110         BOOST_TEST(type::instances == 3);
111         boost::weak_ptr<type[3]> w1 = a1;
112         a1.reset();
113         BOOST_TEST(type::instances == 0);
114     }
115 
116     BOOST_TEST(type::instances == 0);
117     {
118         boost::shared_ptr<type[][2]> a1 = boost::allocate_shared_noinit<type[][2]>(std::allocator<type>(), 2);
119         BOOST_TEST(a1.get() != 0);
120         BOOST_TEST(a1.use_count() == 1);
121         BOOST_TEST(type::instances == 4);
122         a1.reset();
123         BOOST_TEST(type::instances == 0);
124     }
125 
126     BOOST_TEST(type::instances == 0);
127     {
128         boost::shared_ptr<type[2][2]> a1 = boost::allocate_shared_noinit<type[2][2]>(std::allocator<type>());
129         BOOST_TEST(a1.get() != 0);
130         BOOST_TEST(a1.use_count() == 1);
131         BOOST_TEST(type::instances == 4);
132         a1.reset();
133         BOOST_TEST(type::instances == 0);
134     }
135 
136     BOOST_TEST(type::instances == 0);
137     {
138         boost::shared_ptr<const type[]> a1 = boost::allocate_shared_noinit<const type[]>(std::allocator<type>(), 3);
139         const type* a2 = a1.get();
140         BOOST_TEST(a1.use_count() == 1);
141         BOOST_TEST(a2 != 0);
142         BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
143         BOOST_TEST(type::instances == 3);
144         a1.reset();
145         BOOST_TEST(type::instances == 0);
146     }
147 
148     BOOST_TEST(type::instances == 0);
149     {
150         boost::shared_ptr<const type[3]> a1 = boost::allocate_shared_noinit<const type[3]>(std::allocator<type>());
151         const type* a2 = a1.get();
152         BOOST_TEST(a1.use_count() == 1);
153         BOOST_TEST(a2 != 0);
154         BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
155         BOOST_TEST(type::instances == 3);
156         a1.reset();
157         BOOST_TEST(type::instances == 0);
158     }
159 
160     BOOST_TEST(type::instances == 0);
161     {
162         boost::shared_ptr<const type[][2]> a1 = boost::allocate_shared_noinit<const type[][2]>(std::allocator<type>(), 2);
163         BOOST_TEST(a1.get() != 0);
164         BOOST_TEST(a1.use_count() == 1);
165         BOOST_TEST(type::instances == 4);
166         a1.reset();
167         BOOST_TEST(type::instances == 0);
168     }
169 
170     BOOST_TEST(type::instances == 0);
171     {
172         boost::shared_ptr<const type[2][2]> a1 = boost::allocate_shared_noinit<const type[2][2]>(std::allocator<type>());
173         BOOST_TEST(a1.get() != 0);
174         BOOST_TEST(a1.use_count() == 1);
175         BOOST_TEST(type::instances == 4);
176         a1.reset();
177         BOOST_TEST(type::instances == 0);
178     }
179 
180     return boost::report_errors();
181 }
182