shared_ptr_basic_test.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #pragma warning(disable: 4355) // 'this' : used in base member initializer list
  8. #if (BOOST_MSVC >= 1310)
  9. #pragma warning(disable: 4675) // resolved overload found with Koenig lookup
  10. #endif
  11. #endif
  12. #if defined(__GNUC__) && __GNUC__ > 4
  13. # pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
  14. #endif
  15. //
  16. // shared_ptr_basic_test.cpp
  17. //
  18. // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  19. //
  20. // Distributed under the Boost Software License, Version 1.0. (See
  21. // accompanying file LICENSE_1_0.txt or copy at
  22. // http://www.boost.org/LICENSE_1_0.txt)
  23. //
  24. #include <boost/detail/lightweight_test.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <boost/weak_ptr.hpp>
  27. int cnt = 0;
  28. struct X
  29. {
  30. X()
  31. {
  32. ++cnt;
  33. }
  34. ~X() // virtual destructor deliberately omitted
  35. {
  36. --cnt;
  37. }
  38. virtual int id() const
  39. {
  40. return 1;
  41. }
  42. private:
  43. X(X const &);
  44. X & operator= (X const &);
  45. };
  46. struct Y: public X
  47. {
  48. Y()
  49. {
  50. ++cnt;
  51. }
  52. ~Y()
  53. {
  54. --cnt;
  55. }
  56. virtual int id() const
  57. {
  58. return 2;
  59. }
  60. private:
  61. Y(Y const &);
  62. Y & operator= (Y const &);
  63. };
  64. int * get_object()
  65. {
  66. ++cnt;
  67. return &cnt;
  68. }
  69. void release_object(int * p)
  70. {
  71. BOOST_TEST(p == &cnt);
  72. --cnt;
  73. }
  74. template<class T> void test_is_X(boost::shared_ptr<T> const & p)
  75. {
  76. BOOST_TEST(p->id() == 1);
  77. BOOST_TEST((*p).id() == 1);
  78. }
  79. template<class T> void test_is_X(boost::weak_ptr<T> const & p)
  80. {
  81. BOOST_TEST(p.get() != 0);
  82. BOOST_TEST(p.get()->id() == 1);
  83. }
  84. template<class T> void test_is_Y(boost::shared_ptr<T> const & p)
  85. {
  86. BOOST_TEST(p->id() == 2);
  87. BOOST_TEST((*p).id() == 2);
  88. }
  89. template<class T> void test_is_Y(boost::weak_ptr<T> const & p)
  90. {
  91. boost::shared_ptr<T> q = p.lock();
  92. BOOST_TEST(q.get() != 0);
  93. BOOST_TEST(q->id() == 2);
  94. }
  95. template<class T> void test_eq(T const & a, T const & b)
  96. {
  97. BOOST_TEST(a == b);
  98. BOOST_TEST(!(a != b));
  99. BOOST_TEST(!(a < b));
  100. BOOST_TEST(!(b < a));
  101. }
  102. template<class T> void test_ne(T const & a, T const & b)
  103. {
  104. BOOST_TEST(!(a == b));
  105. BOOST_TEST(a != b);
  106. BOOST_TEST(a < b || b < a);
  107. BOOST_TEST(!(a < b && b < a));
  108. }
  109. template<class T, class U> void test_shared(boost::weak_ptr<T> const & a, boost::weak_ptr<U> const & b)
  110. {
  111. BOOST_TEST(!(a < b));
  112. BOOST_TEST(!(b < a));
  113. }
  114. template<class T, class U> void test_nonshared(boost::weak_ptr<T> const & a, boost::weak_ptr<U> const & b)
  115. {
  116. BOOST_TEST(a < b || b < a);
  117. BOOST_TEST(!(a < b && b < a));
  118. }
  119. template<class T, class U> void test_eq2(T const & a, U const & b)
  120. {
  121. BOOST_TEST(a == b);
  122. BOOST_TEST(!(a != b));
  123. }
  124. template<class T, class U> void test_ne2(T const & a, U const & b)
  125. {
  126. BOOST_TEST(!(a == b));
  127. BOOST_TEST(a != b);
  128. }
  129. template<class T> void test_is_zero(boost::shared_ptr<T> const & p)
  130. {
  131. BOOST_TEST(!p);
  132. BOOST_TEST(p.get() == 0);
  133. }
  134. template<class T> void test_is_nonzero(boost::shared_ptr<T> const & p)
  135. {
  136. // p? true: false is used to test p in a boolean context.
  137. // BOOST_TEST(p) is not guaranteed to test the conversion,
  138. // as the macro might test !!p instead.
  139. BOOST_TEST(p? true: false);
  140. BOOST_TEST(p.get() != 0);
  141. }
  142. int main()
  143. {
  144. using namespace boost;
  145. {
  146. shared_ptr<X> p(new Y);
  147. shared_ptr<X> p2(new X);
  148. test_is_nonzero(p);
  149. test_is_nonzero(p2);
  150. test_is_Y(p);
  151. test_is_X(p2);
  152. test_ne(p, p2);
  153. {
  154. shared_ptr<X> q(p);
  155. test_eq(p, q);
  156. }
  157. #if !defined( BOOST_NO_RTTI )
  158. shared_ptr<Y> p3 = dynamic_pointer_cast<Y>(p);
  159. shared_ptr<Y> p4 = dynamic_pointer_cast<Y>(p2);
  160. test_is_nonzero(p3);
  161. test_is_zero(p4);
  162. BOOST_TEST(p.use_count() == 2);
  163. BOOST_TEST(p2.use_count() == 1);
  164. BOOST_TEST(p3.use_count() == 2);
  165. test_is_Y(p3);
  166. test_eq2(p, p3);
  167. test_ne2(p2, p4);
  168. #endif
  169. shared_ptr<void> p5(p);
  170. test_is_nonzero(p5);
  171. test_eq2(p, p5);
  172. weak_ptr<X> wp1(p2);
  173. BOOST_TEST(!wp1.expired());
  174. BOOST_TEST(wp1.use_count() != 0);
  175. p.reset();
  176. p2.reset();
  177. #if !defined( BOOST_NO_RTTI )
  178. p3.reset();
  179. p4.reset();
  180. #endif
  181. test_is_zero(p);
  182. test_is_zero(p2);
  183. #if !defined( BOOST_NO_RTTI )
  184. test_is_zero(p3);
  185. test_is_zero(p4);
  186. #endif
  187. BOOST_TEST(p5.use_count() == 1);
  188. BOOST_TEST(wp1.expired());
  189. BOOST_TEST(wp1.use_count() == 0);
  190. try
  191. {
  192. shared_ptr<X> sp1(wp1);
  193. BOOST_ERROR("shared_ptr<X> sp1(wp1) failed to throw");
  194. }
  195. catch(boost::bad_weak_ptr const &)
  196. {
  197. }
  198. test_is_zero(wp1.lock());
  199. weak_ptr<X> wp2 = static_pointer_cast<X>(p5);
  200. BOOST_TEST(wp2.use_count() == 1);
  201. test_is_Y(wp2);
  202. test_nonshared(wp1, wp2);
  203. // Scoped to not affect the subsequent use_count() tests.
  204. {
  205. shared_ptr<X> sp2(wp2);
  206. test_is_nonzero(wp2.lock());
  207. }
  208. #if !defined( BOOST_NO_RTTI )
  209. weak_ptr<Y> wp3 = dynamic_pointer_cast<Y>(wp2.lock());
  210. BOOST_TEST(wp3.use_count() == 1);
  211. test_shared(wp2, wp3);
  212. weak_ptr<X> wp4(wp3);
  213. BOOST_TEST(wp4.use_count() == 1);
  214. test_shared(wp2, wp4);
  215. #endif
  216. wp1 = p2;
  217. test_is_zero(wp1.lock());
  218. #if !defined( BOOST_NO_RTTI )
  219. wp1 = p4;
  220. wp1 = wp3;
  221. #endif
  222. wp1 = wp2;
  223. BOOST_TEST(wp1.use_count() == 1);
  224. test_shared(wp1, wp2);
  225. weak_ptr<X> wp5;
  226. bool b1 = wp1 < wp5;
  227. bool b2 = wp5 < wp1;
  228. p5.reset();
  229. BOOST_TEST(wp1.use_count() == 0);
  230. BOOST_TEST(wp2.use_count() == 0);
  231. #if !defined( BOOST_NO_RTTI )
  232. BOOST_TEST(wp3.use_count() == 0);
  233. #endif
  234. // Test operator< stability for std::set< weak_ptr<> >
  235. // Thanks to Joe Gottman for pointing this out
  236. BOOST_TEST(b1 == (wp1 < wp5));
  237. BOOST_TEST(b2 == (wp5 < wp1));
  238. {
  239. // note that both get_object and release_object deal with int*
  240. shared_ptr<void> p6(get_object(), release_object);
  241. }
  242. }
  243. BOOST_TEST(cnt == 0);
  244. return boost::report_errors();
  245. }