allocate_shared_array_noinit_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright 2012-2015 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/align/is_aligned.hpp>
  8. #include <boost/core/lightweight_test.hpp>
  9. #include <boost/smart_ptr/make_shared.hpp>
  10. #include <boost/smart_ptr/weak_ptr.hpp>
  11. #include <boost/type_traits/alignment_of.hpp>
  12. template<class T = void>
  13. struct creator {
  14. typedef T value_type;
  15. template<class U>
  16. struct rebind {
  17. typedef creator<U> other;
  18. };
  19. creator() { }
  20. template<class U>
  21. creator(const creator<U>&) { }
  22. T* allocate(std::size_t size) {
  23. return static_cast<T*>(::operator new(sizeof(T) * size));
  24. }
  25. void deallocate(T* ptr, std::size_t) {
  26. ::operator delete(ptr);
  27. }
  28. };
  29. template<class T, class U>
  30. inline bool
  31. operator==(const creator<T>&, const creator<U>&)
  32. {
  33. return true;
  34. }
  35. template<class T, class U>
  36. inline bool
  37. operator!=(const creator<T>&, const creator<U>&)
  38. {
  39. return false;
  40. }
  41. class type {
  42. public:
  43. static unsigned instances;
  44. type()
  45. : value_(0.0) {
  46. ++instances;
  47. }
  48. ~type() {
  49. --instances;
  50. }
  51. void set(long double value) {
  52. value_ = value;
  53. }
  54. long double get() const {
  55. return value_;
  56. }
  57. private:
  58. type(const type&);
  59. type& operator=(const type&);
  60. long double value_;
  61. };
  62. unsigned type::instances = 0;
  63. int main()
  64. {
  65. {
  66. boost::shared_ptr<int[]> result =
  67. boost::allocate_shared_noinit<int[]>(creator<int>(), 3);
  68. BOOST_TEST(result.get() != 0);
  69. BOOST_TEST(result.use_count() == 1);
  70. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  71. boost::alignment_of<int>::value));
  72. }
  73. {
  74. boost::shared_ptr<int[3]> result =
  75. boost::allocate_shared_noinit<int[3]>(creator<int>());
  76. BOOST_TEST(result.get() != 0);
  77. BOOST_TEST(result.use_count() == 1);
  78. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  79. boost::alignment_of<int>::value));
  80. }
  81. {
  82. boost::shared_ptr<int[][2]> result =
  83. boost::allocate_shared_noinit<int[][2]>(creator<>(), 2);
  84. BOOST_TEST(result.get() != 0);
  85. BOOST_TEST(result.use_count() == 1);
  86. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  87. boost::alignment_of<int>::value));
  88. }
  89. {
  90. boost::shared_ptr<int[2][2]> result =
  91. boost::allocate_shared_noinit<int[2][2]>(creator<>());
  92. BOOST_TEST(result.get() != 0);
  93. BOOST_TEST(result.use_count() == 1);
  94. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  95. boost::alignment_of<int>::value));
  96. }
  97. {
  98. boost::shared_ptr<const int[]> result =
  99. boost::allocate_shared_noinit<const int[]>(creator<>(), 3);
  100. BOOST_TEST(result.get() != 0);
  101. BOOST_TEST(result.use_count() == 1);
  102. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  103. boost::alignment_of<int>::value));
  104. }
  105. {
  106. boost::shared_ptr<const int[3]> result =
  107. boost::allocate_shared_noinit<const int[3]>(creator<>());
  108. BOOST_TEST(result.get() != 0);
  109. BOOST_TEST(result.use_count() == 1);
  110. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  111. boost::alignment_of<int>::value));
  112. }
  113. {
  114. boost::shared_ptr<const int[][2]> result =
  115. boost::allocate_shared_noinit<const int[][2]>(creator<>(), 2);
  116. BOOST_TEST(result.get() != 0);
  117. BOOST_TEST(result.use_count() == 1);
  118. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  119. boost::alignment_of<int>::value));
  120. }
  121. {
  122. boost::shared_ptr<const int[2][2]> result =
  123. boost::allocate_shared_noinit<const int[2][2]>(creator<>());
  124. BOOST_TEST(result.get() != 0);
  125. BOOST_TEST(result.use_count() == 1);
  126. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  127. boost::alignment_of<int>::value));
  128. }
  129. {
  130. boost::shared_ptr<type[]> result =
  131. boost::allocate_shared_noinit<type[]>(creator<type>(), 3);
  132. BOOST_TEST(result.get() != 0);
  133. BOOST_TEST(result.use_count() == 1);
  134. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  135. boost::alignment_of<type>::value));
  136. BOOST_TEST(type::instances == 3);
  137. boost::weak_ptr<type[]> other = result;
  138. result.reset();
  139. BOOST_TEST(type::instances == 0);
  140. }
  141. {
  142. boost::shared_ptr<type[3]> result =
  143. boost::allocate_shared_noinit<type[3]>(creator<type>());
  144. BOOST_TEST(result.get() != 0);
  145. BOOST_TEST(result.use_count() == 1);
  146. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  147. boost::alignment_of<type>::value));
  148. BOOST_TEST(type::instances == 3);
  149. boost::weak_ptr<type[3]> other = result;
  150. result.reset();
  151. BOOST_TEST(type::instances == 0);
  152. }
  153. {
  154. boost::shared_ptr<type[][2]> result =
  155. boost::allocate_shared_noinit<type[][2]>(creator<>(), 2);
  156. BOOST_TEST(result.get() != 0);
  157. BOOST_TEST(result.use_count() == 1);
  158. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  159. boost::alignment_of<type>::value));
  160. BOOST_TEST(type::instances == 4);
  161. boost::weak_ptr<type[][2]> other = result;
  162. result.reset();
  163. BOOST_TEST(type::instances == 0);
  164. }
  165. {
  166. boost::shared_ptr<type[2][2]> result =
  167. boost::allocate_shared_noinit<type[2][2]>(creator<>());
  168. BOOST_TEST(result.get() != 0);
  169. BOOST_TEST(result.use_count() == 1);
  170. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  171. boost::alignment_of<type>::value));
  172. BOOST_TEST(type::instances == 4);
  173. boost::weak_ptr<type[2][2]> other = result;
  174. result.reset();
  175. BOOST_TEST(type::instances == 0);
  176. }
  177. {
  178. boost::shared_ptr<const type[]> result =
  179. boost::allocate_shared_noinit<const type[]>(creator<>(), 3);
  180. BOOST_TEST(result.get() != 0);
  181. BOOST_TEST(result.use_count() == 1);
  182. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  183. boost::alignment_of<type>::value));
  184. BOOST_TEST(type::instances == 3);
  185. boost::weak_ptr<const type[]> other = result;
  186. result.reset();
  187. BOOST_TEST(type::instances == 0);
  188. }
  189. {
  190. boost::shared_ptr<const type[3]> result =
  191. boost::allocate_shared_noinit<const type[3]>(creator<>());
  192. BOOST_TEST(result.get() != 0);
  193. BOOST_TEST(result.use_count() == 1);
  194. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  195. boost::alignment_of<type>::value));
  196. BOOST_TEST(type::instances == 3);
  197. boost::weak_ptr<const type[3]> other = result;
  198. result.reset();
  199. BOOST_TEST(type::instances == 0);
  200. }
  201. {
  202. boost::shared_ptr<const type[][2]> result =
  203. boost::allocate_shared_noinit<const
  204. type[][2]>(creator<>(), 2);
  205. BOOST_TEST(result.get() != 0);
  206. BOOST_TEST(result.use_count() == 1);
  207. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  208. boost::alignment_of<type>::value));
  209. BOOST_TEST(type::instances == 4);
  210. boost::weak_ptr<const type[][2]> other = result;
  211. result.reset();
  212. BOOST_TEST(type::instances == 0);
  213. }
  214. {
  215. boost::shared_ptr<const type[2][2]> result =
  216. boost::allocate_shared_noinit<const type[2][2]>(creator<>());
  217. BOOST_TEST(result.get() != 0);
  218. BOOST_TEST(result.use_count() == 1);
  219. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  220. boost::alignment_of<type>::value));
  221. BOOST_TEST(type::instances == 4);
  222. boost::weak_ptr<const type[2][2]> other = result;
  223. result.reset();
  224. BOOST_TEST(type::instances == 0);
  225. }
  226. return boost::report_errors();
  227. }