U.pass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // XFAIL: dylib-has-no-bad_optional_access && !libcpp-no-exceptions
  11. // <optional>
  12. // template <class U>
  13. // constexpr EXPLICIT optional(U&& u);
  14. #include <optional>
  15. #include <type_traits>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "archetypes.h"
  19. #include "test_convertible.h"
  20. using std::optional;
  21. struct ImplicitThrow
  22. {
  23. constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
  24. };
  25. struct ExplicitThrow
  26. {
  27. constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
  28. };
  29. struct ImplicitAny {
  30. template <class U>
  31. constexpr ImplicitAny(U&&) {}
  32. };
  33. template <class To, class From>
  34. constexpr bool implicit_conversion(optional<To>&& opt, const From& v)
  35. {
  36. using O = optional<To>;
  37. static_assert(test_convertible<O, From>(), "");
  38. static_assert(!test_convertible<O, void*>(), "");
  39. static_assert(!test_convertible<O, From, int>(), "");
  40. return opt && *opt == static_cast<To>(v);
  41. }
  42. template <class To, class Input, class Expect>
  43. constexpr bool explicit_conversion(Input&& in, const Expect& v)
  44. {
  45. using O = optional<To>;
  46. static_assert(std::is_constructible<O, Input>::value, "");
  47. static_assert(!std::is_convertible<Input, O>::value, "");
  48. static_assert(!std::is_constructible<O, void*>::value, "");
  49. static_assert(!std::is_constructible<O, Input, int>::value, "");
  50. optional<To> opt(std::forward<Input>(in));
  51. return opt && *opt == static_cast<To>(v);
  52. }
  53. void test_implicit()
  54. {
  55. {
  56. static_assert(implicit_conversion<long long>(42, 42), "");
  57. }
  58. {
  59. static_assert(implicit_conversion<long double>(3.14, 3.14), "");
  60. }
  61. {
  62. int x = 42;
  63. optional<void* const> o(&x);
  64. assert(*o == &x);
  65. }
  66. {
  67. using T = TrivialTestTypes::TestType;
  68. static_assert(implicit_conversion<T>(42, 42), "");
  69. }
  70. {
  71. using T = TestTypes::TestType;
  72. assert(implicit_conversion<T>(3, T(3)));
  73. }
  74. {
  75. using O = optional<ImplicitAny>;
  76. static_assert(!test_convertible<O, std::in_place_t>(), "");
  77. static_assert(!test_convertible<O, std::in_place_t&>(), "");
  78. static_assert(!test_convertible<O, const std::in_place_t&>(), "");
  79. static_assert(!test_convertible<O, std::in_place_t&&>(), "");
  80. static_assert(!test_convertible<O, const std::in_place_t&&>(), "");
  81. }
  82. #ifndef TEST_HAS_NO_EXCEPTIONS
  83. {
  84. try {
  85. using T = ImplicitThrow;
  86. optional<T> t = 42;
  87. assert(false);
  88. ((void)t);
  89. } catch (int) {
  90. }
  91. }
  92. #endif
  93. }
  94. void test_explicit() {
  95. {
  96. using T = ExplicitTrivialTestTypes::TestType;
  97. static_assert(explicit_conversion<T>(42, 42), "");
  98. }
  99. {
  100. using T = ExplicitConstexprTestTypes::TestType;
  101. static_assert(explicit_conversion<T>(42, 42), "");
  102. static_assert(!std::is_convertible<int, T>::value, "");
  103. }
  104. {
  105. using T = ExplicitTestTypes::TestType;
  106. T::reset();
  107. {
  108. assert(explicit_conversion<T>(42, 42));
  109. assert(T::alive == 0);
  110. }
  111. T::reset();
  112. {
  113. optional<T> t(42);
  114. assert(T::alive == 1);
  115. assert(T::value_constructed == 1);
  116. assert(T::move_constructed == 0);
  117. assert(T::copy_constructed == 0);
  118. assert(t.value().value == 42);
  119. }
  120. assert(T::alive == 0);
  121. }
  122. #ifndef TEST_HAS_NO_EXCEPTIONS
  123. {
  124. try {
  125. using T = ExplicitThrow;
  126. optional<T> t(42);
  127. assert(false);
  128. } catch (int) {
  129. }
  130. }
  131. #endif
  132. }
  133. int main(int, char**) {
  134. test_implicit();
  135. test_explicit();
  136. return 0;
  137. }