| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 |
|
| 11 |
// <ranges> |
| 12 |
|
| 13 |
// chunk_by_view() requires std::default_initializable<View> && |
| 14 |
// std::default_initializable<Pred> = default; |
| 15 |
|
| 16 |
#include <ranges> |
| 17 |
|
| 18 |
#include <cassert> |
| 19 |
#include <type_traits> |
| 20 |
|
| 21 |
constexpr int buff[] = {-2, 1, -1, 2}; |
| 22 |
|
| 23 |
struct DefaultConstructibleView : std::ranges::view_base { |
| 24 |
DefaultConstructibleView() = default; |
| 25 |
constexpr int const* begin() const { return buff; } |
| 26 |
constexpr int const* end() const { return buff + 4; } |
| 27 |
}; |
| 28 |
|
| 29 |
struct DefaultConstructiblePredicate { |
| 30 |
DefaultConstructiblePredicate() = default; |
| 31 |
constexpr bool operator()(int x, int y) const { return x != -y; } |
| 32 |
}; |
| 33 |
|
| 34 |
struct NoDefaultView : std::ranges::view_base { |
| 35 |
NoDefaultView() = delete; |
| 36 |
int* begin() const; |
| 37 |
int* end() const; |
| 38 |
}; |
| 39 |
|
| 40 |
struct NoDefaultPredicate { |
| 41 |
NoDefaultPredicate() = delete; |
| 42 |
constexpr bool operator()(int, int) const; |
| 43 |
}; |
| 44 |
|
| 45 |
struct NoexceptView : std::ranges::view_base { |
| 46 |
NoexceptView() noexcept; |
| 47 |
int const* begin() const; |
| 48 |
int const* end() const; |
| 49 |
}; |
| 50 |
|
| 51 |
struct NoexceptPredicate { |
| 52 |
NoexceptPredicate() noexcept; |
| 53 |
bool operator()(int, int) const; |
| 54 |
}; |
| 55 |
|
| 56 |
struct MayThrowView : std::ranges::view_base { |
| 57 |
MayThrowView() noexcept(false); |
| 58 |
int const* begin() const; |
| 59 |
int const* end() const; |
| 60 |
}; |
| 61 |
|
| 62 |
struct MayThrowPredicate { |
| 63 |
MayThrowPredicate() noexcept(false); |
| 64 |
bool operator()(int, int) const; |
| 65 |
}; |
| 66 |
|
| 67 |
constexpr void compareRanges(std::ranges::subrange<const int*> v, std::initializer_list<int> list) { |
| 68 |
assert(v.size() == list.size()); |
| 69 |
for (size_t i = 0; i < v.size(); ++i) { |
| 70 |
assert(v[i] == list.begin()[i]); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
constexpr bool test() { |
| 75 |
// Check default constructor with default initialization |
| 76 |
{ |
| 77 |
using View = std::ranges::chunk_by_view<DefaultConstructibleView, DefaultConstructiblePredicate>; |
| 78 |
View view; |
| 79 |
auto it = view.begin(), end = view.end(); |
| 80 |
compareRanges(*it++, {-2, 1}); |
| 81 |
compareRanges(*it++, {-1, 2}); |
| 82 |
assert(it == end); |
| 83 |
} |
| 84 |
|
| 85 |
// Check default construction with copy-list-initialization |
| 86 |
{ |
| 87 |
using View = std::ranges::chunk_by_view<DefaultConstructibleView, DefaultConstructiblePredicate>; |
| 88 |
View view = {}; |
| 89 |
auto it = view.begin(), end = view.end(); |
| 90 |
compareRanges(*it++, {-2, 1}); |
| 91 |
compareRanges(*it++, {-1, 2}); |
| 92 |
assert(it == end); |
| 93 |
} |
| 94 |
|
| 95 |
// Check cases where the default constructor isn't provided |
| 96 |
{ |
| 97 |
static_assert( |
| 98 |
!std::is_default_constructible_v<std::ranges::chunk_by_view<NoDefaultView, DefaultConstructiblePredicate>>); |
| 99 |
static_assert( |
| 100 |
!std::is_default_constructible_v<std::ranges::chunk_by_view<DefaultConstructibleView, NoDefaultPredicate>>); |
| 101 |
static_assert(!std::is_default_constructible_v<std::ranges::chunk_by_view<NoDefaultView, NoDefaultPredicate>>); |
| 102 |
} |
| 103 |
|
| 104 |
// Check noexcept-ness |
| 105 |
{ |
| 106 |
{ |
| 107 |
using View = std::ranges::chunk_by_view<MayThrowView, MayThrowPredicate>; |
| 108 |
static_assert(!noexcept(View())); |
| 109 |
} |
| 110 |
{ |
| 111 |
using View = std::ranges::chunk_by_view<MayThrowView, NoexceptPredicate>; |
| 112 |
static_assert(!noexcept(View())); |
| 113 |
} |
| 114 |
{ |
| 115 |
using View = std::ranges::chunk_by_view<NoexceptView, MayThrowPredicate>; |
| 116 |
static_assert(!noexcept(View())); |
| 117 |
} |
| 118 |
{ |
| 119 |
using View = std::ranges::chunk_by_view<NoexceptView, NoexceptPredicate>; |
| 120 |
static_assert(noexcept(View())); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
int main(int, char**) { |
| 128 |
test(); |
| 129 |
static_assert(test()); |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |