| 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 | // template<class I2, sentinel_for<I> S2> |
| 12 | // requires sentinel_for<S, I2> |
| 13 | // friend bool operator==( |
| 14 | // const common_iterator& x, const common_iterator<I2, S2>& y); |
| 15 | // template<class I2, sentinel_for<I> S2> |
| 16 | // requires sentinel_for<S, I2> && equality_comparable_with<I, I2> |
| 17 | // friend bool operator==( |
| 18 | // const common_iterator& x, const common_iterator<I2, S2>& y); |
| 19 | |
| 20 | #include <iterator> |
| 21 | #include <cassert> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "types.h" |
| 25 | |
| 26 | void test() { |
| 27 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 28 | |
| 29 | { |
| 30 | auto iter1 = simple_iterator<int*>(buffer); |
| 31 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 32 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 33 | |
| 34 | const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 35 | const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 36 | |
| 37 | assert(commonIter1 != commonSent1); |
| 38 | assert(commonIter2 != commonSent2); |
| 39 | assert(commonSent1 != commonIter1); |
| 40 | assert(commonSent2 != commonIter2); |
| 41 | |
| 42 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 43 | assert(*(commonIter1++) == i); |
| 44 | } |
| 45 | assert(commonIter1 == commonSent1); |
| 46 | assert(commonSent1 == commonIter1); |
| 47 | } |
| 48 | { |
| 49 | auto iter1 = value_iterator<int*>(buffer); |
| 50 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 51 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 52 | |
| 53 | const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 54 | const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 55 | |
| 56 | assert(commonIter1 != commonSent1); |
| 57 | assert(commonIter2 != commonSent2); |
| 58 | assert(commonSent1 != commonIter1); |
| 59 | assert(commonSent2 != commonIter2); |
| 60 | |
| 61 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 62 | assert(*(commonIter1++) == i); |
| 63 | } |
| 64 | assert(commonIter1 == commonSent1); |
| 65 | assert(commonSent1 == commonIter1); |
| 66 | } |
| 67 | { |
| 68 | auto iter1 = simple_iterator<int*>(buffer); |
| 69 | auto iter2 = comparable_iterator<int*>(buffer); |
| 70 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 71 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 72 | |
| 73 | const auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2); |
| 74 | const auto commonSent2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 75 | |
| 76 | assert(commonIter1 == commonIter2); |
| 77 | assert(commonSent1 != commonIter2); |
| 78 | assert(commonSent1 == commonSent2); |
| 79 | assert(commonSent2 == commonSent1); |
| 80 | |
| 81 | assert(commonIter1 != commonSent1); |
| 82 | assert(commonIter2 != commonSent2); |
| 83 | assert(commonSent1 != commonIter1); |
| 84 | assert(commonSent2 != commonIter2); |
| 85 | |
| 86 | assert(commonIter1 == commonIter2); |
| 87 | assert(commonIter2 == commonIter1); |
| 88 | |
| 89 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 90 | assert(*(commonIter1++) == i); |
| 91 | } |
| 92 | assert(commonIter1 == commonSent1); |
| 93 | assert(commonSent1 == commonIter1); |
| 94 | |
| 95 | // This check may *seem* incorrect (our iterators point to two completely different |
| 96 | // elements of buffer). However, this is actually what the Standard wants. |
| 97 | // See https://eel.is/c++draft/iterators.common#common.iter.cmp-2. |
| 98 | assert(commonIter1 == commonIter2); |
| 99 | } |
| 100 | { |
| 101 | auto iter1 = cpp17_input_iterator<int*>(buffer); |
| 102 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 103 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 104 | |
| 105 | const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 106 | const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 107 | |
| 108 | assert(commonIter1 != commonSent1); |
| 109 | assert(commonIter2 != commonSent2); |
| 110 | assert(commonSent1 != commonIter1); |
| 111 | assert(commonSent2 != commonIter2); |
| 112 | |
| 113 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 114 | assert(*(commonIter1++) == i); |
| 115 | } |
| 116 | assert(commonIter1 == commonSent1); |
| 117 | assert(commonSent1 == commonIter1); |
| 118 | } |
| 119 | { |
| 120 | auto iter1 = forward_iterator<int*>(buffer); |
| 121 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 122 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 123 | |
| 124 | const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 125 | const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 126 | |
| 127 | assert(commonIter1 != commonSent1); |
| 128 | assert(commonIter2 != commonSent2); |
| 129 | assert(commonSent1 != commonIter1); |
| 130 | assert(commonSent2 != commonIter2); |
| 131 | |
| 132 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 133 | assert(*(commonIter1++) == i); |
| 134 | } |
| 135 | assert(commonIter1 == commonSent1); |
| 136 | assert(commonSent1 == commonIter1); |
| 137 | } |
| 138 | { |
| 139 | auto iter1 = random_access_iterator<int*>(buffer); |
| 140 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 141 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 142 | |
| 143 | const auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 144 | const auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 145 | |
| 146 | assert(commonIter1 != commonSent1); |
| 147 | assert(commonIter2 != commonSent2); |
| 148 | assert(commonSent1 != commonIter1); |
| 149 | assert(commonSent2 != commonIter2); |
| 150 | |
| 151 | assert(commonSent1 == commonSent2); |
| 152 | assert(commonSent2 == commonSent1); |
| 153 | |
| 154 | for (auto i = 1; commonIter1 != commonSent1; ++i) { |
| 155 | assert(*(commonIter1++) == i); |
| 156 | } |
| 157 | assert(commonIter1 == commonSent1); |
| 158 | assert(commonSent1 == commonIter1); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int main(int, char**) { |
| 163 | test(); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |