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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
//===----------------------------------------------------------------------===//
//
// 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<forward_iterator I, sentinel_for<I> S, class T,
// class Pred = ranges::equal_to, class Proj = identity>
// requires indirectly_comparable<I, const T*, Pred, Proj>
// constexpr subrange<I>
// ranges::search_n(I first, S last, iter_difference_t<I> count,
// const T& value, Pred pred = {}, Proj proj = {});
// template<forward_range R, class T, class Pred = ranges::equal_to,
// class Proj = identity>
// requires indirectly_comparable<iterator_t<R>, const T*, Pred, Proj>
// constexpr borrowed_subrange_t<R>
// ranges::search_n(R&& r, range_difference_t<R> count,
// const T& value, Pred pred = {}, Proj proj = {});
#include <algorithm>
#include <array>
#include <ranges>
#include "almost_satisfies_types.h"
#include "test_iterators.h"
template <class Iter1, class Sent1 = Iter1>
concept HasSearchNIt = requires (Iter1 first1, Sent1 last1) {
std::ranges::search_n(first1, last1, 0, 0);
};
static_assert(HasSearchNIt<int*>);
static_assert(!HasSearchNIt<ForwardIteratorNotDerivedFrom>);
static_assert(!HasSearchNIt<ForwardIteratorNotIncrementable>);
static_assert(!HasSearchNIt<int*, SentinelForNotSemiregular>);
static_assert(!HasSearchNIt<int*, SentinelForNotWeaklyEqualityComparableWith>);
static_assert(!HasSearchNIt<int**, int**>); // not indirectly comparable
template <class Range1, class Range2 = UncheckedRange<int*>>
concept HasSearchNR = requires (Range1 range) {
std::ranges::search_n(range, 0, 0);
};
static_assert(HasSearchNR<UncheckedRange<int*>>);
static_assert(!HasSearchNR<ForwardRangeNotDerivedFrom>);
static_assert(!HasSearchNR<ForwardIteratorNotIncrementable>);
static_assert(!HasSearchNR<ForwardRangeNotSentinelSemiregular>);
static_assert(!HasSearchNR<ForwardRangeNotSentinelEqualityComparableWith>);
static_assert(!HasSearchNR<UncheckedRange<int**>>); // not indirectly comparable
template <class Iter, class Sent = Iter>
constexpr void test_iterators() {
{ // simple test
{
int a[] = {1, 2, 3, 4, 5, 6};
std::same_as<std::ranges::subrange<Iter, Iter>> decltype(auto) ret =
std::ranges::search_n(Iter(a), Sent(Iter(a + 6)), 1, 3);
assert(base(ret.begin()) == a + 2);
assert(base(ret.end()) == a + 3);
}
{
int a[] = {1, 2, 3, 4, 5, 6};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
std::same_as<std::ranges::subrange<Iter, Iter>> decltype(auto) ret = std::ranges::search_n(range, 1, 3);
assert(base(ret.begin()) == a + 2);
assert(base(ret.end()) == a + 3);
}
}
{ // matching part begins at the front
{
int a[] = {7, 7, 3, 7, 3, 6};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 6)), 2, 7);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 2);
}
{
int a[] = {7, 7, 3, 7, 3, 6};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
auto ret = std::ranges::search_n(range, 2, 7);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 2);
}
}
{ // matching part ends at the back
{
int a[] = {9, 3, 6, 4, 4};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 5)), 2, 4);
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 5);
}
{
int a[] = {9, 3, 6, 4, 4};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
auto ret = std::ranges::search_n(range, 2, 4);
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 5);
}
}
{ // pattern does not match
{
int a[] = {9, 3, 6, 4, 8};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 5)), 1, 1);
assert(base(ret.begin()) == a + 5);
assert(base(ret.end()) == a + 5);
}
{
int a[] = {9, 3, 6, 4, 8};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 5)));
auto ret = std::ranges::search_n(range, 1, 1);
assert(base(ret.begin()) == a + 5);
assert(base(ret.end()) == a + 5);
}
}
{ // range and pattern are identical
{
int a[] = {1, 1, 1, 1};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 4)), 4, 1);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 4);
}
{
int a[] = {1, 1, 1, 1};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 4)));
auto ret = std::ranges::search_n(range, 4, 1);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 4);
}
}
{ // pattern is longer than range
{
int a[] = {3, 3, 3};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 3)), 4, 3);
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 3);
}
{
int a[] = {3, 3, 3};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::search_n(range, 4, 3);
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 3);
}
}
{ // pattern has zero length
{
int a[] = {6, 7, 8};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 3)), 0, 7);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a);
}
{
int a[] = {6, 7, 8};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 3)));
auto ret = std::ranges::search_n(range, 0, 7);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a);
}
}
{ // range has zero length
{
int a[] = {};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a)), 1, 1);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a);
}
{
int a[] = {};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a)));
auto ret = std::ranges::search_n(range, 1, 1);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a);
}
}
{ // check that the first match is returned
{
int a[] = {6, 6, 8, 6, 6, 8, 6, 6, 8};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 9)), 2, 6);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 2);
}
{
int a[] = {6, 6, 8, 6, 6, 8, 6, 6, 8};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 9)));
auto ret = std::ranges::search_n(range, 2, 6);
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 2);
}
}
{ // check that the predicate is used
{
int a[] = {1, 4, 4, 3, 6, 1};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 6)),
3,
4,
[](int l, int r) { return l == r || l == 1; });
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 3);
}
{
int a[] = {1, 4, 4, 3, 6, 1};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
auto ret = std::ranges::search_n(range, 3, 4, [](int l, int r) { return l == r || l == 1; });
assert(base(ret.begin()) == a);
assert(base(ret.end()) == a + 3);
}
}
{ // check that the projections are used
{
int a[] = {1, 3, 1, 6, 5, 6};
auto ret = std::ranges::search_n(Iter(a), Sent(Iter(a + 6)),
3,
6,
{},
[](int i) { return i % 2 == 0 ? i : i + 1; });
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 6);
}
{
int a[] = {1, 3, 1, 6, 5, 6};
auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 6)));
auto ret = std::ranges::search_n(range,
3,
6,
{},
[](int i) { return i % 2 == 0 ? i : i + 1; });
assert(base(ret.begin()) == a + 3);
assert(base(ret.end()) == a + 6);
}
}
}
constexpr bool test() {
test_iterators<forward_iterator<int*>>();
test_iterators<forward_iterator<int*>, sized_sentinel<forward_iterator<int*>>>();
test_iterators<bidirectional_iterator<int*>>();
test_iterators<bidirectional_iterator<int*>, sized_sentinel<bidirectional_iterator<int*>>>();
test_iterators<random_access_iterator<int*>>();
test_iterators<random_access_iterator<int*>, sized_sentinel<random_access_iterator<int*>>>();
test_iterators<contiguous_iterator<int*>>();
test_iterators<contiguous_iterator<int*>, sized_sentinel<contiguous_iterator<int*>>>();
test_iterators<int*>();
{ // check that std::invoke is used
struct S {
int i;
constexpr S identity() {
return *this;
}
constexpr bool compare(int o) {
return i == o;
}
};
{
S a[] = {{1}, {2}, {3}, {4}};
auto ret = std::ranges::search_n(a, a + 4, 1, 2, &S::compare, &S::identity);
assert(ret.begin() == a + 1);
assert(ret.end() == a + 2);
}
{
S a[] = {{1}, {2}, {3}, {4}};
auto ret = std::ranges::search_n(a, 1, 2, &S::compare, &S::identity);
assert(ret.begin() == a + 1);
assert(ret.end() == a + 2);
}
}
{ // check that std::ranges::dangling is returned
[[maybe_unused]] std::same_as<std::ranges::dangling> decltype(auto) ret =
std::ranges::search_n(std::array {1, 2, 3, 4}, 1, 0);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|