| 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 | // constexpr auto end(); |
| 14 | |
| 15 | #include <ranges> |
| 16 | |
| 17 | #include <cassert> |
| 18 | #include <concepts> |
| 19 | #include <functional> |
| 20 | |
| 21 | #include "test_iterators.h" |
| 22 | |
| 23 | struct NonCommonRange : std::ranges::view_base { |
| 24 | using Iterator = forward_iterator<int*>; |
| 25 | using Sentinel = sentinel_wrapper<Iterator>; |
| 26 | constexpr explicit NonCommonRange(int* b, int* e) : begin_(b), end_(e) {} |
| 27 | constexpr Iterator begin() const { return Iterator(begin_); } |
| 28 | constexpr Sentinel end() const { return Sentinel(Iterator(end_)); } |
| 29 | |
| 30 | private: |
| 31 | int* begin_; |
| 32 | int* end_; |
| 33 | }; |
| 34 | |
| 35 | static_assert(std::ranges::forward_range<NonCommonRange>); |
| 36 | static_assert(!std::ranges::common_range<NonCommonRange>); |
| 37 | |
| 38 | struct CommonRange : std::ranges::view_base { |
| 39 | using Iterator = bidirectional_iterator<int*>; |
| 40 | constexpr explicit CommonRange(int* b, int* e) : begin_(b), end_(e) {} |
| 41 | constexpr Iterator begin() const { return Iterator(begin_); } |
| 42 | constexpr Iterator end() const { return Iterator(end_); } |
| 43 | |
| 44 | private: |
| 45 | int* begin_; |
| 46 | int* end_; |
| 47 | }; |
| 48 | |
| 49 | static_assert(std::ranges::bidirectional_range<CommonRange>); |
| 50 | static_assert(std::ranges::common_range<CommonRange>); |
| 51 | |
| 52 | constexpr bool test() { |
| 53 | int buff[] = {1, 0, 3, 1, 2, 3, 4, 5}; |
| 54 | |
| 55 | // Check the return type of `end()` |
| 56 | { |
| 57 | CommonRange range(buff, buff + 1); |
| 58 | auto pred = [](int, int) { return true; }; |
| 59 | std::ranges::chunk_by_view view(range, pred); |
| 60 | using ChunkByView = decltype(view); |
| 61 | static_assert(std::ranges::common_range<ChunkByView>); |
| 62 | ASSERT_SAME_TYPE(std::ranges::sentinel_t<ChunkByView>, decltype(view.end())); |
| 63 | } |
| 64 | |
| 65 | // end() on an empty range |
| 66 | { |
| 67 | CommonRange range(buff, buff); |
| 68 | auto pred = [](int x, int y) { return x <= y; }; |
| 69 | std::ranges::chunk_by_view view(range, pred); |
| 70 | auto end = view.end(); |
| 71 | assert(end == std::default_sentinel); |
| 72 | } |
| 73 | |
| 74 | // end() on a 1-element range |
| 75 | { |
| 76 | CommonRange range(buff, buff + 1); |
| 77 | auto pred = [](int& x, int& y) { return x <= y; }; |
| 78 | std::ranges::chunk_by_view view(range, pred); |
| 79 | auto end = view.end(); |
| 80 | assert(base((*--end).begin()) == buff); |
| 81 | assert(base((*end).end()) == buff + 1); |
| 82 | } |
| 83 | |
| 84 | // end() on a 2-element range |
| 85 | { |
| 86 | CommonRange range(buff, buff + 2); |
| 87 | auto pred = [](int const& x, int const& y) { return x <= y; }; |
| 88 | std::ranges::chunk_by_view view(range, pred); |
| 89 | auto end = view.end(); |
| 90 | assert(base((*--end).begin()) == buff + 1); |
| 91 | assert(base((*--end).end()) == buff + 1); |
| 92 | } |
| 93 | |
| 94 | // end() on a 8-element range |
| 95 | { |
| 96 | CommonRange range(buff, buff + 8); |
| 97 | auto pred = [](const int x, const int y) { return x < y; }; |
| 98 | std::ranges::chunk_by_view view(range, pred); |
| 99 | auto end = view.end(); |
| 100 | assert(base((*--end).end()) == buff + 8); |
| 101 | assert(base((*--end).end()) == buff + 3); |
| 102 | } |
| 103 | |
| 104 | // end() on a non-common range |
| 105 | { |
| 106 | NonCommonRange range(buff, buff + 1); |
| 107 | std::ranges::chunk_by_view view(range, std::ranges::less_equal{}); |
| 108 | auto end = view.end(); |
| 109 | ASSERT_SAME_TYPE(std::default_sentinel_t, std::ranges::sentinel_t<decltype(view)>); |
| 110 | ASSERT_SAME_TYPE(std::default_sentinel_t, decltype(end)); |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | int main(int, char**) { |
| 117 | test(); |
| 118 | static_assert(test()); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |