make_shared_array_test.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. class type {
  13. public:
  14. static unsigned instances;
  15. type()
  16. : value_(0.0) {
  17. ++instances;
  18. }
  19. ~type() {
  20. --instances;
  21. }
  22. void set(long double value) {
  23. value_ = value;
  24. }
  25. long double get() const {
  26. return value_;
  27. }
  28. private:
  29. type(const type&);
  30. type& operator=(const type&);
  31. long double value_;
  32. };
  33. unsigned type::instances = 0;
  34. int main()
  35. {
  36. {
  37. boost::shared_ptr<int[]> result =
  38. boost::make_shared<int[]>(3);
  39. BOOST_TEST(result.get() != 0);
  40. BOOST_TEST(result.use_count() == 1);
  41. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  42. boost::alignment_of<int>::value));
  43. BOOST_TEST(result[0] == 0);
  44. BOOST_TEST(result[1] == 0);
  45. BOOST_TEST(result[2] == 0);
  46. }
  47. {
  48. boost::shared_ptr<int[3]> result =
  49. boost::make_shared<int[3]>();
  50. BOOST_TEST(result.get() != 0);
  51. BOOST_TEST(result.use_count() == 1);
  52. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  53. boost::alignment_of<int>::value));
  54. BOOST_TEST(result[0] == 0);
  55. BOOST_TEST(result[1] == 0);
  56. BOOST_TEST(result[2] == 0);
  57. }
  58. {
  59. boost::shared_ptr<int[][2]> result =
  60. boost::make_shared<int[][2]>(2);
  61. BOOST_TEST(result.get() != 0);
  62. BOOST_TEST(result.use_count() == 1);
  63. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  64. boost::alignment_of<int>::value));
  65. BOOST_TEST(result[0][0] == 0);
  66. BOOST_TEST(result[0][1] == 0);
  67. BOOST_TEST(result[1][0] == 0);
  68. BOOST_TEST(result[1][1] == 0);
  69. }
  70. {
  71. boost::shared_ptr<int[2][2]> result =
  72. boost::make_shared<int[2][2]>();
  73. BOOST_TEST(result.get() != 0);
  74. BOOST_TEST(result.use_count() == 1);
  75. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  76. boost::alignment_of<int>::value));
  77. BOOST_TEST(result[0][0] == 0);
  78. BOOST_TEST(result[0][1] == 0);
  79. BOOST_TEST(result[1][0] == 0);
  80. BOOST_TEST(result[1][1] == 0);
  81. }
  82. {
  83. boost::shared_ptr<const int[]> result =
  84. boost::make_shared<const int[]>(3);
  85. BOOST_TEST(result.get() != 0);
  86. BOOST_TEST(result.use_count() == 1);
  87. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  88. boost::alignment_of<int>::value));
  89. BOOST_TEST(result[0] == 0);
  90. BOOST_TEST(result[1] == 0);
  91. BOOST_TEST(result[2] == 0);
  92. }
  93. {
  94. boost::shared_ptr<const int[3]> result =
  95. boost::make_shared<const int[3]>();
  96. BOOST_TEST(result.get() != 0);
  97. BOOST_TEST(result.use_count() == 1);
  98. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  99. boost::alignment_of<int>::value));
  100. BOOST_TEST(result[0] == 0);
  101. BOOST_TEST(result[1] == 0);
  102. BOOST_TEST(result[2] == 0);
  103. }
  104. {
  105. boost::shared_ptr<const int[][2]> result =
  106. boost::make_shared<const int[][2]>(2);
  107. BOOST_TEST(result.get() != 0);
  108. BOOST_TEST(result.use_count() == 1);
  109. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  110. boost::alignment_of<int>::value));
  111. BOOST_TEST(result[0][0] == 0);
  112. BOOST_TEST(result[0][1] == 0);
  113. BOOST_TEST(result[1][0] == 0);
  114. BOOST_TEST(result[1][1] == 0);
  115. }
  116. {
  117. boost::shared_ptr<const int[2][2]> result =
  118. boost::make_shared<const int[2][2]>();
  119. BOOST_TEST(result.get() != 0);
  120. BOOST_TEST(result.use_count() == 1);
  121. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  122. boost::alignment_of<int>::value));
  123. BOOST_TEST(result[0][0] == 0);
  124. BOOST_TEST(result[0][1] == 0);
  125. BOOST_TEST(result[1][0] == 0);
  126. BOOST_TEST(result[1][1] == 0);
  127. }
  128. {
  129. boost::shared_ptr<type[]> result =
  130. boost::make_shared<type[]>(3);
  131. BOOST_TEST(result.get() != 0);
  132. BOOST_TEST(result.use_count() == 1);
  133. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  134. boost::alignment_of<type>::value));
  135. BOOST_TEST(type::instances == 3);
  136. boost::weak_ptr<type[]> w1 = result;
  137. result.reset();
  138. BOOST_TEST(type::instances == 0);
  139. }
  140. {
  141. boost::shared_ptr<type[3]> result =
  142. boost::make_shared<type[3]>();
  143. BOOST_TEST(result.get() != 0);
  144. BOOST_TEST(result.use_count() == 1);
  145. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  146. boost::alignment_of<type>::value));
  147. BOOST_TEST(type::instances == 3);
  148. boost::weak_ptr<type[3]> w1 = result;
  149. result.reset();
  150. BOOST_TEST(type::instances == 0);
  151. }
  152. {
  153. boost::shared_ptr<type[][2]> result =
  154. boost::make_shared<type[][2]>(2);
  155. BOOST_TEST(result.get() != 0);
  156. BOOST_TEST(result.use_count() == 1);
  157. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  158. boost::alignment_of<type>::value));
  159. BOOST_TEST(type::instances == 4);
  160. result.reset();
  161. BOOST_TEST(type::instances == 0);
  162. }
  163. {
  164. boost::shared_ptr<type[2][2]> result =
  165. boost::make_shared<type[2][2]>();
  166. BOOST_TEST(result.get() != 0);
  167. BOOST_TEST(result.use_count() == 1);
  168. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  169. boost::alignment_of<type>::value));
  170. BOOST_TEST(type::instances == 4);
  171. result.reset();
  172. BOOST_TEST(type::instances == 0);
  173. }
  174. {
  175. boost::shared_ptr<const type[]> result =
  176. boost::make_shared<const type[]>(3);
  177. BOOST_TEST(result.get() != 0);
  178. BOOST_TEST(result.use_count() == 1);
  179. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  180. boost::alignment_of<type>::value));
  181. BOOST_TEST(type::instances == 3);
  182. result.reset();
  183. BOOST_TEST(type::instances == 0);
  184. }
  185. {
  186. boost::shared_ptr<const type[3]> result =
  187. boost::make_shared<const type[3]>();
  188. BOOST_TEST(result.get() != 0);
  189. BOOST_TEST(result.use_count() == 1);
  190. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  191. boost::alignment_of<type>::value));
  192. BOOST_TEST(type::instances == 3);
  193. result.reset();
  194. BOOST_TEST(type::instances == 0);
  195. }
  196. {
  197. boost::shared_ptr<const type[][2]> result =
  198. boost::make_shared<const type[][2]>(2);
  199. BOOST_TEST(result.get() != 0);
  200. BOOST_TEST(result.use_count() == 1);
  201. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  202. boost::alignment_of<type>::value));
  203. BOOST_TEST(type::instances == 4);
  204. result.reset();
  205. BOOST_TEST(type::instances == 0);
  206. }
  207. {
  208. boost::shared_ptr<const type[2][2]> result =
  209. boost::make_shared<const type[2][2]>();
  210. BOOST_TEST(result.get() != 0);
  211. BOOST_TEST(result.use_count() == 1);
  212. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  213. boost::alignment_of<type>::value));
  214. BOOST_TEST(type::instances == 4);
  215. result.reset();
  216. BOOST_TEST(type::instances == 0);
  217. }
  218. return boost::report_errors();
  219. }