| 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 |
// <algorithm> |
| 10 |
|
| 11 |
// template<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter> |
| 12 |
// constexpr OutIter // constexpr after C++17 |
| 13 |
// copy_n(InIter first, InIter::difference_type n, OutIter result); |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "test_iterators.h" |
| 20 |
#include "user_defined_integral.h" |
| 21 |
|
| 22 |
typedef UserDefinedIntegral<unsigned> UDI; |
| 23 |
|
| 24 |
class PaddedBase { |
| 25 |
public: |
| 26 |
TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {} |
| 27 |
|
| 28 |
std::int16_t a_; |
| 29 |
std::int8_t b_; |
| 30 |
}; |
| 31 |
|
| 32 |
class Derived : public PaddedBase { |
| 33 |
public: |
| 34 |
TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {} |
| 35 |
|
| 36 |
std::int8_t c_; |
| 37 |
}; |
| 38 |
|
| 39 |
template <class InIter, class OutIter> |
| 40 |
TEST_CONSTEXPR_CXX20 void |
| 41 |
test_copy_n() |
| 42 |
{ |
| 43 |
{ |
| 44 |
const unsigned N = 1000; |
| 45 |
int ia[N] = {}; |
| 46 |
for (unsigned i = 0; i < N; ++i) |
| 47 |
ia[i] = i; |
| 48 |
int ib[N] = {0}; |
| 49 |
|
| 50 |
OutIter r = std::copy_n(InIter(ia), UDI(N/2), OutIter(ib)); |
| 51 |
assert(base(r) == ib+N/2); |
| 52 |
for (unsigned i = 0; i < N/2; ++i) |
| 53 |
assert(ia[i] == ib[i]); |
| 54 |
} |
| 55 |
|
| 56 |
{ // Make sure that padding bits aren't copied |
| 57 |
Derived src(1, 2, 3); |
| 58 |
Derived dst(4, 5, 6); |
| 59 |
std::copy_n(static_cast<PaddedBase*>(&src), 1, static_cast<PaddedBase*>(&dst)); |
| 60 |
assert(dst.a_ == 1); |
| 61 |
assert(dst.b_ == 2); |
| 62 |
assert(dst.c_ == 6); |
| 63 |
} |
| 64 |
|
| 65 |
{ // Make sure that overlapping ranges can be copied |
| 66 |
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| 67 |
std::copy_n(a + 3, 7, a); |
| 68 |
int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10}; |
| 69 |
assert(std::equal(a, a + 10, expected)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
TEST_CONSTEXPR_CXX20 bool |
| 74 |
test() |
| 75 |
{ |
| 76 |
test_copy_n<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 77 |
test_copy_n<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 78 |
test_copy_n<cpp17_input_iterator<const int*>, forward_iterator<int*> >(); |
| 79 |
test_copy_n<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 80 |
test_copy_n<cpp17_input_iterator<const int*>, random_access_iterator<int*> >(); |
| 81 |
test_copy_n<cpp17_input_iterator<const int*>, int*>(); |
| 82 |
|
| 83 |
test_copy_n<forward_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 84 |
test_copy_n<forward_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 85 |
test_copy_n<forward_iterator<const int*>, forward_iterator<int*> >(); |
| 86 |
test_copy_n<forward_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 87 |
test_copy_n<forward_iterator<const int*>, random_access_iterator<int*> >(); |
| 88 |
test_copy_n<forward_iterator<const int*>, int*>(); |
| 89 |
|
| 90 |
test_copy_n<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 91 |
test_copy_n<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 92 |
test_copy_n<bidirectional_iterator<const int*>, forward_iterator<int*> >(); |
| 93 |
test_copy_n<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 94 |
test_copy_n<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); |
| 95 |
test_copy_n<bidirectional_iterator<const int*>, int*>(); |
| 96 |
|
| 97 |
test_copy_n<random_access_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 98 |
test_copy_n<random_access_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 99 |
test_copy_n<random_access_iterator<const int*>, forward_iterator<int*> >(); |
| 100 |
test_copy_n<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 101 |
test_copy_n<random_access_iterator<const int*>, random_access_iterator<int*> >(); |
| 102 |
test_copy_n<random_access_iterator<const int*>, int*>(); |
| 103 |
|
| 104 |
test_copy_n<const int*, cpp17_output_iterator<int*> >(); |
| 105 |
test_copy_n<const int*, cpp17_input_iterator<int*> >(); |
| 106 |
test_copy_n<const int*, forward_iterator<int*> >(); |
| 107 |
test_copy_n<const int*, bidirectional_iterator<int*> >(); |
| 108 |
test_copy_n<const int*, random_access_iterator<int*> >(); |
| 109 |
test_copy_n<const int*, int*>(); |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
int main(int, char**) |
| 115 |
{ |
| 116 |
test(); |
| 117 |
|
| 118 |
#if TEST_STD_VER > 17 |
| 119 |
static_assert(test()); |
| 120 |
#endif |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |