swap_ranges.pass.cpp 5.5 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. // <algorithm>
  9. // template<ForwardIterator Iter1, ForwardIterator Iter2>
  10. // requires HasSwap<Iter1::reference, Iter2::reference>
  11. // Iter2
  12. // swap_ranges(Iter1 first1, Iter1 last1, Iter2 first2);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include <memory>
  16. #include "test_macros.h"
  17. #include "test_iterators.h"
  18. template<class Iter1, class Iter2>
  19. void
  20. test()
  21. {
  22. int i[3] = {1, 2, 3};
  23. int j[3] = {4, 5, 6};
  24. Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
  25. assert(base(r) == j+3);
  26. assert(i[0] == 4);
  27. assert(i[1] == 5);
  28. assert(i[2] == 6);
  29. assert(j[0] == 1);
  30. assert(j[1] == 2);
  31. assert(j[2] == 3);
  32. }
  33. #if TEST_STD_VER >= 11
  34. template<class Iter1, class Iter2>
  35. void
  36. test1()
  37. {
  38. std::unique_ptr<int> i[3];
  39. for (int k = 0; k < 3; ++k)
  40. i[k].reset(new int(k+1));
  41. std::unique_ptr<int> j[3];
  42. for (int k = 0; k < 3; ++k)
  43. j[k].reset(new int(k+4));
  44. Iter2 r = std::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j));
  45. assert(base(r) == j+3);
  46. assert(*i[0] == 4);
  47. assert(*i[1] == 5);
  48. assert(*i[2] == 6);
  49. assert(*j[0] == 1);
  50. assert(*j[1] == 2);
  51. assert(*j[2] == 3);
  52. }
  53. #endif // TEST_STD_VER >= 11
  54. void test2()
  55. {
  56. {
  57. int src[2][2] = {{0, 1}, {2, 3}};
  58. decltype(src) dest = {{9, 8}, {7, 6}};
  59. std::swap(src, dest);
  60. assert ( src[0][0] == 9 );
  61. assert ( src[0][1] == 8 );
  62. assert ( src[1][0] == 7 );
  63. assert ( src[1][1] == 6 );
  64. assert ( dest[0][0] == 0 );
  65. assert ( dest[0][1] == 1 );
  66. assert ( dest[1][0] == 2 );
  67. assert ( dest[1][1] == 3 );
  68. }
  69. {
  70. int src[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
  71. decltype(src) dest = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
  72. std::swap(src, dest);
  73. assert ( src[0][0] == 9 );
  74. assert ( src[0][1] == 8 );
  75. assert ( src[0][2] == 7 );
  76. assert ( src[1][0] == 6 );
  77. assert ( src[1][1] == 5 );
  78. assert ( src[1][2] == 4 );
  79. assert ( src[2][0] == 3 );
  80. assert ( src[2][1] == 2 );
  81. assert ( src[2][2] == 1 );
  82. assert ( dest[0][0] == 0 );
  83. assert ( dest[0][1] == 1 );
  84. assert ( dest[0][2] == 2 );
  85. assert ( dest[1][0] == 3 );
  86. assert ( dest[1][1] == 4 );
  87. assert ( dest[1][2] == 5 );
  88. assert ( dest[2][0] == 6 );
  89. assert ( dest[2][1] == 7 );
  90. assert ( dest[2][2] == 8 );
  91. }
  92. }
  93. #if TEST_STD_VER > 17
  94. constexpr bool test_swap_constexpr()
  95. {
  96. int i[3] = {1, 2, 3};
  97. int j[3] = {4, 5, 6};
  98. std::swap_ranges(i, i+3, j);
  99. return i[0] == 4 &&
  100. i[1] == 5 &&
  101. i[2] == 6 &&
  102. j[0] == 1 &&
  103. j[1] == 2 &&
  104. j[2] == 3;
  105. }
  106. #endif // TEST_STD_VER > 17
  107. int main(int, char**)
  108. {
  109. test<forward_iterator<int*>, forward_iterator<int*> >();
  110. test<forward_iterator<int*>, bidirectional_iterator<int*> >();
  111. test<forward_iterator<int*>, random_access_iterator<int*> >();
  112. test<forward_iterator<int*>, int*>();
  113. test<bidirectional_iterator<int*>, forward_iterator<int*> >();
  114. test<bidirectional_iterator<int*>, bidirectional_iterator<int*> >();
  115. test<bidirectional_iterator<int*>, random_access_iterator<int*> >();
  116. test<bidirectional_iterator<int*>, int*>();
  117. test<random_access_iterator<int*>, forward_iterator<int*> >();
  118. test<random_access_iterator<int*>, bidirectional_iterator<int*> >();
  119. test<random_access_iterator<int*>, random_access_iterator<int*> >();
  120. test<random_access_iterator<int*>, int*>();
  121. test<int*, forward_iterator<int*> >();
  122. test<int*, bidirectional_iterator<int*> >();
  123. test<int*, random_access_iterator<int*> >();
  124. test<int*, int*>();
  125. #if TEST_STD_VER >= 11
  126. test1<forward_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
  127. test1<forward_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
  128. test1<forward_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
  129. test1<forward_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
  130. test1<bidirectional_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
  131. test1<bidirectional_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
  132. test1<bidirectional_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
  133. test1<bidirectional_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
  134. test1<random_access_iterator<std::unique_ptr<int>*>, forward_iterator<std::unique_ptr<int>*> >();
  135. test1<random_access_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
  136. test1<random_access_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
  137. test1<random_access_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
  138. test1<std::unique_ptr<int>*, forward_iterator<std::unique_ptr<int>*> >();
  139. test1<std::unique_ptr<int>*, bidirectional_iterator<std::unique_ptr<int>*> >();
  140. test1<std::unique_ptr<int>*, random_access_iterator<std::unique_ptr<int>*> >();
  141. test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
  142. #endif // TEST_STD_VER >= 11
  143. #if TEST_STD_VER > 17
  144. static_assert(test_swap_constexpr());
  145. #endif // TEST_STD_VER > 17
  146. test2();
  147. return 0;
  148. }