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