make_local_shared_array_test.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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<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. BOOST_TEST(result[0] == 0);
  47. BOOST_TEST(result[1] == 0);
  48. BOOST_TEST(result[2] == 0);
  49. }
  50. {
  51. boost::local_shared_ptr<int[3]> result =
  52. boost::make_local_shared<int[3]>();
  53. BOOST_TEST(result.get() != 0);
  54. BOOST_TEST(result.local_use_count() == 1);
  55. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  56. boost::alignment_of<int>::value));
  57. BOOST_TEST(result[0] == 0);
  58. BOOST_TEST(result[1] == 0);
  59. BOOST_TEST(result[2] == 0);
  60. }
  61. {
  62. boost::local_shared_ptr<int[][2]> result =
  63. boost::make_local_shared<int[][2]>(2);
  64. BOOST_TEST(result.get() != 0);
  65. BOOST_TEST(result.local_use_count() == 1);
  66. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  67. boost::alignment_of<int>::value));
  68. BOOST_TEST(result[0][0] == 0);
  69. BOOST_TEST(result[0][1] == 0);
  70. BOOST_TEST(result[1][0] == 0);
  71. BOOST_TEST(result[1][1] == 0);
  72. }
  73. {
  74. boost::local_shared_ptr<int[2][2]> result =
  75. boost::make_local_shared<int[2][2]>();
  76. BOOST_TEST(result.get() != 0);
  77. BOOST_TEST(result.local_use_count() == 1);
  78. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  79. boost::alignment_of<int>::value));
  80. BOOST_TEST(result[0][0] == 0);
  81. BOOST_TEST(result[0][1] == 0);
  82. BOOST_TEST(result[1][0] == 0);
  83. BOOST_TEST(result[1][1] == 0);
  84. }
  85. {
  86. boost::local_shared_ptr<const int[]> result =
  87. boost::make_local_shared<const int[]>(3);
  88. BOOST_TEST(result.get() != 0);
  89. BOOST_TEST(result.local_use_count() == 1);
  90. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  91. boost::alignment_of<int>::value));
  92. BOOST_TEST(result[0] == 0);
  93. BOOST_TEST(result[1] == 0);
  94. BOOST_TEST(result[2] == 0);
  95. }
  96. {
  97. boost::local_shared_ptr<const int[3]> result =
  98. boost::make_local_shared<const int[3]>();
  99. BOOST_TEST(result.get() != 0);
  100. BOOST_TEST(result.local_use_count() == 1);
  101. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  102. boost::alignment_of<int>::value));
  103. BOOST_TEST(result[0] == 0);
  104. BOOST_TEST(result[1] == 0);
  105. BOOST_TEST(result[2] == 0);
  106. }
  107. {
  108. boost::local_shared_ptr<const int[][2]> result =
  109. boost::make_local_shared<const int[][2]>(2);
  110. BOOST_TEST(result.get() != 0);
  111. BOOST_TEST(result.local_use_count() == 1);
  112. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  113. boost::alignment_of<int>::value));
  114. BOOST_TEST(result[0][0] == 0);
  115. BOOST_TEST(result[0][1] == 0);
  116. BOOST_TEST(result[1][0] == 0);
  117. BOOST_TEST(result[1][1] == 0);
  118. }
  119. {
  120. boost::local_shared_ptr<const int[2][2]> result =
  121. boost::make_local_shared<const int[2][2]>();
  122. BOOST_TEST(result.get() != 0);
  123. BOOST_TEST(result.local_use_count() == 1);
  124. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  125. boost::alignment_of<int>::value));
  126. BOOST_TEST(result[0][0] == 0);
  127. BOOST_TEST(result[0][1] == 0);
  128. BOOST_TEST(result[1][0] == 0);
  129. BOOST_TEST(result[1][1] == 0);
  130. }
  131. {
  132. boost::local_shared_ptr<type[]> result =
  133. boost::make_local_shared<type[]>(3);
  134. BOOST_TEST(result.get() != 0);
  135. BOOST_TEST(result.local_use_count() == 1);
  136. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  137. boost::alignment_of<type>::value));
  138. BOOST_TEST(type::instances == 3);
  139. boost::weak_ptr<type[]> w1 = result;
  140. result.reset();
  141. BOOST_TEST(type::instances == 0);
  142. }
  143. {
  144. boost::local_shared_ptr<type[3]> result =
  145. boost::make_local_shared<type[3]>();
  146. BOOST_TEST(result.get() != 0);
  147. BOOST_TEST(result.local_use_count() == 1);
  148. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  149. boost::alignment_of<type>::value));
  150. BOOST_TEST(type::instances == 3);
  151. boost::weak_ptr<type[3]> w1 = result;
  152. result.reset();
  153. BOOST_TEST(type::instances == 0);
  154. }
  155. {
  156. boost::local_shared_ptr<type[][2]> result =
  157. boost::make_local_shared<type[][2]>(2);
  158. BOOST_TEST(result.get() != 0);
  159. BOOST_TEST(result.local_use_count() == 1);
  160. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  161. boost::alignment_of<type>::value));
  162. BOOST_TEST(type::instances == 4);
  163. result.reset();
  164. BOOST_TEST(type::instances == 0);
  165. }
  166. {
  167. boost::local_shared_ptr<type[2][2]> result =
  168. boost::make_local_shared<type[2][2]>();
  169. BOOST_TEST(result.get() != 0);
  170. BOOST_TEST(result.local_use_count() == 1);
  171. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  172. boost::alignment_of<type>::value));
  173. BOOST_TEST(type::instances == 4);
  174. result.reset();
  175. BOOST_TEST(type::instances == 0);
  176. }
  177. {
  178. boost::local_shared_ptr<const type[]> result =
  179. boost::make_local_shared<const type[]>(3);
  180. BOOST_TEST(result.get() != 0);
  181. BOOST_TEST(result.local_use_count() == 1);
  182. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  183. boost::alignment_of<type>::value));
  184. BOOST_TEST(type::instances == 3);
  185. result.reset();
  186. BOOST_TEST(type::instances == 0);
  187. }
  188. {
  189. boost::local_shared_ptr<const type[3]> result =
  190. boost::make_local_shared<const type[3]>();
  191. BOOST_TEST(result.get() != 0);
  192. BOOST_TEST(result.local_use_count() == 1);
  193. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  194. boost::alignment_of<type>::value));
  195. BOOST_TEST(type::instances == 3);
  196. result.reset();
  197. BOOST_TEST(type::instances == 0);
  198. }
  199. {
  200. boost::local_shared_ptr<const type[][2]> result =
  201. boost::make_local_shared<const type[][2]>(2);
  202. BOOST_TEST(result.get() != 0);
  203. BOOST_TEST(result.local_use_count() == 1);
  204. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  205. boost::alignment_of<type>::value));
  206. BOOST_TEST(type::instances == 4);
  207. result.reset();
  208. BOOST_TEST(type::instances == 0);
  209. }
  210. {
  211. boost::local_shared_ptr<const type[2][2]> result =
  212. boost::make_local_shared<const type[2][2]>();
  213. BOOST_TEST(result.get() != 0);
  214. BOOST_TEST(result.local_use_count() == 1);
  215. BOOST_TEST(boost::alignment::is_aligned(result.get(),
  216. boost::alignment_of<type>::value));
  217. BOOST_TEST(type::instances == 4);
  218. result.reset();
  219. BOOST_TEST(type::instances == 0);
  220. }
  221. return boost::report_errors();
  222. }
  223. #else
  224. int main()
  225. {
  226. return 0;
  227. }
  228. #endif