lsp_array_test.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // lsp_array_test.cpp
  3. //
  4. // Copyright 2012, 2017 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. //
  10. #include <boost/smart_ptr/local_shared_ptr.hpp>
  11. #include <boost/weak_ptr.hpp>
  12. #include <boost/enable_shared_from_this.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <memory>
  15. #include <utility>
  16. class X: public boost::enable_shared_from_this< X >
  17. {
  18. public:
  19. static int allocations;
  20. static int instances;
  21. X()
  22. {
  23. ++instances;
  24. }
  25. ~X()
  26. {
  27. --instances;
  28. }
  29. void* operator new[]( std::size_t n )
  30. {
  31. ++allocations;
  32. return ::operator new[]( n );
  33. }
  34. void operator delete[]( void* p )
  35. {
  36. --allocations;
  37. ::operator delete[]( p );
  38. }
  39. private:
  40. X( X const& );
  41. X& operator=( X const& );
  42. };
  43. int X::allocations = 0;
  44. int X::instances = 0;
  45. template< class T> class array_deleter
  46. {
  47. public:
  48. static int calls;
  49. void operator()( T * p ) const
  50. {
  51. ++calls;
  52. delete[] p;
  53. }
  54. private:
  55. template< class Y > void operator()( Y * p ) const;
  56. };
  57. template< class T > int array_deleter< T >::calls = 0;
  58. int main()
  59. {
  60. BOOST_TEST( X::allocations == 0 );
  61. BOOST_TEST( X::instances == 0 );
  62. {
  63. boost::local_shared_ptr<X[]> px;
  64. BOOST_TEST( !px );
  65. BOOST_TEST( X::allocations == 0 );
  66. BOOST_TEST( X::instances == 0 );
  67. boost::local_shared_ptr<X[]> px2( new X[ 3 ] );
  68. BOOST_TEST( px2 );
  69. try
  70. {
  71. px2[0].shared_from_this();
  72. BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
  73. }
  74. catch( boost::bad_weak_ptr const& )
  75. {
  76. }
  77. catch( ... )
  78. {
  79. BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
  80. }
  81. BOOST_TEST( X::allocations == 1 );
  82. BOOST_TEST( X::instances == 3 );
  83. {
  84. X & rx = px2[ 0 ];
  85. BOOST_TEST( &rx == px2.get() );
  86. }
  87. boost::local_shared_ptr<X const[]> px3( px2 );
  88. BOOST_TEST( px3 == px2 );
  89. BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
  90. {
  91. X const & rx = px3[ 1 ];
  92. BOOST_TEST( &rx == px3.get() + 1 );
  93. }
  94. px3.reset();
  95. px3 = px2;
  96. BOOST_TEST( px3 == px2 );
  97. BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
  98. boost::local_shared_ptr<X volatile[]> px4( px2 );
  99. BOOST_TEST( px4 == px2 );
  100. BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
  101. {
  102. X volatile & rx = px4[ 2 ];
  103. BOOST_TEST( &rx == px4.get() + 2 );
  104. }
  105. px4.reset();
  106. px4 = px2;
  107. BOOST_TEST( px4 == px2 );
  108. BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
  109. boost::local_shared_ptr<void> px5( px2 );
  110. BOOST_TEST( px5 == px2 );
  111. BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
  112. px5.reset();
  113. px5 = px2;
  114. BOOST_TEST( px5 == px2 );
  115. BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
  116. boost::weak_ptr<X[]> wp( px );
  117. BOOST_TEST( wp.lock() == px );
  118. boost::weak_ptr<X[]> wp2( px2 );
  119. BOOST_TEST( wp2.lock() == px2 );
  120. wp2.reset();
  121. wp2 = px2;
  122. BOOST_TEST( wp2.lock() == px2 );
  123. boost::weak_ptr<X const[]> wp3( px2 );
  124. BOOST_TEST( wp3.lock() == px2 );
  125. wp3.reset();
  126. wp3 = px2;
  127. BOOST_TEST( wp3.lock() == px2 );
  128. boost::weak_ptr<X volatile[]> wp4( px2 );
  129. BOOST_TEST( wp4.lock() == px2 );
  130. wp4.reset();
  131. wp4 = px2;
  132. BOOST_TEST( wp4.lock() == px2 );
  133. boost::weak_ptr<void> wp5( px2 );
  134. BOOST_TEST( wp5.lock() == px2 );
  135. wp5.reset();
  136. wp5 = px2;
  137. BOOST_TEST( wp5.lock() == px2 );
  138. px2.reset();
  139. BOOST_TEST( X::allocations == 1 );
  140. BOOST_TEST( X::instances == 3 );
  141. px3.reset();
  142. px4.reset();
  143. px5.reset();
  144. BOOST_TEST( X::allocations == 0 );
  145. BOOST_TEST( X::instances == 0 );
  146. BOOST_TEST( wp2.lock() == 0 );
  147. BOOST_TEST( wp3.lock() == 0 );
  148. BOOST_TEST( wp4.lock() == 0 );
  149. BOOST_TEST( wp5.lock() == 0 );
  150. }
  151. #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
  152. {
  153. std::unique_ptr<X[]> px( new X[ 4 ] );
  154. BOOST_TEST( X::allocations == 1 );
  155. BOOST_TEST( X::instances == 4 );
  156. boost::local_shared_ptr<X[]> px2( std::move( px ) );
  157. BOOST_TEST( X::allocations == 1 );
  158. BOOST_TEST( X::instances == 4 );
  159. BOOST_TEST( px.get() == 0 );
  160. try
  161. {
  162. px2[0].shared_from_this();
  163. BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
  164. }
  165. catch( boost::bad_weak_ptr const& )
  166. {
  167. }
  168. catch( ... )
  169. {
  170. BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
  171. }
  172. px2.reset();
  173. BOOST_TEST( X::allocations == 0 );
  174. BOOST_TEST( X::instances == 0 );
  175. }
  176. {
  177. std::unique_ptr<X[]> px( new X[ 4 ] );
  178. BOOST_TEST( X::allocations == 1 );
  179. BOOST_TEST( X::instances == 4 );
  180. boost::local_shared_ptr<X[]> px2;
  181. px2 = std::move( px );
  182. BOOST_TEST( X::allocations == 1 );
  183. BOOST_TEST( X::instances == 4 );
  184. BOOST_TEST( px.get() == 0 );
  185. try
  186. {
  187. px2[0].shared_from_this();
  188. BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
  189. }
  190. catch( boost::bad_weak_ptr const& )
  191. {
  192. }
  193. catch( ... )
  194. {
  195. BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
  196. }
  197. px2.reset();
  198. BOOST_TEST( X::allocations == 0 );
  199. BOOST_TEST( X::instances == 0 );
  200. }
  201. #endif
  202. {
  203. boost::local_shared_ptr<X[]> px( new X[ 5 ], array_deleter< X >() );
  204. BOOST_TEST( X::allocations == 1 );
  205. BOOST_TEST( X::instances == 5 );
  206. try
  207. {
  208. px[0].shared_from_this();
  209. BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
  210. }
  211. catch( boost::bad_weak_ptr const& )
  212. {
  213. }
  214. catch( ... )
  215. {
  216. BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
  217. }
  218. px.reset();
  219. BOOST_TEST( X::allocations == 0 );
  220. BOOST_TEST( X::instances == 0 );
  221. BOOST_TEST( array_deleter< X >::calls == 1 );
  222. }
  223. {
  224. boost::local_shared_ptr<X[]> px( new X[ 6 ], array_deleter< X >(), std::allocator< X >() );
  225. BOOST_TEST( X::allocations == 1 );
  226. BOOST_TEST( X::instances == 6 );
  227. try
  228. {
  229. px[0].shared_from_this();
  230. BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
  231. }
  232. catch( boost::bad_weak_ptr const& )
  233. {
  234. }
  235. catch( ... )
  236. {
  237. BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
  238. }
  239. px.reset();
  240. BOOST_TEST( X::allocations == 0 );
  241. BOOST_TEST( X::instances == 0 );
  242. BOOST_TEST( array_deleter< X >::calls == 2 );
  243. }
  244. return boost::report_errors();
  245. }