make_shared_array_noinit_test.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_noinit<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. }
  44. {
  45. boost::shared_ptr<int[3]> result =
  46. boost::make_shared_noinit<int[3]>();
  47. BOOST_TEST(result.get() != 0);
  48. BOOST_TEST(result.use_count() == 1);
  49. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  50. boost::alignment_of<int>::value));
  51. }
  52. {
  53. boost::shared_ptr<int[][2]> result =
  54. boost::make_shared_noinit<int[][2]>(2);
  55. BOOST_TEST(result.get() != 0);
  56. BOOST_TEST(result.use_count() == 1);
  57. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  58. boost::alignment_of<int>::value));
  59. }
  60. {
  61. boost::shared_ptr<int[2][2]> result =
  62. boost::make_shared_noinit<int[2][2]>();
  63. BOOST_TEST(result.get() != 0);
  64. BOOST_TEST(result.use_count() == 1);
  65. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  66. boost::alignment_of<int>::value));
  67. }
  68. {
  69. boost::shared_ptr<const int[]> result =
  70. boost::make_shared_noinit<const int[]>(3);
  71. BOOST_TEST(result.get() != 0);
  72. BOOST_TEST(result.use_count() == 1);
  73. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  74. boost::alignment_of<int>::value));
  75. }
  76. {
  77. boost::shared_ptr<const int[3]> result =
  78. boost::make_shared_noinit<const int[3]>();
  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. }
  84. {
  85. boost::shared_ptr<const int[][2]> result =
  86. boost::make_shared_noinit<const int[][2]>(2);
  87. BOOST_TEST(result.get() != 0);
  88. BOOST_TEST(result.use_count() == 1);
  89. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  90. boost::alignment_of<int>::value));
  91. }
  92. {
  93. boost::shared_ptr<const int[2][2]> result =
  94. boost::make_shared_noinit<const int[2][2]>();
  95. BOOST_TEST(result.get() != 0);
  96. BOOST_TEST(result.use_count() == 1);
  97. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  98. boost::alignment_of<int>::value));
  99. }
  100. {
  101. boost::shared_ptr<type[]> result =
  102. boost::make_shared_noinit<type[]>(3);
  103. BOOST_TEST(result.get() != 0);
  104. BOOST_TEST(result.use_count() == 1);
  105. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  106. boost::alignment_of<type>::value));
  107. BOOST_TEST(type::instances == 3);
  108. boost::weak_ptr<type[]> other = result;
  109. result.reset();
  110. BOOST_TEST(type::instances == 0);
  111. }
  112. {
  113. boost::shared_ptr<type[3]> result =
  114. boost::make_shared_noinit<type[3]>();
  115. BOOST_TEST(result.get() != 0);
  116. BOOST_TEST(result.use_count() == 1);
  117. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  118. boost::alignment_of<type>::value));
  119. BOOST_TEST(type::instances == 3);
  120. boost::weak_ptr<type[3]> other = result;
  121. result.reset();
  122. BOOST_TEST(type::instances == 0);
  123. }
  124. {
  125. boost::shared_ptr<type[][2]> result =
  126. boost::make_shared_noinit<type[][2]>(2);
  127. BOOST_TEST(result.get() != 0);
  128. BOOST_TEST(result.use_count() == 1);
  129. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  130. boost::alignment_of<type>::value));
  131. BOOST_TEST(type::instances == 4);
  132. boost::weak_ptr<type[][2]> other = result;
  133. result.reset();
  134. BOOST_TEST(type::instances == 0);
  135. }
  136. {
  137. boost::shared_ptr<type[2][2]> result =
  138. boost::make_shared_noinit<type[2][2]>();
  139. BOOST_TEST(result.get() != 0);
  140. BOOST_TEST(result.use_count() == 1);
  141. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  142. boost::alignment_of<type>::value));
  143. BOOST_TEST(type::instances == 4);
  144. boost::weak_ptr<type[2][2]> other = result;
  145. result.reset();
  146. BOOST_TEST(type::instances == 0);
  147. }
  148. {
  149. boost::shared_ptr<const type[]> result =
  150. boost::make_shared_noinit<const type[]>(3);
  151. BOOST_TEST(result.get() != 0);
  152. BOOST_TEST(result.use_count() == 1);
  153. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  154. boost::alignment_of<type>::value));
  155. BOOST_TEST(type::instances == 3);
  156. boost::weak_ptr<const type[]> other = result;
  157. result.reset();
  158. BOOST_TEST(type::instances == 0);
  159. }
  160. {
  161. boost::shared_ptr<const type[3]> result =
  162. boost::make_shared_noinit<const type[3]>();
  163. BOOST_TEST(result.get() != 0);
  164. BOOST_TEST(result.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<const type[3]> other = result;
  169. result.reset();
  170. BOOST_TEST(type::instances == 0);
  171. }
  172. {
  173. boost::shared_ptr<const type[][2]> result =
  174. boost::make_shared_noinit<const type[][2]>(2);
  175. BOOST_TEST(result.get() != 0);
  176. BOOST_TEST(result.use_count() == 1);
  177. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  178. boost::alignment_of<type>::value));
  179. BOOST_TEST(type::instances == 4);
  180. boost::weak_ptr<const type[][2]> other = result;
  181. result.reset();
  182. BOOST_TEST(type::instances == 0);
  183. }
  184. {
  185. boost::shared_ptr<const type[2][2]> result =
  186. boost::make_shared_noinit<const type[2][2]>();
  187. BOOST_TEST(result.get() != 0);
  188. BOOST_TEST(result.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. boost::weak_ptr<const type[2][2]> other = result;
  193. result.reset();
  194. BOOST_TEST(type::instances == 0);
  195. }
  196. return boost::report_errors();
  197. }