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 |
10 | |
11 | // constexpr auto end() requires (!simple-view<V>) |
12 | // constexpr auto end() const requires range<const V> |
13 | |
14 | #include <ranges> |
15 | #include <cassert> |
16 | |
17 | #include "test_macros.h" |
18 | #include "test_iterators.h" |
19 | #include "test_range.h" |
20 | #include "types.h" |
21 | |
22 | constexpr bool test() { |
23 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
24 | |
25 | // sized_range && random_access_iterator |
26 | { |
27 | std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView{buffer}, 0); |
28 | assert(tv.end() == std::ranges::next(tv.begin(), 0)); |
29 | ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); |
30 | } |
31 | |
32 | { |
33 | const std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView{buffer}, 1); |
34 | assert(tv.end() == std::ranges::next(tv.begin(), 1)); |
35 | ASSERT_SAME_TYPE(decltype(tv.end()), RandomAccessIter); |
36 | } |
37 | |
38 | // sized_range && !random_access_iterator |
39 | { |
40 | std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 2); |
41 | assert(tv.end() == std::ranges::next(tv.begin(), 2)); |
42 | ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); |
43 | } |
44 | |
45 | { |
46 | const std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 3); |
47 | assert(tv.end() == std::ranges::next(tv.begin(), 3)); |
48 | ASSERT_SAME_TYPE(decltype(tv.end()), std::default_sentinel_t); |
49 | } |
50 | |
51 | // !sized_range |
52 | { |
53 | std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 4); |
54 | assert(tv.end() == std::ranges::next(tv.begin(), 4)); |
55 | |
56 | // The <sentinel> type. |
57 | static_assert(!std::same_as<decltype(tv.end()), std::default_sentinel_t>); |
58 | static_assert(!std::same_as<decltype(tv.end()), int*>); |
59 | } |
60 | |
61 | { |
62 | const std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 5); |
63 | assert(tv.end() == std::ranges::next(tv.begin(), 5)); |
64 | } |
65 | |
66 | // Just to cover the case where count == 8. |
67 | { |
68 | std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView{buffer}, 8); |
69 | assert(tv.end() == std::ranges::next(tv.begin(), 8)); |
70 | } |
71 | |
72 | { |
73 | // __iterator<false> has base with type std::ranges::sentinel_t<NonSimpleViewNonSized>; adding a const qualifier |
74 | // would change the equality. |
75 | std::ranges::take_view<NonSimpleNonSizedView> tvns(NonSimpleNonSizedView{buffer, buffer + 8}, 0); |
76 | static_assert(!std::is_same_v<decltype(tvns.end().base()), std::ranges::sentinel_t<const NonSimpleNonSizedView>>); |
77 | } |
78 | |
79 | { |
80 | // __iterator<true> has base with type std::ranges::sentinel_t<const NonSimpleViewNonSized>; adding a const qualifier |
81 | // would not change the equality. |
82 | std::ranges::take_view<SimpleViewNonSized> tvs(SimpleViewNonSized{buffer, buffer + 8}, 0); |
83 | static_assert(std::is_same_v<decltype(tvs.end().base()), std::ranges::sentinel_t<const SimpleViewNonSized>>); |
84 | } |
85 | |
86 | return true; |
87 | } |
88 | |
89 | int main(int, char**) { |
90 | test(); |
91 | static_assert(test()); |
92 | |
93 | return 0; |
94 | } |
95 | |