//===----------------------------------------------------------------------===// // // 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 // partition(I first, S last, Pred pred, Proj proj = {}); // Since C++20 // // template, Proj>> Pred> // requires permutable> // constexpr borrowed_subrange_t // partition(R&& r, Pred pred, Proj proj = {}); // Since C++20 #include #include #include #include #include #include "almost_satisfies_types.h" #include "test_iterators.h" struct UnaryPred { bool operator()(int) const; }; // Test constraints of the (iterator, sentinel) overload. // ====================================================== template concept HasPartitionIter = requires(Iter&& iter, Sent&& sent, Pred&& pred) { std::ranges::partition(std::forward(iter), std::forward(sent), std::forward(pred)); }; static_assert(HasPartitionIter); // !permutable static_assert(!HasPartitionIter); static_assert(!HasPartitionIter); // !sentinel_for static_assert(!HasPartitionIter); static_assert(!HasPartitionIter); // !indirect_unary_predicate> static_assert(!HasPartitionIter); static_assert(!HasPartitionIter); // Test constraints of the (range) overload. // ========================================= template concept HasPartitionRange = requires(Range&& range, Pred&& pred) { std::ranges::partition(std::forward(range), std::forward(pred)); }; template using R = UncheckedRange; static_assert(HasPartitionRange, UnaryPred>); // !forward_range static_assert(!HasPartitionRange); static_assert(!HasPartitionRange); // !indirect_unary_predicate, Proj>> Pred> static_assert(!HasPartitionRange, IndirectUnaryPredicateNotPredicate>); static_assert(!HasPartitionRange, IndirectUnaryPredicateNotCopyConstructible>); // !permutable> static_assert(!HasPartitionRange, UnaryPred>); static_assert(!HasPartitionRange, UnaryPred>); // `partition` isn't a stable algorithm so this function cannot test the exact output. template constexpr void test_one(std::array input, Pred pred, std::size_t partition_point) { auto neg_pred = [&](int x) { return !pred(x); }; { // (iterator, sentinel) overload. auto partitioned = input; auto b = Iter(partitioned.data()); auto e = Sent(Iter(partitioned.data() + partitioned.size())); std::same_as> decltype(auto) result = std::ranges::partition(b, e, pred); assert(base(result.begin()) == partitioned.data() + partition_point); assert(base(result.end()) == partitioned.data() + partitioned.size()); assert(std::ranges::all_of(b, result.begin(), pred)); assert(std::ranges::all_of(result.begin(), e, neg_pred)); } { // (range) overload. auto partitioned = input; auto b = Iter(partitioned.data()); auto e = Sent(Iter(partitioned.data() + partitioned.size())); auto range = std::ranges::subrange(b, e); std::same_as> decltype(auto) result = std::ranges::partition(range, pred); assert(base(result.begin()) == partitioned.data() + partition_point); assert(base(result.end()) == partitioned.data() + partitioned.size()); assert(std::ranges::all_of(b, result.begin(), pred)); assert(std::ranges::all_of(result.begin(), e, neg_pred)); } } template constexpr void test_iterators_2() { auto is_odd = [](int x) { return x % 2 != 0; }; // Empty sequence. test_one({}, is_odd, 0); // 1-element sequence, the element satisfies the predicate. test_one({1}, is_odd, 1); // 1-element sequence, the element doesn't satisfy the predicate. test_one({2}, is_odd, 0); // 2-element sequence, not in order. test_one({2, 1}, is_odd, 1); // 2-element sequence, already in order. test_one({1, 2}, is_odd, 1); // 3-element sequence. test_one({2, 1, 3}, is_odd, 2); // Longer sequence. test_one({2, 1, 3, 6, 8, 4, 11, 5}, is_odd, 4); // Longer sequence with duplicates. test_one({2, 1, 3, 6, 2, 8, 1, 6}, is_odd, 3); // All elements are the same and satisfy the predicate. test_one({1, 1, 1}, is_odd, 3); // All elements are the same and don't satisfy the predicate. test_one({2, 2, 2}, is_odd, 0); // Already partitioned. test_one({1, 3, 5, 4, 6, 8}, is_odd, 3); // Reverse-partitioned. test_one({4, 6, 8, 1, 3, 5}, is_odd, 3); // Repeating pattern. test_one({1, 2, 1, 2, 1, 2}, is_odd, 3); auto is_negative = [](int x) { return x < 0; }; // Different comparator. test_one({-3, 5, 7, -6, 2}, is_negative, 2); } template constexpr void test_iterators_1() { test_iterators_2(); test_iterators_2>(); } constexpr void test_iterators() { test_iterators_1>(); test_iterators_1>(); test_iterators_1>(); test_iterators_1>(); test_iterators_1(); } constexpr bool test() { test_iterators(); { // A custom projection works. const std::array input = {1, -1}; auto is_negative = [](int x) { return x < 0; }; auto negate = [](int x) { return -x; }; const std::array expected_no_proj = {-1, 1}; const std::array expected_with_proj = {1, -1}; { // (iterator, sentinel) overload. { auto in = input; std::ranges::partition(in.begin(), in.end(), is_negative); assert(in == expected_no_proj); } { auto in = input; std::ranges::partition(in.begin(), in.end(), is_negative, negate); assert(in == expected_with_proj); } } { // (range) overload. { auto in = input; std::ranges::partition(in, is_negative); assert(in == expected_no_proj); } { auto in = input; std::ranges::partition(in, is_negative, negate); assert(in == expected_with_proj); } } } return true; } int main(int, char**) { test(); static_assert(test()); return 0; }