make_local_shared_esft_test.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // make_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. class X: public boost::enable_shared_from_this<X>
  19. {
  20. private:
  21. X( X const & );
  22. X & operator=( X const & );
  23. public:
  24. static int instances;
  25. explicit X( int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0, int = 0 )
  26. {
  27. ++instances;
  28. }
  29. ~X()
  30. {
  31. --instances;
  32. }
  33. };
  34. int X::instances = 0;
  35. int main()
  36. {
  37. BOOST_TEST( X::instances == 0 );
  38. {
  39. boost::shared_ptr< X > px = boost::make_local_shared< X >();
  40. BOOST_TEST( px.use_count() == 1 );
  41. BOOST_TEST( X::instances == 1 );
  42. try
  43. {
  44. boost::shared_ptr< X > qx = px->shared_from_this();
  45. BOOST_TEST( px == qx );
  46. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  47. px.reset();
  48. BOOST_TEST( 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( X::instances == 0 );
  56. {
  57. boost::shared_ptr< X > px = boost::make_local_shared_noinit< X >();
  58. BOOST_TEST( px.use_count() == 1 );
  59. BOOST_TEST( X::instances == 1 );
  60. try
  61. {
  62. boost::shared_ptr< X > qx = px->shared_from_this();
  63. BOOST_TEST( px == qx );
  64. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  65. px.reset();
  66. BOOST_TEST( X::instances == 1 );
  67. }
  68. catch( boost::bad_weak_ptr const& )
  69. {
  70. BOOST_ERROR( "px->shared_from_this() failed" );
  71. }
  72. }
  73. BOOST_TEST( X::instances == 0 );
  74. {
  75. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1 );
  76. BOOST_TEST( px.use_count() == 1 );
  77. BOOST_TEST( X::instances == 1 );
  78. try
  79. {
  80. boost::shared_ptr< X > qx = px->shared_from_this();
  81. BOOST_TEST( px == qx );
  82. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  83. px.reset();
  84. BOOST_TEST( X::instances == 1 );
  85. }
  86. catch( boost::bad_weak_ptr const& )
  87. {
  88. BOOST_ERROR( "px->shared_from_this() failed" );
  89. }
  90. }
  91. BOOST_TEST( X::instances == 0 );
  92. {
  93. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2 );
  94. BOOST_TEST( px.use_count() == 1 );
  95. BOOST_TEST( X::instances == 1 );
  96. try
  97. {
  98. boost::shared_ptr< X > qx = px->shared_from_this();
  99. BOOST_TEST( px == qx );
  100. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  101. px.reset();
  102. BOOST_TEST( X::instances == 1 );
  103. }
  104. catch( boost::bad_weak_ptr const& )
  105. {
  106. BOOST_ERROR( "px->shared_from_this() failed" );
  107. }
  108. }
  109. BOOST_TEST( X::instances == 0 );
  110. {
  111. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3 );
  112. BOOST_TEST( px.use_count() == 1 );
  113. BOOST_TEST( X::instances == 1 );
  114. try
  115. {
  116. boost::shared_ptr< X > qx = px->shared_from_this();
  117. BOOST_TEST( px == qx );
  118. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  119. px.reset();
  120. BOOST_TEST( X::instances == 1 );
  121. }
  122. catch( boost::bad_weak_ptr const& )
  123. {
  124. BOOST_ERROR( "px->shared_from_this() failed" );
  125. }
  126. }
  127. BOOST_TEST( X::instances == 0 );
  128. {
  129. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4 );
  130. BOOST_TEST( px.use_count() == 1 );
  131. BOOST_TEST( X::instances == 1 );
  132. try
  133. {
  134. boost::shared_ptr< X > qx = px->shared_from_this();
  135. BOOST_TEST( px == qx );
  136. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  137. px.reset();
  138. BOOST_TEST( X::instances == 1 );
  139. }
  140. catch( boost::bad_weak_ptr const& )
  141. {
  142. BOOST_ERROR( "px->shared_from_this() failed" );
  143. }
  144. }
  145. BOOST_TEST( X::instances == 0 );
  146. {
  147. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5 );
  148. BOOST_TEST( px.use_count() == 1 );
  149. BOOST_TEST( X::instances == 1 );
  150. try
  151. {
  152. boost::shared_ptr< X > qx = px->shared_from_this();
  153. BOOST_TEST( px == qx );
  154. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  155. px.reset();
  156. BOOST_TEST( X::instances == 1 );
  157. }
  158. catch( boost::bad_weak_ptr const& )
  159. {
  160. BOOST_ERROR( "px->shared_from_this() failed" );
  161. }
  162. }
  163. BOOST_TEST( X::instances == 0 );
  164. {
  165. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6 );
  166. BOOST_TEST( px.use_count() == 1 );
  167. BOOST_TEST( X::instances == 1 );
  168. try
  169. {
  170. boost::shared_ptr< X > qx = px->shared_from_this();
  171. BOOST_TEST( px == qx );
  172. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  173. px.reset();
  174. BOOST_TEST( X::instances == 1 );
  175. }
  176. catch( boost::bad_weak_ptr const& )
  177. {
  178. BOOST_ERROR( "px->shared_from_this() failed" );
  179. }
  180. }
  181. BOOST_TEST( X::instances == 0 );
  182. {
  183. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7 );
  184. BOOST_TEST( px.use_count() == 1 );
  185. BOOST_TEST( X::instances == 1 );
  186. try
  187. {
  188. boost::shared_ptr< X > qx = px->shared_from_this();
  189. BOOST_TEST( px == qx );
  190. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  191. px.reset();
  192. BOOST_TEST( X::instances == 1 );
  193. }
  194. catch( boost::bad_weak_ptr const& )
  195. {
  196. BOOST_ERROR( "px->shared_from_this() failed" );
  197. }
  198. }
  199. BOOST_TEST( X::instances == 0 );
  200. {
  201. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8 );
  202. BOOST_TEST( px.use_count() == 1 );
  203. BOOST_TEST( X::instances == 1 );
  204. try
  205. {
  206. boost::shared_ptr< X > qx = px->shared_from_this();
  207. BOOST_TEST( px == qx );
  208. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  209. px.reset();
  210. BOOST_TEST( X::instances == 1 );
  211. }
  212. catch( boost::bad_weak_ptr const& )
  213. {
  214. BOOST_ERROR( "px->shared_from_this() failed" );
  215. }
  216. }
  217. BOOST_TEST( X::instances == 0 );
  218. {
  219. boost::shared_ptr< X > px = boost::make_local_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  220. BOOST_TEST( px.use_count() == 1 );
  221. BOOST_TEST( X::instances == 1 );
  222. try
  223. {
  224. boost::shared_ptr< X > qx = px->shared_from_this();
  225. BOOST_TEST( px == qx );
  226. BOOST_TEST( !( px < qx ) && !( qx < px ) );
  227. px.reset();
  228. BOOST_TEST( X::instances == 1 );
  229. }
  230. catch( boost::bad_weak_ptr const& )
  231. {
  232. BOOST_ERROR( "px->shared_from_this() failed" );
  233. }
  234. }
  235. BOOST_TEST( X::instances == 0 );
  236. return boost::report_errors();
  237. }
  238. #endif