File: arithmetic.pass.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 1,998,492 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (123 lines) | stat: -rw-r--r-- 3,858 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

// constexpr iterator& operator+=(difference_type n)
//    requires random_access_range<Base>;
//
// constexpr iterator& operator-=(difference_type n)
//   requires random_access_range<Base>;
//
// friend constexpr iterator operator+(const iterator& x, difference_type y)
//     requires random_access_range<Base>;
//
// friend constexpr iterator operator+(difference_type x, const iterator& y)
//   requires random_access_range<Base>;
//
// friend constexpr iterator operator-(const iterator& x, difference_type y)
//   requires random_access_range<Base>;
//
// friend constexpr difference_type operator-(const iterator& x, const iterator& y)
//   requires sized_sentinel_for<iterator_t<Base>, iterator_t<Base>>;

#include <ranges>

#include <tuple>

#include "test_iterators.h"

template <class T, class U>
concept CanPlus = requires(T t, U u) { t + u; };

template <class T, class U>
concept CanPlusEqual = requires(T t, U u) { t += u; };

template <class T, class U>
concept CanMinus = requires(T t, U u) { t - u; };

template <class T, class U>
concept CanMinusEqual = requires(T t, U u) { t -= u; };

template <class BaseRange>
using ElemIter = std::ranges::iterator_t<std::ranges::elements_view<BaseRange, 0>>;

using RandomAccessRange = std::ranges::subrange<std::tuple<int>*>;
static_assert(std::ranges::random_access_range<RandomAccessRange>);
static_assert(std::sized_sentinel_for<std::ranges::iterator_t<RandomAccessRange>, //
                                      std::ranges::iterator_t<RandomAccessRange>>);

static_assert(CanPlus<ElemIter<RandomAccessRange>, int>);
static_assert(CanPlus<int, ElemIter<RandomAccessRange>>);
static_assert(CanPlusEqual<ElemIter<RandomAccessRange>, int>);
static_assert(CanMinus<ElemIter<RandomAccessRange>, int>);
static_assert(CanMinus<ElemIter<RandomAccessRange>, ElemIter<RandomAccessRange>>);
static_assert(CanMinusEqual<ElemIter<RandomAccessRange>, int>);

using BidiRange = std::ranges::subrange<bidirectional_iterator<std::tuple<int>*>>;
static_assert(!std::ranges::random_access_range<BidiRange>);
static_assert(!std::sized_sentinel_for<std::ranges::iterator_t<BidiRange>, //
                                       std::ranges::iterator_t<BidiRange>>);

static_assert(!CanPlus<ElemIter<BidiRange>, int>);
static_assert(!CanPlus<int, ElemIter<BidiRange>>);
static_assert(!CanPlusEqual<ElemIter<BidiRange>, int>);
static_assert(!CanMinus<ElemIter<BidiRange>, int>);
static_assert(!CanMinus<ElemIter<BidiRange>, ElemIter<BidiRange>>);
static_assert(!CanMinusEqual<ElemIter<BidiRange>, int>);

constexpr bool test() {
  std::tuple<int> ts[] = {{1}, {2}, {3}, {4}};

  RandomAccessRange r{&ts[0], &ts[0] + 4};
  auto ev = r | std::views::elements<0>;
  {
    // operator+(x, n) operator+(n,x) and operator+=
    auto it1 = ev.begin();

    auto it2 = it1 + 3;
    assert(it2.base() == &ts[3]);

    auto it3 = 3 + it1;
    assert(it3.base() == &ts[3]);

    it1 += 3;
    assert(it1 == it2);
    assert(it1.base() == &ts[3]);
  }

  {
    // operator-(x, n) and operator-=
    auto it1 = ev.end();

    auto it2 = it1 - 3;
    assert(it2.base() == &ts[1]);

    it1 -= 3;
    assert(it1 == it2);
    assert(it1.base() == &ts[1]);
  }

  {
    // operator-(x, y)
    assert((ev.end() - ev.begin()) == 4);

    auto it1 = ev.begin() + 2;
    auto it2 = ev.end() - 1;
    assert((it1 - it2) == -1);
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}