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