allocate_local_shared_array_noinit_test.cpp 8.2 KB

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