xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.zip/iterator/iter_move.pass.cpp (revision f5832bab6f5024cabe32a9f668b7f44e6b7cfef5)
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 
11 // friend constexpr auto iter_move(const iterator& i) noexcept(see below);
12 
13 #include <array>
14 #include <cassert>
15 #include <iterator>
16 #include <ranges>
17 #include <tuple>
18 
19 #include "../types.h"
20 
21 struct ThrowingMove {
22   ThrowingMove() = default;
ThrowingMoveThrowingMove23   ThrowingMove(ThrowingMove&&) {}
24 };
25 
test()26 constexpr bool test() {
27   {
28     // underlying iter_move noexcept
29     std::array a1{1, 2, 3, 4};
30     const std::array a2{3.0, 4.0};
31 
32     std::ranges::zip_view v(a1, a2, std::views::iota(3L));
33     assert(std::ranges::iter_move(v.begin()) == std::make_tuple(1, 3.0, 3L));
34     static_assert(std::is_same_v<decltype(std::ranges::iter_move(v.begin())), std::tuple<int&&, const double&&, long>>);
35 
36     auto it = v.begin();
37     static_assert(noexcept(std::ranges::iter_move(it)));
38   }
39 
40   {
41     // underlying iter_move may throw
42     auto throwingMoveRange =
43         std::views::iota(0, 2) | std::views::transform([](auto) noexcept { return ThrowingMove{}; });
44     std::ranges::zip_view v(throwingMoveRange);
45     auto it = v.begin();
46     static_assert(!noexcept(std::ranges::iter_move(it)));
47   }
48 
49   {
50     // underlying iterators' iter_move are called through ranges::iter_move
51     adltest::IterMoveSwapRange r1{}, r2{};
52     assert(r1.iter_move_called_times == 0);
53     assert(r2.iter_move_called_times == 0);
54     std::ranges::zip_view v(r1, r2);
55     auto it = v.begin();
56     {
57       [[maybe_unused]] auto&& i = std::ranges::iter_move(it);
58       assert(r1.iter_move_called_times == 1);
59       assert(r2.iter_move_called_times == 1);
60     }
61     {
62       [[maybe_unused]] auto&& i = std::ranges::iter_move(it);
63       assert(r1.iter_move_called_times == 2);
64       assert(r2.iter_move_called_times == 2);
65     }
66   }
67 
68   return true;
69 }
70 
main(int,char **)71 int main(int, char**) {
72   test();
73   static_assert(test());
74 
75   return 0;
76 }
77