unique_ptr_types.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Howard Hinnant 2009
  4. // (C) Copyright Ion Gaztanaga 2014-2014.
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/move for documentation.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. #include <boost/move/utility_core.hpp>
  14. #include <boost/move/unique_ptr.hpp>
  15. #include <boost/move/detail/type_traits.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. //////////////////////////////////////////////
  19. //
  20. // The initial implementation of these tests
  21. // was written by Howard Hinnant.
  22. //
  23. // These test were later refactored grouping
  24. // and porting them to Boost.Move.
  25. //
  26. // Many thanks to Howard for releasing his C++03
  27. // unique_ptr implementation with such detailed
  28. // test cases.
  29. //
  30. //////////////////////////////////////////////
  31. #include "unique_ptr_test_utils_beg.hpp"
  32. namespace bml = ::boost::movelib;
  33. namespace bmupmu = ::boost::move_upmu;
  34. ////////////////////////////////
  35. // unique_ptr_pointer_type
  36. ////////////////////////////////
  37. namespace unique_ptr_pointer_type {
  38. struct Deleter
  39. {
  40. struct pointer {};
  41. };
  42. // Test unique_ptr::pointer type
  43. void test()
  44. {
  45. //Single unique_ptr
  46. {
  47. typedef bml::unique_ptr<int> P;
  48. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value));
  49. }
  50. {
  51. typedef bml::unique_ptr<int, Deleter> P;
  52. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value));
  53. }
  54. //Unbounded array unique_ptr
  55. {
  56. typedef bml::unique_ptr<int[]> P;
  57. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value));
  58. }
  59. {
  60. typedef bml::unique_ptr<int[], Deleter> P;
  61. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value));
  62. }
  63. //Bounded array unique_ptr
  64. {
  65. typedef bml::unique_ptr<int[5]> P;
  66. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, int*>::value));
  67. }
  68. {
  69. typedef bml::unique_ptr<int[5], Deleter> P;
  70. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value));
  71. }
  72. //Unbounded array of bounded array unique_ptr
  73. {
  74. typedef int int_5_t [5];
  75. typedef bml::unique_ptr<int_5_t[]> P;
  76. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, int_5_t*>::value));
  77. }
  78. {
  79. typedef int int_5_t [5];
  80. typedef bml::unique_ptr<int_5_t[], Deleter> P;
  81. BOOST_STATIC_ASSERT((bmupmu::is_same<P::pointer, Deleter::pointer>::value));
  82. }
  83. }
  84. } //namespace unique_ptr_pointer_type {
  85. ////////////////////////////////
  86. // unique_ptr_deleter_type
  87. ////////////////////////////////
  88. namespace unique_ptr_deleter_type {
  89. struct Deleter
  90. {};
  91. // Test unique_ptr::deleter type
  92. void test()
  93. {
  94. //Single unique_ptr
  95. {
  96. typedef bml::unique_ptr<int> P;
  97. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int> >::value));
  98. }
  99. {
  100. typedef bml::unique_ptr<int, Deleter> P;
  101. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value));
  102. }
  103. //Unbounded array unique_ptr
  104. {
  105. typedef bml::unique_ptr<int[]> P;
  106. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int[]> >::value));
  107. }
  108. {
  109. typedef bml::unique_ptr<int[], Deleter> P;
  110. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value));
  111. }
  112. //Bounded array unique_ptr
  113. {
  114. typedef bml::unique_ptr<int[2]> P;
  115. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, bml::default_delete<int[2]> >::value));
  116. }
  117. {
  118. typedef bml::unique_ptr<int[2], Deleter> P;
  119. BOOST_STATIC_ASSERT((bmupmu::is_same<P::deleter_type, Deleter >::value));
  120. }
  121. }
  122. } //namespace unique_ptr_deleter_type {
  123. ////////////////////////////////
  124. // unique_ptr_element_type
  125. ////////////////////////////////
  126. namespace unique_ptr_element_type {
  127. // Test unique_ptr::deleter type
  128. void test()
  129. {
  130. //Single unique_ptr
  131. {
  132. typedef bml::unique_ptr<const int> P;
  133. BOOST_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value));
  134. }
  135. //Unbounded array unique_ptr
  136. {
  137. typedef bml::unique_ptr<const int[]> P;
  138. BOOST_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value));
  139. }
  140. //Bounded array unique_ptr
  141. {
  142. typedef bml::unique_ptr<const int[2]> P;
  143. BOOST_STATIC_ASSERT((bmupmu::is_same<P::element_type, const int>::value));
  144. }
  145. }
  146. } //namespace unique_ptr_element_type {
  147. ////////////////////////////////
  148. // unique_ptr_construct_assign_traits
  149. ////////////////////////////////
  150. namespace unique_ptr_construct_assign_traits {
  151. void test()
  152. {
  153. typedef bml::unique_ptr<int> unique_ptr_t;
  154. //Even if BOOST_MOVE_TT_CXX11_IS_COPY_CONSTRUCTIBLE is not defined
  155. //boost::unique_ptr shall work with boost::movelib traits
  156. BOOST_STATIC_ASSERT(!(boost::move_detail::is_copy_constructible<unique_ptr_t>::value));
  157. //Even if BOOST_MOVE_TT_CXX11_IS_COPY_ASSIGNABLE is not defined
  158. //boost::unique_ptr shall work with boost::movelib traits
  159. BOOST_STATIC_ASSERT(!(boost::move_detail::is_copy_assignable<unique_ptr_t>::value));
  160. //Important traits for containers like boost::vector
  161. BOOST_STATIC_ASSERT(!(boost::move_detail::is_trivially_copy_constructible<unique_ptr_t>::value));
  162. BOOST_STATIC_ASSERT(!(boost::move_detail::is_trivially_copy_assignable<unique_ptr_t>::value));
  163. }
  164. } //namespace unique_ptr_construct_assign_traits {
  165. ////////////////////////////////
  166. // main
  167. ////////////////////////////////
  168. int main()
  169. {
  170. //General
  171. unique_ptr_pointer_type::test();
  172. unique_ptr_deleter_type::test();
  173. unique_ptr_element_type::test();
  174. unique_ptr_construct_assign_traits::test();
  175. //Test results
  176. return boost::report_errors();
  177. }
  178. #include "unique_ptr_test_utils_end.hpp"