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