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 | |
23 | creator() { } |
24 | |
25 | template<class U> |
26 | creator(const creator<U>&) { } |
27 | |
28 | T* allocate(std::size_t size) { |
29 | return static_cast<T*>(::operator new(sizeof(T) * size)); |
30 | } |
31 | |
32 | void deallocate(T* ptr, std::size_t) { |
33 | ::operator delete(ptr); |
34 | } |
35 | }; |
36 | |
37 | template<class T, class U> |
38 | inline bool |
39 | operator==(const creator<T>&, const creator<U>&) |
40 | { |
41 | return true; |
42 | } |
43 | |
44 | template<class T, class U> |
45 | inline bool |
46 | operator!=(const creator<T>&, const creator<U>&) |
47 | { |
48 | return false; |
49 | } |
50 | |
51 | class type { |
52 | public: |
53 | static unsigned instances; |
54 | |
55 | type() { |
56 | if (instances == 5) { |
57 | throw true; |
58 | } |
59 | ++instances; |
60 | } |
61 | |
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 | |
73 | int main() |
74 | { |
75 | try { |
76 | boost::allocate_local_shared<type[]>(allocator: creator<type>(), count: 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]>(allocator: creator<type>(), count: 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]>(allocator: 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]>(allocator: 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[]>(allocator: creator<>(), count: 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]>(allocator: creator<>(), count: 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]>(allocator: 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]>(allocator: 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 |
126 | int main() |
127 | { |
128 | return 0; |
129 | } |
130 | #endif |
131 | |