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// template <class T>
12// views::repeat(T &&) requires constructible_from<ranges::repeat_view<T>, T>;
13
14// template <class T, class Bound>
15// views::repeat(T &&, Bound &&) requires constructible_from<ranges::repeat_view<T, Bound>, T, Bound>;
16
17#include <cassert>
18#include <concepts>
19#include <ranges>
20#include <tuple>
21#include <type_traits>
22
23#include "MoveOnly.h"
24
25struct NonCopyable {
26 NonCopyable(NonCopyable&) = delete;
27};
28
29struct NonDefaultCtor {
30 NonDefaultCtor(int) {}
31};
32
33struct Empty {};
34
35struct LessThan3 {
36 constexpr bool operator()(int i) const { return i < 3; }
37};
38
39struct EqualTo33 {
40 constexpr bool operator()(int i) const { return i == 33; }
41};
42
43struct Add3 {
44 constexpr int operator()(int i) const { return i + 3; }
45};
46
47// Tp is_object
48static_assert(std::is_invocable_v<decltype(std::views::repeat), int>);
49static_assert(!std::is_invocable_v<decltype(std::views::repeat), void>);
50
51// _Bound is semiregular, integer like or std::unreachable_sentinel_t
52static_assert(!std::is_invocable_v<decltype(std::views::repeat), int, Empty>);
53static_assert(!std::is_invocable_v<decltype(std::views::repeat), int, NonCopyable>);
54static_assert(!std::is_invocable_v<decltype(std::views::repeat), int, NonDefaultCtor>);
55static_assert(std::is_invocable_v<decltype(std::views::repeat), int, std::unreachable_sentinel_t>);
56
57// Tp is copy_constructible
58static_assert(!std::is_invocable_v<decltype(std::views::repeat), NonCopyable>);
59
60// Tp is move_constructible
61static_assert(std::is_invocable_v<decltype(std::views::repeat), MoveOnly>);
62
63constexpr bool test() {
64 assert(*std::views::repeat(33).begin() == 33);
65 assert(*std::views::repeat(33, 10).begin() == 33);
66 static_assert(std::same_as<decltype(std::views::repeat(42)), std::ranges::repeat_view<int>>);
67 static_assert(std::same_as<decltype(std::views::repeat(42, 3)), std::ranges::repeat_view<int, int>>);
68 static_assert(std::same_as<decltype(std::views::repeat), decltype(std::ranges::views::repeat)>);
69
70 // unbound && drop_view
71 {
72 auto r = std::views::repeat(33) | std::views::drop(3);
73 static_assert(!std::ranges::sized_range<decltype(r)>);
74 assert(*r.begin() == 33);
75 }
76
77 // bound && drop_view
78 {
79 auto r = std::views::repeat(33, 8) | std::views::drop(3);
80 static_assert(std::ranges::sized_range<decltype(r)>);
81 assert(*r.begin() == 33);
82 assert(r.size() == 5);
83 }
84
85 // unbound && take_view
86 {
87 auto r = std::views::repeat(33) | std::views::take(3);
88 static_assert(std::ranges::sized_range<decltype(r)>);
89 assert(*r.begin() == 33);
90 assert(r.size() == 3);
91 }
92
93 // bound && take_view
94 {
95 auto r = std::views::repeat(33, 8) | std::views::take(3);
96 static_assert(std::ranges::sized_range<decltype(r)>);
97 assert(*r.begin() == 33);
98 assert(r.size() == 3);
99 }
100
101 // bound && transform_view
102 {
103 auto r = std::views::repeat(33, 8) | std::views::transform(Add3{});
104 assert(*r.begin() == 36);
105 assert(r.size() == 8);
106 }
107
108 // unbound && transform_view
109 {
110 auto r = std::views::repeat(33) | std::views::transform(Add3{});
111 assert(*r.begin() == 36);
112 }
113
114 return true;
115}
116
117int main(int, char**) {
118 test();
119 static_assert(test());
120
121 return 0;
122}
123

source code of libcxx/test/std/ranges/range.factories/range.repeat.view/views_repeat.pass.cpp