Files
llvm-project/libcxx/test/std/ranges/range.adaptors/range.stride.view/base.pass.cpp
Will Hawkins 2f28e1db53 [libc++] Implement P1899 ranges::stride_view (#65200)
Implement `ranges::stride_view` in libc++. This PR was migrated from
Phabricator (https://reviews.llvm.org/D156924).

Closes #105198

Co-authored-by: Louis Dionne <ldionne.2@gmail.com>
Co-authored-by: A. Jiang <de34@live.cn>
2026-04-25 06:51:37 -04:00

108 lines
3.2 KiB
C++

//===----------------------------------------------------------------------===//
//
// 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, c++20
// constexpr V base() const& requires copy_constructible<V>;
// constexpr V base() &&;
#include <cassert>
#include <concepts>
#include <ranges>
#include <utility>
#include "test_iterators.h"
#include "types.h"
template <typename T>
constexpr bool has_lvalue_qualified_base(T&& t) {
// Thanks to forwarding references, t's type is
// preserved from the caller. No matter the type of
// the argument, when it is used here, t is an l value
// (after all, it has a name). Therefore, this code
// will test whether the l value const-ref-qualified
// version of base is callable.
return requires { t.base(); };
}
using CopyableInputView = CopyableView<cpp17_input_iterator<int*>>;
using MoveOnlyInputView = MoveOnlyView<cpp17_input_iterator<int*>>;
constexpr bool test() {
int buff[] = {1, 2, 3, 4, 5, 6, 7, 8};
constexpr int N = 8;
// l-value ref qualified
// const &
{
const std::ranges::stride_view<CopyableInputView> view(
CopyableInputView(cpp17_input_iterator<int*>(buff), cpp17_input_iterator<int*>(buff + N)), 1);
static_assert(has_lvalue_qualified_base(view));
std::same_as<CopyableInputView> decltype(auto) s = view.base();
assert(*s.begin() == *buff);
}
// &
{
std::ranges::stride_view<CopyableInputView> view(
CopyableInputView(cpp17_input_iterator<int*>(buff), cpp17_input_iterator<int*>(buff + N)), 1);
std::same_as<CopyableInputView> decltype(auto) s = view.base();
assert(*s.begin() == *buff);
static_assert(has_lvalue_qualified_base(view));
}
// r-value ref qualified
// &&
{
std::ranges::stride_view<CopyableInputView> view(
CopyableInputView(cpp17_input_iterator<int*>(buff), cpp17_input_iterator<int*>(buff + N)), 1);
static_assert(has_lvalue_qualified_base(view));
std::same_as<CopyableInputView> decltype(auto) s = std::move(view).base();
assert(*s.begin() == *buff);
}
// const &&
{
const std::ranges::stride_view<CopyableInputView> view(
CopyableInputView(cpp17_input_iterator<int*>(buff), cpp17_input_iterator<int*>(buff + N)), 1);
static_assert(has_lvalue_qualified_base(view));
std::same_as<CopyableInputView> decltype(auto) s = std::move(view).base();
assert(*s.begin() == *buff);
}
// &&
{
std::ranges::stride_view<MoveOnlyInputView> view(
MoveOnlyInputView(cpp17_input_iterator<int*>(buff), cpp17_input_iterator<int*>(buff + N)), 1);
// Because the base of the stride view is move only,
// the const & version is not applicable and, therefore,
// there is no l-value qualified base method.
static_assert(!has_lvalue_qualified_base(view));
std::same_as<MoveOnlyInputView> decltype(auto) s = std::move(view).base();
assert(*s.begin() == *buff);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}