Skip to content

Commit ad87d5f

Browse files
authored
[libc++][test] Refactor tests for std::{copy, move, fill} algorithms (#120909)
This refactor includes the following changes: - Refactor similar tests using `types::for_each` to remove redundant code; - Explicitly include the missing header `type_algorithms.h` in some test files; - Some tests scattered in different test functions with ad-hoc names (e.g., `test5()`, `test6()`) but belong to the same kind are now grouped into one function (`test_struct_array()`).
1 parent 3e8db13 commit ad87d5f

File tree

14 files changed

+204
-296
lines changed

14 files changed

+204
-296
lines changed

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "test_macros.h"
2020
#include "test_iterators.h"
21+
#include "type_algorithms.h"
2122

2223
class PaddedBase {
2324
public:
@@ -81,7 +82,7 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
8182
}
8283

8384
TEST_CONSTEXPR_CXX20 bool test() {
84-
types::for_each(types::cpp17_input_iterator_list<int*>(), TestInIters());
85+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
8586

8687
{ // Make sure that padding bits aren't copied
8788
Derived src(1, 2, 3);
@@ -91,7 +92,6 @@ TEST_CONSTEXPR_CXX20 bool test() {
9192
assert(dst.b_ == 2);
9293
assert(dst.c_ == 6);
9394
}
94-
9595
{ // Make sure that overlapping ranges can be copied
9696
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
9797
std::copy(a + 3, a + 10, a);

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_backward.pass.cpp

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "test_macros.h"
2121
#include "test_iterators.h"
22+
#include "type_algorithms.h"
2223
#include "user_defined_integral.h"
2324

2425
class PaddedBase {
@@ -36,21 +37,29 @@ class Derived : public PaddedBase {
3637
std::int8_t c_;
3738
};
3839

39-
template <class InIter, class OutIter>
40-
TEST_CONSTEXPR_CXX20 void test_copy_backward() {
41-
{
42-
const unsigned N = 1000;
43-
int ia[N] = {};
44-
for (unsigned i = 0; i < N; ++i)
45-
ia[i] = i;
46-
int ib[N] = {0};
47-
48-
OutIter r = std::copy_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
49-
assert(base(r) == ib);
50-
for (unsigned i = 0; i < N; ++i)
51-
assert(ia[i] == ib[i]);
40+
struct TestIterators {
41+
template <class InIter>
42+
TEST_CONSTEXPR_CXX20 void operator()() {
43+
types::for_each(types::bidirectional_iterator_list<int*>(), TestImpl<InIter>());
5244
}
53-
}
45+
46+
template <class InIter>
47+
struct TestImpl {
48+
template <class OutIter>
49+
TEST_CONSTEXPR_CXX20 void operator()() {
50+
const unsigned N = 1000;
51+
int ia[N] = {};
52+
for (unsigned i = 0; i < N; ++i)
53+
ia[i] = i;
54+
int ib[N] = {0};
55+
56+
OutIter r = std::copy_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
57+
assert(base(r) == ib);
58+
for (unsigned i = 0; i < N; ++i)
59+
assert(ia[i] == ib[i]);
60+
}
61+
};
62+
};
5463

5564
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
5665
std::vector<bool> in(N, false);
@@ -70,31 +79,10 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
7079
}
7180

7281
return true;
73-
};
82+
}
7483

7584
TEST_CONSTEXPR_CXX20 bool test() {
76-
test_copy_backward<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
77-
test_copy_backward<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
78-
test_copy_backward<bidirectional_iterator<const int*>, int*>();
79-
80-
test_copy_backward<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
81-
test_copy_backward<random_access_iterator<const int*>, random_access_iterator<int*> >();
82-
test_copy_backward<random_access_iterator<const int*>, int*>();
83-
84-
test_copy_backward<const int*, bidirectional_iterator<int*> >();
85-
test_copy_backward<const int*, random_access_iterator<int*> >();
86-
test_copy_backward<const int*, int*>();
87-
88-
#if TEST_STD_VER > 17
89-
test_copy_backward<contiguous_iterator<const int*>, bidirectional_iterator<int*>>();
90-
test_copy_backward<contiguous_iterator<const int*>, random_access_iterator<int*>>();
91-
test_copy_backward<contiguous_iterator<const int*>, int*>();
92-
93-
test_copy_backward<bidirectional_iterator<const int*>, contiguous_iterator<int*>>();
94-
test_copy_backward<random_access_iterator<const int*>, contiguous_iterator<int*>>();
95-
test_copy_backward<contiguous_iterator<const int*>, contiguous_iterator<int*>>();
96-
test_copy_backward<const int*, contiguous_iterator<int*>>();
97-
#endif
85+
types::for_each(types::bidirectional_iterator_list<const int*>(), TestIterators());
9886

9987
{ // Make sure that padding bits aren't copied
10088
Derived src(1, 2, 3);

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_if.pass.cpp

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,48 @@
1919

2020
#include "test_macros.h"
2121
#include "test_iterators.h"
22+
#include "type_algorithms.h"
2223

23-
struct Pred
24-
{
25-
TEST_CONSTEXPR_CXX14 bool operator()(int i) {return i % 3 == 0;}
24+
struct Pred {
25+
TEST_CONSTEXPR_CXX14 bool operator()(int i) { return i % 3 == 0; }
2626
};
2727

28-
template <class InIter, class OutIter>
29-
TEST_CONSTEXPR_CXX20 void
30-
test_copy_if()
31-
{
28+
template <class InIter>
29+
struct TestOutIters {
30+
template <class OutIter>
31+
TEST_CONSTEXPR_CXX20 void operator()() {
3232
const unsigned N = 1000;
33-
int ia[N] = {};
33+
int ia[N] = {};
3434
for (unsigned i = 0; i < N; ++i)
35-
ia[i] = i;
35+
ia[i] = i;
3636
int ib[N] = {0};
3737

38-
OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred());
39-
assert(base(r) == ib+N/3+1);
40-
for (unsigned i = 0; i < N/3+1; ++i)
41-
assert(ib[i] % 3 == 0);
42-
}
43-
44-
TEST_CONSTEXPR_CXX20 bool
45-
test()
46-
{
47-
test_copy_if<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
48-
test_copy_if<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
49-
test_copy_if<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
50-
test_copy_if<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
51-
test_copy_if<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
52-
test_copy_if<cpp17_input_iterator<const int*>, int*>();
53-
54-
test_copy_if<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
55-
test_copy_if<forward_iterator<const int*>, cpp17_input_iterator<int*> >();
56-
test_copy_if<forward_iterator<const int*>, forward_iterator<int*> >();
57-
test_copy_if<forward_iterator<const int*>, bidirectional_iterator<int*> >();
58-
test_copy_if<forward_iterator<const int*>, random_access_iterator<int*> >();
59-
test_copy_if<forward_iterator<const int*>, int*>();
60-
61-
test_copy_if<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
62-
test_copy_if<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
63-
test_copy_if<bidirectional_iterator<const int*>, forward_iterator<int*> >();
64-
test_copy_if<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
65-
test_copy_if<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
66-
test_copy_if<bidirectional_iterator<const int*>, int*>();
67-
68-
test_copy_if<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
69-
test_copy_if<random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
70-
test_copy_if<random_access_iterator<const int*>, forward_iterator<int*> >();
71-
test_copy_if<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
72-
test_copy_if<random_access_iterator<const int*>, random_access_iterator<int*> >();
73-
test_copy_if<random_access_iterator<const int*>, int*>();
38+
OutIter r = std::copy_if(InIter(ia), InIter(ia + N), OutIter(ib), Pred());
39+
assert(base(r) == ib + N / 3 + 1);
40+
for (unsigned i = 0; i < N / 3 + 1; ++i)
41+
assert(ib[i] % 3 == 0);
42+
}
43+
};
7444

75-
test_copy_if<const int*, cpp17_output_iterator<int*> >();
76-
test_copy_if<const int*, cpp17_input_iterator<int*> >();
77-
test_copy_if<const int*, forward_iterator<int*> >();
78-
test_copy_if<const int*, bidirectional_iterator<int*> >();
79-
test_copy_if<const int*, random_access_iterator<int*> >();
80-
test_copy_if<const int*, int*>();
45+
struct TestInIters {
46+
template <class InIter>
47+
TEST_CONSTEXPR_CXX20 void operator()() {
48+
types::for_each(
49+
types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(),
50+
TestOutIters<InIter>());
51+
}
52+
};
8153

54+
TEST_CONSTEXPR_CXX20 bool test() {
55+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
8256
return true;
8357
}
8458

85-
int main(int, char**)
86-
{
87-
test();
59+
int main(int, char**) {
60+
test();
8861

8962
#if TEST_STD_VER > 17
90-
static_assert(test());
63+
static_assert(test());
9164
#endif
9265

9366
return 0;

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/copy_n.pass.cpp

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "test_macros.h"
2020
#include "test_iterators.h"
21+
#include "type_algorithms.h"
2122
#include "user_defined_integral.h"
2223

2324
typedef UserDefinedIntegral<unsigned> UDI;
@@ -37,37 +38,31 @@ class Derived : public PaddedBase {
3738
std::int8_t c_;
3839
};
3940

40-
template <class InIter, class OutIter>
41-
TEST_CONSTEXPR_CXX20 void test_copy_n() {
42-
{
43-
const unsigned N = 1000;
44-
int ia[N] = {};
45-
for (unsigned i = 0; i < N; ++i)
46-
ia[i] = i;
47-
int ib[N] = {0};
48-
49-
OutIter r = std::copy_n(InIter(ia), UDI(N / 2), OutIter(ib));
50-
assert(base(r) == ib + N / 2);
51-
for (unsigned i = 0; i < N / 2; ++i)
52-
assert(ia[i] == ib[i]);
41+
struct TestIterators {
42+
template <class InIter>
43+
TEST_CONSTEXPR_CXX20 void operator()() {
44+
types::for_each(
45+
types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(),
46+
TestImpl<InIter>());
5347
}
5448

55-
{ // Make sure that padding bits aren't copied
56-
Derived src(1, 2, 3);
57-
Derived dst(4, 5, 6);
58-
std::copy_n(static_cast<PaddedBase*>(&src), 1, static_cast<PaddedBase*>(&dst));
59-
assert(dst.a_ == 1);
60-
assert(dst.b_ == 2);
61-
assert(dst.c_ == 6);
62-
}
63-
64-
{ // Make sure that overlapping ranges can be copied
65-
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
66-
std::copy_n(a + 3, 7, a);
67-
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
68-
assert(std::equal(a, a + 10, expected));
69-
}
70-
}
49+
template <class InIter>
50+
struct TestImpl {
51+
template <class OutIter>
52+
TEST_CONSTEXPR_CXX20 void operator()() {
53+
const unsigned N = 1000;
54+
int ia[N] = {};
55+
for (unsigned i = 0; i < N; ++i)
56+
ia[i] = i;
57+
int ib[N] = {0};
58+
59+
OutIter r = std::copy_n(InIter(ia), UDI(N / 2), OutIter(ib));
60+
assert(base(r) == ib + N / 2);
61+
for (unsigned i = 0; i < N / 2; ++i)
62+
assert(ia[i] == ib[i]);
63+
}
64+
};
65+
};
7166

7267
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
7368
std::vector<bool> in(N, false);
@@ -90,40 +85,23 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
9085
}
9186

9287
TEST_CONSTEXPR_CXX20 bool test() {
93-
test_copy_n<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
94-
test_copy_n<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >();
95-
test_copy_n<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
96-
test_copy_n<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
97-
test_copy_n<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
98-
test_copy_n<cpp17_input_iterator<const int*>, int*>();
99-
100-
test_copy_n<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
101-
test_copy_n<forward_iterator<const int*>, cpp17_input_iterator<int*> >();
102-
test_copy_n<forward_iterator<const int*>, forward_iterator<int*> >();
103-
test_copy_n<forward_iterator<const int*>, bidirectional_iterator<int*> >();
104-
test_copy_n<forward_iterator<const int*>, random_access_iterator<int*> >();
105-
test_copy_n<forward_iterator<const int*>, int*>();
106-
107-
test_copy_n<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
108-
test_copy_n<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >();
109-
test_copy_n<bidirectional_iterator<const int*>, forward_iterator<int*> >();
110-
test_copy_n<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
111-
test_copy_n<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
112-
test_copy_n<bidirectional_iterator<const int*>, int*>();
113-
114-
test_copy_n<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
115-
test_copy_n<random_access_iterator<const int*>, cpp17_input_iterator<int*> >();
116-
test_copy_n<random_access_iterator<const int*>, forward_iterator<int*> >();
117-
test_copy_n<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
118-
test_copy_n<random_access_iterator<const int*>, random_access_iterator<int*> >();
119-
test_copy_n<random_access_iterator<const int*>, int*>();
120-
121-
test_copy_n<const int*, cpp17_output_iterator<int*> >();
122-
test_copy_n<const int*, cpp17_input_iterator<int*> >();
123-
test_copy_n<const int*, forward_iterator<int*> >();
124-
test_copy_n<const int*, bidirectional_iterator<int*> >();
125-
test_copy_n<const int*, random_access_iterator<int*> >();
126-
test_copy_n<const int*, int*>();
88+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestIterators());
89+
90+
{ // Make sure that padding bits aren't copied
91+
Derived src(1, 2, 3);
92+
Derived dst(4, 5, 6);
93+
std::copy_n(static_cast<PaddedBase*>(&src), 1, static_cast<PaddedBase*>(&dst));
94+
assert(dst.a_ == 1);
95+
assert(dst.b_ == 2);
96+
assert(dst.c_ == 6);
97+
}
98+
99+
{ // Make sure that overlapping ranges can be copied
100+
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
101+
std::copy_n(a + 3, 7, a);
102+
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
103+
assert(std::equal(a, a + 10, expected));
104+
}
127105

128106
{ // Test vector<bool>::iterator optimization
129107
assert(test_vector_bool(8));

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/pstl.copy.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "test_macros.h"
2424
#include "test_execution_policies.h"
2525
#include "test_iterators.h"
26+
#include "type_algorithms.h"
2627

2728
EXECUTION_POLICY_SFINAE_TEST(copy);
2829

libcxx/test/std/algorithms/alg.modifying.operations/alg.copy/pstl.copy_n.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "test_macros.h"
2424
#include "test_execution_policies.h"
2525
#include "test_iterators.h"
26+
#include "type_algorithms.h"
2627

2728
EXECUTION_POLICY_SFINAE_TEST(copy_n);
2829

@@ -58,7 +59,7 @@ struct TestIteratorsInt {
5859
};
5960

6061
struct CopiedToTester {
61-
bool copied_to = false;
62+
bool copied_to = false;
6263
CopiedToTester() = default;
6364
CopiedToTester(const CopiedToTester&) {}
6465
CopiedToTester& operator=(const CopiedToTester&) {

0 commit comments

Comments
 (0)