copy.pass.cpp 3.6 KB

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