Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / smart_ptr / test / make_shared_array_esft_test.cpp
1 /*
2 Copyright 2012-2015 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/core/lightweight_test.hpp>
9 #include <boost/smart_ptr/enable_shared_from_this.hpp>
10 #include <boost/smart_ptr/make_shared.hpp>
11
12 class type
13     : public boost::enable_shared_from_this<type> {
14 public:
15     static unsigned instances;
16
17     type() {
18         ++instances;
19     }
20
21     ~type() {
22         --instances;
23     }
24
25 private:
26     type(const type&);
27     type& operator=(const type&);
28 };
29
30 unsigned type::instances = 0;
31
32 int main()
33 {
34     BOOST_TEST(type::instances == 0);
35     {
36         boost::shared_ptr<type[]> result =
37             boost::make_shared<type[]>(3);
38         try {
39             result[0].shared_from_this();
40             BOOST_ERROR("shared_from_this did not throw");
41         } catch (...) {
42             BOOST_TEST(type::instances == 3);
43         }
44     }
45     BOOST_TEST(type::instances == 0);
46     {
47         boost::shared_ptr<type[3]> result =
48             boost::make_shared_noinit<type[3]>();
49         try {
50             result[0].shared_from_this();
51             BOOST_ERROR("shared_from_this did not throw");
52         } catch (...) {
53             BOOST_TEST(type::instances == 3);
54         }
55     }
56     return boost::report_errors();
57 }