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
35template <class T, class U>
36concept CanPlus = requires(T t, U u) { t + u; };
37
38template <class T, class U>
39concept CanPlusEqual = requires(T t, U u) { t += u; };
40
41template <class T, class U>
42concept CanMinus = requires(T t, U u) { t - u; };
43
44template <class T, class U>
45concept CanMinusEqual = requires(T t, U u) { t -= u; };
46
47template <class BaseRange>
48using ElemIter = std::ranges::iterator_t<std::ranges::elements_view<BaseRange, 0>>;
49
50using RandomAccessRange = std::ranges::subrange<std::tuple<int>*>;
51static_assert(std::ranges::random_access_range<RandomAccessRange>);
52static_assert(std::sized_sentinel_for<std::ranges::iterator_t<RandomAccessRange>, //
53 std::ranges::iterator_t<RandomAccessRange>>);
54
55static_assert(CanPlus<ElemIter<RandomAccessRange>, int>);
56static_assert(CanPlus<int, ElemIter<RandomAccessRange>>);
57static_assert(CanPlusEqual<ElemIter<RandomAccessRange>, int>);
58static_assert(CanMinus<ElemIter<RandomAccessRange>, int>);
59static_assert(CanMinus<ElemIter<RandomAccessRange>, ElemIter<RandomAccessRange>>);
60static_assert(CanMinusEqual<ElemIter<RandomAccessRange>, int>);
61
62using BidiRange = std::ranges::subrange<bidirectional_iterator<std::tuple<int>*>>;
63static_assert(!std::ranges::random_access_range<BidiRange>);
64static_assert(!std::sized_sentinel_for<std::ranges::iterator_t<BidiRange>, //
65 std::ranges::iterator_t<BidiRange>>);
66
67static_assert(!CanPlus<ElemIter<BidiRange>, int>);
68static_assert(!CanPlus<int, ElemIter<BidiRange>>);
69static_assert(!CanPlusEqual<ElemIter<BidiRange>, int>);
70static_assert(!CanMinus<ElemIter<BidiRange>, int>);
71static_assert(!CanMinus<ElemIter<BidiRange>, ElemIter<BidiRange>>);
72static_assert(!CanMinusEqual<ElemIter<BidiRange>, int>);
73
74constexpr 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
118int main(int, char**) {
119 test();
120 static_assert(test());
121
122 return 0;
123}
124

source code of libcxx/test/std/ranges/range.adaptors/range.elements/iterator/arithmetic.pass.cpp