File: end.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 (121 lines) | stat: -rw-r--r-- 3,562 bytes parent folder | download | duplicates (9)
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
//===----------------------------------------------------------------------===//
//
// 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, c++20

// <ranges>

// constexpr auto end();

#include <ranges>

#include <cassert>
#include <concepts>
#include <functional>

#include "test_iterators.h"

struct NonCommonRange : std::ranges::view_base {
  using Iterator = forward_iterator<int*>;
  using Sentinel = sentinel_wrapper<Iterator>;
  constexpr explicit NonCommonRange(int* b, int* e) : begin_(b), end_(e) {}
  constexpr Iterator begin() const { return Iterator(begin_); }
  constexpr Sentinel end() const { return Sentinel(Iterator(end_)); }

private:
  int* begin_;
  int* end_;
};

static_assert(std::ranges::forward_range<NonCommonRange>);
static_assert(!std::ranges::common_range<NonCommonRange>);

struct CommonRange : std::ranges::view_base {
  using Iterator = bidirectional_iterator<int*>;
  constexpr explicit CommonRange(int* b, int* e) : begin_(b), end_(e) {}
  constexpr Iterator begin() const { return Iterator(begin_); }
  constexpr Iterator end() const { return Iterator(end_); }

private:
  int* begin_;
  int* end_;
};

static_assert(std::ranges::bidirectional_range<CommonRange>);
static_assert(std::ranges::common_range<CommonRange>);

constexpr bool test() {
  int buff[] = {1, 0, 3, 1, 2, 3, 4, 5};

  // Check the return type of `end()`
  {
    CommonRange range(buff, buff + 1);
    auto pred = [](int, int) { return true; };
    std::ranges::chunk_by_view view(range, pred);
    using ChunkByView = decltype(view);
    static_assert(std::ranges::common_range<ChunkByView>);
    ASSERT_SAME_TYPE(std::ranges::sentinel_t<ChunkByView>, decltype(view.end()));
  }

  // end() on an empty range
  {
    CommonRange range(buff, buff);
    auto pred = [](int x, int y) { return x <= y; };
    std::ranges::chunk_by_view view(range, pred);
    auto end = view.end();
    assert(end == std::default_sentinel);
  }

  // end() on a 1-element range
  {
    CommonRange range(buff, buff + 1);
    auto pred = [](int& x, int& y) { return x <= y; };
    std::ranges::chunk_by_view view(range, pred);
    auto end = view.end();
    assert(base((*--end).begin()) == buff);
    assert(base((*end).end()) == buff + 1);
  }

  // end() on a 2-element range
  {
    CommonRange range(buff, buff + 2);
    auto pred = [](int const& x, int const& y) { return x <= y; };
    std::ranges::chunk_by_view view(range, pred);
    auto end = view.end();
    assert(base((*--end).begin()) == buff + 1);
    assert(base((*--end).end()) == buff + 1);
  }

  // end() on a 8-element range
  {
    CommonRange range(buff, buff + 8);
    auto pred = [](const int x, const int y) { return x < y; };
    std::ranges::chunk_by_view view(range, pred);
    auto end = view.end();
    assert(base((*--end).end()) == buff + 8);
    assert(base((*--end).end()) == buff + 3);
  }

  // end() on a non-common range
  {
    NonCommonRange range(buff, buff + 1);
    std::ranges::chunk_by_view view(range, std::ranges::less_equal{});
    auto end = view.end();
    ASSERT_SAME_TYPE(std::default_sentinel_t, std::ranges::sentinel_t<decltype(view)>);
    ASSERT_SAME_TYPE(std::default_sentinel_t, decltype(end));
  }

  return true;
}

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

  return 0;
}