| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <ranges> |
| 12 | |
| 13 | // Check constraints on the type itself. |
| 14 | // |
| 15 | // template <forward_range View, indirect_binary_predicate<iterator_t<View>, iterator_t<View>> Pred> |
| 16 | // requires view<View> && is_object_v<Pred> |
| 17 | // class chunk_by_view; |
| 18 | |
| 19 | #include <ranges> |
| 20 | |
| 21 | #include <concepts> |
| 22 | #include <cstddef> |
| 23 | #include <iterator> |
| 24 | #include <type_traits> |
| 25 | |
| 26 | #include "almost_satisfies_types.h" |
| 27 | #include "test_iterators.h" |
| 28 | |
| 29 | template <class View, class Pred> |
| 30 | concept CanFormChunkByView = requires { typename std::ranges::chunk_by_view<View, Pred>; }; |
| 31 | |
| 32 | // chunk_by_view is not valid when the view is not a forward_range |
| 33 | namespace test_when_view_is_not_a_forward_range { |
| 34 | |
| 35 | struct View : std::ranges::view_base { |
| 36 | ForwardIteratorNotDerivedFrom begin() const; |
| 37 | ForwardIteratorNotDerivedFrom end() const; |
| 38 | }; |
| 39 | struct Pred { |
| 40 | bool operator()(int, int) const; |
| 41 | }; |
| 42 | |
| 43 | static_assert(!std::ranges::forward_range<View>); |
| 44 | static_assert(std::indirect_binary_predicate<Pred, int*, int*>); |
| 45 | static_assert(std::ranges::view<View>); |
| 46 | static_assert(std::is_object_v<Pred>); |
| 47 | static_assert(!CanFormChunkByView<View, Pred>); |
| 48 | |
| 49 | } // namespace test_when_view_is_not_a_forward_range |
| 50 | |
| 51 | // chunk_by_view is not valid when the predicate is not indirect_binary_predicate |
| 52 | namespace test_when_the_predicate_is_not_indirect_binary_predicate { |
| 53 | |
| 54 | struct View : std::ranges::view_base { |
| 55 | int* begin() const; |
| 56 | int* end() const; |
| 57 | }; |
| 58 | struct Pred {}; |
| 59 | |
| 60 | static_assert(std::ranges::forward_range<View>); |
| 61 | static_assert(!std::indirect_binary_predicate<Pred, int*, int*>); |
| 62 | static_assert(std::ranges::view<View>); |
| 63 | static_assert(std::is_object_v<Pred>); |
| 64 | static_assert(!CanFormChunkByView<View, Pred>); |
| 65 | |
| 66 | } // namespace test_when_the_predicate_is_not_indirect_binary_predicate |
| 67 | |
| 68 | // chunk_by_view is not valid when the view is not a view |
| 69 | namespace test_when_the_view_param_is_not_a_view { |
| 70 | |
| 71 | struct View { |
| 72 | int* begin() const; |
| 73 | int* end() const; |
| 74 | }; |
| 75 | struct Pred { |
| 76 | bool operator()(int, int) const; |
| 77 | }; |
| 78 | |
| 79 | static_assert(std::ranges::input_range<View>); |
| 80 | static_assert(std::indirect_binary_predicate<Pred, int*, int*>); |
| 81 | static_assert(!std::ranges::view<View>); |
| 82 | static_assert(std::is_object_v<Pred>); |
| 83 | static_assert(!CanFormChunkByView<View, Pred>); |
| 84 | |
| 85 | } // namespace test_when_the_view_param_is_not_a_view |
| 86 | |
| 87 | // chunk_by_view is not valid when the predicate is not an object type |
| 88 | namespace test_when_the_predicate_is_not_an_object_type { |
| 89 | |
| 90 | struct View : std::ranges::view_base { |
| 91 | int* begin() const; |
| 92 | int* end() const; |
| 93 | }; |
| 94 | using Pred = bool (&)(int, int); |
| 95 | |
| 96 | static_assert(std::ranges::input_range<View>); |
| 97 | static_assert(std::indirect_binary_predicate<Pred, int*, int*>); |
| 98 | static_assert(std::ranges::view<View>); |
| 99 | static_assert(!std::is_object_v<Pred>); |
| 100 | static_assert(!CanFormChunkByView<View, Pred>); |
| 101 | |
| 102 | } // namespace test_when_the_predicate_is_not_an_object_type |
| 103 | |
| 104 | // chunk_by_view is valid when all the constraints are satisfied (test the test) |
| 105 | namespace test_when_all_the_constraints_are_satisfied { |
| 106 | |
| 107 | struct View : std::ranges::view_base { |
| 108 | int* begin() const; |
| 109 | int* end() const; |
| 110 | }; |
| 111 | struct Pred { |
| 112 | bool operator()(int, int) const; |
| 113 | }; |
| 114 | |
| 115 | static_assert(std::ranges::input_range<View>); |
| 116 | static_assert(std::indirect_binary_predicate<Pred, int*, int*>); |
| 117 | static_assert(std::ranges::view<View>); |
| 118 | static_assert(std::is_object_v<Pred>); |
| 119 | static_assert(CanFormChunkByView<View, Pred>); |
| 120 | |
| 121 | } // namespace test_when_all_the_constraints_are_satisfied |
| 122 | |