//===----------------------------------------------------------------------===// // // 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, class Proj = identity, // indirect_unary_predicate> Pred> // requires indirectly_copyable // constexpr ranges::copy_if_result // ranges::copy_if(I first, S last, O result, Pred pred, Proj proj = {}); // template, Proj>> Pred> // requires indirectly_copyable, O> // constexpr ranges::copy_if_result, O> // ranges::copy_if(R&& r, O result, Pred pred, Proj proj = {}); #include #include #include #include #include "almost_satisfies_types.h" #include "test_iterators.h" struct Functor { bool operator()(int); }; template , class Func = Functor> concept HasCopyIfIt = requires(In first, Sent last, Out result) { std::ranges::copy_if(first, last, result, Func{}); }; static_assert(HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); struct NotIndirectlyCopyable {}; static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); static_assert(!HasCopyIfIt); template concept HasCopyIfR = requires(Range range, Out out) { std::ranges::copy_if(range, out, Func{}); }; static_assert(HasCopyIfR, int*>); static_assert(!HasCopyIfR); static_assert(!HasCopyIfR); static_assert(!HasCopyIfR); static_assert(!HasCopyIfR); static_assert(!HasCopyIfR, int*>); static_assert(!HasCopyIfR); static_assert(!HasCopyIfR); static_assert(std::is_same_v, std::ranges::in_out_result>); 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_if(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()), [](int) { return true; }); 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_if(range, Out(out.data()), [](int) { return true; }); 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_if(In(in.data()), Sent(In(in.data())), Out(out.data()), [](int) { return true; }); 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()))); auto ret = std::ranges::copy_if(range, Out(out.data()), [](int) { return true; }); assert(base(ret.in) == in.data()); assert(base(ret.out) == out.data()); } } { // check that the predicate is used { std::array in = {4, 6, 87, 3, 88, 44, 45, 9}; std::array out; auto ret = std::ranges::copy_if(In(in.data()), Sent(In(in.data() + in.size())), Out(out.data()), [](int i) { return i % 2 == 0; }); assert((out == std::array{4, 6, 88, 44})); assert(base(ret.in) == in.data() + in.size()); assert(base(ret.out) == out.data() + out.size()); } { std::array in = {4, 6, 87, 3, 88, 44, 45, 9}; std::array out; auto range = std::ranges::subrange(In(in.data()), Sent(In(in.data() + in.size()))); auto ret = std::ranges::copy_if(range, Out(out.data()), [](int i) { return i % 2 == 0; }); assert((out == std::array{4, 6, 88, 44})); assert(base(ret.in) == in.data() + in.size()); assert(base(ret.out) == out.data() + out.size()); } } } template constexpr bool test_in_iterators() { test_iterators, Out, sentinel_wrapper>>(); test_iterators, Out, sentinel_wrapper>>(); test_iterators, Out>(); test_iterators, Out>(); test_iterators, Out>(); test_iterators, Out>(); test_iterators(); return true; } constexpr bool test() { test_in_iterators>(); test_in_iterators>(); test_in_iterators>(); test_in_iterators>(); test_in_iterators>(); test_in_iterators>(); test_in_iterators(); { // check that std::invoke is used { struct S { int val; int other; }; std::array in = {{{4, 2}, {1, 3}, {3, 4}, {3, 5}}}; std::array out; auto ret = std::ranges::copy_if(in.begin(), in.end(), out.begin(), [](int i) { return i == 3; }, &S::val); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(out[0].val == 3); assert(out[0].other == 4); assert(out[1].val == 3); assert(out[1].other == 5); } { struct S { int val; int other; }; std::array in = {{{4, 2}, {1, 3}, {3, 4}, {3, 5}}}; std::array out; auto ret = std::ranges::copy_if(in, out.begin(), [](int i) { return i == 3; }, &S::val); assert(ret.in == in.end()); assert(ret.out == out.end()); assert(out[0].val == 3); assert(out[0].other == 4); assert(out[1].val == 3); assert(out[1].other == 5); } } { // check that the complexity requirements are met { int predicateCount = 0; int projectionCount = 0; auto pred = [&](int i) { ++predicateCount; return i != 0; }; auto proj = [&](int i) { ++projectionCount; return i; }; int a[] = {5, 4, 3, 2, 1}; int b[5]; std::ranges::copy_if(a, a + 5, b, pred, proj); assert(predicateCount == 5); assert(projectionCount == 5); } { int predicateCount = 0; int projectionCount = 0; auto pred = [&](int i) { ++predicateCount; return i != 0; }; auto proj = [&](int i) { ++projectionCount; return i; }; int a[] = {5, 4, 3, 2, 1}; int b[5]; std::ranges::copy_if(a, b, pred, proj); assert(predicateCount == 5); assert(projectionCount == 5); } } { // test proxy iterator { std::array in = {4, 6, 87, 3, 88, 44, 45, 9}; std::array out; ProxyRange proxyIn{in}; ProxyRange proxyOut{out}; std::ranges::copy_if(proxyIn.begin(), proxyIn.end(), proxyOut.begin(), [](auto const& i) { return i.data % 2 == 0; }); assert((out == std::array{4, 6, 88, 44})); } { std::array in = {4, 6, 87, 3, 88, 44, 45, 9}; std::array out; ProxyRange proxyIn{in}; ProxyRange proxyOut{out}; std::ranges::copy_if(proxyIn, proxyOut.begin(), [](const auto& i) { return i.data % 2 == 0; }); assert((out == std::array{4, 6, 88, 44})); } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }