1 /*
2 Copyright 2017 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_NO_CXX11_RVALUE_REFERENCES) && \
10 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
11 #include <boost/core/lightweight_test.hpp>
12 #include <boost/smart_ptr/make_local_shared.hpp>
13
14 template<class T = void>
15 struct creator {
16 typedef T value_type;
17
18 template<class U>
19 struct rebind {
20 typedef creator<U> other;
21 };
22
creatorcreator23 creator() { }
24
25 template<class U>
creatorcreator26 creator(const creator<U>&) { }
27
allocatecreator28 T* allocate(std::size_t size) {
29 return static_cast<T*>(::operator new(sizeof(T) * size));
30 }
31
deallocatecreator32 void deallocate(T* ptr, std::size_t) {
33 ::operator delete(ptr);
34 }
35 };
36
37 template<class T, class U>
38 inline bool
operator ==(const creator<T> &,const creator<U> &)39 operator==(const creator<T>&, const creator<U>&)
40 {
41 return true;
42 }
43
44 template<class T, class U>
45 inline bool
operator !=(const creator<T> &,const creator<U> &)46 operator!=(const creator<T>&, const creator<U>&)
47 {
48 return false;
49 }
50
51 class type {
52 public:
53 static unsigned instances;
54
type()55 type() {
56 if (instances == 5) {
57 throw true;
58 }
59 ++instances;
60 }
61
~type()62 ~type() {
63 --instances;
64 }
65
66 private:
67 type(const type&);
68 type& operator=(const type&);
69 };
70
71 unsigned type::instances = 0;
72
main()73 int main()
74 {
75 try {
76 boost::allocate_local_shared<type[]>(creator<type>(), 6);
77 BOOST_ERROR("allocate_local_shared did not throw");
78 } catch (...) {
79 BOOST_TEST(type::instances == 0);
80 }
81 try {
82 boost::allocate_local_shared<type[][2]>(creator<type>(), 3);
83 BOOST_ERROR("allocate_local_shared did not throw");
84 } catch (...) {
85 BOOST_TEST(type::instances == 0);
86 }
87 try {
88 boost::allocate_local_shared<type[6]>(creator<>());
89 BOOST_ERROR("allocate_local_shared did not throw");
90 } catch (...) {
91 BOOST_TEST(type::instances == 0);
92 }
93 try {
94 boost::allocate_local_shared<type[3][2]>(creator<>());
95 BOOST_ERROR("allocate_local_shared did not throw");
96 } catch (...) {
97 BOOST_TEST(type::instances == 0);
98 }
99 try {
100 boost::allocate_local_shared_noinit<type[]>(creator<>(), 6);
101 BOOST_ERROR("allocate_local_shared_noinit did not throw");
102 } catch (...) {
103 BOOST_TEST(type::instances == 0);
104 }
105 try {
106 boost::allocate_local_shared_noinit<type[][2]>(creator<>(), 3);
107 BOOST_ERROR("allocate_local_shared_noinit did not throw");
108 } catch (...) {
109 BOOST_TEST(type::instances == 0);
110 }
111 try {
112 boost::allocate_local_shared_noinit<type[6]>(creator<>());
113 BOOST_ERROR("allocate_local_shared_noinit did not throw");
114 } catch (...) {
115 BOOST_TEST(type::instances == 0);
116 }
117 try {
118 boost::allocate_local_shared_noinit<type[3][2]>(creator<>());
119 BOOST_ERROR("allocate_local_shared_noinit did not throw");
120 } catch (...) {
121 BOOST_TEST(type::instances == 0);
122 }
123 return boost::report_errors();
124 }
125 #else
main()126 int main()
127 {
128 return 0;
129 }
130 #endif
131