//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // UNSUPPORTED: c++03, c++11, c++14, c++17 // template S, weakly_incrementable O> // requires indirectly_copyable // constexpr ranges::copy_result ranges::copy(I first, S last, O result); // template // requires indirectly_copyable, O> // constexpr ranges::copy_result, O> ranges::copy(R&& r, O result); #include #include #include #include #include #include #include "almost_satisfies_types.h" #include "test_iterators.h" #include "type_algorithms.h" template > concept HasCopyIt = requires(In in, Sent sent, Out out) { std::ranges::copy(in, sent, out); }; static_assert(HasCopyIt); static_assert(!HasCopyIt); static_assert(!HasCopyIt); static_assert(!HasCopyIt); static_assert(!HasCopyIt); struct NotIndirectlyCopyable {}; static_assert(!HasCopyIt); static_assert(!HasCopyIt); static_assert(!HasCopyIt); template concept HasCopyR = requires(Range range, Out out) { std::ranges::copy(range, out); }; static_assert(HasCopyR, int*>); static_assert(!HasCopyR); static_assert(!HasCopyR); static_assert(!HasCopyR); static_assert(!HasCopyR); static_assert(!HasCopyR, int*>); static_assert(!HasCopyR); static_assert(!HasCopyR); static_assert(std::is_same_v, std::ranges::in_out_result>); // clang-format off template constexpr void test_iterators() { { // simple test { std::array in{1, 2, 3, 4}; std::array out; std::same_as> auto ret = std::ranges::copy(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data())); assert(in == out); assert(base(ret.in) == in.data() + in.size()); assert(base(ret.out) == out.data() + out.size()); } { std::array in{1, 2, 3, 4}; std::array out; auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); std::same_as> auto ret = std::ranges::copy(range, Out(out.data())); assert(in == out); assert(base(ret.in) == in.data() + in.size()); assert(base(ret.out) == out.data() + out.size()); } } { // check that an empty range works { std::array in; std::array out; auto ret = std::ranges::copy(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data())); assert(base(ret.in) == in.data()); assert(base(ret.out) == out.data()); } { std::array in; std::array out; auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); auto ret = std::ranges::copy(range, Out(out.data())); assert(base(ret.in) == in.data()); assert(base(ret.out) == out.data()); } } } // clang-format on constexpr bool test() { types::for_each(types::forward_iterator_list{}, []() { test_iterators, Out, sentinel_wrapper>>(); test_iterators>, ProxyIterator, sentinel_wrapper>>>(); types::for_each(types::forward_iterator_list{}, []() { test_iterators(); test_iterators>(); test_iterators>(); test_iterators, ProxyIterator>(); test_iterators, ProxyIterator, sized_sentinel>>(); test_iterators, ProxyIterator, sentinel_wrapper>>(); }); }); { // check that ranges::dangling is returned std::array out; std::same_as> auto ret = std::ranges::copy(std::array{1, 2, 3, 4}, out.data()); assert(ret.out == out.data() + 4); assert((out == std::array{1, 2, 3, 4})); } { // check that an iterator is returned with a borrowing range std::array in{1, 2, 3, 4}; std::array out; std::same_as::iterator, int*>> auto ret = std::ranges::copy(std::views::all(in), out.data()); assert(ret.in == in.end()); assert(ret.out == out.data() + 4); assert(in == out); } { // check that every element is copied exactly once struct CopyOnce { bool copied = false; constexpr CopyOnce() = default; constexpr CopyOnce(const CopyOnce& other) = delete; constexpr CopyOnce& operator=(const CopyOnce& other) { assert(!other.copied); copied = true; return *this; } }; { std::array in{}; std::array out{}; auto ret = std::ranges::copy(in.begin(), in.end(), out.begin()); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); } { std::array in{}; std::array out{}; auto ret = std::ranges::copy(in, out.begin()); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(std::all_of(out.begin(), out.end(), [](const auto& e) { return e.copied; })); } } { // check that the range is copied forwards struct OnlyForwardsCopyable { OnlyForwardsCopyable* next = nullptr; bool canCopy = false; OnlyForwardsCopyable() = default; constexpr OnlyForwardsCopyable& operator=(const OnlyForwardsCopyable&) { assert(canCopy); if (next != nullptr) next->canCopy = true; return *this; } }; { std::array in{}; std::array out{}; out[0].next = &out[1]; out[1].next = &out[2]; out[0].canCopy = true; auto ret = std::ranges::copy(in.begin(), in.end(), out.begin()); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(out[0].canCopy); assert(out[1].canCopy); assert(out[2].canCopy); } { std::array in{}; std::array out{}; out[0].next = &out[1]; out[1].next = &out[2]; out[0].canCopy = true; auto ret = std::ranges::copy(in, out.begin()); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(out[0].canCopy); assert(out[1].canCopy); assert(out[2].canCopy); } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }