Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.copy / copy_n.pass.cpp
blobb0acc1060101c20d210bafa1d1929ace85783490
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 //===----------------------------------------------------------------------===//
9 // <algorithm>
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);
15 #include <algorithm>
16 #include <cassert>
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 #include "user_defined_integral.h"
22 typedef UserDefinedIntegral<unsigned> UDI;
24 class PaddedBase {
25 public:
26 TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}
28 std::int16_t a_;
29 std::int8_t b_;
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) {}
36 std::int8_t c_;
39 template <class InIter, class OutIter>
40 TEST_CONSTEXPR_CXX20 void
41 test_copy_n()
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};
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
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);
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
74 test()
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*>();
111 return true;
114 int main(int, char**)
116 test();
118 #if TEST_STD_VER > 17
119 static_assert(test());
120 #endif
122 return 0;