| 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 | // reverse_iterator |
| 12 | |
| 13 | // template <RandomAccessIterator Iter1, RandomAccessIterator Iter2> |
| 14 | // requires HasMinus<Iter2, Iter1> |
| 15 | // auto operator-(const reverse_iterator<Iter1>& x, const reverse_iterator<Iter2>& y) // constexpr in C++17 |
| 16 | // -> decltype(y.base() - x.base()); |
| 17 | |
| 18 | #include <iterator> |
| 19 | #include <cstddef> |
| 20 | #include <cassert> |
| 21 | #include <type_traits> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "test_iterators.h" |
| 25 | |
| 26 | template <class, class, class = void> |
| 27 | struct HasMinus : std::false_type {}; |
| 28 | template <class R1, class R2> |
| 29 | struct HasMinus<R1, R2, decltype((R1() - R2(), void()))> : std::true_type {}; |
| 30 | |
| 31 | // Test non-subtractable base iterator types |
| 32 | static_assert(HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<int*> >::value, "" ); |
| 33 | static_assert(HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<const int*> >::value, "" ); |
| 34 | |
| 35 | #if TEST_STD_VER >= 11 |
| 36 | static_assert(!HasMinus<std::reverse_iterator<int*>, std::reverse_iterator<char*> >::value, "" ); |
| 37 | static_assert(!HasMinus<std::reverse_iterator<bidirectional_iterator<int*> >, |
| 38 | std::reverse_iterator<bidirectional_iterator<int*> > >::value, |
| 39 | "" ); |
| 40 | #endif |
| 41 | |
| 42 | template <class It1, class It2> |
| 43 | TEST_CONSTEXPR_CXX17 void test_one(It1 l, It2 r, std::ptrdiff_t x) { |
| 44 | const std::reverse_iterator<It1> r1(l); |
| 45 | const std::reverse_iterator<It2> r2(r); |
| 46 | assert((r1 - r2) == x); |
| 47 | } |
| 48 | |
| 49 | template <class Iter> |
| 50 | TEST_CONSTEXPR_CXX17 void test() { |
| 51 | // Test same base iterator type |
| 52 | char s[3] = {0}; |
| 53 | |
| 54 | test_one(Iter(s), Iter(s), 0); |
| 55 | test_one(Iter(s), Iter(s + 1), 1); |
| 56 | test_one(Iter(s + 1), Iter(s), -1); |
| 57 | } |
| 58 | |
| 59 | TEST_CONSTEXPR_CXX17 bool tests() { |
| 60 | { |
| 61 | test<char*>(); |
| 62 | test<random_access_iterator<char*> >(); |
| 63 | #if TEST_STD_VER >= 20 |
| 64 | test<cpp20_random_access_iterator<char*>>(); |
| 65 | #endif |
| 66 | } |
| 67 | { |
| 68 | // Test different (but subtractable) base iterator types |
| 69 | using PC = const char*; |
| 70 | char s[3] = {0}; |
| 71 | test_one(PC(s), s, 0); |
| 72 | test_one(PC(s), s + 1, 1); |
| 73 | test_one(PC(s + 1), s, -1); |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | int main(int, char**) { |
| 80 | tests(); |
| 81 | #if TEST_STD_VER > 14 |
| 82 | static_assert(tests(), "" ); |
| 83 | #endif |
| 84 | return 0; |
| 85 | } |
| 86 | |