| 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 | // ranges::advance(it, sent) |
| 12 | |
| 13 | #include <iterator> |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <cassert> |
| 17 | #include <cstddef> |
| 18 | |
| 19 | #include "../types.h" |
| 20 | #include "test_iterators.h" |
| 21 | #include "test_macros.h" |
| 22 | |
| 23 | template <bool Count, class It> |
| 24 | constexpr void check_assignable(int* first, int* last, int* expected) { |
| 25 | { |
| 26 | It it(first); |
| 27 | auto sent = assignable_sentinel(It(last)); |
| 28 | std::ranges::advance(it, sent); |
| 29 | ASSERT_SAME_TYPE(decltype(std::ranges::advance(it, sent)), void); |
| 30 | assert(base(it) == expected); |
| 31 | } |
| 32 | |
| 33 | // Count operations |
| 34 | if constexpr (Count) { |
| 35 | IteratorOpCounts ops; |
| 36 | auto it = operation_counting_iterator(It(first), &ops); |
| 37 | auto sent = assignable_sentinel(operation_counting_iterator(It(last), &ops)); |
| 38 | std::ranges::advance(it, sent); |
| 39 | assert(base(base(it)) == expected); |
| 40 | assert(ops.increments + ops.decrements == 0); // because we got here by assigning from last, not by incrementing |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | template <bool Count, class It> |
| 45 | constexpr void check_sized_sentinel(int* first, int* last, int* expected) { |
| 46 | auto size = (last - first); |
| 47 | |
| 48 | { |
| 49 | It it(first); |
| 50 | auto sent = distance_apriori_sentinel(size); |
| 51 | std::ranges::advance(it, sent); |
| 52 | ASSERT_SAME_TYPE(decltype(std::ranges::advance(it, sent)), void); |
| 53 | assert(base(it) == expected); |
| 54 | } |
| 55 | |
| 56 | // Count operations |
| 57 | if constexpr (Count) { |
| 58 | IteratorOpCounts ops; |
| 59 | auto it = operation_counting_iterator(It(first), &ops); |
| 60 | auto sent = distance_apriori_sentinel(size); |
| 61 | std::ranges::advance(it, sent); |
| 62 | if constexpr (std::random_access_iterator<It>) { |
| 63 | assert(ops.increments + ops.decrements + ops.zero_moves == 1); |
| 64 | } else { |
| 65 | const auto big = std::max(ops.increments, ops.decrements); |
| 66 | const auto small = std::min(ops.increments, ops.decrements); |
| 67 | assert(big == static_cast<size_t>(size > 0 ? size : -size)); |
| 68 | assert(small == 0); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | template <bool Count, class It> |
| 74 | constexpr void check_sentinel(int* first, int* last, int* expected) { |
| 75 | auto size = (last - first); |
| 76 | |
| 77 | { |
| 78 | It it(first); |
| 79 | auto sent = sentinel_wrapper(It(last)); |
| 80 | std::ranges::advance(it, sent); |
| 81 | ASSERT_SAME_TYPE(decltype(std::ranges::advance(it, sent)), void); |
| 82 | assert(base(it) == expected); |
| 83 | } |
| 84 | |
| 85 | // Count operations |
| 86 | if constexpr (Count) { |
| 87 | IteratorOpCounts ops; |
| 88 | auto it = operation_counting_iterator(It(first), &ops); |
| 89 | auto sent = sentinel_wrapper(operation_counting_iterator(It(last), &ops)); |
| 90 | std::ranges::advance(it, sent); |
| 91 | const auto big = std::max(ops.increments, ops.decrements); |
| 92 | const auto small = std::min(ops.increments, ops.decrements); |
| 93 | assert(big == static_cast<size_t>(size > 0 ? size : -size)); |
| 94 | assert(small == 0); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | constexpr bool test() { |
| 99 | int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 100 | |
| 101 | for (int n = 0; n != 10; ++n) { |
| 102 | check_assignable<false, cpp17_input_iterator<int*>>( range, range+n, range+n); |
| 103 | check_assignable<false, cpp20_input_iterator<int*>>( range, range+n, range+n); |
| 104 | check_assignable<true, forward_iterator<int*>>( range, range+n, range+n); |
| 105 | check_assignable<true, bidirectional_iterator<int*>>(range, range+n, range+n); |
| 106 | check_assignable<true, random_access_iterator<int*>>(range, range+n, range+n); |
| 107 | check_assignable<true, contiguous_iterator<int*>>( range, range+n, range+n); |
| 108 | check_assignable<true, int*>( first: range, last: range+n, expected: range+n); |
| 109 | |
| 110 | check_sized_sentinel<false, cpp17_input_iterator<int*>>( range, range+n, range+n); |
| 111 | check_sized_sentinel<false, cpp20_input_iterator<int*>>( range, range+n, range+n); |
| 112 | check_sized_sentinel<true, forward_iterator<int*>>( range, range+n, range+n); |
| 113 | check_sized_sentinel<true, bidirectional_iterator<int*>>(range, range+n, range+n); |
| 114 | check_sized_sentinel<true, random_access_iterator<int*>>(range, range+n, range+n); |
| 115 | check_sized_sentinel<true, contiguous_iterator<int*>>( range, range+n, range+n); |
| 116 | check_sized_sentinel<true, int*>( first: range, last: range+n, expected: range+n); |
| 117 | |
| 118 | check_sentinel<false, cpp17_input_iterator<int*>>( range, range+n, range+n); |
| 119 | check_sentinel<false, cpp20_input_iterator<int*>>( range, range+n, range+n); |
| 120 | check_sentinel<true, forward_iterator<int*>>( range, range+n, range+n); |
| 121 | check_sentinel<true, bidirectional_iterator<int*>>(range, range+n, range+n); |
| 122 | check_sentinel<true, random_access_iterator<int*>>(range, range+n, range+n); |
| 123 | check_sentinel<true, contiguous_iterator<int*>>( range, range+n, range+n); |
| 124 | check_sentinel<true, int*>( first: range, last: range+n, expected: range+n); |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | int main(int, char**) { |
| 131 | assert(test()); |
| 132 | static_assert(test()); |
| 133 | return 0; |
| 134 | } |
| 135 | |