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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
//===----------------------------------------------------------------------===//
//
// 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"
template <class T>
concept HasPreDecrement = requires(T t) {
{ --t };
};
template <class T>
concept HasPostDecrement = requires(T t) {
{ t-- };
};
struct TrackingPred : TrackInitialization {
using TrackInitialization::TrackInitialization;
constexpr bool operator()(int x, int y) const { return x <= y; }
};
template <class Iter, IsConst Constant, class Sent = sentinel_wrapper<Iter>>
constexpr void test() {
using Underlying = View<Iter, Sent>;
using ChunkByView = std::ranges::chunk_by_view<Underlying, std::ranges::less_equal>;
using ChunkByIterator = std::ranges::iterator_t<ChunkByView>;
static_assert(HasPostDecrement<ChunkByIterator>);
static_assert(HasPreDecrement<ChunkByIterator>);
auto make_chunk_by_view = [](auto& arr) {
View view{Iter{arr.data()}, Sent{Iter{arr.data() + arr.size()}}};
return ChunkByView{std::move(view), std::ranges::less_equal{}};
};
// Test with a single chunk
{
std::array array{0, 1, 2, 3, 4};
ChunkByView view = make_chunk_by_view(array);
ChunkByIterator it = std::ranges::next(view.begin(), view.end());
std::same_as<ChunkByIterator&> decltype(auto) result = --it;
assert(&result == &it);
assert(base((*result).begin()) == array.data());
}
// Test with two chunks
{
std::array array{0, 1, 2, 0, 1, 2};
ChunkByView view = make_chunk_by_view(array);
ChunkByIterator it = std::ranges::next(view.begin(), view.end());
std::same_as<ChunkByIterator&> decltype(auto) result = --it;
assert(&result == &it);
assert(base((*result).begin()) == array.data() + 3);
--it;
assert(base((*result).begin()) == array.data());
}
// Test going forward and then backward on the same iterator
{
std::array array{7, 8, 9, 4, 5, 6, 1, 2, 3, 0};
ChunkByView view = make_chunk_by_view(array);
ChunkByIterator it = view.begin();
++it;
--it;
assert(base((*it).begin()) == array.data());
assert(base((*it).end()) == array.data() + 3);
++it;
++it;
--it;
assert(base((*it).begin()) == array.data() + 3);
assert(base((*it).end()) == array.data() + 6);
++it;
++it;
--it;
assert(base((*it).begin()) == array.data() + 6);
assert(base((*it).end()) == array.data() + 9);
++it;
++it;
--it;
assert(base((*it).begin()) == array.data() + 9);
}
// Decrement an iterator multiple times
if constexpr (std::ranges::common_range<Underlying>) {
std::array array{1, 2, 1, 2, 1};
ChunkByView view = make_chunk_by_view(array);
ChunkByIterator it = view.end();
--it;
--it;
--it;
assert(base((*it).begin()) == array.data());
}
// 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{Iter{array.data()}, Sent{Iter{array.data() + array.size()}}};
auto view = std::views::chunk_by(std::move(v), [](int& x, int& y) { return x <= y; });
auto it = std::ranges::next(view.begin());
assert(base((*it).begin()) == array.data() + 3);
--it;
assert(base((*it).begin()) == array.data());
}
// 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{Iter{array.data()}, Sent{Iter{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 = std::ranges::next(view.begin());
assert(base((*it).begin().base()) == array.data() + 3);
--it;
assert(base((*it).begin().base()) == array.data());
}
// Make sure we do not make a copy of the predicate when we decrement
if constexpr (std::ranges::common_range<Underlying>) {
bool moved = false, copied = false;
std::array array{1, 2, 1, 3};
View v{Iter(array.data()), Sent(Iter(array.data() + array.size()))};
auto view = std::views::chunk_by(std::move(v), TrackingPred(&moved, &copied));
assert(std::exchange(moved, false));
auto it = view.end();
--it;
it--;
assert(!moved);
assert(!copied);
}
// Check post-decrement
{
std::array array{0, 1, 2, -3, -2, -1, -6, -5, -4};
ChunkByView view = make_chunk_by_view(array);
ChunkByIterator it = std::ranges::next(view.begin(), view.end());
std::same_as<ChunkByIterator> decltype(auto) result = it--;
assert(result != it);
assert(result == std::default_sentinel);
assert(base((*it).begin()) == array.data() + 6);
result = it--;
assert(base((*it).begin()) == array.data() + 3);
assert(base((*result).begin()) == array.data() + 6);
result = it--;
assert(base((*it).begin()) == array.data());
assert(base((*result).begin()) == array.data() + 3);
}
}
template <class Iterator, IsConst Constant>
constexpr void test_with_pair() {
// Test with pair of iterators
test<Iterator, Constant>();
// Test with iterator-sentinel pair
test<Iterator, Constant, Iterator>();
}
constexpr bool tests() {
test_with_pair<bidirectional_iterator<int*>, IsConst::no>();
test_with_pair<random_access_iterator<int*>, IsConst::no>();
test_with_pair<contiguous_iterator<int*>, IsConst::no>();
test_with_pair<int*, IsConst::no>();
test_with_pair<bidirectional_iterator<int const*>, IsConst::yes>();
test_with_pair<random_access_iterator<int const*>, IsConst::yes>();
test_with_pair<contiguous_iterator<int const*>, IsConst::yes>();
test_with_pair<int const*, IsConst::yes>();
// Make sure `operator--` isn't provided for non bidirectional ranges
{
using ForwardView = View<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>;
using ChunkByView = std::ranges::chunk_by_view<ForwardView, std::ranges::less_equal>;
static_assert(!HasPreDecrement<std::ranges::iterator_t<ChunkByView>>);
static_assert(!HasPostDecrement<std::ranges::iterator_t<ChunkByView>>);
}
return true;
}
int main(int, char**) {
tests();
static_assert(tests());
return 0;
}
|