copy.pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // <optional>
  10. // constexpr optional(const optional<T>& rhs);
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "archetypes.h"
  16. using std::optional;
  17. template <class T, class ...InitArgs>
  18. void test(InitArgs&&... args)
  19. {
  20. const optional<T> rhs(std::forward<InitArgs>(args)...);
  21. bool rhs_engaged = static_cast<bool>(rhs);
  22. optional<T> lhs = rhs;
  23. assert(static_cast<bool>(lhs) == rhs_engaged);
  24. if (rhs_engaged)
  25. assert(*lhs == *rhs);
  26. }
  27. template <class T, class ...InitArgs>
  28. constexpr bool constexpr_test(InitArgs&&... args)
  29. {
  30. static_assert( std::is_trivially_copy_constructible_v<T>, ""); // requirement
  31. const optional<T> rhs(std::forward<InitArgs>(args)...);
  32. optional<T> lhs = rhs;
  33. return (lhs.has_value() == rhs.has_value()) &&
  34. (lhs.has_value() ? *lhs == *rhs : true);
  35. }
  36. void test_throwing_ctor() {
  37. #ifndef TEST_HAS_NO_EXCEPTIONS
  38. struct Z {
  39. Z() : count(0) {}
  40. Z(Z const& o) : count(o.count + 1)
  41. { if (count == 2) throw 6; }
  42. int count;
  43. };
  44. const Z z;
  45. const optional<Z> rhs(z);
  46. try
  47. {
  48. optional<Z> lhs(rhs);
  49. assert(false);
  50. }
  51. catch (int i)
  52. {
  53. assert(i == 6);
  54. }
  55. #endif
  56. }
  57. template <class T, class ...InitArgs>
  58. void test_ref(InitArgs&&... args)
  59. {
  60. const optional<T> rhs(std::forward<InitArgs>(args)...);
  61. bool rhs_engaged = static_cast<bool>(rhs);
  62. optional<T> lhs = rhs;
  63. assert(static_cast<bool>(lhs) == rhs_engaged);
  64. if (rhs_engaged)
  65. assert(&(*lhs) == &(*rhs));
  66. }
  67. void test_reference_extension()
  68. {
  69. #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled.
  70. using T = TestTypes::TestType;
  71. T::reset();
  72. {
  73. T t;
  74. T::reset_constructors();
  75. test_ref<T&>();
  76. test_ref<T&>(t);
  77. assert(T::alive == 1);
  78. assert(T::constructed == 0);
  79. assert(T::assigned == 0);
  80. assert(T::destroyed == 0);
  81. }
  82. assert(T::destroyed == 1);
  83. assert(T::alive == 0);
  84. {
  85. T t;
  86. const T& ct = t;
  87. T::reset_constructors();
  88. test_ref<T const&>();
  89. test_ref<T const&>(t);
  90. test_ref<T const&>(ct);
  91. assert(T::alive == 1);
  92. assert(T::constructed == 0);
  93. assert(T::assigned == 0);
  94. assert(T::destroyed == 0);
  95. }
  96. assert(T::alive == 0);
  97. assert(T::destroyed == 1);
  98. {
  99. static_assert(!std::is_copy_constructible<std::optional<T&&>>::value, "");
  100. static_assert(!std::is_copy_constructible<std::optional<T const&&>>::value, "");
  101. }
  102. #endif
  103. }
  104. int main(int, char**)
  105. {
  106. test<int>();
  107. test<int>(3);
  108. static_assert(constexpr_test<int>(), "" );
  109. static_assert(constexpr_test<int>(3), "" );
  110. {
  111. const optional<const int> o(42);
  112. optional<const int> o2(o);
  113. assert(*o2 == 42);
  114. }
  115. {
  116. using T = TestTypes::TestType;
  117. T::reset();
  118. const optional<T> rhs;
  119. assert(T::alive == 0);
  120. const optional<T> lhs(rhs);
  121. assert(lhs.has_value() == false);
  122. assert(T::alive == 0);
  123. }
  124. TestTypes::TestType::reset();
  125. {
  126. using T = TestTypes::TestType;
  127. T::reset();
  128. const optional<T> rhs(42);
  129. assert(T::alive == 1);
  130. assert(T::value_constructed == 1);
  131. assert(T::copy_constructed == 0);
  132. const optional<T> lhs(rhs);
  133. assert(lhs.has_value());
  134. assert(T::copy_constructed == 1);
  135. assert(T::alive == 2);
  136. }
  137. TestTypes::TestType::reset();
  138. {
  139. using namespace ConstexprTestTypes;
  140. test<TestType>();
  141. test<TestType>(42);
  142. }
  143. {
  144. using namespace TrivialTestTypes;
  145. test<TestType>();
  146. test<TestType>(42);
  147. }
  148. {
  149. test_throwing_ctor();
  150. }
  151. {
  152. test_reference_extension();
  153. }
  154. {
  155. constexpr std::optional<int> o1{4};
  156. constexpr std::optional<int> o2 = o1;
  157. static_assert( *o2 == 4, "" );
  158. }
  159. return 0;
  160. }