summaryrefslogtreecommitdiff
path: root/libcxx/test/std/ranges/range.adaptors/range.elements/end.pass.cpp
blob: 003ce50381f7787303f82e2ed2b3e0253b8f3ae5 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//===----------------------------------------------------------------------===//
//
// 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 auto end() requires (!simple-view<V> && !common_range<V>)
// constexpr auto end() requires (!simple-view<V> && common_range<V>)
// constexpr auto end() const requires range<const V>
// constexpr auto end() const requires common_range<const V>

#include <cassert>
#include <iterator>
#include <ranges>
#include <type_traits>
#include <utility>

#include "types.h"

// | simple | common |      v.end()     | as_const(v)
// |        |        |                  |   .end()
// |--------|--------|------------------|---------------
// |   Y    |   Y    |  iterator<true>  | iterator<true>
// |   Y    |   N    |  sentinel<true>  | sentinel<true>
// |   N    |   Y    |  iterator<false> | iterator<true>
// |   N    |   N    |  sentinel<false> | sentinel<true>

// !range<const V>
template <class T>
concept HasEnd = requires(T t) { t.end(); };

template <class T>
concept HasConstEnd = requires(const T ct) { ct.end(); };

struct NoConstEndView : TupleBufferView {
  using TupleBufferView::TupleBufferView;
  constexpr std::tuple<int>* begin() { return buffer_; }
  constexpr std::tuple<int>* end() { return buffer_ + size_; }
};

static_assert(HasEnd<std::ranges::elements_view<NoConstEndView, 0>>);
static_assert(!HasConstEnd<std::ranges::elements_view<NoConstEndView, 0>>);

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

  // simple-view && common_view
  {
    SimpleCommon v{buffer};
    auto ev = std::views::elements<0>(v);

    auto it           = ev.begin();
    decltype(auto) st = ev.end();
    assert(st == it + 3);

    auto const_it           = std::as_const(ev).begin();
    decltype(auto) const_st = std::as_const(ev).end();
    assert(const_st == const_it + 3);

    // Both iterator<true>
    static_assert(std::same_as<decltype(st), decltype(const_st)>);
    static_assert(std::same_as<decltype(st), decltype(it)>);
    static_assert(std::same_as<decltype(const_st), decltype(const_it)>);
  }

  // simple-view && !common_view
  {
    SimpleNonCommon v{buffer};
    auto ev = std::views::elements<0>(v);

    auto it           = ev.begin();
    decltype(auto) st = ev.end();
    assert(st == it + 3);

    auto const_it           = std::as_const(ev).begin();
    decltype(auto) const_st = std::as_const(ev).end();
    assert(const_st == const_it + 3);

    // Both iterator<true>
    static_assert(std::same_as<decltype(st), decltype(const_st)>);
    static_assert(!std::same_as<decltype(st), decltype(it)>);
    static_assert(!std::same_as<decltype(const_st), decltype(const_it)>);
  }

  // !simple-view && common_view
  {
    NonSimpleCommon v{buffer};
    auto ev = std::views::elements<0>(v);

    auto it           = ev.begin();
    decltype(auto) st = ev.end();
    assert(st == it + 3);

    auto const_it           = std::as_const(ev).begin();
    decltype(auto) const_st = std::as_const(ev).end();
    assert(const_st == const_it + 3);

    // iterator<false> and iterator<true>
    static_assert(!std::same_as<decltype(st), decltype(const_st)>);
    static_assert(std::same_as<decltype(st), decltype(it)>);
    static_assert(std::same_as<decltype(const_st), decltype(const_it)>);
  }

  // !simple-view && !common_view
  {
    NonSimpleNonCommon v{buffer};
    auto ev = std::views::elements<0>(v);

    auto it           = ev.begin();
    decltype(auto) st = ev.end();
    assert(st == it + 3);

    auto const_it           = std::as_const(ev).begin();
    decltype(auto) const_st = std::as_const(ev).end();
    assert(const_st == const_it + 3);

    // sentinel<false> and sentinel<true>
    static_assert(!std::same_as<decltype(st), decltype(const_st)>);
    static_assert(!std::same_as<decltype(st), decltype(it)>);
    static_assert(!std::same_as<decltype(const_st), decltype(const_it)>);
  }

  // LWG 3406 elements_view::begin() and elements_view::end() have incompatible constraints
  {
    std::tuple<int, int> x[] = {{0, 0}};
    std::ranges::subrange r  = {std::counted_iterator(x, 1), std::default_sentinel};
    auto v                   = r | std::views::elements<0>;
    assert(v.begin() != v.end());
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());
  return 0;
}