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