1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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);
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 #include "user_defined_integral.h"
22 typedef UserDefinedIntegral
<unsigned> UDI
;
26 TEST_CONSTEXPR
PaddedBase(std::int16_t a
, std::int8_t b
) : a_(a
), b_(b
) {}
32 class Derived
: public PaddedBase
{
34 TEST_CONSTEXPR
Derived(std::int16_t a
, std::int8_t b
, std::int8_t c
) : PaddedBase(a
, b
), c_(c
) {}
39 template <class InIter
, class OutIter
>
40 TEST_CONSTEXPR_CXX20
void
44 const unsigned N
= 1000;
46 for (unsigned i
= 0; i
< N
; ++i
)
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
]);
56 { // Make sure that padding bits aren't copied
59 std::copy_n(static_cast<PaddedBase
*>(&src
), 1, static_cast<PaddedBase
*>(&dst
));
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
));
73 TEST_CONSTEXPR_CXX20
bool
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*>();
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*>();
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*>();
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*>();
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*>();
114 int main(int, char**)
118 #if TEST_STD_VER > 17
119 static_assert(test());