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<common_with<I> I2>
13 //   friend constexpr iter_difference_t<I2> operator-(
14 //     const counted_iterator& x, const counted_iterator<I2>& y);
15 
16 #include <iterator>
17 
18 #include "test_macros.h"
19 #include "test_iterators.h"
20 
21 // This iterator is common_with forward_iterator but NOT comparable with it.
22 template <class It>
23 class CommonWithForwardIter
24 {
25     It it_;
26 
27 public:
28     typedef          std::input_iterator_tag                   iterator_category;
29     typedef typename std::iterator_traits<It>::value_type      value_type;
30     typedef typename std::iterator_traits<It>::difference_type difference_type;
31     typedef It                                                 pointer;
32     typedef typename std::iterator_traits<It>::reference       reference;
33 
34     constexpr It base() const {return it_;}
35 
36     CommonWithForwardIter() = default;
37     explicit constexpr CommonWithForwardIter(It it) : it_(it) {}
38     constexpr CommonWithForwardIter(const forward_iterator<It>& it) : it_(it.base()) {}
39 
40     constexpr reference operator*() const {return *it_;}
41 
42     constexpr CommonWithForwardIter& operator++() {++it_; return *this;}
43     constexpr CommonWithForwardIter operator++(int)
44         {CommonWithForwardIter tmp(*this); ++(*this); return tmp;}
45 };
46 
47 constexpr bool test() {
48   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
49 
50   {
51     std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
52     std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
53     assert(iter1 - iter2 == -4);
54     assert(iter2 - iter1 == 4);
55     assert(iter1.count() == 8);
56     assert(iter2.count() == 4);
57 
58     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
59   }
60   {
61     const std::counted_iterator iter1(random_access_iterator<int*>{buffer}, 8);
62     const std::counted_iterator iter2(random_access_iterator<int*>{buffer + 4}, 4);
63     assert(iter1 - iter2 == -4);
64     assert(iter2 - iter1 == 4);
65     assert(iter1.count() == 8);
66     assert(iter2.count() == 4);
67 
68     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
69   }
70   {
71     std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
72     std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
73     assert(iter1 - iter2 == -2);
74     assert(iter2 - iter1 == 2);
75     assert(iter1.count() == 8);
76     assert(iter2.count() == 6);
77 
78     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
79   }
80   {
81     const std::counted_iterator iter1(contiguous_iterator<int*>{buffer}, 8);
82     const std::counted_iterator iter2(contiguous_iterator<int*>{buffer + 2}, 6);
83     assert(iter1 - iter2 == -2);
84     assert(iter2 - iter1 == 2);
85     assert(iter1.count() == 8);
86     assert(iter2.count() == 6);
87 
88     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
89   }
90   // The minus operator works even if Iter is not a random_access_iterator because
91   // counted_iterator is able to implement it by subtracting the counts rather than
92   // the underlying iterators.
93   {
94     std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
95     std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
96     assert(iter1 - iter2 == -2);
97     assert(iter2 - iter1 == 2);
98     assert(iter1.count() == 8);
99     assert(iter2.count() == 6);
100 
101     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
102   }
103   {
104     const std::counted_iterator iter1(CommonWithForwardIter<int*>{buffer}, 8);
105     const std::counted_iterator iter2(forward_iterator<int*>{buffer + 2}, 6);
106     assert(iter1 - iter2 == -2);
107     assert(iter2 - iter1 == 2);
108     assert(iter1.count() == 8);
109     assert(iter2.count() == 6);
110 
111     ASSERT_SAME_TYPE(decltype(iter1 - iter2), std::iter_difference_t<int*>);
112   }
113 
114   return true;
115 }
116 
117 int main(int, char**) {
118   test();
119   static_assert(test());
120 
121   return 0;
122 }
123