move.pass.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: with_system_cxx_lib=macosx10.12
  11. // XFAIL: with_system_cxx_lib=macosx10.11
  12. // XFAIL: with_system_cxx_lib=macosx10.10
  13. // XFAIL: with_system_cxx_lib=macosx10.9
  14. // XFAIL: with_system_cxx_lib=macosx10.7
  15. // XFAIL: with_system_cxx_lib=macosx10.8
  16. // <optional>
  17. // constexpr optional(optional<T>&& rhs);
  18. #include <optional>
  19. #include <type_traits>
  20. #include <cassert>
  21. #include "test_macros.h"
  22. #include "archetypes.hpp"
  23. using std::optional;
  24. template <class T, class ...InitArgs>
  25. void test(InitArgs&&... args)
  26. {
  27. const optional<T> orig(std::forward<InitArgs>(args)...);
  28. optional<T> rhs(orig);
  29. bool rhs_engaged = static_cast<bool>(rhs);
  30. optional<T> lhs = std::move(rhs);
  31. assert(static_cast<bool>(lhs) == rhs_engaged);
  32. if (rhs_engaged)
  33. assert(*lhs == *orig);
  34. }
  35. template <class T, class ...InitArgs>
  36. constexpr bool constexpr_test(InitArgs&&... args)
  37. {
  38. static_assert( std::is_trivially_copy_constructible_v<T>, ""); // requirement
  39. const optional<T> orig(std::forward<InitArgs>(args)...);
  40. optional<T> rhs(orig);
  41. optional<T> lhs = std::move(rhs);
  42. return (lhs.has_value() == orig.has_value()) &&
  43. (lhs.has_value() ? *lhs == *orig : true);
  44. }
  45. void test_throwing_ctor() {
  46. #ifndef TEST_HAS_NO_EXCEPTIONS
  47. struct Z {
  48. Z() : count(0) {}
  49. Z(Z&& o) : count(o.count + 1)
  50. { if (count == 2) throw 6; }
  51. int count;
  52. };
  53. Z z;
  54. optional<Z> rhs(std::move(z));
  55. try
  56. {
  57. optional<Z> lhs(std::move(rhs));
  58. assert(false);
  59. }
  60. catch (int i)
  61. {
  62. assert(i == 6);
  63. }
  64. #endif
  65. }
  66. template <class T, class ...InitArgs>
  67. void test_ref(InitArgs&&... args)
  68. {
  69. optional<T> rhs(std::forward<InitArgs>(args)...);
  70. bool rhs_engaged = static_cast<bool>(rhs);
  71. optional<T> lhs = std::move(rhs);
  72. assert(static_cast<bool>(lhs) == rhs_engaged);
  73. if (rhs_engaged)
  74. assert(&(*lhs) == &(*rhs));
  75. }
  76. void test_reference_extension()
  77. {
  78. #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
  79. using T = TestTypes::TestType;
  80. T::reset();
  81. {
  82. T t;
  83. T::reset_constructors();
  84. test_ref<T&>();
  85. test_ref<T&>(t);
  86. assert(T::alive == 1);
  87. assert(T::constructed == 0);
  88. assert(T::assigned == 0);
  89. assert(T::destroyed == 0);
  90. }
  91. assert(T::destroyed == 1);
  92. assert(T::alive == 0);
  93. {
  94. T t;
  95. const T& ct = t;
  96. T::reset_constructors();
  97. test_ref<T const&>();
  98. test_ref<T const&>(t);
  99. test_ref<T const&>(ct);
  100. assert(T::alive == 1);
  101. assert(T::constructed == 0);
  102. assert(T::assigned == 0);
  103. assert(T::destroyed == 0);
  104. }
  105. assert(T::alive == 0);
  106. assert(T::destroyed == 1);
  107. {
  108. T t;
  109. T::reset_constructors();
  110. test_ref<T&&>();
  111. test_ref<T&&>(std::move(t));
  112. assert(T::alive == 1);
  113. assert(T::constructed == 0);
  114. assert(T::assigned == 0);
  115. assert(T::destroyed == 0);
  116. }
  117. assert(T::alive == 0);
  118. assert(T::destroyed == 1);
  119. {
  120. T t;
  121. const T& ct = t;
  122. T::reset_constructors();
  123. test_ref<T const&&>();
  124. test_ref<T const&&>(std::move(t));
  125. test_ref<T const&&>(std::move(ct));
  126. assert(T::alive == 1);
  127. assert(T::constructed == 0);
  128. assert(T::assigned == 0);
  129. assert(T::destroyed == 0);
  130. }
  131. assert(T::alive == 0);
  132. assert(T::destroyed == 1);
  133. {
  134. static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
  135. static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
  136. }
  137. #endif
  138. }
  139. int main()
  140. {
  141. test<int>();
  142. test<int>(3);
  143. static_assert(constexpr_test<int>(), "" );
  144. static_assert(constexpr_test<int>(3), "" );
  145. {
  146. optional<const int> o(42);
  147. optional<const int> o2(std::move(o));
  148. assert(*o2 == 42);
  149. }
  150. {
  151. using T = TestTypes::TestType;
  152. T::reset();
  153. optional<T> rhs;
  154. assert(T::alive == 0);
  155. const optional<T> lhs(std::move(rhs));
  156. assert(lhs.has_value() == false);
  157. assert(rhs.has_value() == false);
  158. assert(T::alive == 0);
  159. }
  160. TestTypes::TestType::reset();
  161. {
  162. using T = TestTypes::TestType;
  163. T::reset();
  164. optional<T> rhs(42);
  165. assert(T::alive == 1);
  166. assert(T::value_constructed == 1);
  167. assert(T::move_constructed == 0);
  168. const optional<T> lhs(std::move(rhs));
  169. assert(lhs.has_value());
  170. assert(rhs.has_value());
  171. assert(lhs.value().value == 42);
  172. assert(rhs.value().value == -1);
  173. assert(T::move_constructed == 1);
  174. assert(T::alive == 2);
  175. }
  176. TestTypes::TestType::reset();
  177. {
  178. using namespace ConstexprTestTypes;
  179. test<TestType>();
  180. test<TestType>(42);
  181. }
  182. {
  183. using namespace TrivialTestTypes;
  184. test<TestType>();
  185. test<TestType>(42);
  186. }
  187. {
  188. test_throwing_ctor();
  189. }
  190. {
  191. struct ThrowsMove {
  192. ThrowsMove() noexcept(false) {}
  193. ThrowsMove(ThrowsMove const&) noexcept(false) {}
  194. ThrowsMove(ThrowsMove &&) noexcept(false) {}
  195. };
  196. static_assert(!std::is_nothrow_move_constructible<optional<ThrowsMove>>::value, "");
  197. struct NoThrowMove {
  198. NoThrowMove() noexcept(false) {}
  199. NoThrowMove(NoThrowMove const&) noexcept(false) {}
  200. NoThrowMove(NoThrowMove &&) noexcept(true) {}
  201. };
  202. static_assert(std::is_nothrow_move_constructible<optional<NoThrowMove>>::value, "");
  203. }
  204. {
  205. test_reference_extension();
  206. }
  207. {
  208. constexpr std::optional<int> o1{4};
  209. constexpr std::optional<int> o2 = std::move(o1);
  210. static_assert( *o2 == 4, "" );
  211. }
  212. }