brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.2 KiB · 79098b0 Raw
174 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <algorithm>10 11// UNSUPPORTED: c++03, c++11, c++14, c++1712 13// template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity,14//          indirect_unary_predicate<projected<I, Proj>> Pred>15//   requires indirectly_writable<I, const T&>16//   constexpr I ranges::replace_if(I first, S last, Pred pred, const T& new_value, Proj proj = {});17// template<input_range R, class T, class Proj = identity,18//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>19//   requires indirectly_writable<iterator_t<R>, const T&>20//   constexpr borrowed_iterator_t<R>21//     ranges::replace_if(R&& r, Pred pred, const T& new_value, Proj proj = {});22 23#include <algorithm>24#include <array>25#include <cassert>26#include <ranges>27 28#include "almost_satisfies_types.h"29#include "test_iterators.h"30 31struct FalsePredicate {32  bool operator()(auto&&) { return false; }33};34 35template <class Iter, class Sent = sentinel_wrapper<Iter>>36concept HasReplaceIt = requires(Iter iter, Sent sent) { std::ranges::replace_if(iter, sent, FalsePredicate{}, 0); };37 38static_assert(HasReplaceIt<int*>);39static_assert(!HasReplaceIt<InputIteratorNotDerivedFrom>);40static_assert(!HasReplaceIt<InputIteratorNotIndirectlyReadable>);41static_assert(!HasReplaceIt<InputIteratorNotInputOrOutputIterator>);42static_assert(!HasReplaceIt<int*, SentinelForNotSemiregular>);43static_assert(!HasReplaceIt<int*, SentinelForNotWeaklyEqualityComparableWith>);44static_assert(!HasReplaceIt<int**>); // not indirectly_writable45static_assert(!HasReplaceIt<IndirectBinaryPredicateNotIndirectlyReadable>);46 47template <class Range>48concept HasReplaceR = requires(Range range) { std::ranges::replace_if(range, FalsePredicate{}, 0); };49 50static_assert(HasReplaceR<UncheckedRange<int*>>);51static_assert(!HasReplaceR<InputRangeNotDerivedFrom>);52static_assert(!HasReplaceR<InputRangeNotIndirectlyReadable>);53static_assert(!HasReplaceR<InputRangeNotInputOrOutputIterator>);54static_assert(!HasReplaceR<InputRangeNotSentinelSemiregular>);55static_assert(!HasReplaceR<InputRangeNotSentinelEqualityComparableWith>);56static_assert(!HasReplaceR<UncheckedRange<int**>>); // not indirectly_writable57static_assert(!HasReplaceR<InputRangeIndirectBinaryPredicateNotIndirectlyReadable>);58 59template <class Iter, class Sent, int N, class Pred>60constexpr void test(std::array<int, N> a_, Pred pred, int val, std::array<int, N> expected) {61  {62    auto a = a_;63    std::same_as<Iter> auto ret = std::ranges::replace_if(Iter(a.data()), Sent(Iter(a.data() + N)),64                                                          pred,65                                                          val);66    assert(base(ret) == a.data() + N);67    assert(a == expected);68  }69  {70    auto a = a_;71    auto range = std::ranges::subrange(Iter(a.data()), Sent(Iter(a.data() + N)));72    std::same_as<Iter> auto ret = std::ranges::replace_if(range, pred, val);73    assert(base(ret) == a.data() + N);74    assert(a == expected);75  }76}77 78template <class Iter, class Sent = Iter>79constexpr void test_iterators() {80  // simple test81  test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 3; }, 23, {23, 23, 3, 4});82  // no match83  test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i < 0; }, 23, {1, 2, 3, 4});84  // all match85  test<Iter, Sent, 4>({1, 2, 3, 4}, [](int i) { return i > 0; }, 23, {23, 23, 23, 23});86  // empty range87  test<Iter, Sent, 0>({}, [](int i) { return i > 0; }, 23, {});88  // single element range89  test<Iter, Sent, 1>({1}, [](int i) { return i > 0; }, 2, {2});90}91 92constexpr bool test() {93  test_iterators<cpp17_input_iterator<int*>, sentinel_wrapper<cpp17_input_iterator<int*>>>();94  test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();95  test_iterators<forward_iterator<int*>>();96  test_iterators<bidirectional_iterator<int*>>();97  test_iterators<random_access_iterator<int*>>();98  test_iterators<contiguous_iterator<int*>>();99  test_iterators<int*>();100 101  { // check that the projection is used102    struct S {103      constexpr S(int i_) : i(i_) {}104      int i;105    };106    {107      S a[] = {1, 2, 3, 4};108      std::ranges::replace_if(a, a + 4, [](int i) { return i == 3; }, S{0}, &S::i);109    }110    {111      S a[] = {1, 2, 3, 4};112      std::ranges::replace_if(a, [](int i) { return i == 3; }, S{0}, &S::i);113    }114  }115 116  { // check that std::invoke is used117    struct S {118      constexpr S(int i_) : i(i_) {}119      constexpr bool check() const { return false; }120      constexpr const S& identity() const { return *this; }121      int i;122    };123    {124      S a[] = {1, 2, 3, 4};125      auto ret = std::ranges::replace_if(std::begin(a), std::end(a), &S::check, S{2}, &S::identity);126      assert(ret == std::end(a));127    }128    {129      S a[] = {1, 2, 3, 4};130      auto ret = std::ranges::replace_if(a, &S::check, S{2}, &S::identity);131      assert(ret == std::end(a));132    }133  }134 135  { // check that std::ranges::dangling is returned136    [[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =137        std::ranges::replace_if(std::array {1, 2, 3, 4}, [](int) { return false; }, 1);138  }139 140  { // check that the complexity requirements are met141    {142      int predicateCount = 0;143      auto pred = [&](int) { ++predicateCount; return false; };144      auto projectionCount = 0;145      auto proj = [&](int i) { ++projectionCount; return i; };146      int a[] = {1, 2, 3, 4, 5};147      auto ret = std::ranges::replace_if(a, a + 5, pred, 1, proj);148      assert(ret == a + 5);149      assert(predicateCount == 5);150      assert(projectionCount == 5);151    }152    {153      int predicateCount = 0;154      auto pred = [&](int) { ++predicateCount; return false; };155      auto projectionCount = 0;156      auto proj = [&](int i) { ++projectionCount; return i; };157      int a[] = {1, 2, 3, 4, 5};158      auto ret = std::ranges::replace_if(a, pred, 1, proj);159      assert(ret == a + 5);160      assert(predicateCount == 5);161      assert(projectionCount == 5);162    }163  }164 165  return true;166}167 168int main(int, char**) {169  test();170  static_assert(test());171 172  return 0;173}174