allocate_shared_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // allocate_shared_test.cpp
  2. //
  3. // Copyright 2007-2009 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/detail/lightweight_test.hpp>
  9. #include <boost/make_shared.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/weak_ptr.hpp>
  12. #include <cstddef>
  13. class X
  14. {
  15. private:
  16. X( X const & );
  17. X & operator=( X const & );
  18. void * operator new( std::size_t n )
  19. {
  20. // lack of this definition causes link errors on Comeau C++
  21. BOOST_ERROR( "private X::new called" );
  22. return ::operator new( n );
  23. }
  24. void operator delete( void * p )
  25. {
  26. // lack of this definition causes link errors on MSVC
  27. BOOST_ERROR( "private X::delete called" );
  28. ::operator delete( p );
  29. }
  30. public:
  31. static int instances;
  32. int v;
  33. explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0, int a5 = 0, int a6 = 0, int a7 = 0, int a8 = 0, int a9 = 0 ): v( a1+a2+a3+a4+a5+a6+a7+a8+a9 )
  34. {
  35. ++instances;
  36. }
  37. ~X()
  38. {
  39. --instances;
  40. }
  41. };
  42. int X::instances = 0;
  43. int main()
  44. {
  45. {
  46. boost::shared_ptr< int > pi = boost::allocate_shared< int >( std::allocator<int>() );
  47. BOOST_TEST( pi.get() != 0 );
  48. BOOST_TEST( *pi == 0 );
  49. }
  50. {
  51. boost::shared_ptr< int > pi = boost::allocate_shared_noinit< int >( std::allocator<int>() );
  52. BOOST_TEST( pi.get() != 0 );
  53. }
  54. {
  55. boost::shared_ptr< int > pi = boost::allocate_shared< int >( std::allocator<int>(), 5 );
  56. BOOST_TEST( pi.get() != 0 );
  57. BOOST_TEST( *pi == 5 );
  58. }
  59. BOOST_TEST( X::instances == 0 );
  60. {
  61. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>() );
  62. boost::weak_ptr<X> wp( pi );
  63. BOOST_TEST( X::instances == 1 );
  64. BOOST_TEST( pi.get() != 0 );
  65. BOOST_TEST( pi->v == 0 );
  66. pi.reset();
  67. BOOST_TEST( X::instances == 0 );
  68. }
  69. {
  70. boost::shared_ptr< X > pi = boost::allocate_shared_noinit< X >( std::allocator<void>() );
  71. boost::weak_ptr<X> wp( pi );
  72. BOOST_TEST( X::instances == 1 );
  73. BOOST_TEST( pi.get() != 0 );
  74. BOOST_TEST( pi->v == 0 );
  75. pi.reset();
  76. BOOST_TEST( X::instances == 0 );
  77. }
  78. {
  79. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1 );
  80. boost::weak_ptr<X> wp( pi );
  81. BOOST_TEST( X::instances == 1 );
  82. BOOST_TEST( pi.get() != 0 );
  83. BOOST_TEST( pi->v == 1 );
  84. pi.reset();
  85. BOOST_TEST( X::instances == 0 );
  86. }
  87. {
  88. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2 );
  89. boost::weak_ptr<X> wp( pi );
  90. BOOST_TEST( X::instances == 1 );
  91. BOOST_TEST( pi.get() != 0 );
  92. BOOST_TEST( pi->v == 1+2 );
  93. pi.reset();
  94. BOOST_TEST( X::instances == 0 );
  95. }
  96. {
  97. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3 );
  98. boost::weak_ptr<X> wp( pi );
  99. BOOST_TEST( X::instances == 1 );
  100. BOOST_TEST( pi.get() != 0 );
  101. BOOST_TEST( pi->v == 1+2+3 );
  102. pi.reset();
  103. BOOST_TEST( X::instances == 0 );
  104. }
  105. {
  106. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4 );
  107. boost::weak_ptr<X> wp( pi );
  108. BOOST_TEST( X::instances == 1 );
  109. BOOST_TEST( pi.get() != 0 );
  110. BOOST_TEST( pi->v == 1+2+3+4 );
  111. pi.reset();
  112. BOOST_TEST( X::instances == 0 );
  113. }
  114. {
  115. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5 );
  116. boost::weak_ptr<X> wp( pi );
  117. BOOST_TEST( X::instances == 1 );
  118. BOOST_TEST( pi.get() != 0 );
  119. BOOST_TEST( pi->v == 1+2+3+4+5 );
  120. pi.reset();
  121. BOOST_TEST( X::instances == 0 );
  122. }
  123. {
  124. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6 );
  125. boost::weak_ptr<X> wp( pi );
  126. BOOST_TEST( X::instances == 1 );
  127. BOOST_TEST( pi.get() != 0 );
  128. BOOST_TEST( pi->v == 1+2+3+4+5+6 );
  129. pi.reset();
  130. BOOST_TEST( X::instances == 0 );
  131. }
  132. {
  133. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
  134. boost::weak_ptr<X> wp( pi );
  135. BOOST_TEST( X::instances == 1 );
  136. BOOST_TEST( pi.get() != 0 );
  137. BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
  138. pi.reset();
  139. BOOST_TEST( X::instances == 0 );
  140. }
  141. {
  142. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
  143. boost::weak_ptr<X> wp( pi );
  144. BOOST_TEST( X::instances == 1 );
  145. BOOST_TEST( pi.get() != 0 );
  146. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
  147. pi.reset();
  148. BOOST_TEST( X::instances == 0 );
  149. }
  150. {
  151. boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  152. boost::weak_ptr<X> wp( pi );
  153. BOOST_TEST( X::instances == 1 );
  154. BOOST_TEST( pi.get() != 0 );
  155. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
  156. pi.reset();
  157. BOOST_TEST( X::instances == 0 );
  158. }
  159. return boost::report_errors();
  160. }