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++03, c++11, c++14, c++17, c++20
10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
11 
12 // friend constexpr void iter_swap(const iterator& l, const iterator& r) noexcept(see below)
13 //   requires (indirectly_­swappable<iterator_t<maybe-const<Const, Views>>> && ...);
14 
15 #include <array>
16 #include <cassert>
17 #include <ranges>
18 
19 #include "../types.h"
20 
21 struct ThrowingMove {
22   ThrowingMove() = default;
ThrowingMoveThrowingMove23   ThrowingMove(ThrowingMove&&){};
operator =ThrowingMove24   ThrowingMove& operator=(ThrowingMove&&){return *this;}
25 };
26 
test()27 constexpr bool test() {
28   {
29     std::array a1{1, 2, 3, 4};
30     std::array a2{0.1, 0.2, 0.3};
31     std::ranges::zip_view v(a1, a2);
32     auto iter1 = v.begin();
33     auto iter2 = ++v.begin();
34 
35     std::ranges::iter_swap(iter1, iter2);
36 
37     assert(a1[0] == 2);
38     assert(a1[1] == 1);
39     assert(a2[0] == 0.2);
40     assert(a2[1] == 0.1);
41 
42     auto [x1, y1] = *iter1;
43     assert(&x1 == &a1[0]);
44     assert(&y1 == &a2[0]);
45 
46     auto [x2, y2] = *iter2;
47     assert(&x2 == &a1[1]);
48     assert(&y2 == &a2[1]);
49 
50     static_assert(noexcept(std::ranges::iter_swap(iter1, iter2)));
51   }
52 
53   {
54     // underlying iter_swap may throw
55     std::array<ThrowingMove, 2> iterSwapMayThrow{};
56     std::ranges::zip_view v(iterSwapMayThrow);
57     auto iter1 = v.begin();
58     auto iter2 = ++v.begin();
59     static_assert(!noexcept(std::ranges::iter_swap(iter1, iter2)));
60   }
61 
62   {
63     // underlying iterators' iter_move are called through ranges::iter_swap
64     adltest::IterMoveSwapRange r1, r2;
65     assert(r1.iter_swap_called_times == 0);
66     assert(r2.iter_swap_called_times == 0);
67 
68     std::ranges::zip_view v{r1, r2};
69     auto it1 = v.begin();
70     auto it2 = std::ranges::next(it1, 3);
71 
72     std::ranges::iter_swap(it1, it2);
73     assert(r1.iter_swap_called_times == 2);
74     assert(r2.iter_swap_called_times == 2);
75 
76     std::ranges::iter_swap(it1, it2);
77     assert(r1.iter_swap_called_times == 4);
78     assert(r2.iter_swap_called_times == 4);
79   }
80   return true;
81 }
82 
main(int,char **)83 int main(int, char**) {
84   test();
85   static_assert(test());
86 
87   return 0;
88 }
89