#include <algorithm>
#include <array>
#include <concepts>
#include <functional>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
struct UnaryPred { bool operator()(int) const; };
template <class Iter = int*, class Sent = int*, class Pred = UnaryPred>
concept HasPartitionIter =
requires(Iter&& iter, Sent&& sent, Pred&& pred) {
std::ranges::partition(std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<Pred>(pred));
};
static_assert(HasPartitionIter<int*, int*, UnaryPred>);
static_assert(!HasPartitionIter<PermutableNotForwardIterator>);
static_assert(!HasPartitionIter<PermutableNotSwappable>);
static_assert(!HasPartitionIter<int*, SentinelForNotSemiregular>);
static_assert(!HasPartitionIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasPartitionIter<int*, int*, IndirectUnaryPredicateNotPredicate>);
static_assert(!HasPartitionIter<int*, int*, IndirectUnaryPredicateNotCopyConstructible>);
template <class Range, class Pred>
concept HasPartitionRange =
requires(Range&& range, Pred&& pred) {
std::ranges::partition(std::forward<Range>(range), std::forward<Pred>(pred));
};
template <class T>
using R = UncheckedRange<T>;
static_assert(HasPartitionRange<R<int*>, UnaryPred>);
static_assert(!HasPartitionRange<ForwardRangeNotDerivedFrom, UnaryPred>);
static_assert(!HasPartitionRange<ForwardRangeNotIncrementable, UnaryPred>);
static_assert(!HasPartitionRange<R<int*>, IndirectUnaryPredicateNotPredicate>);
static_assert(!HasPartitionRange<R<int*>, IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasPartitionRange<R<PermutableNotForwardIterator>, UnaryPred>);
static_assert(!HasPartitionRange<R<PermutableNotSwappable>, UnaryPred>);
template <class Iter, class Sent, std::size_t N, class Pred>
constexpr void test_one(std::array<int, N> input, Pred pred, std::size_t partition_point) {
auto neg_pred = [&](int x) { return !pred(x); };
{
auto partitioned = input;
auto b = Iter(partitioned.data());
auto e = Sent(Iter(partitioned.data() + partitioned.size()));
std::same_as<std::ranges::subrange<Iter>> 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));
}
{
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<std::ranges::subrange<Iter>> 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 <class Iter, class Sent>
constexpr void test_iterators_2() {
auto is_odd = [](int x) { return x % 2 != 0; };
test_one<Iter, Sent, 0>({}, is_odd, 0);
test_one<Iter, Sent, 1>({1}, is_odd, 1);
test_one<Iter, Sent, 1>({2}, is_odd, 0);
test_one<Iter, Sent, 2>({2, 1}, is_odd, 1);
test_one<Iter, Sent, 2>({1, 2}, is_odd, 1);
test_one<Iter, Sent, 3>({2, 1, 3}, is_odd, 2);
test_one<Iter, Sent, 8>({2, 1, 3, 6, 8, 4, 11, 5}, is_odd, 4);
test_one<Iter, Sent, 8>({2, 1, 3, 6, 2, 8, 1, 6}, is_odd, 3);
test_one<Iter, Sent, 3>({1, 1, 1}, is_odd, 3);
test_one<Iter, Sent, 3>({2, 2, 2}, is_odd, 0);
test_one<Iter, Sent, 6>({1, 3, 5, 4, 6, 8}, is_odd, 3);
test_one<Iter, Sent, 6>({4, 6, 8, 1, 3, 5}, is_odd, 3);
test_one<Iter, Sent, 6>({1, 2, 1, 2, 1, 2}, is_odd, 3);
auto is_negative = [](int x) { return x < 0; };
test_one<Iter, Sent, 5>({-3, 5, 7, -6, 2}, is_negative, 2);
}
template <class Iter>
constexpr void test_iterators_1() {
test_iterators_2<Iter, Iter>();
test_iterators_2<Iter, sentinel_wrapper<Iter>>();
}
constexpr void test_iterators() {
test_iterators_1<forward_iterator<int*>>();
test_iterators_1<bidirectional_iterator<int*>>();
test_iterators_1<random_access_iterator<int*>>();
test_iterators_1<contiguous_iterator<int*>>();
test_iterators_1<int*>();
}
constexpr bool test() {
test_iterators();
{
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};
{
{
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);
}
}
{
{
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;
}