Skip to content

Commit 319d569

Browse files
committed
Refactor tests for std::{copy, move, fill} algorithms
1 parent c89735d commit 319d569

14 files changed

+352
-411
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
#ifndef TEST_STD_ALGORITHMS_ALG_MODIFYING_OPERATIONS_ALG_COPY_COMMON_H
10+
#define TEST_STD_ALGORITHMS_ALG_MODIFYING_OPERATIONS_ALG_COPY_COMMON_H
11+
12+
#include <cstdint>
13+
#include "test_macros.h"
14+
15+
class PaddedBase {
16+
public:
17+
TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
18+
19+
std::int16_t a_;
20+
std::int8_t b_;
21+
};
22+
23+
class Derived : public PaddedBase {
24+
public:
25+
TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {}
26+
27+
std::int8_t c_;
28+
};
29+
30+
#endif

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@
1616
#include <cassert>
1717
#include <vector>
1818

19+
#include "common.h"
1920
#include "test_macros.h"
2021
#include "test_iterators.h"
22+
#include "type_algorithms.h"
2123

22-
class PaddedBase {
23-
public:
24-
TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
25-
26-
std::int16_t a_;
27-
std::int8_t b_;
28-
};
29-
30-
class Derived : public PaddedBase {
31-
public:
32-
TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {}
24+
TEST_CONSTEXPR_CXX20 void test_padding() {
25+
{ // Make sure that padding bits aren't copied
26+
Derived src(1, 2, 3);
27+
Derived dst(4, 5, 6);
28+
std::copy(static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst));
29+
assert(dst.a_ == 1);
30+
assert(dst.b_ == 2);
31+
assert(dst.c_ == 6);
32+
}
33+
}
3334

34-
std::int8_t c_;
35-
};
35+
TEST_CONSTEXPR_CXX20 void test_overlapping() {
36+
{ // Make sure that overlapping ranges can be copied
37+
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
38+
std::copy(a + 3, a + 10, a);
39+
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
40+
assert(std::equal(a, a + 10, expected));
41+
}
42+
}
3643

3744
template <class InIter>
3845
struct Test {
@@ -81,23 +88,9 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
8188
}
8289

8390
TEST_CONSTEXPR_CXX20 bool test() {
84-
types::for_each(types::cpp17_input_iterator_list<int*>(), TestInIters());
85-
86-
{ // Make sure that padding bits aren't copied
87-
Derived src(1, 2, 3);
88-
Derived dst(4, 5, 6);
89-
std::copy(static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst));
90-
assert(dst.a_ == 1);
91-
assert(dst.b_ == 2);
92-
assert(dst.c_ == 6);
93-
}
94-
95-
{ // Make sure that overlapping ranges can be copied
96-
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
97-
std::copy(a + 3, a + 10, a);
98-
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
99-
assert(std::equal(a, a + 10, expected));
100-
}
91+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
92+
test_padding();
93+
test_overlapping();
10194

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

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

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
#include <cassert>
1818
#include <vector>
1919

20+
#include "common.h"
2021
#include "test_macros.h"
2122
#include "test_iterators.h"
23+
#include "type_algorithms.h"
2224
#include "user_defined_integral.h"
2325

2426
class PaddedBase {
@@ -36,21 +38,29 @@ class Derived : public PaddedBase {
3638
std::int8_t c_;
3739
};
3840

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

5565
TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
5666
std::vector<bool> in(N, false);
@@ -70,32 +80,11 @@ TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {
7080
}
7181

7282
return true;
73-
};
83+
}
7484

7585
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
98-
86+
types::for_each(types::bidirectional_iterator_list<const int*>(), TestIterators());
87+
9988
{ // Make sure that padding bits aren't copied
10089
Derived src(1, 2, 3);
10190
Derived dst(4, 5, 6);
@@ -105,7 +94,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
10594
assert(dst.b_ == 2);
10695
assert(dst.c_ == 6);
10796
}
108-
97+
10998
{ // Make sure that overlapping ranges can be copied
11099
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
111100
std::copy_backward(a, a + 7, a + 10);

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;

0 commit comments

Comments
 (0)