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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
// Range v3 library
//
// Copyright Casey Carter 2016
// Copyright Eric Niebler 2018
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
#include <range/v3/range/access.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/view/subrange.hpp>
#include <range/v3/view/ref.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/algorithm/find.hpp>
#include <vector>
#include "../simple_test.hpp"
#if defined(__clang__)
RANGES_DIAGNOSTIC_IGNORE("-Wunused-const-variable")
#endif
void test_range_access_ambiguity()
{
std::vector<ranges::reverse_iterator<int*>> vri{};
using namespace ranges;
(void)begin(vri);
(void)end(vri);
(void)cbegin(vri);
(void)cend(vri);
(void)rbegin(vri);
(void)rend(vri);
(void)crbegin(vri);
(void)crend(vri);
}
void test_initializer_list()
{
std::initializer_list<int> il = {0,1,2};
{
int count = 0;
for(auto p = ranges::begin(il), e = ranges::end(il); p != e; ++p) {
CHECK(*p == count++);
}
}
{
int count = 3;
for(auto p = ranges::rbegin(il), e = ranges::rend(il); p != e; ++p) {
CHECK(*p == --count);
}
}
CHECK(ranges::size(il) == std::size_t{3});
CHECK(ranges::data(il) == &*il.begin());
CHECK(ranges::empty(il) == false);
}
template<class Value, typename T, T... Is>
void test_array(std::integer_sequence<T, Is...>)
{
Value a[sizeof...(Is)] = { Is... };
{
int count = 0;
for(auto p = ranges::begin(a), e = ranges::end(a); p != e; ++p) {
CHECK(*p == count++);
}
}
{
int count = sizeof...(Is);
for(auto p = ranges::rbegin(a), e = ranges::rend(a); p != e; ++p) {
CHECK(*p == --count);
}
}
CHECK(ranges::size(a) == sizeof...(Is));
CHECK(ranges::data(a) == a + 0);
CHECK(ranges::empty(a) == false);
}
namespace begin_testing
{
template<class R>
CPP_requires(can_begin_,
requires(R&& r) //
(
ranges::begin((R &&) r)
));
template<class R>
CPP_concept can_begin =
CPP_requires_ref(can_begin_, R);
template<class R>
CPP_requires(can_cbegin_,
requires(R&& r) //
(
ranges::cbegin((R &&) r)
));
template<class R>
CPP_concept can_cbegin =
CPP_requires_ref(can_cbegin_, R);
struct A
{
int* begin();
int* end();
int const * begin() const;
int const * end() const;
};
struct B : A {};
void* begin(B&);
struct C : A {};
void begin(C&);
struct D : A {};
char* begin(D&);
void test()
{
// Valid
CPP_assert(can_begin<int(&)[2]>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<int(&)[2]>())), int*>);
CPP_assert(can_begin<int const(&)[2]>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<int const(&)[2]>())), int const *>);
CPP_assert(can_cbegin<int(&)[2]>);
CPP_assert(ranges::same_as<decltype(ranges::cbegin(std::declval<int(&)[2]>())), int const *>);
CPP_assert(can_cbegin<int const(&)[2]>);
CPP_assert(ranges::same_as<decltype(ranges::cbegin(std::declval<int const(&)[2]>())), int const *>);
#ifndef RANGES_WORKAROUND_MSVC_573728
// Ill-formed: array rvalue
CPP_assert(!can_begin<int(&&)[2]>);
CPP_assert(!can_begin<int const(&&)[2]>);
CPP_assert(!can_cbegin<int(&&)[2]>);
CPP_assert(!can_cbegin<int const(&&)[2]>);
#endif // RANGES_WORKAROUND_MSVC_573728
// Valid: only member begin
CPP_assert(can_begin<A&>);
CPP_assert(!can_begin<A>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<A&>())), int*>);
CPP_assert(can_begin<const A&>);
CPP_assert(!can_begin<const A>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<const A&>())), int const *>);
// Valid: Both member and non-member begin, but non-member returns non-Iterator.
CPP_assert(can_begin<B&>);
CPP_assert(!can_begin<B>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<B&>())), int*>);
CPP_assert(can_begin<const B&>);
CPP_assert(!can_begin<const B>);
CPP_assert(ranges::same_as<decltype(ranges::begin(std::declval<const B&>())), int const *>);
// Valid: Both member and non-member begin, but non-member returns non-Iterator.
CPP_assert(can_begin<C&>);
CPP_assert(!can_begin<C>);
CPP_assert(can_begin<const C&>);
CPP_assert(!can_begin<const C>);
// Valid: Prefer member begin
CPP_assert(can_begin<D&>);
CPP_assert(!can_begin<D>);
CPP_assert(ranges::same_as<int*, decltype(ranges::begin(std::declval<D&>()))>);
CPP_assert(can_begin<const D&>);
CPP_assert(!can_begin<const D>);
CPP_assert(ranges::same_as<int const *, decltype(ranges::begin(std::declval<const D&>()))>);
{
using T = std::initializer_list<int>;
// Valid: begin accepts lvalue initializer_list
CPP_assert(ranges::same_as<int const *, decltype(ranges::begin(std::declval<T&>()))>);
CPP_assert(ranges::same_as<int const *, decltype(ranges::begin(std::declval<const T&>()))>);
CPP_assert(!can_begin<T>);
CPP_assert(!can_begin<T const>);
}
CPP_assert(can_begin<ranges::subrange<int*, int*>&>);
CPP_assert(can_begin<const ranges::subrange<int*, int*>&>);
CPP_assert(can_begin<ranges::subrange<int*, int*>>);
CPP_assert(can_begin<const ranges::subrange<int*, int*>>);
CPP_assert(can_cbegin<ranges::subrange<int*, int*>&>);
CPP_assert(can_cbegin<const ranges::subrange<int*, int*>&>);
CPP_assert(can_cbegin<ranges::subrange<int*, int*>>);
CPP_assert(can_cbegin<const ranges::subrange<int*, int*>>);
CPP_assert(can_begin<ranges::ref_view<int[5]>&>);
CPP_assert(can_begin<const ranges::ref_view<int[5]>&>);
CPP_assert(can_begin<ranges::ref_view<int[5]>>);
CPP_assert(can_begin<const ranges::ref_view<int[5]>>);
CPP_assert(can_cbegin<ranges::ref_view<int[5]>&>);
CPP_assert(can_cbegin<const ranges::ref_view<int[5]>&>);
CPP_assert(can_cbegin<ranges::ref_view<int[5]>>);
CPP_assert(can_cbegin<const ranges::ref_view<int[5]>>);
// TODO
// CPP_assert(can_begin<ranges::iota_view<int, int>&>);
// CPP_assert(can_begin<const ranges::iota_view<int, int>&>);
// CPP_assert(can_begin<ranges::iota_view<int, int>>);
// CPP_assert(can_begin<const ranges::iota_view<int, int>>);
// CPP_assert(can_cbegin<ranges::iota_view<int, int>&>);
// CPP_assert(can_cbegin<const ranges::iota_view<int, int>&>);
// CPP_assert(can_cbegin<ranges::iota_view<int, int>>);
// CPP_assert(can_cbegin<const ranges::iota_view<int, int>>);
}
} // namespace begin_testing
namespace X
{
template<class T, std::size_t N>
struct array
{
T elements_[N];
constexpr bool empty() const noexcept { return N == 0; }
constexpr T* data() noexcept { return elements_; }
constexpr T const *data() const noexcept { return elements_; }
};
template<class T, std::size_t N>
constexpr T* begin(array<T, N> &a) noexcept { return a.elements_; }
template<class T, std::size_t N>
constexpr T* end(array<T, N> &a) noexcept { return a.elements_ + N; }
template<class T, std::size_t N>
constexpr T const *begin(array<T, N> const &a) noexcept { return a.elements_; }
template<class T, std::size_t N>
constexpr T const *end(array<T, N> const &a) noexcept { return a.elements_ + N; }
} // namespace X
using I = int*;
using CI = int const *;
CPP_assert(ranges::input_or_output_iterator<I>);
CPP_assert(ranges::input_or_output_iterator<CI>);
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201603L
void test_string_view_p0970()
{
// basic_string_views are non-dangling
using I2 = ranges::iterator_t<std::string_view>;
CPP_assert(ranges::same_as<I2, decltype(ranges::begin(std::declval<std::string_view>()))>);
CPP_assert(ranges::same_as<I2, decltype(ranges::end(std::declval<std::string_view>()))>);
CPP_assert(ranges::same_as<I2, decltype(ranges::begin(std::declval<const std::string_view>()))>);
CPP_assert(ranges::same_as<I2, decltype(ranges::end(std::declval<const std::string_view>()))>);
{
const char hw[] = "Hello, World!";
auto result = ranges::find(std::string_view{hw}, 'W');
CPP_assert(ranges::same_as<I2, decltype(result)>);
CHECK(result == std::string_view{hw}.begin() + 7);
}
}
#endif
int main()
{
using namespace ranges;
static constexpr X::array<int, 4> some_ints = {{0,1,2,3}};
CPP_assert(begin_testing::can_begin<X::array<int, 4> &>);
CPP_assert(begin_testing::can_begin<X::array<int, 4> const &>);
CPP_assert(!begin_testing::can_begin<X::array<int, 4>>);
CPP_assert(!begin_testing::can_begin<X::array<int, 4> const>);
CPP_assert(begin_testing::can_cbegin<X::array<int, 4> &>);
CPP_assert(begin_testing::can_cbegin<X::array<int, 4> const &>);
CPP_assert(!begin_testing::can_cbegin<X::array<int, 4>>);
CPP_assert(!begin_testing::can_cbegin<X::array<int, 4> const>);
constexpr auto first = begin(some_ints);
constexpr auto last = end(some_ints);
CPP_assert(ranges::same_as<const CI, decltype(first)>);
CPP_assert(ranges::same_as<const CI, decltype(last)>);
static_assert(first == cbegin(some_ints), "");
static_assert(last == cend(some_ints), "");
static_assert(noexcept(begin(some_ints)), "");
static_assert(noexcept(end(some_ints)), "");
static_assert(noexcept(cbegin(some_ints)), "");
static_assert(noexcept(cend(some_ints)), "");
static_assert(noexcept(empty(some_ints)), "");
static_assert(noexcept(data(some_ints)), "");
constexpr bool output = false;
static_assert(!empty(some_ints), "");
if(output)
std::cout << '{';
auto is_first = true;
auto count = 0;
for(auto&& i : some_ints)
{
CHECK(i == count++);
if(is_first)
is_first = false;
else
if(output) std::cout << ", ";
if(output) std::cout << i;
}
if(output)
std::cout << "}\n";
test_initializer_list();
test_array<int>(std::make_integer_sequence<int, 3>{});
test_array<int const>(std::make_integer_sequence<int, 3>{});
begin_testing::test();
#if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201603L
test_string_view_p0970();
#endif
return ::test_result();
}
|