allocate_shared_array_test.cpp 8.4 KB

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