Skip to content

Commit 7003c10

Browse files
committed
Refactor tests for std::{copy, move, fill} algorithms
1 parent 34531cf commit 7003c10

14 files changed

+351
-456
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
@@ -15,23 +15,30 @@
1515
#include <algorithm>
1616
#include <cassert>
1717

18+
#include "common.h"
1819
#include "test_macros.h"
1920
#include "test_iterators.h"
21+
#include "type_algorithms.h"
2022

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

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

3643
template <class InIter>
3744
struct Test {
@@ -60,23 +67,9 @@ struct TestInIters {
6067
};
6168

6269
TEST_CONSTEXPR_CXX20 bool test() {
63-
types::for_each(types::cpp17_input_iterator_list<int*>(), TestInIters());
64-
65-
{ // Make sure that padding bits aren't copied
66-
Derived src(1, 2, 3);
67-
Derived dst(4, 5, 6);
68-
std::copy(static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst));
69-
assert(dst.a_ == 1);
70-
assert(dst.b_ == 2);
71-
assert(dst.c_ == 6);
72-
}
73-
74-
{ // Make sure that overlapping ranges can be copied
75-
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
76-
std::copy(a + 3, a + 10, a);
77-
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};
78-
assert(std::equal(a, a + 10, expected));
79-
}
70+
types::for_each(types::cpp17_input_iterator_list<const int*>(), TestInIters());
71+
test_padding();
72+
test_overlapping();
8073

8174
return true;
8275
}

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

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

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

23-
class PaddedBase {
24-
public:
25-
TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
26-
27-
std::int16_t a_;
28-
std::int8_t b_;
29-
};
30-
31-
class Derived : public PaddedBase {
32-
public:
33-
TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {}
34-
35-
std::int8_t c_;
36-
};
37-
38-
template <class InIter, class OutIter>
39-
TEST_CONSTEXPR_CXX20 void
40-
test_copy_backward()
41-
{
42-
{
25+
template <class InIter>
26+
struct TestOutIters {
27+
template <class OutIter>
28+
TEST_CONSTEXPR_CXX20 void operator()() {
4329
const unsigned N = 1000;
44-
int ia[N] = {};
30+
int ia[N] = {};
4531
for (unsigned i = 0; i < N; ++i)
46-
ia[i] = i;
32+
ia[i] = i;
4733
int ib[N] = {0};
4834

49-
OutIter r = std::copy_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
35+
OutIter r = std::copy_backward(InIter(ia), InIter(ia + N), OutIter(ib + N));
5036
assert(base(r) == ib);
5137
for (unsigned i = 0; i < N; ++i)
52-
assert(ia[i] == ib[i]);
38+
assert(ia[i] == ib[i]);
5339
}
54-
}
55-
56-
TEST_CONSTEXPR_CXX20 bool
57-
test()
58-
{
59-
test_copy_backward<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
60-
test_copy_backward<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
61-
test_copy_backward<bidirectional_iterator<const int*>, int*>();
62-
63-
test_copy_backward<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
64-
test_copy_backward<random_access_iterator<const int*>, random_access_iterator<int*> >();
65-
test_copy_backward<random_access_iterator<const int*>, int*>();
66-
67-
test_copy_backward<const int*, bidirectional_iterator<int*> >();
68-
test_copy_backward<const int*, random_access_iterator<int*> >();
69-
test_copy_backward<const int*, int*>();
70-
71-
#if TEST_STD_VER > 17
72-
test_copy_backward<contiguous_iterator<const int*>, bidirectional_iterator<int*>>();
73-
test_copy_backward<contiguous_iterator<const int*>, random_access_iterator<int*>>();
74-
test_copy_backward<contiguous_iterator<const int*>, int*>();
75-
76-
test_copy_backward<bidirectional_iterator<const int*>, contiguous_iterator<int*>>();
77-
test_copy_backward<random_access_iterator<const int*>, contiguous_iterator<int*>>();
78-
test_copy_backward<contiguous_iterator<const int*>, contiguous_iterator<int*>>();
79-
test_copy_backward<const int*, contiguous_iterator<int*>>();
80-
#endif
40+
};
8141

42+
TEST_CONSTEXPR_CXX20 void test_padding() {
8243
{ // Make sure that padding bits aren't copied
8344
Derived src(1, 2, 3);
8445
Derived dst(4, 5, 6);
@@ -88,23 +49,37 @@ test()
8849
assert(dst.b_ == 2);
8950
assert(dst.c_ == 6);
9051
}
52+
}
9153

54+
TEST_CONSTEXPR_CXX20 void test_overlapping() {
9255
{ // Make sure that overlapping ranges can be copied
9356
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
9457
std::copy_backward(a, a + 7, a + 10);
9558
int expected[] = {1, 2, 3, 1, 2, 3, 4, 5, 6, 7};
9659
assert(std::equal(a, a + 10, expected));
9760
}
61+
}
62+
63+
struct TestInIters {
64+
template <class InIter>
65+
TEST_CONSTEXPR_CXX20 void operator()() {
66+
types::for_each(types::bidirectional_iterator_list<int*>(), TestOutIters<InIter>());
67+
}
68+
};
69+
70+
TEST_CONSTEXPR_CXX20 bool test() {
71+
types::for_each(types::bidirectional_iterator_list<const int*>(), TestInIters());
72+
test_padding();
73+
test_overlapping();
9874

99-
return true;
75+
return true;
10076
}
10177

102-
int main(int, char**)
103-
{
104-
test();
78+
int main(int, char**) {
79+
test();
10580

10681
#if TEST_STD_VER > 17
107-
static_assert(test());
82+
static_assert(test());
10883
#endif
10984

11085
return 0;

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)