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
|
//===----------------------------------------------------------------------===//
//
// 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>
// chunk_by_view() requires std::default_initializable<View> &&
// std::default_initializable<Pred> = default;
#include <ranges>
#include <cassert>
#include <type_traits>
constexpr int buff[] = {-2, 1, -1, 2};
struct DefaultConstructibleView : std::ranges::view_base {
DefaultConstructibleView() = default;
constexpr int const* begin() const { return buff; }
constexpr int const* end() const { return buff + 4; }
};
struct DefaultConstructiblePredicate {
DefaultConstructiblePredicate() = default;
constexpr bool operator()(int x, int y) const { return x != -y; }
};
struct NoDefaultView : std::ranges::view_base {
NoDefaultView() = delete;
int* begin() const;
int* end() const;
};
struct NoDefaultPredicate {
NoDefaultPredicate() = delete;
constexpr bool operator()(int, int) const;
};
struct NoexceptView : std::ranges::view_base {
NoexceptView() noexcept;
int const* begin() const;
int const* end() const;
};
struct NoexceptPredicate {
NoexceptPredicate() noexcept;
bool operator()(int, int) const;
};
struct MayThrowView : std::ranges::view_base {
MayThrowView() noexcept(false);
int const* begin() const;
int const* end() const;
};
struct MayThrowPredicate {
MayThrowPredicate() noexcept(false);
bool operator()(int, int) const;
};
constexpr void compareRanges(std::ranges::subrange<const int*> v, std::initializer_list<int> list) {
assert(v.size() == list.size());
for (size_t i = 0; i < v.size(); ++i) {
assert(v[i] == list.begin()[i]);
}
}
constexpr bool test() {
// Check default constructor with default initialization
{
using View = std::ranges::chunk_by_view<DefaultConstructibleView, DefaultConstructiblePredicate>;
View view;
auto it = view.begin(), end = view.end();
compareRanges(*it++, {-2, 1});
compareRanges(*it++, {-1, 2});
assert(it == end);
}
// Check default construction with copy-list-initialization
{
using View = std::ranges::chunk_by_view<DefaultConstructibleView, DefaultConstructiblePredicate>;
View view = {};
auto it = view.begin(), end = view.end();
compareRanges(*it++, {-2, 1});
compareRanges(*it++, {-1, 2});
assert(it == end);
}
// Check cases where the default constructor isn't provided
{
static_assert(
!std::is_default_constructible_v<std::ranges::chunk_by_view<NoDefaultView, DefaultConstructiblePredicate>>);
static_assert(
!std::is_default_constructible_v<std::ranges::chunk_by_view<DefaultConstructibleView, NoDefaultPredicate>>);
static_assert(!std::is_default_constructible_v<std::ranges::chunk_by_view<NoDefaultView, NoDefaultPredicate>>);
}
// Check noexcept-ness
{
{
using View = std::ranges::chunk_by_view<MayThrowView, MayThrowPredicate>;
static_assert(!noexcept(View()));
}
{
using View = std::ranges::chunk_by_view<MayThrowView, NoexceptPredicate>;
static_assert(!noexcept(View()));
}
{
using View = std::ranges::chunk_by_view<NoexceptView, MayThrowPredicate>;
static_assert(!noexcept(View()));
}
{
using View = std::ranges::chunk_by_view<NoexceptView, NoexceptPredicate>;
static_assert(noexcept(View()));
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|