//===----------------------------------------------------------------------===// // // 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, class Proj = identity, // indirect_unary_predicate> Pred> // constexpr subrange ranges::remove_if(I first, S last, Pred pred, Proj proj = {}); // template, Proj>> Pred> // requires permutable> // constexpr borrowed_subrange_t // ranges::remove_if(R&& r, Pred pred, Proj proj = {}); #include #include #include #include #include "almost_satisfies_types.h" #include "test_iterators.h" struct FalsePredicate { bool operator()(int) { return false; } }; template > concept HasRemoveIfIt = requires(Iter first, Sent last) { std::ranges::remove_if(first, last, FalsePredicate{}); }; static_assert(HasRemoveIfIt); static_assert(!HasRemoveIfIt); static_assert(!HasRemoveIfIt); static_assert(!HasRemoveIfIt); static_assert(!HasRemoveIfIt); static_assert(!HasRemoveIfIt); // not indirect_unary_predicate template concept HasRemoveIfR = requires(Range range) { std::ranges::remove_if(range, FalsePredicate{}); }; static_assert(HasRemoveIfR>); static_assert(!HasRemoveIfR); static_assert(!HasRemoveIfR); static_assert(!HasRemoveIfR); static_assert(!HasRemoveIfR); static_assert(!HasRemoveIfR>); // not indirect_unary_predicate template struct Data { std::array input; std::array expected; int cutoff; }; template constexpr void test(Data d) { { // iterator overload auto input = d.input; auto first = Iter(input.data()); auto last = Sent(Iter(input.data() + input.size())); std::same_as> decltype(auto) ret = std::ranges::remove_if(std::move(first), std::move(last), [&](int i) { return i < d.cutoff; }); assert(base(ret.begin()) == input.data() + M); assert(base(ret.end()) == input.data() + N); assert(std::ranges::equal(input.data(), base(ret.begin()), d.expected.begin(), d.expected.end())); } { // range overload auto input = d.input; auto range = std::ranges::subrange(Iter(input.data()), Sent(Iter(input.data() + input.size()))); std::same_as> decltype(auto) ret = std::ranges::remove_if(range, [&](int i) { return i < d.cutoff; }); assert(base(ret.begin()) == input.data() + M); assert(base(ret.end()) == input.data() + N); assert(std::ranges::equal(input.data(), base(ret.begin()), d.expected.begin(), d.expected.end())); } } template constexpr void tests() { // simple test test({.input = {1, 2, 3, 4, 5, 6}, .expected = {5, 6}, .cutoff = 5}); // empty range test({}); // single element range - no match test({.input = {1}, .expected = {1}, .cutoff = 1}); // single element range - match test({.input = {1}, .expected = {}, .cutoff = 2}); // two element range test({.input = {1, 2}, .expected = {2}, .cutoff = 2}); // all elements match test({.input = {1, 1, 1, 1, 1}, .expected = {}, .cutoff = 2}); // no elements match test({.input = {1, 1, 1, 1, 1}, .expected = {1, 1, 1, 1, 1}, .cutoff = 0}); // the relative order of elements isn't changed test({.input = {1, 2, 3, 2, 3, 4, 2, 5}, .expected = {2, 3, 2, 3, 4, 2, 5}, .cutoff = 2}); // multiple matches in a row test({.input = {1, 2, 2, 2, 1}, .expected = {2, 2, 2}, .cutoff = 2}); // only the last element matches test({.input = {2, 2, 1}, .expected = {2, 2}, .cutoff = 2}); // only the last element doesn't match test({.input = {1, 1, 2}, .expected = {2}, .cutoff = 2}); } template constexpr void test_sentinels() { tests(); tests>(); tests>(); } constexpr void test_iterators() { test_sentinels>(); test_sentinels>(); test_sentinels>(); test_sentinels>(); test_sentinels(); } constexpr bool test() { test_iterators(); { // check that ranges::dangling is returned [[maybe_unused]] std::same_as decltype(auto) ret = std::ranges::remove_if(std::array{1, 2, 3, 4}, [](int i) { return i < 0; }); } {// check complexity requirements // This is https://llvm.org/PR56382 - clang-format behaves weird if function-local structs are used // clang-format off { int comp_count = 0; auto comp = [&](int i) { ++comp_count; return i == 0; }; int proj_count = 0; auto proj = [&](int i) { ++proj_count; return i; }; int a[] = {1, 2, 3, 4}; auto ret = std::ranges::remove_if(std::begin(a), std::end(a), comp, proj); assert(ret.begin() == std::end(a) && ret.end() == std::end(a)); assert(comp_count == 4); assert(proj_count == 4); } { int comp_count = 0; auto comp = [&](int i) { ++comp_count; return i == 0; }; int proj_count = 0; auto proj = [&](int i) { ++proj_count; return i; }; int a[] = {1, 2, 3, 4}; auto ret = std::ranges::remove_if(a, comp, proj); assert(ret.begin() == std::end(a) && ret.end() == std::end(a)); assert(comp_count == 4); assert(proj_count == 4); } } { // check that std::invoke is used struct S { constexpr S& identity() { return *this; } constexpr bool predicate() const { return true; } }; { S a[4] = {}; auto ret = std::ranges::remove_if(std::begin(a), std::end(a), &S::predicate, &S::identity); assert(ret.begin() == std::begin(a)); assert(ret.end() == std::end(a)); } { S a[4] = {}; auto ret = std::ranges::remove_if(a, &S::predicate, &S::identity); assert(ret.begin() == std::begin(a)); assert(ret.end() == std::end(a)); } } return true; } // clang-format on int main(int, char**) { test(); static_assert(test()); return 0; }