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
|
//===----------------------------------------------------------------------===//
//
// 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
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// template<range R>
// requires is_object_v<R>
// class ref_view;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
int globalBuff[8];
template<class T>
concept ValidRefView = requires { typename std::ranges::ref_view<T>; };
struct Range {
int start = 0;
friend constexpr int* begin(Range const& range) { return globalBuff + range.start; }
friend constexpr int* end(Range const&) { return globalBuff + 8; }
friend constexpr int* begin(Range& range) { return globalBuff + range.start; }
friend constexpr int* end(Range&) { return globalBuff + 8; }
};
struct BeginOnly {
friend int* begin(BeginOnly const&);
friend int* begin(BeginOnly &);
};
static_assert( ValidRefView<Range>);
static_assert(!ValidRefView<BeginOnly>);
static_assert(!ValidRefView<int (&)[4]>);
static_assert( ValidRefView<int[4]>);
static_assert(std::derived_from<std::ranges::ref_view<Range>, std::ranges::view_interface<std::ranges::ref_view<Range>>>);
struct RangeConvertible {
operator Range& ();
};
struct RValueRangeConvertible {
operator Range&& ();
};
static_assert( std::is_constructible_v<std::ranges::ref_view<Range>, Range&>);
static_assert( std::is_constructible_v<std::ranges::ref_view<Range>, RangeConvertible>);
static_assert(!std::is_constructible_v<std::ranges::ref_view<Range>, RValueRangeConvertible>);
struct ConstConvertibleToLValueAndRValue {
operator Range& () const;
operator Range&& () const;
};
static_assert( std::is_convertible_v<RangeConvertible, std::ranges::ref_view<Range>>);
static_assert(!std::is_convertible_v<RValueRangeConvertible, std::ranges::ref_view<Range>>);
static_assert(!std::is_convertible_v<ConstConvertibleToLValueAndRValue, std::ranges::ref_view<Range>>);
struct ForwardRange {
constexpr forward_iterator<int*> begin() const { return forward_iterator<int*>(globalBuff); }
constexpr forward_iterator<int*> end() const { return forward_iterator<int*>(globalBuff + 8); }
};
struct Cpp17InputRange {
struct sentinel {
friend constexpr bool operator==(sentinel, cpp17_input_iterator<int*> iter) { return iter.base() == globalBuff + 8; }
friend constexpr std::ptrdiff_t operator-(sentinel, cpp17_input_iterator<int*>) { return -8; }
friend constexpr std::ptrdiff_t operator-(cpp17_input_iterator<int*>, sentinel) { return 8; }
};
constexpr cpp17_input_iterator<int*> begin() const {
return cpp17_input_iterator<int*>(globalBuff);
}
constexpr sentinel end() const { return {}; }
};
struct Cpp20InputRange {
struct sentinel {
friend constexpr bool operator==(sentinel, const cpp20_input_iterator<int*> &iter) { return iter.base() == globalBuff + 8; }
friend constexpr std::ptrdiff_t operator-(sentinel, const cpp20_input_iterator<int*>&) { return -8; }
};
constexpr cpp20_input_iterator<int*> begin() const {
return cpp20_input_iterator<int*>(globalBuff);
}
constexpr sentinel end() const { return {}; }
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<Cpp20InputRange> = true;
template<class R>
concept EmptyIsInvocable = requires (std::ranges::ref_view<R> view) { view.empty(); };
template<class R>
concept SizeIsInvocable = requires (std::ranges::ref_view<R> view) { view.size(); };
template<class R>
concept DataIsInvocable = requires (std::ranges::ref_view<R> view) { view.data(); };
// Testing ctad.
static_assert(std::same_as<decltype(std::ranges::ref_view(std::declval<Range&>())),
std::ranges::ref_view<Range>>);
constexpr bool test() {
{
// ref_view::base
Range range;
std::ranges::ref_view<Range> view{range};
assert(view.begin() == globalBuff);
view.base() = Range{2};
assert(view.begin() == globalBuff + 2);
}
{
// ref_view::begin
Range range1;
std::ranges::ref_view<Range> view1 = range1;
assert(view1.begin() == globalBuff);
ForwardRange range2;
std::ranges::ref_view<ForwardRange> view2 = range2;
assert(view2.begin().base() == globalBuff);
Cpp17InputRange range3;
std::ranges::ref_view<Cpp17InputRange> view3 = range3;
assert(view3.begin().base() == globalBuff);
Cpp20InputRange range4;
std::ranges::ref_view<Cpp20InputRange> view4 = range4;
assert(view4.begin().base() == globalBuff);
}
{
// ref_view::end
Range range1;
std::ranges::ref_view<Range> view1 = range1;
assert(view1.end() == globalBuff + 8);
ForwardRange range2;
std::ranges::ref_view<ForwardRange> view2 = range2;
assert(view2.end().base() == globalBuff + 8);
Cpp17InputRange range3;
std::ranges::ref_view<Cpp17InputRange> view3 = range3;
assert(view3.end() == cpp17_input_iterator(globalBuff + 8));
Cpp20InputRange range4;
std::ranges::ref_view<Cpp20InputRange> view4 = range4;
assert(view4.end() == cpp20_input_iterator(globalBuff + 8));
}
{
// ref_view::empty
Range range{8};
std::ranges::ref_view<Range> view1 = range;
assert(view1.empty());
ForwardRange range2;
std::ranges::ref_view<ForwardRange> view2 = range2;
assert(!view2.empty());
static_assert(!EmptyIsInvocable<Cpp17InputRange>);
static_assert(!EmptyIsInvocable<Cpp20InputRange>);
}
{
// ref_view::size
Range range1{8};
std::ranges::ref_view<Range> view1 = range1;
assert(view1.size() == 0);
Range range2{2};
std::ranges::ref_view<Range> view2 = range2;
assert(view2.size() == 6);
static_assert(!SizeIsInvocable<ForwardRange>);
}
{
// ref_view::data
Range range1;
std::ranges::ref_view<Range> view1 = range1;
assert(view1.data() == globalBuff);
Range range2{2};
std::ranges::ref_view<Range> view2 = range2;
assert(view2.data() == globalBuff + 2);
static_assert(!DataIsInvocable<ForwardRange>);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|