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 12 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 13 14 // template<input_iterator I, sentinel_for<I> S, class Proj = identity, 15 // indirect_unary_predicate<projected<I, Proj>> Pred> 16 // constexpr bool ranges::none_of(I first, S last, Pred pred, Proj proj = {}); 17 // template<input_range R, class Proj = identity, 18 // indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred> 19 // constexpr bool ranges::none_of(R&& r, Pred pred, Proj proj = {}); 20 21 #include <algorithm> 22 #include <array> 23 #include <cassert> 24 #include <ranges> 25 26 #include "almost_satisfies_types.h" 27 #include "test_iterators.h" 28 29 struct UnaryFunctor { 30 bool operator()(auto&&); 31 }; 32 33 template <class It, class Sent = sentinel_wrapper<It>> 34 concept HasNoneOfIt = requires(It first, Sent last) { std::ranges::none_of(first, last, UnaryFunctor{}); }; 35 36 static_assert(HasNoneOfIt<int*>); 37 static_assert(!HasNoneOfIt<InputIteratorNotDerivedFrom>); 38 static_assert(!HasNoneOfIt<InputIteratorNotIndirectlyReadable>); 39 static_assert(!HasNoneOfIt<InputIteratorNotInputOrOutputIterator>); 40 static_assert(!HasNoneOfIt<int*, SentinelForNotSemiregular>); 41 static_assert(!HasNoneOfIt<int*, SentinelForNotWeaklyEqualityComparableWith>); 42 43 template <class Func> 44 concept HasNoneOfItFunc = requires(int* ptr) { std::ranges::none_of(ptr, ptr, Func{}); }; 45 46 static_assert(HasNoneOfItFunc<UnaryFunctor>); 47 static_assert(!HasNoneOfItFunc<IndirectUnaryPredicateNotCopyConstructible>); 48 static_assert(!HasNoneOfItFunc<IndirectUnaryPredicateNotPredicate>); 49 50 template <class Range> 51 concept HasNoneOfR = requires(Range range) { std::ranges::none_of(range, UnaryFunctor{}); }; 52 53 static_assert(HasNoneOfR<std::array<int, 10>>); 54 static_assert(!HasNoneOfR<InputRangeNotDerivedFrom>); 55 static_assert(!HasNoneOfR<InputRangeNotIndirectlyReadable>); 56 static_assert(!HasNoneOfR<InputRangeNotInputOrOutputIterator>); 57 static_assert(!HasNoneOfR<InputRangeNotSentinelSemiregular>); 58 static_assert(!HasNoneOfR<InputRangeNotSentinelEqualityComparableWith>); 59 60 template <class Func> 61 concept HasNoneOfRFunc = requires(std::array<int, 10> range) { std::ranges::none_of(range, Func{}); }; 62 63 static_assert(HasNoneOfRFunc<UnaryFunctor>); 64 static_assert(!HasNoneOfRFunc<IndirectUnaryPredicateNotCopyConstructible>); 65 static_assert(!HasNoneOfRFunc<IndirectUnaryPredicateNotPredicate>); 66 67 template <class It, class Sent = It> 68 constexpr void test_iterators() { 69 { // simple test 70 { 71 int a[] = {1, 2, 3, 4}; 72 std::same_as<bool> decltype(auto) ret = std::ranges::none_of(It(a), Sent(It(a + 4)), [](int) { return true; }); 73 assert(!ret); 74 } 75 { 76 int a[] = {1, 2, 3, 4}; 77 auto range = std::ranges::subrange(It(a), Sent(It(a + 4))); 78 std::same_as<bool> decltype(auto) ret = std::ranges::none_of(range, [](int) { return true; }); 79 assert(!ret); 80 } 81 } 82 83 { // check that an empty range works 84 std::array<int, 0> a; 85 assert(std::ranges::none_of(It(a.data()), Sent(It(a.data())), [](int) { return false; })); 86 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data()))); 87 assert(std::ranges::none_of(range, [](int) { return false; })); 88 } 89 90 { // check that the complexity requirements are met 91 { 92 int predicateCount = 0; 93 int projectionCount = 0; 94 auto pred = [&](int) { ++predicateCount; return false; }; 95 auto proj = [&](int i) { ++projectionCount; return i; }; 96 std::array a = {9, 7, 5, 3}; 97 assert(std::ranges::none_of(It(a.begin()), Sent(It(a.end())), pred, proj)); 98 assert(predicateCount == 4); 99 assert(projectionCount == 4); 100 } 101 { 102 int predicateCount = 0; 103 int projectionCount = 0; 104 auto pred = [&](int) { ++predicateCount; return false; }; 105 auto proj = [&](int i) { ++projectionCount; return i; }; 106 std::array a = {9, 7, 5, 3}; 107 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); 108 assert(std::ranges::none_of(range, pred, proj)); 109 assert(predicateCount == 4); 110 assert(projectionCount == 4); 111 } 112 } 113 114 { // check that true is returned if no element satisfies the condition 115 std::array a = {1, 2, 3, 4}; 116 assert(std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); 117 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); 118 assert(std::ranges::none_of(range, [](int i) { return i > 5; })); 119 } 120 121 { // check that false is returned if all elements satisfy the condition 122 std::array a = {1, 2, 3, 4}; 123 assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i < 5; })); 124 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); 125 assert(!std::ranges::none_of(range, [](int i) { return i < 5; })); 126 } 127 128 { // check that false is returned if ony one elements satisfies the condition 129 std::array a = {1, 2, 3, 4, 6}; 130 assert(!std::ranges::none_of(It(a.data()), Sent(It(a.data() + a.size())), [](int i) { return i > 5; })); 131 auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data() + a.size()))); 132 assert(!std::ranges::none_of(range, [](int i) { return i > 5; })); 133 } 134 } 135 136 constexpr bool test() { 137 test_iterators<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>(); 138 test_iterators<forward_iterator<int*>>(); 139 test_iterators<bidirectional_iterator<int*>>(); 140 test_iterators<random_access_iterator<int*>>(); 141 test_iterators<contiguous_iterator<int*>>(); 142 test_iterators<int*>(); 143 144 { // check that std::invoke is used 145 struct S { int check; int other; }; 146 S a[] = {{1, 2}, {1, 7}, {1, 3}}; 147 assert(std::ranges::none_of(a, a + 3, [](int i) { return i != 1; }, &S::check)); 148 assert(std::ranges::none_of(a, [](int i) { return i != 1; }, &S::check)); 149 } 150 151 return true; 152 } 153 154 int main(int, char**) { 155 test(); 156 static_assert(test()); 157 158 return 0; 159 } 160