File: ranges.find_last_if_not.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (254 lines) | stat: -rw-r--r-- 8,554 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <algorithm>

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20

// template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
//          indirect_unary_predicate<projected<I, Proj>> Pred>
//   constexpr subrange<I> ranges::find_last_if_not(I first, S last, Pred pred, Proj proj = {});
// template<forward_range R, class Proj = identity,
//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
//   constexpr borrowed_subrange_t<R> ranges::find_last_if_not(R&& r, Pred pred, Proj proj = {});

#include <algorithm>
#include <array>
#include <cassert>
#include <ranges>

#include "almost_satisfies_types.h"
#include "test_iterators.h"

struct Predicate {
  bool operator()(int);
};

template <class It, class Sent = It>
concept HasFindLastIfIt = requires(It it, Sent sent) { std::ranges::find_last_if_not(it, sent, Predicate{}); };
static_assert(HasFindLastIfIt<int*>);
static_assert(HasFindLastIfIt<forward_iterator<int*>>);
static_assert(!HasFindLastIfIt<cpp20_input_iterator<int*>>);
static_assert(!HasFindLastIfIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasFindLastIfIt<ForwardIteratorNotIncrementable>);
static_assert(!HasFindLastIfIt<forward_iterator<int*>, SentinelForNotSemiregular>);
static_assert(!HasFindLastIfIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);

static_assert(!HasFindLastIfIt<int*, int>);
static_assert(!HasFindLastIfIt<int, int*>);

template <class Pred>
concept HasFindLastIfPred = requires(int* it, Pred pred) { std::ranges::find_last_if_not(it, it, pred); };

static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotCopyConstructible>);
static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotPredicate>);

template <class R>
concept HasFindLastIfR = requires(R r) { std::ranges::find_last_if_not(r, Predicate{}); };
static_assert(HasFindLastIfR<std::array<int, 0>>);
static_assert(!HasFindLastIfR<int>);
static_assert(!HasFindLastIfR<ForwardRangeNotDerivedFrom>);
static_assert(!HasFindLastIfR<ForwardRangeNotIncrementable>);
static_assert(!HasFindLastIfR<ForwardRangeNotSentinelSemiregular>);
static_assert(!HasFindLastIfR<ForwardRangeNotSentinelEqualityComparableWith>);

template <class It, class Sent>
constexpr auto make_range(auto& a) {
  return std::ranges::subrange(It(std::ranges::begin(a)), Sent(It(std::ranges::end(a))));
}

template <template <class> class IteratorT, template <class> class SentinelT>
constexpr void test_iterator_classes() {
  {
    using it   = IteratorT<int*>;
    using sent = SentinelT<it>;

    {
      int a[] = {1, 2, 3, 4};
      std::same_as<std::ranges::subrange<it>> auto ret =
          std::ranges::find_last_if_not(it(a), sent(it(a + 4)), [](int x) { return x != 4; });
      assert(base(ret.begin()) == a + 3);
      assert(*ret.begin() == 4);
    }
    {
      int a[] = {1, 2, 3, 4};

      std::same_as<std::ranges::subrange<it>> auto ret =
          std::ranges::find_last_if_not(make_range<it, sent>(a), [](int x) { return x != 4; });
      assert(ret.begin() == it(a + 3));
      assert(*ret.begin() == 4);
    }
  }

  { // check that an empty range works
    using it   = IteratorT<std::ranges::iterator_t<std::array<int, 0>&>>;
    using sent = SentinelT<it>;

    {
      std::array<int, 0> a = {};

      auto ret = std::ranges::find_last_if_not(it(a.data()), sent(it(a.data())), [](auto&&) { return false; }).begin();
      assert(ret == it(a.data()));
    }
    {
      std::array<int, 0> a = {};

      auto ret = std::ranges::find_last_if_not(make_range<it, sent>(a), [](auto&&) { return false; }).begin();
      assert(ret == it(a.begin()));
    }
  }

  { // check that last is returned with no match
    using it   = IteratorT<int*>;
    using sent = SentinelT<it>;

    {
      int a[] = {1, 1, 1};

      auto ret = std::ranges::find_last_if_not(it(a), sent(it(a + 3)), [](auto&&) { return true; }).begin();
      assert(ret == it(a + 3));
    }
    {
      int a[] = {1, 1, 1};

      auto ret = std::ranges::find_last_if_not(make_range<it, sent>(a), [](auto&&) { return true; }).begin();
      assert(ret == it(a + 3));
    }
  }

  { // check that the last element is returned
    struct S {
      int comp;
      int other;
    };
    using it   = IteratorT<S*>;
    using sent = SentinelT<it>;

    S a[] = {{0, 0}, {0, 2}, {0, 1}};

    auto ret = std::ranges::find_last_if_not(
                   it(std::begin(a)), sent(it(std::end(a))), [](int c) { return c != 0; }, &S::comp)
                   .begin();
    assert(ret == it(a + 2));
    assert((*ret).comp == 0);
    assert((*ret).other == 1);
  }

  {
    // count projection and predicate invocation count
    {
      int a[]              = {1, 2, 3, 4};
      int predicate_count  = 0;
      int projection_count = 0;

      using it   = IteratorT<int*>;
      using sent = SentinelT<it>;

      auto ret =
          std::ranges::find_last_if_not(
              it(a),
              sent(it(a + 4)),
              [&](int i) {
                ++predicate_count;
                return i != 2;
              },
              [&](int i) {
                ++projection_count;
                return i;
              })
              .begin();
      assert(ret == it(a + 1));
      assert(*ret == 2);

      if constexpr (std::bidirectional_iterator<it>) {
        assert(predicate_count == 3);
        assert(projection_count == 3);
      } else {
        assert(predicate_count == 4);
        assert(projection_count == 4);
      }
    }
  }
}

struct NonConstComparable {
  friend constexpr bool operator!=(const NonConstComparable&, const NonConstComparable&) { return true; }
  friend constexpr bool operator!=(NonConstComparable&, NonConstComparable&) { return true; }
  friend constexpr bool operator!=(const NonConstComparable&, NonConstComparable&) { return true; }
  friend constexpr bool operator!=(NonConstComparable&, const NonConstComparable&) { return false; }
};

template <class T>
using add_const_to_ptr_t = std::add_pointer_t<std::add_const_t<std::remove_pointer_t<T>>>;

constexpr bool test() {
  test_iterator_classes<std::type_identity_t, std::type_identity_t>();
  test_iterator_classes<add_const_to_ptr_t, std::type_identity_t>();
  test_iterator_classes<contiguous_iterator, std::type_identity_t>();
  test_iterator_classes<random_access_iterator, std::type_identity_t>();
  test_iterator_classes<bidirectional_iterator, std::type_identity_t>();
  test_iterator_classes<forward_iterator, std::type_identity_t>();
  test_iterator_classes<forward_iterator, sentinel_wrapper>();

  {
    // check that projections are used properly and that they are called with the iterator directly
    {
      int a[]  = {1, 2, 3, 4};
      auto ret = std::ranges::find_last_if_not(
                     a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; })
                     .begin();
      assert(ret == a + 3);
    }
    {
      int a[] = {1, 2, 3, 4};
      auto ret =
          std::ranges::find_last_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; }).begin();
      assert(ret == a + 3);
    }
  }

  {
    // check that ranges::dangling is returned
    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
        std::ranges::find_last_if_not(std::array{1, 2}, [](int) { return false; });
  }

  {
    // check that a subrange is returned with a borrowing range
    int a[] = {1, 2, 3, 4};
    std::same_as<std::ranges::subrange<int*>> auto ret =
        std::ranges::find_last_if_not(std::views::all(a), [](int) { return false; });
    assert(ret.begin() == a + 3);
    assert(*ret.begin() == 4);
  }

  {
    // check that the return type of `iter::operator*` doesn't change
    {
      NonConstComparable a[] = {NonConstComparable{}};

      auto ret = std::ranges::find_last_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparable{}; }).begin();
      assert(ret == a);
    }
    {
      NonConstComparable a[] = {NonConstComparable{}};

      auto ret = std::ranges::find_last_if_not(a, [](auto&& e) { return e != NonConstComparable{}; }).begin();
      assert(ret == a);
    }
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}