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 // UNSUPPORTED: c++03, c++11, c++14, c++17 12 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 13 14 // template<input_iterator I, weakly_incrementable O> 15 // requires indirectly_copyable<I, O> 16 // constexpr ranges::copy_n_result<I, O> 17 // ranges::copy_n(I first, iter_difference_t<I> n, O result); 18 19 #include <algorithm> 20 #include <array> 21 #include <cassert> 22 #include <ranges> 23 24 #include "almost_satisfies_types.h" 25 #include "test_iterators.h" 26 27 template <class In, class Out = In, class Count = size_t> 28 concept HasCopyNIt = requires(In in, Count count, Out out) { std::ranges::copy_n(in, count, out); }; 29 30 static_assert(HasCopyNIt<int*>); 31 static_assert(!HasCopyNIt<InputIteratorNotDerivedFrom>); 32 static_assert(!HasCopyNIt<InputIteratorNotIndirectlyReadable>); 33 static_assert(!HasCopyNIt<InputIteratorNotInputOrOutputIterator>); 34 static_assert(!HasCopyNIt<int*, WeaklyIncrementableNotMovable>); 35 struct NotIndirectlyCopyable {}; 36 static_assert(!HasCopyNIt<int*, NotIndirectlyCopyable*>); 37 static_assert(!HasCopyNIt<int*, int*, SentinelForNotSemiregular>); 38 static_assert(!HasCopyNIt<int*, int*, SentinelForNotWeaklyEqualityComparableWith>); 39 40 static_assert(std::is_same_v<std::ranges::copy_result<int, long>, std::ranges::in_out_result<int, long>>); 41 42 template <class In, class Out, class Sent = In> 43 constexpr void test_iterators() { 44 { // simple test 45 std::array in {1, 2, 3, 4}; 46 std::array<int, 4> out; 47 std::same_as<std::ranges::in_out_result<In, Out>> auto ret = 48 std::ranges::copy_n(In(in.data()), in.size(), Out(out.data())); 49 assert(in == out); 50 assert(base(ret.in) == in.data() + in.size()); 51 assert(base(ret.out) == out.data() + out.size()); 52 } 53 54 { // check that an empty range works 55 std::array<int, 0> in; 56 std::array<int, 0> out; 57 auto ret = std::ranges::copy_n(In(in.data()), in.size(), Out(out.begin())); 58 assert(base(ret.in) == in.data()); 59 assert(base(ret.out) == out.data()); 60 } 61 } 62 63 template <class Out> 64 constexpr void test_in_iterators() { 65 test_iterators<cpp20_input_iterator<int*>, Out, sentinel_wrapper<cpp20_input_iterator<int*>>>(); 66 test_iterators<forward_iterator<int*>, Out>(); 67 test_iterators<bidirectional_iterator<int*>, Out>(); 68 test_iterators<random_access_iterator<int*>, Out>(); 69 test_iterators<contiguous_iterator<int*>, Out>(); 70 } 71 72 constexpr bool test() { 73 test_in_iterators<cpp20_input_iterator<int*>>(); 74 test_in_iterators<forward_iterator<int*>>(); 75 test_in_iterators<bidirectional_iterator<int*>>(); 76 test_in_iterators<random_access_iterator<int*>>(); 77 test_in_iterators<contiguous_iterator<int*>>(); 78 79 { // check that every element is copied exactly once 80 struct CopyOnce { 81 bool copied = false; 82 constexpr CopyOnce() = default; 83 constexpr CopyOnce(const CopyOnce& other) = delete; 84 constexpr CopyOnce& operator=(const CopyOnce& other) { 85 assert(!other.copied); 86 copied = true; 87 return *this; 88 } 89 }; 90 std::array<CopyOnce, 4> in {}; 91 std::array<CopyOnce, 4> out {}; 92 auto ret = std::ranges::copy_n(in.data(), in.size(), out.begin()); 93 assert(ret.in == in.end()); 94 assert(ret.out == out.end()); 95 assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); 96 } 97 98 return true; 99 } 100 101 int main(int, char**) { 102 test(); 103 static_assert(test()); 104 105 return 0; 106 } 107