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 | // <iterator> |
10 | |
11 | // move_iterator |
12 | |
13 | // template <class Iter1, class Iter2> |
14 | // bool operator==(const move_iterator<Iter1>& x, const move_iterator<Iter2>& y); |
15 | // |
16 | // constexpr in C++17 |
17 | |
18 | #include <iterator> |
19 | #include <cassert> |
20 | |
21 | #include "test_macros.h" |
22 | #include "test_iterators.h" |
23 | |
24 | // move_iterator's operator== calls the underlying iterator's operator== |
25 | struct CustomIt { |
26 | using value_type = int; |
27 | using difference_type = int; |
28 | using reference = int&; |
29 | using pointer = int*; |
30 | using iterator_category = std::input_iterator_tag; |
31 | CustomIt() = default; |
32 | TEST_CONSTEXPR_CXX17 explicit CustomIt(int* p) : p_(p) {} |
33 | int& operator*() const; |
34 | CustomIt& operator++(); |
35 | CustomIt operator++(int); |
36 | TEST_CONSTEXPR_CXX17 friend bool operator==(const CustomIt& a, const CustomIt& b) { return a.p_ == b.p_; } |
37 | int *p_ = nullptr; |
38 | }; |
39 | |
40 | template <class It> |
41 | TEST_CONSTEXPR_CXX17 void test_one() |
42 | { |
43 | int a[] = {3, 1, 4}; |
44 | const std::move_iterator<It> r1 = std::move_iterator<It>(It(a)); |
45 | const std::move_iterator<It> r2 = std::move_iterator<It>(It(a)); |
46 | const std::move_iterator<It> r3 = std::move_iterator<It>(It(a + 2)); |
47 | ASSERT_SAME_TYPE(decltype(r1 == r2), bool); |
48 | assert( (r1 == r1)); |
49 | assert( (r1 == r2)); |
50 | assert( (r2 == r1)); |
51 | assert(!(r1 == r3)); |
52 | assert(!(r3 == r1)); |
53 | } |
54 | |
55 | TEST_CONSTEXPR_CXX17 bool test() |
56 | { |
57 | test_one<CustomIt>(); |
58 | test_one<cpp17_input_iterator<int*> >(); |
59 | test_one<forward_iterator<int*> >(); |
60 | test_one<bidirectional_iterator<int*> >(); |
61 | test_one<random_access_iterator<int*> >(); |
62 | test_one<int*>(); |
63 | test_one<const int*>(); |
64 | |
65 | #if TEST_STD_VER > 17 |
66 | test_one<contiguous_iterator<int*>>(); |
67 | test_one<three_way_contiguous_iterator<int*>>(); |
68 | #endif |
69 | |
70 | return true; |
71 | } |
72 | |
73 | int main(int, char**) |
74 | { |
75 | test(); |
76 | #if TEST_STD_VER > 14 |
77 | static_assert(test()); |
78 | #endif |
79 | |
80 | return 0; |
81 | } |
82 | |