| 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 + 2)); |
| 46 | ASSERT_SAME_TYPE(decltype(r1 < r2), bool); |
| 47 | assert(!(r1 < r1)); |
| 48 | assert( (r1 < r2)); |
| 49 | assert(!(r2 < r1)); |
| 50 | } |
| 51 | |
| 52 | TEST_CONSTEXPR_CXX17 bool test() |
| 53 | { |
| 54 | test_one<CustomIt>(); |
| 55 | test_one<int*>(); |
| 56 | test_one<const int*>(); |
| 57 | test_one<random_access_iterator<int*> >(); |
| 58 | #if TEST_STD_VER > 17 |
| 59 | test_one<contiguous_iterator<int*>>(); |
| 60 | #endif |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | int main(int, char**) |
| 65 | { |
| 66 | assert(test()); |
| 67 | #if TEST_STD_VER > 14 |
| 68 | static_assert(test()); |
| 69 | #endif |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |