Skip to content

Commit 04602db

Browse files
committed
Refactor tests for swap_range algorithms
1 parent 70965ef commit 04602db

File tree

3 files changed

+184
-276
lines changed

3 files changed

+184
-276
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,37 @@
1717
#include <cassert>
1818

1919
#include "test_macros.h"
20+
#include "test_iterators.h"
21+
#include "type_algorithms.h"
2022

21-
#if TEST_STD_VER > 17
22-
constexpr bool test_swap_constexpr()
23-
{
23+
template <class Iter1>
24+
struct Test {
25+
template <class Iter2>
26+
TEST_CONSTEXPR_CXX20 void operator()() {
2427
int i = 1;
2528
int j = 2;
26-
std::iter_swap(&i, &j);
27-
return i == 2 && j == 1;
29+
std::iter_swap(Iter1(&i), Iter2(&j));
30+
assert(i == 2 && j == 1);
31+
}
32+
};
33+
34+
struct TestIterators {
35+
template <class Iter>
36+
TEST_CONSTEXPR_CXX20 void operator()() {
37+
types::for_each(types::forward_iterator_list<int*>(), Test<Iter>());
38+
}
39+
};
40+
41+
TEST_CONSTEXPR_CXX20 bool test() {
42+
types::for_each(types::forward_iterator_list<int*>(), TestIterators());
43+
return true;
2844
}
29-
#endif // TEST_STD_VER > 17
3045

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
46+
int main(int, char**) {
47+
test();
48+
#if TEST_STD_VER >= 20
49+
static_assert(test());
50+
#endif
4251

4352
return 0;
4453
}

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

Lines changed: 69 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,42 @@
2525
#include <ranges>
2626

2727
#include "test_iterators.h"
28+
#include "type_algorithms.h"
2829

2930
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};
31+
using Expected = std::ranges::swap_ranges_result<int*, int*>;
32+
int i[3] = {1, 2, 3};
33+
int j[1] = {4};
3334
std::same_as<Expected> auto r = std::ranges::swap_ranges(i, i + 3, j, j + 1);
3435
assert(r.in1 == i + 1);
3536
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);
37+
assert(std::ranges::equal(i, std::array{4, 2, 3}));
38+
assert(std::ranges::equal(j, std::array{1}));
4039
std::same_as<Expected> auto r2 = std::ranges::swap_ranges(i, j);
4140
assert(r2.in1 == i + 1);
4241
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);
42+
assert(std::ranges::equal(i, std::array{1, 2, 3}));
43+
assert(std::ranges::equal(j, std::array{4}));
4744
std::same_as<Expected> auto r3 = std::ranges::swap_ranges(j, j + 1, i, i + 3);
4845
assert(r3.in1 == j + 1);
4946
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);
47+
assert(std::ranges::equal(i, std::array{4, 2, 3}));
48+
assert(std::ranges::equal(j, std::array{1}));
5449
std::same_as<Expected> auto r4 = std::ranges::swap_ranges(j, i);
5550
assert(r4.in1 == j + 1);
5651
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);
52+
assert(std::ranges::equal(i, std::array{1, 2, 3}));
53+
assert(std::ranges::equal(j, std::array{4}));
6154
}
6255

6356
constexpr void test_range() {
6457
std::array r1 = {1, 2, 3};
6558
std::array r2 = {4, 5, 6};
6659

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);
60+
std::same_as<std::ranges::in_in_result<std::array<int, 3>::iterator, std::array<int, 3>::iterator>> auto r =
61+
std::ranges::swap_ranges(r1, r2);
6962
assert(r.in1 == r1.end());
7063
assert(r.in2 == r2.end());
71-
7264
assert((r1 == std::array{4, 5, 6}));
7365
assert((r2 == std::array{1, 2, 3}));
7466
}
@@ -78,141 +70,101 @@ constexpr void test_borrowed_input_range() {
7870
int r1[] = {1, 2, 3};
7971
int r2[] = {4, 5, 6};
8072
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);
73+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
74+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
8775
}
8876
{
8977
int r1[] = {1, 2, 3};
9078
int r2[] = {4, 5, 6};
9179
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);
80+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
81+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
9882
}
9983
{
10084
int r1[] = {1, 2, 3};
10185
int r2[] = {4, 5, 6};
10286
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);
87+
assert(std::ranges::equal(r1, std::array{4, 5, 6}));
88+
assert(std::ranges::equal(r2, std::array{1, 2, 3}));
10989
}
11090
}
11191

11292
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)));
93+
int i[3] = {1, 2, 3};
94+
int j[3] = {4, 5, 6};
95+
using It = cpp17_input_iterator<int*>;
96+
using Sent = sentinel_wrapper<It>;
97+
using Expected = std::ranges::swap_ranges_result<It, It>;
98+
std::same_as<Expected> auto r = std::ranges::swap_ranges(It(i), Sent(It(i + 3)), It(j), Sent(It(j + 3)));
12099
assert(base(r.in1) == i + 3);
121100
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);
101+
assert(std::ranges::equal(i, std::array{4, 5, 6}));
102+
assert(std::ranges::equal(j, std::array{1, 2, 3}));
128103
}
129104

130-
template <class Iter1, class Iter2>
131-
constexpr void test_iterators() {
132-
using Expected = std::ranges::swap_ranges_result<Iter1, Iter2>;
133-
int i[3] = {1, 2, 3};
134-
int j[3] = {4, 5, 6};
135-
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);
145-
}
105+
template <class Iter1>
106+
struct Test {
107+
template <class Iter2>
108+
TEST_CONSTEXPR_CXX20 void operator()() {
109+
using Expected = std::ranges::swap_ranges_result<Iter1, Iter2>;
110+
int a[3] = {1, 2, 3};
111+
int b[3] = {4, 5, 6};
112+
std::same_as<Expected> auto r =
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}));
118+
}
119+
};
120+
121+
struct TestIterators {
122+
template <class Iter>
123+
TEST_CONSTEXPR_CXX20 void operator()() {
124+
types::for_each(types::cpp20_input_iterator_list<int*>(), Test<Iter>());
125+
}
126+
};
127+
128+
template <class Ptr>
129+
using cpp20_proxy_in_iterator_list =
130+
types::type_list<Cpp20InputProxyIterator<Ptr>,
131+
ForwardProxyIterator<Ptr>,
132+
BidirectionalProxyIterator<Ptr>,
133+
RandomAccessProxyIterator<Ptr>,
134+
ContiguousProxyIterator<Ptr>>;
135+
136+
struct TestProxyInIterators {
137+
template <class Iter>
138+
TEST_CONSTEXPR_CXX20 void operator()() {
139+
types::for_each(cpp20_proxy_in_iterator_list<int*>(), Test<Iter>());
140+
}
141+
};
146142

147143
constexpr void test_rval_range() {
148144
{
149-
using Expected = std::ranges::swap_ranges_result<std::array<int, 3>::iterator, std::ranges::dangling>;
145+
using Expected = std::ranges::swap_ranges_result<std::array<int, 3>::iterator, std::ranges::dangling>;
150146
std::array<int, 3> r = {1, 2, 3};
151147
std::same_as<Expected> auto a = std::ranges::swap_ranges(r, std::array{4, 5, 6});
152148
assert((r == std::array{4, 5, 6}));
153149
assert(a.in1 == r.begin() + 3);
154150
}
155151
{
156152
std::array<int, 3> r = {1, 2, 3};
157-
using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, std::array<int, 3>::iterator>;
153+
using Expected = std::ranges::swap_ranges_result<std::ranges::dangling, std::array<int, 3>::iterator>;
158154
std::same_as<Expected> auto b = std::ranges::swap_ranges(std::array{4, 5, 6}, r);
159155
assert((r == std::array{4, 5, 6}));
160156
assert(b.in2 == r.begin() + 3);
161157
}
162158
}
163159

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-
173160
constexpr bool test() {
174161
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-
212162
test_sentinel();
213163
test_different_lengths();
214164
test_borrowed_input_range();
215165
test_rval_range();
166+
types::for_each(types::cpp20_input_iterator_list<int*>(), TestIterators());
167+
types::for_each(cpp20_proxy_in_iterator_list<int*>(), TestProxyInIterators());
216168

217169
return true;
218170
}

0 commit comments

Comments
 (0)