allocate_local_shared_array_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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<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. BOOST_TEST(result[0] == 0);
  76. BOOST_TEST(result[1] == 0);
  77. BOOST_TEST(result[2] == 0);
  78. }
  79. {
  80. boost::local_shared_ptr<int[3]> result =
  81. boost::allocate_local_shared<int[3]>(creator<int>());
  82. BOOST_TEST(result.get() != 0);
  83. BOOST_TEST(result.local_use_count() == 1);
  84. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  85. boost::alignment_of<int>::value));
  86. BOOST_TEST(result[0] == 0);
  87. BOOST_TEST(result[1] == 0);
  88. BOOST_TEST(result[2] == 0);
  89. }
  90. {
  91. boost::local_shared_ptr<int[][2]> result =
  92. boost::allocate_local_shared<int[][2]>(creator<>(), 2);
  93. BOOST_TEST(result.get() != 0);
  94. BOOST_TEST(result.local_use_count() == 1);
  95. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  96. boost::alignment_of<int>::value));
  97. BOOST_TEST(result[0][0] == 0);
  98. BOOST_TEST(result[0][1] == 0);
  99. BOOST_TEST(result[1][0] == 0);
  100. BOOST_TEST(result[1][1] == 0);
  101. }
  102. {
  103. boost::local_shared_ptr<int[2][2]> result =
  104. boost::allocate_local_shared<int[2][2]>(creator<>());
  105. BOOST_TEST(result.get() != 0);
  106. BOOST_TEST(result.local_use_count() == 1);
  107. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  108. boost::alignment_of<int>::value));
  109. BOOST_TEST(result[0][0] == 0);
  110. BOOST_TEST(result[0][1] == 0);
  111. BOOST_TEST(result[1][0] == 0);
  112. BOOST_TEST(result[1][1] == 0);
  113. }
  114. {
  115. boost::local_shared_ptr<const int[]> result =
  116. boost::allocate_local_shared<const int[]>(creator<>(), 3);
  117. BOOST_TEST(result.get() != 0);
  118. BOOST_TEST(result.local_use_count() == 1);
  119. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  120. boost::alignment_of<int>::value));
  121. BOOST_TEST(result[0] == 0);
  122. BOOST_TEST(result[1] == 0);
  123. BOOST_TEST(result[2] == 0);
  124. }
  125. {
  126. boost::local_shared_ptr<const int[3]> result =
  127. boost::allocate_local_shared<const int[3]>(creator<>());
  128. BOOST_TEST(result.get() != 0);
  129. BOOST_TEST(result.local_use_count() == 1);
  130. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  131. boost::alignment_of<int>::value));
  132. BOOST_TEST(result[0] == 0);
  133. BOOST_TEST(result[1] == 0);
  134. BOOST_TEST(result[2] == 0);
  135. }
  136. {
  137. boost::local_shared_ptr<const int[][2]> result =
  138. boost::allocate_local_shared<const int[][2]>(creator<>(), 2);
  139. BOOST_TEST(result.get() != 0);
  140. BOOST_TEST(result.local_use_count() == 1);
  141. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  142. boost::alignment_of<int>::value));
  143. BOOST_TEST(result[0][0] == 0);
  144. BOOST_TEST(result[0][1] == 0);
  145. BOOST_TEST(result[1][0] == 0);
  146. BOOST_TEST(result[1][1] == 0);
  147. }
  148. {
  149. boost::local_shared_ptr<const int[2][2]> result =
  150. boost::allocate_local_shared<const int[2][2]>(creator<>());
  151. BOOST_TEST(result.get() != 0);
  152. BOOST_TEST(result.local_use_count() == 1);
  153. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  154. boost::alignment_of<int>::value));
  155. BOOST_TEST(result[0][0] == 0);
  156. BOOST_TEST(result[0][1] == 0);
  157. BOOST_TEST(result[1][0] == 0);
  158. BOOST_TEST(result[1][1] == 0);
  159. }
  160. {
  161. boost::local_shared_ptr<type[]> result =
  162. boost::allocate_local_shared<type[]>(creator<type>(), 3);
  163. BOOST_TEST(result.get() != 0);
  164. BOOST_TEST(result.local_use_count() == 1);
  165. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  166. boost::alignment_of<type>::value));
  167. BOOST_TEST(type::instances == 3);
  168. boost::weak_ptr<type[]> w1 = result;
  169. result.reset();
  170. BOOST_TEST(type::instances == 0);
  171. }
  172. {
  173. boost::local_shared_ptr<type[3]> result =
  174. boost::allocate_local_shared<type[3]>(creator<type>());
  175. BOOST_TEST(result.get() != 0);
  176. BOOST_TEST(result.local_use_count() == 1);
  177. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  178. boost::alignment_of<type>::value));
  179. BOOST_TEST(type::instances == 3);
  180. boost::weak_ptr<type[3]> w1 = result;
  181. result.reset();
  182. BOOST_TEST(type::instances == 0);
  183. }
  184. {
  185. boost::local_shared_ptr<type[][2]> result =
  186. boost::allocate_local_shared<type[][2]>(creator<>(), 2);
  187. BOOST_TEST(result.get() != 0);
  188. BOOST_TEST(result.local_use_count() == 1);
  189. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  190. boost::alignment_of<type>::value));
  191. BOOST_TEST(type::instances == 4);
  192. result.reset();
  193. BOOST_TEST(type::instances == 0);
  194. }
  195. {
  196. boost::local_shared_ptr<type[2][2]> result =
  197. boost::allocate_local_shared<type[2][2]>(creator<>());
  198. BOOST_TEST(result.get() != 0);
  199. BOOST_TEST(result.local_use_count() == 1);
  200. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  201. boost::alignment_of<type>::value));
  202. BOOST_TEST(type::instances == 4);
  203. result.reset();
  204. BOOST_TEST(type::instances == 0);
  205. }
  206. {
  207. boost::local_shared_ptr<const type[]> result =
  208. boost::allocate_local_shared<const type[]>(creator<>(), 3);
  209. BOOST_TEST(result.get() != 0);
  210. BOOST_TEST(result.local_use_count() == 1);
  211. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  212. boost::alignment_of<type>::value));
  213. BOOST_TEST(type::instances == 3);
  214. result.reset();
  215. BOOST_TEST(type::instances == 0);
  216. }
  217. {
  218. boost::local_shared_ptr<const type[3]> result =
  219. boost::allocate_local_shared<const type[3]>(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 == 3);
  225. result.reset();
  226. BOOST_TEST(type::instances == 0);
  227. }
  228. {
  229. boost::local_shared_ptr<const type[][2]> result =
  230. boost::allocate_local_shared<const type[][2]>(creator<>(), 2);
  231. BOOST_TEST(result.get() != 0);
  232. BOOST_TEST(result.local_use_count() == 1);
  233. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  234. boost::alignment_of<type>::value));
  235. BOOST_TEST(type::instances == 4);
  236. result.reset();
  237. BOOST_TEST(type::instances == 0);
  238. }
  239. {
  240. boost::local_shared_ptr<const type[2][2]> result =
  241. boost::allocate_local_shared<const type[2][2]>(creator<>());
  242. BOOST_TEST(result.get() != 0);
  243. BOOST_TEST(result.local_use_count() == 1);
  244. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  245. boost::alignment_of<type>::value));
  246. BOOST_TEST(type::instances == 4);
  247. result.reset();
  248. BOOST_TEST(type::instances == 0);
  249. }
  250. return boost::report_errors();
  251. }
  252. #else
  253. int main()
  254. {
  255. return 0;
  256. }
  257. #endif