|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// <algorithm> |
| 10 | + |
| 11 | +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 12 | +// template<input_iterator I, sentinel_for<I> S, class T, class Proj = identity> |
| 13 | +// requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*> |
| 14 | +// constexpr bool ranges::contains(I first, S last, const T& value, Proj proj = {}); // since C++23 |
| 15 | + |
| 16 | +// template<input_range R, class T, class Proj = identity> |
| 17 | +// requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*> |
| 18 | +// constexpr bool ranges::contains(R&& r, const T& value, Proj proj = {}); // since C++23 |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <array> |
| 22 | +#include <cassert> |
| 23 | +#include <ranges> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "almost_satisfies_types.h" |
| 27 | +#include "boolean_testable.h" |
| 28 | +#include "test_iterators.h" |
| 29 | + |
| 30 | +struct NotEqualityComparable {}; |
| 31 | + |
| 32 | +template <class Iter, class Sent = Iter> |
| 33 | +concept HasContainsIt = requires(Iter iter, Sent sent) { std::ranges::contains(iter, sent, *iter); }; |
| 34 | + |
| 35 | +static_assert(HasContainsIt<int*>); |
| 36 | +static_assert(!HasContainsIt<NotEqualityComparable*>); |
| 37 | +static_assert(!HasContainsIt<InputIteratorNotDerivedFrom>); |
| 38 | +static_assert(!HasContainsIt<InputIteratorNotIndirectlyReadable>); |
| 39 | +static_assert(!HasContainsIt<InputIteratorNotInputOrOutputIterator>); |
| 40 | +static_assert(!HasContainsIt<cpp20_input_iterator<int*>, SentinelForNotSemiregular>); |
| 41 | +static_assert(!HasContainsIt<cpp20_input_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>); |
| 42 | + |
| 43 | +static_assert(!HasContainsIt<int*, int>); |
| 44 | +static_assert(!HasContainsIt<int, int*>); |
| 45 | + |
| 46 | +template <class Range, class ValT> |
| 47 | +concept HasContainsR = requires(Range range) { std::ranges::contains(range, ValT{}); }; |
| 48 | + |
| 49 | +static_assert(HasContainsR<std::array<int, 1>, int>); |
| 50 | +static_assert(!HasContainsR<int, int>); |
| 51 | +static_assert(!HasContainsR<std::array<NotEqualityComparable, 1>, NotEqualityComparable>); |
| 52 | +static_assert(!HasContainsR<InputRangeNotDerivedFrom, int>); |
| 53 | +static_assert(!HasContainsR<InputRangeNotIndirectlyReadable, int>); |
| 54 | +static_assert(!HasContainsR<InputRangeNotInputOrOutputIterator, int>); |
| 55 | +static_assert(!HasContainsR<InputRangeNotSentinelSemiregular, int>); |
| 56 | +static_assert(!HasContainsR<InputRangeNotSentinelEqualityComparableWith, int>); |
| 57 | + |
| 58 | +static std::vector<int> comparable_data; |
| 59 | + |
| 60 | +// clang-format off |
| 61 | +template <class Iter, class Sent = Iter> |
| 62 | +constexpr void test_iterators() { |
| 63 | + using ValueT = std::iter_value_t<Iter>; |
| 64 | + { // simple tests |
| 65 | + { |
| 66 | + ValueT a[] = {1, 2, 3, 4, 5, 6}; |
| 67 | + std::same_as<bool> auto ret = |
| 68 | + std::ranges::contains(Iter(a), Sent(Iter(a + 6)), 3); |
| 69 | + assert(ret); |
| 70 | + } |
| 71 | + { |
| 72 | + ValueT a[] = {1, 2, 3, 4, 5, 6}; |
| 73 | + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 6))); |
| 74 | + std::same_as<bool> decltype(auto) ret = |
| 75 | + std::ranges::contains(range, 3); |
| 76 | + assert(ret); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + { // check that an empty range works |
| 81 | + { |
| 82 | + ValueT a[] = {}; |
| 83 | + auto ret = std::ranges::contains(Iter(a), Sent(Iter(a)), 1); |
| 84 | + assert(!ret); |
| 85 | + } |
| 86 | + { |
| 87 | + ValueT a[] = {}; |
| 88 | + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a))); |
| 89 | + auto ret = std::ranges::contains(range, 1); |
| 90 | + assert(!ret); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + { // check that no match |
| 95 | + { |
| 96 | + ValueT a[] = {13, 1, 21, 4, 5}; |
| 97 | + auto ret = std::ranges::contains(Iter(a), Sent(Iter(a + 5)), 10); |
| 98 | + assert(!ret); |
| 99 | + } |
| 100 | + { |
| 101 | + ValueT a[] = {13, 1, 21, 4, 5}; |
| 102 | + auto range = std::ranges::subrange(Iter(a), Sent(Iter(a + 5))); |
| 103 | + auto ret = std::ranges::contains(range, 10); |
| 104 | + assert(!ret); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (!std::is_constant_evaluated()) |
| 109 | + comparable_data.clear(); |
| 110 | +} |
| 111 | +template <class ElementT> |
| 112 | +class TriviallyComparable { |
| 113 | + ElementT el_; |
| 114 | + |
| 115 | +public: |
| 116 | + TEST_CONSTEXPR TriviallyComparable(ElementT el) : el_(el) {} |
| 117 | + bool operator==(const TriviallyComparable&) const = default; |
| 118 | +}; |
| 119 | + |
| 120 | +template <class IndexT> |
| 121 | +class Comparable { |
| 122 | + IndexT index_; |
| 123 | + |
| 124 | +public: |
| 125 | + Comparable(IndexT i) |
| 126 | + : index_([&]() { |
| 127 | + IndexT size = static_cast<IndexT>(comparable_data.size()); |
| 128 | + comparable_data.push_back(i); |
| 129 | + return size; |
| 130 | + }()) {} |
| 131 | + |
| 132 | + bool operator==(const Comparable& other) const { |
| 133 | + return comparable_data[other.index_] == comparable_data[index_]; |
| 134 | + } |
| 135 | + |
| 136 | + friend bool operator==(const Comparable& lhs, long long rhs) { return comparable_data[lhs.index_] == rhs; } |
| 137 | +}; |
| 138 | + |
| 139 | +constexpr bool test() { |
| 140 | + types::for_each(types::type_list<char, wchar_t, int, long, |
| 141 | + TriviallyComparable<char>, TriviallyComparable<wchar_t>>{}, |
| 142 | + []<class T> { |
| 143 | + types::for_each(types::cpp20_input_iterator_list<T*>{}, |
| 144 | + []<class Iter> { |
| 145 | + if constexpr (std::forward_iterator<Iter>) |
| 146 | + test_iterators<Iter>(); |
| 147 | + test_iterators<Iter, sentinel_wrapper<Iter>>(); |
| 148 | + test_iterators<Iter, sized_sentinel<Iter>>(); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + { |
| 153 | + // count invocations of the projection |
| 154 | + { |
| 155 | + int a[] = {1, 9, 0, 13, 25}; |
| 156 | + int projection_count = 0; |
| 157 | + auto ret = std::ranges::contains(a, a + 5, 0, |
| 158 | + [&](int i) { ++projection_count; return i; }); |
| 159 | + assert(ret); |
| 160 | + assert(projection_count == 3); |
| 161 | + } |
| 162 | + { |
| 163 | + int a[] ={1, 9, 0, 13, 25}; |
| 164 | + int projection_count = 0; |
| 165 | + auto range = std::ranges::subrange(a, a + 5); |
| 166 | + auto ret = std::ranges::contains(range, 0, [&](int i) { ++projection_count; return i; }); |
| 167 | + assert(ret); |
| 168 | + assert(projection_count == 3); |
| 169 | + } |
| 170 | + } |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +int main(int, char**) { |
| 175 | + test(); |
| 176 | + static_assert(test()); |
| 177 | + |
| 178 | + types::for_each(types::type_list<Comparable<char>, Comparable<wchar_t>>{}, |
| 179 | + []<class T> { |
| 180 | + types::for_each(types::cpp20_input_iterator_list<T*>{}, |
| 181 | + []<class Iter> { |
| 182 | + if constexpr (std::forward_iterator<Iter>) |
| 183 | + test_iterators<Iter>(); |
| 184 | + test_iterators<Iter, sentinel_wrapper<Iter>>(); |
| 185 | + test_iterators<Iter, sized_sentinel<Iter>>(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + return 0; |
| 190 | +} |
0 commit comments