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