Skip to content

Commit fc4f7e5

Browse files
test iterator iter swap
1 parent ea055e2 commit fc4f7e5

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

libcxx/test/std/ranges/range.adaptors/range.concat/iterator/iter_swap.pass.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,46 @@
2121
template <class It>
2222
concept has_iter_swap = requires(It it) { std::ranges::iter_swap(it, it); };
2323

24+
struct ThrowingMove {
25+
ThrowingMove() = default;
26+
ThrowingMove(ThrowingMove&&) {}
27+
ThrowingMove& operator=(ThrowingMove&&){return *this;}
28+
};
29+
2430
template <class Iterator, bool IsNoexcept>
2531
constexpr void test() {
2632
using Sentinel = sentinel_wrapper<Iterator>;
2733
using View = minimal_view<Iterator, Sentinel>;
2834
using ConcatView = std::ranges::concat_view<View>;
2935

30-
auto make_concat_view = [](auto begin, auto end) {
31-
View view{Iterator(begin), Sentinel(Iterator(end))};
32-
return ConcatView(std::move(view));
33-
};
34-
3536
{
36-
std::array<int, 5> array{0, 1, 2, 3, 4};
37-
ConcatView view = make_concat_view(array.data(), array.data() + array.size());
38-
std::array<int, 5> another_array{5, 6, 7, 8, 9};
39-
ConcatView another_view = make_concat_view(another_array.data(), another_array.data() + another_array.size());
37+
std::array<int, 5> array1{0, 1, 2, 3, 4};
38+
std::array<int, 5> array2{5, 6, 7, 8, 9};
39+
40+
View v1{Iterator(array1.data()), Sentinel(Iterator(array1.data() + array1.size()))};
41+
View v2{Iterator(array2.data()), Sentinel(Iterator(array2.data() + array2.size()))};
42+
std::ranges::concat_view view(std::move(v1), std::move(v2));
43+
4044
auto it1 = view.begin();
41-
auto it2 = another_view.begin();
45+
auto it2 = ++view.begin();
4246

4347
static_assert(std::is_same_v<decltype(iter_swap(it1, it2)), void>);
4448
static_assert(noexcept(iter_swap(it1, it2)) == IsNoexcept);
4549

46-
assert(*it1 == 0 && *it2 == 5); // test the test
50+
assert(*it1 == 0 && *it2 == 1);
4751
iter_swap(it1, it2);
48-
assert(*it1 == 5);
52+
assert(*it1 == 1);
4953
assert(*it2 == 0);
5054
}
55+
56+
{
57+
// iter swap may throw
58+
std::array<ThrowingMove, 2> iterSwapMayThrow{};
59+
std::ranges::concat_view v(iterSwapMayThrow);
60+
auto iter1 = v.begin();
61+
auto iter2 = ++v.begin();
62+
static_assert(!noexcept(std::ranges::iter_swap(iter1, iter2)));
63+
}
5164
}
5265

5366
constexpr bool tests() {

0 commit comments

Comments
 (0)