|
| 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 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +// cuda::std::views::drop_while |
| 11 | + |
| 12 | +// #include <cuda/std/algorithm> |
| 13 | +#include <cuda/std/cassert> |
| 14 | +#include <cuda/std/ranges> |
| 15 | +#include <cuda/std/type_traits> |
| 16 | +#include <cuda/std/utility> |
| 17 | + |
| 18 | +#include "test_macros.h" |
| 19 | + |
| 20 | +struct Pred |
| 21 | +{ |
| 22 | + TEST_FUNC constexpr bool operator()(int i) const |
| 23 | + { |
| 24 | + return i < 3; |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +struct Foo |
| 29 | +{}; |
| 30 | + |
| 31 | +template <class T> |
| 32 | +struct BufferView : cuda::std::ranges::view_base |
| 33 | +{ |
| 34 | + T* buffer_; |
| 35 | + cuda::std::size_t size_; |
| 36 | + |
| 37 | + template <cuda::std::size_t N> |
| 38 | + TEST_FUNC constexpr BufferView(T (&b)[N]) |
| 39 | + : buffer_(b) |
| 40 | + , size_(N) |
| 41 | + {} |
| 42 | +}; |
| 43 | + |
| 44 | +using IntBufferView = BufferView<int>; |
| 45 | + |
| 46 | +struct MoveOnlyView : IntBufferView |
| 47 | +{ |
| 48 | +#if TEST_COMPILER(NVRTC) |
| 49 | + MoveOnlyView() noexcept = default; |
| 50 | + |
| 51 | + template <class T> |
| 52 | + TEST_FUNC constexpr MoveOnlyView(T&& arr) noexcept(noexcept(IntBufferView(cuda::std::declval<T>()))) |
| 53 | + : IntBufferView(cuda::std::forward<T>(arr)) |
| 54 | + {} |
| 55 | +#else |
| 56 | + using IntBufferView::IntBufferView; |
| 57 | +#endif |
| 58 | + |
| 59 | + MoveOnlyView(const MoveOnlyView&) = delete; |
| 60 | + MoveOnlyView& operator=(const MoveOnlyView&) = delete; |
| 61 | + MoveOnlyView(MoveOnlyView&&) = default; |
| 62 | + MoveOnlyView& operator=(MoveOnlyView&&) = default; |
| 63 | + TEST_FUNC constexpr const int* begin() const |
| 64 | + { |
| 65 | + return buffer_; |
| 66 | + } |
| 67 | + TEST_FUNC constexpr const int* end() const |
| 68 | + { |
| 69 | + return buffer_ + size_; |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +static_assert(!cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while))>); |
| 74 | +static_assert(cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), int>); |
| 75 | +static_assert(cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), Pred>); |
| 76 | +static_assert(!cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), int, Pred>); |
| 77 | +static_assert(cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), int (&)[2], Pred>); |
| 78 | +static_assert(!cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), Foo (&)[2], Pred>); |
| 79 | +static_assert(cuda::std::is_invocable_v<decltype((cuda::std::views::drop_while)), MoveOnlyView, Pred>); |
| 80 | + |
| 81 | +template <class View, class T> |
| 82 | +_CCCL_CONCEPT CanBePiped = |
| 83 | + _CCCL_REQUIRES_EXPR((View, T), View&& view, T&& t)((cuda::std::forward<View>(view) | cuda::std::forward<T>(t))); |
| 84 | + |
| 85 | +static_assert(!CanBePiped<MoveOnlyView, decltype(cuda::std::views::drop_while)>); |
| 86 | +static_assert(CanBePiped<MoveOnlyView, decltype(cuda::std::views::drop_while(Pred{}))>); |
| 87 | +static_assert(CanBePiped<int (&)[2], decltype(cuda::std::views::drop_while(Pred{}))>); |
| 88 | +static_assert(!CanBePiped<int, decltype(cuda::std::views::drop_while(Pred{}))>); |
| 89 | +static_assert(!CanBePiped<Foo (&)[2], decltype(cuda::std::views::drop_while(Pred{}))>); |
| 90 | + |
| 91 | +template <class Range, class Expected> |
| 92 | +TEST_FUNC constexpr bool equal(Range&& range, Expected&& expected) |
| 93 | +{ |
| 94 | + auto irange = range.begin(); |
| 95 | + auto iexpected = cuda::std::begin(expected); |
| 96 | + for (; irange != range.end(); ++irange, ++iexpected) |
| 97 | + { |
| 98 | + if (*irange != *iexpected) |
| 99 | + { |
| 100 | + return false; |
| 101 | + } |
| 102 | + } |
| 103 | + return true; |
| 104 | +} |
| 105 | + |
| 106 | +TEST_FUNC constexpr bool test() |
| 107 | +{ |
| 108 | + int buff[] = {1, 2, 3, 4, 3, 2, 1}; |
| 109 | + |
| 110 | + // Test `views::drop_while(p)(v)` |
| 111 | + { |
| 112 | + using Result = cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>; |
| 113 | + decltype(auto) result = cuda::std::views::drop_while(Pred{})(MoveOnlyView{buff}); |
| 114 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 115 | + auto expected = {3, 4, 3, 2, 1}; |
| 116 | + assert(equal(result, expected)); |
| 117 | + } |
| 118 | + { |
| 119 | + auto const partial = cuda::std::views::drop_while(Pred{}); |
| 120 | + using Result = cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>; |
| 121 | + decltype(auto) result = partial(MoveOnlyView{buff}); |
| 122 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 123 | + auto expected = {3, 4, 3, 2, 1}; |
| 124 | + assert(equal(result, expected)); |
| 125 | + } |
| 126 | + |
| 127 | + // Test `v | views::drop_while(p)` |
| 128 | + { |
| 129 | + using Result = cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>; |
| 130 | + decltype(auto) result = MoveOnlyView{buff} | cuda::std::views::drop_while(Pred{}); |
| 131 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 132 | + auto expected = {3, 4, 3, 2, 1}; |
| 133 | + assert(equal(result, expected)); |
| 134 | + } |
| 135 | + { |
| 136 | + auto const partial = cuda::std::views::drop_while(Pred{}); |
| 137 | + using Result = cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>; |
| 138 | + decltype(auto) result = MoveOnlyView{buff} | partial; |
| 139 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 140 | + auto expected = {3, 4, 3, 2, 1}; |
| 141 | + assert(equal(result, expected)); |
| 142 | + } |
| 143 | + |
| 144 | + // Test `views::drop_while(v, p)` |
| 145 | + { |
| 146 | + using Result = cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>; |
| 147 | + decltype(auto) result = cuda::std::views::drop_while(MoveOnlyView{buff}, Pred{}); |
| 148 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 149 | + auto expected = {3, 4, 3, 2, 1}; |
| 150 | + assert(equal(result, expected)); |
| 151 | + } |
| 152 | + |
| 153 | + // Test adaptor | adaptor |
| 154 | + { |
| 155 | + struct Pred2 |
| 156 | + { |
| 157 | + TEST_FUNC constexpr bool operator()(int i) const |
| 158 | + { |
| 159 | + return i < 4; |
| 160 | + } |
| 161 | + }; |
| 162 | + auto const partial = cuda::std::views::drop_while(Pred{}) | cuda::std::views::drop_while(Pred2{}); |
| 163 | + using Result = cuda::std::ranges::drop_while_view<cuda::std::ranges::drop_while_view<MoveOnlyView, Pred>, Pred2>; |
| 164 | + decltype(auto) result = MoveOnlyView{buff} | partial; |
| 165 | + static_assert(cuda::std::same_as<decltype(result), Result>); |
| 166 | + auto expected = {4, 3, 2, 1}; |
| 167 | + assert(equal(result, expected)); |
| 168 | + } |
| 169 | + return true; |
| 170 | +} |
| 171 | + |
| 172 | +int main(int, char**) |
| 173 | +{ |
| 174 | + test(); |
| 175 | +#if TEST_STD_VER >= 2020 && defined(_CCCL_BUILTIN_ADDRESSOF) |
| 176 | + static_assert(test()); |
| 177 | +#endif // TEST_STD_VER >= 2020 && defined(_CCCL_BUILTIN_ADDRESSOF) |
| 178 | + |
| 179 | + return 0; |
| 180 | +} |
0 commit comments