| 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, class S2> |
| 12 | // requires convertible_to<const I2&, I> && convertible_to<const S2&, S> && |
| 13 | // assignable_from<I&, const I2&> && assignable_from<S&, const S2&> |
| 14 | // common_iterator& operator=(const common_iterator<I2, S2>& x); |
| 15 | |
| 16 | #include <iterator> |
| 17 | #include <ranges> |
| 18 | #include <cassert> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "types.h" |
| 22 | |
| 23 | void test() { |
| 24 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 25 | |
| 26 | { |
| 27 | auto iter1 = cpp17_input_iterator<int*>(buffer); |
| 28 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 29 | auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(cpp17_input_iterator<int*>(buffer + 1)); |
| 30 | |
| 31 | assert(*commonIter1 == 1); |
| 32 | assert(*commonIter2 == 2); |
| 33 | assert(commonIter1 != commonIter2); |
| 34 | |
| 35 | commonIter1 = commonIter2; |
| 36 | |
| 37 | assert(*commonIter1 == 2); |
| 38 | assert(*commonIter2 == 2); |
| 39 | assert(commonIter1 == commonIter2); |
| 40 | } |
| 41 | { |
| 42 | auto iter1 = forward_iterator<int*>(buffer); |
| 43 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 44 | auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(forward_iterator<int*>(buffer + 1)); |
| 45 | |
| 46 | assert(*commonIter1 == 1); |
| 47 | assert(*commonIter2 == 2); |
| 48 | assert(commonIter1 != commonIter2); |
| 49 | |
| 50 | commonIter1 = commonIter2; |
| 51 | |
| 52 | assert(*commonIter1 == 2); |
| 53 | assert(*commonIter2 == 2); |
| 54 | assert(commonIter1 == commonIter2); |
| 55 | } |
| 56 | { |
| 57 | auto iter1 = random_access_iterator<int*>(buffer); |
| 58 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 59 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 60 | |
| 61 | auto commonIter2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1 + 1); |
| 62 | auto commonSent2 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 7}); |
| 63 | |
| 64 | assert(*commonIter1 == 1); |
| 65 | assert(*commonIter2 == 2); |
| 66 | assert(commonIter1 != commonIter2); |
| 67 | |
| 68 | commonIter1 = commonIter2; |
| 69 | |
| 70 | assert(*commonIter1 == 2); |
| 71 | assert(*commonIter2 == 2); |
| 72 | assert(commonIter1 == commonIter2); |
| 73 | |
| 74 | assert(std::next(commonIter1, 6) != commonSent1); |
| 75 | assert(std::next(commonIter1, 6) == commonSent2); |
| 76 | |
| 77 | commonSent1 = commonSent2; |
| 78 | |
| 79 | assert(std::next(commonIter1, 6) == commonSent1); |
| 80 | assert(std::next(commonIter1, 6) == commonSent2); |
| 81 | } |
| 82 | { |
| 83 | auto iter1 = assignable_iterator<int*>(buffer); |
| 84 | auto iter2 = forward_iterator<int*>(buffer + 1); |
| 85 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 86 | auto commonSent1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(sentinel_type<int*>{buffer + 8}); |
| 87 | |
| 88 | auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2); |
| 89 | auto commonSent2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(sentinel_type<int*>{buffer + 7}); |
| 90 | |
| 91 | assert(*commonIter1 == 1); |
| 92 | assert(*commonIter2 == 2); |
| 93 | |
| 94 | commonIter1 = commonIter2; |
| 95 | |
| 96 | assert(*commonIter1 == 2); |
| 97 | assert(*commonIter2 == 2); |
| 98 | assert(commonIter1 == commonIter2); |
| 99 | |
| 100 | assert(std::next(commonIter1, 6) != commonSent1); |
| 101 | assert(std::next(commonIter1, 6) == commonSent2); |
| 102 | |
| 103 | commonSent1 = commonSent2; |
| 104 | |
| 105 | assert(std::next(commonIter1, 6) == commonSent1); |
| 106 | assert(std::next(commonIter1, 6) == commonSent2); |
| 107 | |
| 108 | commonIter1 = commonSent1; |
| 109 | |
| 110 | assert(commonIter1 == commonSent2); |
| 111 | |
| 112 | commonIter1 = commonSent2; |
| 113 | |
| 114 | assert(commonIter1 == commonSent2); |
| 115 | } |
| 116 | #ifndef TEST_HAS_NO_EXCEPTIONS |
| 117 | { |
| 118 | auto iter1 = maybe_valueless_iterator<int*>(buffer); |
| 119 | auto iter2 = forward_iterator<int*>(buffer); |
| 120 | auto commonIter1 = std::common_iterator<decltype(iter1), sentinel_type<int*>>(iter1); |
| 121 | auto commonSent2 = std::common_iterator<decltype(iter1), |
| 122 | sentinel_throws_on_convert<int*>>(sentinel_throws_on_convert<int*>{buffer + 8}); |
| 123 | auto commonIter2 = std::common_iterator<decltype(iter2), sentinel_type<int*>>(iter2); |
| 124 | |
| 125 | try { |
| 126 | commonIter1 = commonSent2; |
| 127 | assert(false); |
| 128 | } catch (int x) { |
| 129 | assert(x == 42); |
| 130 | commonIter1 = commonIter2; |
| 131 | } |
| 132 | |
| 133 | assert(*commonIter1 == 1); |
| 134 | } |
| 135 | #endif // TEST_HAS_NO_EXCEPTIONS |
| 136 | } |
| 137 | |
| 138 | int main(int, char**) { |
| 139 | test(); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |