make_local_shared_array_noinit_test.cpp 7.3 KB

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