swap_array.pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // <utility>
  9. // template<ValueType T, size_t N>
  10. // requires Swappable<T>
  11. // void
  12. // swap(T (&a)[N], T (&b)[N]);
  13. #include <utility>
  14. #include <cassert>
  15. #include <memory>
  16. #include "test_macros.h"
  17. #if TEST_STD_VER >= 11
  18. struct CopyOnly {
  19. CopyOnly() {}
  20. CopyOnly(CopyOnly const&) noexcept {}
  21. CopyOnly& operator=(CopyOnly const&) { return *this; }
  22. };
  23. struct NoexceptMoveOnly {
  24. NoexceptMoveOnly() {}
  25. NoexceptMoveOnly(NoexceptMoveOnly&&) noexcept {}
  26. NoexceptMoveOnly& operator=(NoexceptMoveOnly&&) noexcept { return *this; }
  27. };
  28. struct NotMoveConstructible {
  29. NotMoveConstructible() {}
  30. NotMoveConstructible& operator=(NotMoveConstructible&&) { return *this; }
  31. private:
  32. NotMoveConstructible(NotMoveConstructible&&);
  33. };
  34. template <class Tp>
  35. auto can_swap_test(int) -> decltype(std::swap(std::declval<Tp>(), std::declval<Tp>()));
  36. template <class Tp>
  37. auto can_swap_test(...) -> std::false_type;
  38. template <class Tp>
  39. constexpr bool can_swap() {
  40. return std::is_same<decltype(can_swap_test<Tp>(0)), void>::value;
  41. }
  42. #endif
  43. #if TEST_STD_VER > 17
  44. constexpr bool test_swap_constexpr()
  45. {
  46. int i[3] = {1, 2, 3};
  47. int j[3] = {4, 5, 6};
  48. std::swap(i, j);
  49. return i[0] == 4 &&
  50. i[1] == 5 &&
  51. i[2] == 6 &&
  52. j[0] == 1 &&
  53. j[1] == 2 &&
  54. j[2] == 3;
  55. }
  56. #endif // TEST_STD_VER > 17
  57. int main(int, char**)
  58. {
  59. {
  60. int i[3] = {1, 2, 3};
  61. int j[3] = {4, 5, 6};
  62. std::swap(i, j);
  63. assert(i[0] == 4);
  64. assert(i[1] == 5);
  65. assert(i[2] == 6);
  66. assert(j[0] == 1);
  67. assert(j[1] == 2);
  68. assert(j[2] == 3);
  69. }
  70. #if TEST_STD_VER >= 11
  71. {
  72. std::unique_ptr<int> i[3];
  73. for (int k = 0; k < 3; ++k)
  74. i[k].reset(new int(k+1));
  75. std::unique_ptr<int> j[3];
  76. for (int k = 0; k < 3; ++k)
  77. j[k].reset(new int(k+4));
  78. std::swap(i, j);
  79. assert(*i[0] == 4);
  80. assert(*i[1] == 5);
  81. assert(*i[2] == 6);
  82. assert(*j[0] == 1);
  83. assert(*j[1] == 2);
  84. assert(*j[2] == 3);
  85. }
  86. {
  87. using CA = CopyOnly[42];
  88. using MA = NoexceptMoveOnly[42];
  89. using NA = NotMoveConstructible[42];
  90. static_assert(can_swap<CA&>(), "");
  91. static_assert(can_swap<MA&>(), "");
  92. static_assert(!can_swap<NA&>(), "");
  93. CA ca;
  94. MA ma;
  95. static_assert(!noexcept(std::swap(ca, ca)), "");
  96. static_assert(noexcept(std::swap(ma, ma)), "");
  97. }
  98. #endif
  99. #if TEST_STD_VER > 17
  100. static_assert(test_swap_constexpr());
  101. #endif // TEST_STD_VER > 17
  102. return 0;
  103. }