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
|
//===----------------------------------------------------------------------===//
//
// 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
// constexpr auto begin();
#include <array>
#include <cassert>
#include <ranges>
#include <type_traits>
#include <utility>
#include "test_iterators.h"
struct View : std::ranges::view_base {
int* begin() const;
int* end() const;
};
// Test that begin is not const
template <class T>
concept HasBegin = requires(T t) { t.begin(); };
static_assert(HasBegin<std::ranges::split_view<View, View>>);
static_assert(!HasBegin<const std::ranges::split_view<View, View>>);
template <template <class> class MakeIter>
constexpr void testOne() {
constexpr auto make_subrange = []<class T, std::size_t N>(T(&buffer)[N]) {
using Iter = MakeIter<T*>;
using Sent = sentinel_wrapper<Iter>;
return std::ranges::subrange<Iter, Sent>{Iter{buffer}, Sent{Iter{buffer + N}}};
};
using Iter = MakeIter<int*>;
using Sent = sentinel_wrapper<Iter>;
using Range = std::ranges::subrange<Iter, Sent>;
// empty view
{
std::array<int, 0> a;
Range range{Iter{a.data()}, Sent{Iter{a.data() + a.size()}}};
std::ranges::split_view sv{std::move(range), 1};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == range.end());
}
// empty pattern
{
int buffer[] = {1, 2, 3};
auto range = make_subrange(buffer);
std::ranges::split_view sv{std::move(range), std::views::empty<int>};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == std::next(range.begin()));
}
// empty view and empty pattern
{
std::array<int, 0> a;
Range range{Iter{a.data()}, Sent{Iter{a.data() + a.size()}}};
std::ranges::split_view sv{std::move(range), std::views::empty<int>};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == range.end());
}
// pattern found at the beginning
{
int buffer[] = {1, 2, 3};
auto range = make_subrange(buffer);
int pattern[] = {1, 2};
std::ranges::split_view sv{range, pattern};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == range.begin());
}
// pattern found in the middle
{
int buffer[] = {1, 2, 3, 4};
auto range = make_subrange(buffer);
int pattern[] = {2, 3};
std::ranges::split_view sv{range, pattern};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == std::next(range.begin()));
}
// pattern found at the end
{
int buffer[] = {1, 2, 3};
auto range = make_subrange(buffer);
int pattern[] = {2, 3};
std::ranges::split_view sv{range, pattern};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == std::next(range.begin()));
}
// pattern not found
{
int buffer[] = {1, 2, 3};
auto range = make_subrange(buffer);
int pattern[] = {1, 3};
std::ranges::split_view sv{range, pattern};
auto it = sv.begin();
auto firstRange = *it;
assert(firstRange.begin() == range.begin());
assert(firstRange.end() == range.end());
}
// Make sure that we cache the result of begin() on subsequent calls
{
struct Foo {
int& equalsCalledTimes;
constexpr bool operator==(const Foo&) const {
++equalsCalledTimes;
return true;
}
};
int equalsCalledTimes = 0;
Foo buffer[] = {Foo{equalsCalledTimes}, Foo{equalsCalledTimes}};
auto range = make_subrange(buffer);
std::ranges::split_view sv{range, Foo{equalsCalledTimes}};
assert(equalsCalledTimes == 0);
[[maybe_unused]] auto it1 = sv.begin();
auto calledTimesAfterFirstBegin = equalsCalledTimes;
assert(calledTimesAfterFirstBegin != 0);
for (int i = 0; i < 10; ++i) {
[[maybe_unused]] auto it2 = sv.begin();
assert(equalsCalledTimes == calledTimesAfterFirstBegin);
}
}
}
constexpr bool test() {
testOne<forward_iterator>();
testOne<bidirectional_iterator>();
testOne<random_access_iterator>();
testOne<contiguous_iterator>();
testOne<std::type_identity_t>();
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|