allocate_shared_alloc11_test.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // allocate_shared_alloc11_test.cpp
  2. //
  3. // allocate_shared with a minimal C++11 allocator
  4. //
  5. // Copyright 2007-2009, 2014 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/make_shared.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <boost/weak_ptr.hpp>
  14. #include <boost/config.hpp>
  15. #include <cstddef>
  16. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  17. template< class T > class cxx11_allocator
  18. {
  19. public:
  20. typedef T value_type;
  21. cxx11_allocator()
  22. {
  23. }
  24. template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
  25. {
  26. }
  27. T * allocate( std::size_t n )
  28. {
  29. return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
  30. }
  31. void deallocate( T * p, std::size_t n )
  32. {
  33. ::operator delete( p );
  34. }
  35. };
  36. class X
  37. {
  38. private:
  39. X( X const & );
  40. X & operator=( X const & );
  41. void * operator new( std::size_t n )
  42. {
  43. BOOST_ERROR( "private X::new called" );
  44. return ::operator new( n );
  45. }
  46. void operator delete( void * p )
  47. {
  48. BOOST_ERROR( "private X::delete called" );
  49. ::operator delete( p );
  50. }
  51. public:
  52. static int instances;
  53. int v;
  54. 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 )
  55. {
  56. ++instances;
  57. }
  58. ~X()
  59. {
  60. --instances;
  61. }
  62. };
  63. int X::instances = 0;
  64. int main()
  65. {
  66. {
  67. boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>() );
  68. BOOST_TEST( pi.get() != 0 );
  69. BOOST_TEST( *pi == 0 );
  70. }
  71. {
  72. boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>(), 5 );
  73. BOOST_TEST( pi.get() != 0 );
  74. BOOST_TEST( *pi == 5 );
  75. }
  76. BOOST_TEST( X::instances == 0 );
  77. {
  78. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>() );
  79. boost::weak_ptr<X> wp( pi );
  80. BOOST_TEST( X::instances == 1 );
  81. BOOST_TEST( pi.get() != 0 );
  82. BOOST_TEST( pi->v == 0 );
  83. pi.reset();
  84. BOOST_TEST( X::instances == 0 );
  85. }
  86. {
  87. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1 );
  88. boost::weak_ptr<X> wp( pi );
  89. BOOST_TEST( X::instances == 1 );
  90. BOOST_TEST( pi.get() != 0 );
  91. BOOST_TEST( pi->v == 1 );
  92. pi.reset();
  93. BOOST_TEST( X::instances == 0 );
  94. }
  95. {
  96. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2 );
  97. boost::weak_ptr<X> wp( pi );
  98. BOOST_TEST( X::instances == 1 );
  99. BOOST_TEST( pi.get() != 0 );
  100. BOOST_TEST( pi->v == 1+2 );
  101. pi.reset();
  102. BOOST_TEST( X::instances == 0 );
  103. }
  104. {
  105. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3 );
  106. boost::weak_ptr<X> wp( pi );
  107. BOOST_TEST( X::instances == 1 );
  108. BOOST_TEST( pi.get() != 0 );
  109. BOOST_TEST( pi->v == 1+2+3 );
  110. pi.reset();
  111. BOOST_TEST( X::instances == 0 );
  112. }
  113. {
  114. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4 );
  115. boost::weak_ptr<X> wp( pi );
  116. BOOST_TEST( X::instances == 1 );
  117. BOOST_TEST( pi.get() != 0 );
  118. BOOST_TEST( pi->v == 1+2+3+4 );
  119. pi.reset();
  120. BOOST_TEST( X::instances == 0 );
  121. }
  122. {
  123. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5 );
  124. boost::weak_ptr<X> wp( pi );
  125. BOOST_TEST( X::instances == 1 );
  126. BOOST_TEST( pi.get() != 0 );
  127. BOOST_TEST( pi->v == 1+2+3+4+5 );
  128. pi.reset();
  129. BOOST_TEST( X::instances == 0 );
  130. }
  131. {
  132. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6 );
  133. boost::weak_ptr<X> wp( pi );
  134. BOOST_TEST( X::instances == 1 );
  135. BOOST_TEST( pi.get() != 0 );
  136. BOOST_TEST( pi->v == 1+2+3+4+5+6 );
  137. pi.reset();
  138. BOOST_TEST( X::instances == 0 );
  139. }
  140. {
  141. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
  142. boost::weak_ptr<X> wp( pi );
  143. BOOST_TEST( X::instances == 1 );
  144. BOOST_TEST( pi.get() != 0 );
  145. BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
  146. pi.reset();
  147. BOOST_TEST( X::instances == 0 );
  148. }
  149. {
  150. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
  151. boost::weak_ptr<X> wp( pi );
  152. BOOST_TEST( X::instances == 1 );
  153. BOOST_TEST( pi.get() != 0 );
  154. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
  155. pi.reset();
  156. BOOST_TEST( X::instances == 0 );
  157. }
  158. {
  159. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  160. boost::weak_ptr<X> wp( pi );
  161. BOOST_TEST( X::instances == 1 );
  162. BOOST_TEST( pi.get() != 0 );
  163. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
  164. pi.reset();
  165. BOOST_TEST( X::instances == 0 );
  166. }
  167. return boost::report_errors();
  168. }
  169. #else // !defined( BOOST_NO_CXX11_ALLOCATOR )
  170. int main()
  171. {
  172. return 0;
  173. }
  174. #endif