File: ranges.count.pass.cpp

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (264 lines) | stat: -rw-r--r-- 8,196 bytes parent folder | download | duplicates (4)
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
255
256
257
258
259
260
261
262
263
264
//===----------------------------------------------------------------------===//
//
// 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

// template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity>
//   requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
//   constexpr iter_difference_t<I>
//     ranges::count(I first, S last, const T& value, Proj proj = {});
// template<input_range R, class T, class Proj = identity>
//   requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
//   constexpr range_difference_t<R>
//     ranges::count(R&& r, const T& value, Proj proj = {});

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

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

struct NotEqualityComparable {
  bool operator==(NotEqualityComparable&&) const;
  bool operator==(NotEqualityComparable&) const;
  bool operator==(const NotEqualityComparable&&) const;
};

template <class It, class Sent = It>
concept HasCountIt = requires(It it, Sent sent) { std::ranges::count(it, sent, *it); };
static_assert(HasCountIt<int*>);
static_assert(!HasCountIt<NotEqualityComparable*>);
static_assert(!HasCountIt<InputIteratorNotDerivedFrom>);
static_assert(!HasCountIt<InputIteratorNotIndirectlyReadable>);
static_assert(!HasCountIt<InputIteratorNotInputOrOutputIterator>);
static_assert(!HasCountIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>);
static_assert(!HasCountIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);

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

template <class Range, class ValT>
concept HasCountR = requires(Range r) { std::ranges::count(r, ValT{}); };
static_assert(HasCountR<std::array<int, 1>, int>);
static_assert(!HasCountR<int, int>);
static_assert(!HasCountR<std::array<NotEqualityComparable, 1>, NotEqualityComparable>);
static_assert(!HasCountR<InputRangeNotDerivedFrom, int>);
static_assert(!HasCountR<InputRangeNotIndirectlyReadable, int>);
static_assert(!HasCountR<InputRangeNotInputOrOutputIterator, int>);
static_assert(!HasCountR<InputRangeNotSentinelSemiregular, int>);
static_assert(!HasCountR<InputRangeNotSentinelEqualityComparableWith, int>);

template <class It, class Sent = It>
constexpr void test_iterators() {
  {
    // simple test
    {
      int a[] = {1, 2, 3, 4};
      std::same_as<std::ptrdiff_t> auto ret = std::ranges::count(It(a), Sent(It(a + 4)), 3);
      assert(ret == 1);
    }
    {
      int a[] = {1, 2, 3, 4};
      auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
      std::same_as<std::ptrdiff_t> auto ret = std::ranges::count(range, 3);
      assert(ret == 1);
    }
  }

  {
    // check that an empty range works
    {
      std::array<int, 0> a = {};
      auto ret = std::ranges::count(It(a.data()), Sent(It(a.data() + a.size())), 1);
      assert(ret == 0);
    }
    {
      std::array<int, 0> a = {};
      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size())));
      auto ret = std::ranges::count(range, 1);
      assert(ret == 0);
    }
  }

  {
    // check that a range with a single element works
    {
      std::array a = {2};
      auto ret = std::ranges::count(It(a.data()), Sent(It(a.data() + a.size())), 2);
      assert(ret == 1);
    }
    {
      std::array a = {2};
      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size())));
      auto ret = std::ranges::count(range, 2);
      assert(ret == 1);
    }
  }

  {
    // check that 0 is returned with no match
    {
      std::array a = {1, 1, 1};
      auto ret = std::ranges::count(It(a.data()), Sent(It(a.data() + a.size())), 0);
      assert(ret == 0);
    }
    {
      std::array a = {1, 1, 1};
      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size())));
      auto ret = std::ranges::count(range, 0);
      assert(ret == 0);
    }
  }

  {
    // check that more than one element is counted
    {
      std::array a = {3, 3, 4, 3, 3};
      auto ret = std::ranges::count(It(a.data()), Sent(It(a.data() + a.size())), 3);
      assert(ret == 4);
    }
    {
      std::array a = {3, 3, 4, 3, 3};
      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size())));
      auto ret = std::ranges::count(range, 3);
      assert(ret == 4);
    }
  }

  {
    // check that all elements are counted
    {
      std::array a = {5, 5, 5, 5};
      auto ret = std::ranges::count(It(a.data()), Sent(It(a.data() + a.size())), 5);
      assert(ret == 4);
    }
    {
      std::array a = {5, 5, 5, 5};
      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size())));
      auto ret = std::ranges::count(range, 5);
      assert(ret == 4);
    }
  }
}

constexpr bool test() {
  test_iterators<int*>();
  test_iterators<const int*>();
  test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>();
  test_iterators<bidirectional_iterator<int*>>();
  test_iterators<forward_iterator<int*>>();
  test_iterators<random_access_iterator<int*>>();
  test_iterators<contiguous_iterator<int*>>();

  {
    // 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::count(a, a + 4, a + 3, [](int& i) { return &i; });
      assert(ret == 1);
    }
    {
      int a[] = {1, 2, 3, 4};
      auto ret = std::ranges::count(a, a + 3, [](int& i) { return &i; });
      assert(ret == 1);
    }
  }

  {
    // check that std::invoke is used
    struct S { int i; };
    S a[] = { S{1}, S{3}, S{2} };
    std::same_as<std::ptrdiff_t> auto ret = std::ranges::count(a, 4, &S::i);
    assert(ret == 0);
  }

  {
    // count invocations of the projection
    {
      int a[] = {1, 2, 3, 4};
      int projection_count = 0;
      auto ret = std::ranges::count(a, a + 4, 2, [&](int i) { ++projection_count; return i; });
      assert(ret == 1);
      assert(projection_count == 4);
    }
    {
      int a[] = {1, 2, 3, 4};
      int projection_count = 0;
      auto ret = std::ranges::count(a, 2, [&](int i) { ++projection_count; return i; });
      assert(ret == 1);
      assert(projection_count == 4);
    }
  }

  {
    // check that an immobile type works
    struct NonMovable {
      NonMovable(const NonMovable&) = delete;
      NonMovable(NonMovable&&) = delete;
      constexpr NonMovable(int i_) : i(i_) {}
      int i;

      bool operator==(const NonMovable&) const = default;
    };
    {
      NonMovable a[] = {9, 8, 4, 3};
      auto ret = std::ranges::count(a, a + 4, NonMovable(8));
      assert(ret == 1);
    }
    {
      NonMovable a[] = {9, 8, 4, 3};
      auto ret = std::ranges::count(a, NonMovable(8));
      assert(ret == 1);
    }
  }

  {
    // check that difference_type is used
    struct DiffTypeIterator {
      using difference_type = signed char;
      using value_type = int;

      int* it = nullptr;

      constexpr DiffTypeIterator() = default;
      constexpr DiffTypeIterator(int* i) : it(i) {}

      constexpr int& operator*() const { return *it; }
      constexpr DiffTypeIterator& operator++() { ++it; return *this; }
      constexpr void operator++(int) { ++it; }

      bool operator==(const DiffTypeIterator&) const = default;
    };

    {
      int a[] = {5, 5, 4, 3, 2, 1};
      std::same_as<signed char> decltype(auto) ret =
          std::ranges::count(DiffTypeIterator(a), DiffTypeIterator(a + 6), 4);
      assert(ret == 1);
    }
    {
      int a[] = {5, 5, 4, 3, 2, 1};
      auto range = std::ranges::subrange(DiffTypeIterator(a), DiffTypeIterator(a + 6));
      std::same_as<signed char> decltype(auto) ret = std::ranges::count(range, 4);
      assert(ret == 1);
    }
  }

  return true;
}

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

  return 0;
}