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
11 // constexpr iterator& operator+=(difference_type n)
12 // requires random_access_range<Base>;
13 //
14 // constexpr iterator& operator-=(difference_type n)
15 // requires random_access_range<Base>;
16 //
17 // friend constexpr iterator operator+(const iterator& x, difference_type y)
18 // requires random_access_range<Base>;
19 //
20 // friend constexpr iterator operator+(difference_type x, const iterator& y)
21 // requires random_access_range<Base>;
22 //
23 // friend constexpr iterator operator-(const iterator& x, difference_type y)
24 // requires random_access_range<Base>;
25 //
26 // friend constexpr difference_type operator-(const iterator& x, const iterator& y)
27 // requires sized_sentinel_for<iterator_t<Base>, iterator_t<Base>>;
28
29 #include <ranges>
30
31 #include <tuple>
32
33 #include "test_iterators.h"
34
35 template <class T, class U>
36 concept CanPlus = requires(T t, U u) { t + u; };
37
38 template <class T, class U>
39 concept CanPlusEqual = requires(T t, U u) { t += u; };
40
41 template <class T, class U>
42 concept CanMinus = requires(T t, U u) { t - u; };
43
44 template <class T, class U>
45 concept CanMinusEqual = requires(T t, U u) { t -= u; };
46
47 template <class BaseRange>
48 using ElemIter = std::ranges::iterator_t<std::ranges::elements_view<BaseRange, 0>>;
49
50 using RandomAccessRange = std::ranges::subrange<std::tuple<int>*>;
51 static_assert(std::ranges::random_access_range<RandomAccessRange>);
52 static_assert(std::sized_sentinel_for<std::ranges::iterator_t<RandomAccessRange>, //
53 std::ranges::iterator_t<RandomAccessRange>>);
54
55 static_assert(CanPlus<ElemIter<RandomAccessRange>, int>);
56 static_assert(CanPlus<int, ElemIter<RandomAccessRange>>);
57 static_assert(CanPlusEqual<ElemIter<RandomAccessRange>, int>);
58 static_assert(CanMinus<ElemIter<RandomAccessRange>, int>);
59 static_assert(CanMinus<ElemIter<RandomAccessRange>, ElemIter<RandomAccessRange>>);
60 static_assert(CanMinusEqual<ElemIter<RandomAccessRange>, int>);
61
62 using BidiRange = std::ranges::subrange<bidirectional_iterator<std::tuple<int>*>>;
63 static_assert(!std::ranges::random_access_range<BidiRange>);
64 static_assert(!std::sized_sentinel_for<std::ranges::iterator_t<BidiRange>, //
65 std::ranges::iterator_t<BidiRange>>);
66
67 static_assert(!CanPlus<ElemIter<BidiRange>, int>);
68 static_assert(!CanPlus<int, ElemIter<BidiRange>>);
69 static_assert(!CanPlusEqual<ElemIter<BidiRange>, int>);
70 static_assert(!CanMinus<ElemIter<BidiRange>, int>);
71 static_assert(!CanMinus<ElemIter<BidiRange>, ElemIter<BidiRange>>);
72 static_assert(!CanMinusEqual<ElemIter<BidiRange>, int>);
73
test()74 constexpr bool test() {
75 std::tuple<int> ts[] = {{1}, {2}, {3}, {4}};
76
77 RandomAccessRange r{&ts[0], &ts[0] + 4};
78 auto ev = r | std::views::elements<0>;
79 {
80 // operator+(x, n) operator+(n,x) and operator+=
81 auto it1 = ev.begin();
82
83 auto it2 = it1 + 3;
84 assert(it2.base() == &ts[3]);
85
86 auto it3 = 3 + it1;
87 assert(it3.base() == &ts[3]);
88
89 it1 += 3;
90 assert(it1 == it2);
91 assert(it1.base() == &ts[3]);
92 }
93
94 {
95 // operator-(x, n) and operator-=
96 auto it1 = ev.end();
97
98 auto it2 = it1 - 3;
99 assert(it2.base() == &ts[1]);
100
101 it1 -= 3;
102 assert(it1 == it2);
103 assert(it1.base() == &ts[1]);
104 }
105
106 {
107 // operator-(x, y)
108 assert((ev.end() - ev.begin()) == 4);
109
110 auto it1 = ev.begin() + 2;
111 auto it2 = ev.end() - 1;
112 assert((it1 - it2) == -1);
113 }
114
115 return true;
116 }
117
main(int,char **)118 int main(int, char**) {
119 test();
120 static_assert(test());
121
122 return 0;
123 }
124