allocate_local_shared_esft_test.cpp 7.3 KB

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