File: increment.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 (179 lines) | stat: -rw-r--r-- 5,716 bytes parent folder | download | duplicates (8)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===----------------------------------------------------------------------===//
//
// 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 iterator& operator++();
// constexpr iterator operator++(int);

#include <ranges>

#include <array>
#include <cassert>
#include <concepts>
#include <functional>
#include <span>
#include <type_traits>
#include <utility>

#include "../types.h"
#include "test_iterators.h"
#include "test_macros.h"

struct TrackingPred : TrackInitialization {
  using TrackInitialization::TrackInitialization;
  constexpr bool operator()(int x, int y) const { return x <= y; }
};

template <class Iterator, IsConst Constant>
constexpr void test() {
  using Sentinel        = sentinel_wrapper<Iterator>;
  using Underlying      = View<Iterator, Sentinel>;
  using ChunkByView     = std::ranges::chunk_by_view<Underlying, std::ranges::less_equal>;
  using ChunkByIterator = std::ranges::iterator_t<ChunkByView>;

  auto make_chunk_by_view = [](auto& arr) {
    View view{Iterator{arr.data()}, Sentinel{Iterator{arr.data() + arr.size()}}};
    return ChunkByView{std::move(view), std::ranges::less_equal{}};
  };

  // Increment the iterator when it won't find another satisfied value after begin()
  {
    std::array array{0, 1, 2, 3, 4};
    ChunkByView view   = make_chunk_by_view(array);
    ChunkByIterator it = view.begin();

    std::same_as<ChunkByIterator&> decltype(auto) result = ++it;
    assert(&result == &it);
    assert(result == view.end());
    assert(result == std::default_sentinel);
  }

  // Increment the iterator and it finds another value after begin()
  {
    std::array array{1, 2, 3, -1, -2, -3};
    int const* second_chunk = array.data() + 3;
    ChunkByView view        = make_chunk_by_view(array);

    ChunkByIterator it = view.begin();
    ++it;
    assert(base((*it).begin()) == second_chunk);
  }

  // Increment advances all the way to the end of the range
  {
    std::array array{1, 2, 3, 4, 1};
    ChunkByView view = make_chunk_by_view(array);

    ChunkByIterator it = view.begin();
    ++it;
    assert(base((*it).begin()) == array.data() + 4);
  }

  // Increment an iterator multiple times
  {
    std::array array{0, 1, 0, 2, 0, 3, 0, 4};
    ChunkByView view = make_chunk_by_view(array);

    ChunkByIterator it = view.begin();
    assert(base((*it).begin()) == array.data());
    ++it;
    assert(base((*it).begin()) == array.data() + 2);
    ++it;
    assert(base((*it).begin()) == array.data() + 4);
    ++it;
    assert(base((*it).begin()) == array.data() + 6);
    ++it;
    assert(it == std::default_sentinel);
  }

  // Test with a predicate that takes by non-const reference
  if constexpr (!std::to_underlying(Constant)) {
    std::array array{1, 2, 3, -3, -2, -1};
    View v{Iterator{array.data()}, Sentinel{Iterator{array.data() + array.size()}}};
    auto view = std::views::chunk_by(std::move(v), [](int& x, int& y) { return x <= y; });

    auto it = view.begin();
    assert(base((*it).begin()) == array.data());
    ++it;
    assert(base((*it).begin()) == array.data() + 3);
  }

  // Test with a predicate that is invocable but not callable (i.e. cannot be called like regular function 'f()')
  {
    std::array array = {1, 2, 3, -3, -2, -1};
    auto v           = View{Iterator{array.data()}, Sentinel{Iterator{array.data() + array.size()}}} |
             std::views::transform([](int x) { return IntWrapper{x}; });
    auto view = std::views::chunk_by(std::move(v), &IntWrapper::lessEqual);

    auto it = view.begin();
    assert(base((*it).begin().base()) == array.data());
    ++it;
    assert(base((*it).begin().base()) == array.data() + 3);
  }

  // Make sure we do not make a copy of the predicate when we increment
  // (we should be passing it to ranges::adjacent_find using std::ref)
  {
    bool moved = false, copied = false;
    std::array array{1, 2, 1, 3};
    View v{Iterator(array.data()), Sentinel(Iterator(array.data() + array.size()))};
    auto view = std::views::chunk_by(std::move(v), TrackingPred(&moved, &copied));
    assert(std::exchange(moved, false));
    auto it = view.begin();
    ++it;
    it++;
    assert(!moved);
    assert(!copied);
  }

  // Check post-increment
  {
    std::array array{0, 1, 2, -3, -2, -1, -6, -5, -4};
    ChunkByView view   = make_chunk_by_view(array);
    ChunkByIterator it = view.begin();

    std::same_as<ChunkByIterator> decltype(auto) result = it++;
    assert(result != it);
    assert(base((*result).begin()) == array.data());
    assert(base((*it).begin()) == array.data() + 3);

    result = it++;
    assert(base((*result).begin()) == array.data() + 3);
    assert(base((*it).begin()) == array.data() + 6);

    result = it++;
    assert(base((*result).begin()) == array.data() + 6);
    assert(it == std::default_sentinel);
  }
}

constexpr bool tests() {
  test<forward_iterator<int*>, IsConst::no>();
  test<bidirectional_iterator<int*>, IsConst::no>();
  test<random_access_iterator<int*>, IsConst::no>();
  test<contiguous_iterator<int*>, IsConst::no>();
  test<int*, IsConst::no>();

  test<forward_iterator<int const*>, IsConst::yes>();
  test<bidirectional_iterator<int const*>, IsConst::yes>();
  test<random_access_iterator<int const*>, IsConst::yes>();
  test<contiguous_iterator<int const*>, IsConst::yes>();
  test<int const*, IsConst::yes>();

  return true;
}

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

  return 0;
}