move.pass.cpp 5.9 KB

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