swap_ranges.pass.cpp 5.0 KB

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