Skip to content

Commit ffc5d2b

Browse files
authored
[libc++][test] Refactor tests for ranges::swap_range algorithms (#121138)
This PR refactors tests for `ranges::swap_range`, `std::{swap_range, iter_swap, swap}` algorithms to eliminate redundant code.
1 parent 6c2e170 commit ffc5d2b

File tree

4 files changed

+290
-348
lines changed

4 files changed

+290
-348
lines changed

libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/iter_swap.pass.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,43 @@
1010

1111
// template<Iterator Iter1, Iterator Iter2>
1212
// requires HasSwap<Iter1::reference, Iter2::reference>
13-
// void
14-
// iter_swap(Iter1 a, Iter2 b);
13+
// void iter_swap(Iter1 a, Iter2 b); // constexpr since C++20
1514

1615
#include <algorithm>
1716
#include <cassert>
1817

1918
#include "test_macros.h"
20-
21-
#if TEST_STD_VER > 17
22-
constexpr bool test_swap_constexpr()
23-
{
24-
int i = 1;
25-
int j = 2;
26-
std::iter_swap(&i, &j);
27-
return i == 2 && j == 1;
19+
#include "test_iterators.h"
20+
#include "type_algorithms.h"
21+
22+
struct TestIterators {
23+
template <class Iter>
24+
TEST_CONSTEXPR_CXX20 void operator()() {
25+
types::for_each(types::forward_iterator_list<int*>(), TestImpl<Iter>());
26+
}
27+
28+
template <class Iter1>
29+
struct TestImpl {
30+
template <class Iter2>
31+
TEST_CONSTEXPR_CXX20 void operator()() {
32+
int i = 1;
33+
int j = 2;
34+
std::iter_swap(Iter1(&i), Iter2(&j));
35+
assert(i == 2 && j == 1);
36+
}
37+
};
38+
};
39+
40+
TEST_CONSTEXPR_CXX20 bool test() {
41+
types::for_each(types::forward_iterator_list<int*>(), TestIterators());
42+
return true;
2843
}
29-
#endif // TEST_STD_VER > 17
30-
31-
int main(int, char**)
32-
{
33-
int i = 1;
34-
int j = 2;
35-
std::iter_swap(&i, &j);
36-
assert(i == 2);
37-
assert(j == 1);
38-
39-
#if TEST_STD_VER > 17
40-
static_assert(test_swap_constexpr());
41-
#endif // TEST_STD_VER > 17
44+
45+
int main(int, char**) {
46+
test();
47+
#if TEST_STD_VER >= 20
48+
static_assert(test());
49+
#endif
4250

4351
return 0;
4452
}

libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/ranges.swap_ranges.pass.cpp

Lines changed: 47 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,45 @@
2424
#include <cassert>
2525
#include <ranges>
2626

27+
#include <cstdio>
28+
2729
#include "test_iterators.h"
30+
#include "type_algorithms.h"
2831

2932
constexpr void test_different_lengths() {
30-
using Expected = std::ranges::swap_ranges_result<int*, int*>;
31-
int i[3] = {1, 2, 3};
32-
int j[1] = {4};
33+
using Expected = std::ranges::swap_ranges_result<int*, int*>;
34+
int i[3] = {1, 2, 3};
35+
int j[1] = {4};
3336
std::same_as<Expected> auto r = std::ranges::swap_ranges(i, i + 3, j, j + 1);
3437
assert(r.in1 == i + 1);
3538
assert(r.in2 == j + 1);
36-
assert(i[0] == 4);
37-
assert(i[1] == 2);
38-
assert(i[2] == 3);
39-
assert(j[0] == 1);
39+
assert(std::ranges::equal(i, std::array{4, 2, 3}));
40+
assert(std::ranges::equal(j, std::array{1}));
4041
std::same_as<Expected> auto r2 = std::ranges::swap_ranges(i, j);
4142
assert(r2.in1 == i + 1);
4243
assert(r2.in2 == j + 1);
43-
assert(i[0] == 1);
44-
assert(i[1] == 2);
45-
assert(i[2] == 3);
46-
assert(j[0] == 4);
44+
assert(std::ranges::equal(i, std::array{1, 2, 3}));
45+
assert(std::ranges::equal(j, std::array{4}));
4746
std::same_as<Expected> auto r3 = std::ranges::swap_ranges(j, j + 1, i, i + 3);
4847
assert(r3.in1 == j + 1);
4948
assert(r3.in2 == i + 1);
50-
assert(i[0] == 4);
51-
assert(i[1] == 2);
52-
assert(i[2] == 3);
53-
assert(j[0] == 1);
49+
assert(std::ranges::equal(i, std::array{4, 2, 3}));
50+
assert(std::ranges::equal(j, std::array{1}));
5451
std::same_as<Expected> auto r4 = std::ranges::swap_ranges(j, i);
5552
assert(r4.in1 == j + 1);
5653
assert(r4.in2 == i + 1);
57-
assert(i[0] == 1);
58-
assert(i[1] == 2);
59-
assert(i[2] == 3);
60-
assert(j[0] == 4);
54+
assert(std::ranges::equal(i, std::array{1, 2, 3}));
55+
assert(std::ranges::equal(j, std::array{4}));
6156
}
6257

6358
constexpr void test_range() {
6459
std::array r1 = {1, 2, 3};
6560
std::array r2 = {4, 5, 6};
6661

67-
68-
std::same_as<std::ranges::in_in_result<std::array<int, 3>::iterator, std::array<int, 3>::iterator>> auto r = std::ranges::swap_ranges(r1, r2);
62+
std::same_as<std::ranges::in_in_result<std::array<int, 3>::iterator, std::array<int, 3>::iterator>> auto r =
63+
std::ranges::swap_ranges(r1, r2);
6964
assert(r.in1 == r1.end());
7065
assert(r.in2 == r2.end());
71-
7266
assert((r1 == std::array{4, 5, 6}));
7367
assert((r2 == std::array{1, 2, 3}));
7468
}
@@ -78,142 +72,82 @@ constexpr void test_borrowed_input_range() {
7872
int r1[] = {1, 2, 3};
7973
int r2[] = {4, 5, 6};
8074
std::ranges::swap_ranges(std::views::all(r1), r2);
81-
assert(r1[0] == 4);
82-
assert(r1[1] == 5);
83-
assert(r1[2] == 6);
84-
assert(r2[0] == 1);
85-
assert(r2[1] == 2);
86-
assert(r2[2] == 3);
75+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
76+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
8777
}
8878
{
8979
int r1[] = {1, 2, 3};
9080
int r2[] = {4, 5, 6};
9181
std::ranges::swap_ranges(r1, std::views::all(r2));
92-
assert(r1[0] == 4);
93-
assert(r1[1] == 5);
94-
assert(r1[2] == 6);
95-
assert(r2[0] == 1);
96-
assert(r2[1] == 2);
97-
assert(r2[2] == 3);
82+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
83+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
9884
}
9985
{
10086
int r1[] = {1, 2, 3};
10187
int r2[] = {4, 5, 6};
10288
std::ranges::swap_ranges(std::views::all(r1), std::views::all(r2));
103-
assert(r1[0] == 4);
104-
assert(r1[1] == 5);
105-
assert(r1[2] == 6);
106-
assert(r2[0] == 1);
107-
assert(r2[1] == 2);
108-
assert(r2[2] == 3);
89+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
90+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
10991
}
11092
}
11193

11294
constexpr void test_sentinel() {
113-
int i[3] = {1, 2, 3};
114-
int j[3] = {4, 5, 6};
115-
using It = cpp17_input_iterator<int*>;
116-
using Sent = sentinel_wrapper<It>;
117-
using Expected = std::ranges::swap_ranges_result<It, It>;
118-
std::same_as<Expected> auto r =
119-
std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3)));
95+
int i[3] = {1, 2, 3};
96+
int j[3] = {4, 5, 6};
97+
using It = cpp17_input_iterator<int*>;
98+
using Sent = sentinel_wrapper<It>;
99+
using Expected = std::ranges::swap_ranges_result<It, It>;
100+
std::same_as<Expected> auto r = std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3)));
120101
assert(base(r.in1) == i + 3);
121102
assert(base(r.in2) == j + 3);
122-
assert(i[0] == 4);
123-
assert(i[1] == 5);
124-
assert(i[2] == 6);
125-
assert(j[0] == 1);
126-
assert(j[1] == 2);
127-
assert(j[2] == 3);
103+
assert(std::ranges::equal(i, std::array{4, 5, 6}));
104+
assert(std::ranges::equal(j, std::array{1, 2, 3}));
128105
}
129106

130107
template <class Iter1, class Iter2>
131-
constexpr void test_iterators() {
108+
TEST_CONSTEXPR_CXX20 void test_iterators() {
132109
using Expected = std::ranges::swap_ranges_result<Iter1, Iter2>;
133-
int i[3] = {1, 2, 3};
134-
int j[3] = {4, 5, 6};
110+
int a[3] = {1, 2, 3};
111+
int b[3] = {4, 5, 6};
135112
std::same_as<Expected> auto r =
136-
std::ranges::swap_ranges(Iter1(i), sentinel_wrapper(Iter1(i + 3)), Iter2(j), sentinel_wrapper(Iter2(j + 3)));
137-
assert(base(r.in1) == i + 3);
138-
assert(base(r.in2) == j + 3);
139-
assert(i[0] == 4);
140-
assert(i[1] == 5);
141-
assert(i[2] == 6);
142-
assert(j[0] == 1);
143-
assert(j[1] == 2);
144-
assert(j[2] == 3);
113+
std::ranges::swap_ranges(Iter1(a), sentinel_wrapper(Iter1(a + 3)), Iter2(b), sentinel_wrapper(Iter2(b + 3)));
114+
assert(base(r.in1) == a + 3);
115+
assert(base(r.in2) == b + 3);
116+
assert(std::ranges::equal(a, std::array{4, 5, 6}));
117+
assert(std::ranges::equal(b, std::array{1, 2, 3}));
145118
}
146119

147120
constexpr void test_rval_range() {
148121
{
149-
using Expected = std::ranges::swap_ranges_result<std::array<int, 3>::iterator, std::ranges::dangling>;
122+
using Expected = std::ranges::swap_ranges_result<std::array<int, 3>::iterator, std::ranges::dangling>;
150123
std::array<int, 3> r = {1, 2, 3};
151124
std::same_as<Expected> auto a = std::ranges::swap_ranges(r, std::array{4, 5, 6});
152125
assert((r == std::array{4, 5, 6}));
153126
assert(a.in1 == r.begin() + 3);
154127
}
155128
{
156129
std::array<int, 3> r = {1, 2, 3};
157-
using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, std::array<int, 3>::iterator>;
130+
using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, std::array<int, 3>::iterator>;
158131
std::same_as<Expected> auto b = std::ranges::swap_ranges(std::array{4, 5, 6}, r);
159132
assert((r == std::array{4, 5, 6}));
160133
assert(b.in2 == r.begin() + 3);
161134
}
162135
}
163136

164-
template <class Out>
165-
constexpr void test_proxy_in_iterators() {
166-
test_iterators<ProxyIterator<cpp20_input_iterator<int*>>, Out>();
167-
test_iterators<ProxyIterator<forward_iterator<int*>>, Out>();
168-
test_iterators<ProxyIterator<bidirectional_iterator<int*>>, Out>();
169-
test_iterators<ProxyIterator<random_access_iterator<int*>>, Out>();
170-
test_iterators<ProxyIterator<contiguous_iterator<int*>>, Out>();
171-
}
172-
173137
constexpr bool test() {
174138
test_range();
175-
176-
test_iterators<cpp20_input_iterator<int*>, cpp20_input_iterator<int*>>();
177-
test_iterators<cpp20_input_iterator<int*>, forward_iterator<int*>>();
178-
test_iterators<cpp20_input_iterator<int*>, bidirectional_iterator<int*>>();
179-
test_iterators<cpp20_input_iterator<int*>, random_access_iterator<int*>>();
180-
test_iterators<cpp20_input_iterator<int*>, int*>();
181-
182-
test_iterators<forward_iterator<int*>, cpp20_input_iterator<int*>>();
183-
test_iterators<forward_iterator<int*>, forward_iterator<int*>>();
184-
test_iterators<forward_iterator<int*>, bidirectional_iterator<int*>>();
185-
test_iterators<forward_iterator<int*>, random_access_iterator<int*>>();
186-
test_iterators<forward_iterator<int*>, int*>();
187-
188-
test_iterators<bidirectional_iterator<int*>, cpp20_input_iterator<int*>>();
189-
test_iterators<bidirectional_iterator<int*>, forward_iterator<int*>>();
190-
test_iterators<bidirectional_iterator<int*>, bidirectional_iterator<int*>>();
191-
test_iterators<bidirectional_iterator<int*>, random_access_iterator<int*>>();
192-
test_iterators<bidirectional_iterator<int*>, int*>();
193-
194-
test_iterators<random_access_iterator<int*>, cpp20_input_iterator<int*>>();
195-
test_iterators<random_access_iterator<int*>, forward_iterator<int*>>();
196-
test_iterators<random_access_iterator<int*>, bidirectional_iterator<int*>>();
197-
test_iterators<random_access_iterator<int*>, random_access_iterator<int*>>();
198-
test_iterators<random_access_iterator<int*>, int*>();
199-
200-
test_iterators<int*, cpp20_input_iterator<int*>>();
201-
test_iterators<int*, forward_iterator<int*>>();
202-
test_iterators<int*, bidirectional_iterator<int*>>();
203-
test_iterators<int*, random_access_iterator<int*>>();
204-
test_iterators<int*, int*>();
205-
206-
test_proxy_in_iterators<ProxyIterator<cpp20_input_iterator<int*>>>();
207-
test_proxy_in_iterators<ProxyIterator<forward_iterator<int*>>>();
208-
test_proxy_in_iterators<ProxyIterator<bidirectional_iterator<int*>>>();
209-
test_proxy_in_iterators<ProxyIterator<random_access_iterator<int*>>>();
210-
test_proxy_in_iterators<ProxyIterator<contiguous_iterator<int*>>>();
211-
212139
test_sentinel();
213140
test_different_lengths();
214141
test_borrowed_input_range();
215142
test_rval_range();
216143

144+
types::for_each(types::cpp20_input_iterator_list<int*>(), []<class Iter1>() {
145+
types::for_each(types::cpp20_input_iterator_list<int*>(), []<class Iter2>() {
146+
test_iterators<Iter1, Iter2>();
147+
test_iterators<ProxyIterator<Iter1>, ProxyIterator<Iter2>>();
148+
});
149+
});
150+
217151
return true;
218152
}
219153

0 commit comments

Comments
 (0)