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