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 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 // UNSUPPORTED: libcpp-has-no-incomplete-ranges 11 12 // std::views::lazy_split 13 14 #include <ranges> 15 16 #include <array> 17 #include <cassert> 18 #include <concepts> 19 #include <string_view> 20 #include <utility> 21 22 #include "test_iterators.h" 23 #include "types.h" 24 25 template <class View, class T> 26 concept CanBePiped = requires (View&& view, T&& t) { 27 { std::forward<View>(view) | std::forward<T>(t) }; 28 }; 29 30 struct SomeView : std::ranges::view_base { 31 const std::string_view* v_; 32 constexpr SomeView(const std::string_view& v) : v_(&v) {} 33 constexpr auto begin() const { return v_->begin(); } 34 constexpr auto end() const { return v_->end(); } 35 }; 36 37 struct NotAView { }; 38 39 static_assert(!std::is_invocable_v<decltype(std::views::lazy_split)>); 40 static_assert(!std::is_invocable_v<decltype(std::views::lazy_split), SomeView, NotAView>); 41 static_assert(!std::is_invocable_v<decltype(std::views::lazy_split), NotAView, SomeView>); 42 static_assert( std::is_invocable_v<decltype(std::views::lazy_split), SomeView, SomeView>); 43 44 static_assert( CanBePiped<SomeView&, decltype(std::views::lazy_split)>); 45 static_assert( CanBePiped<char(&)[10], decltype(std::views::lazy_split)>); 46 static_assert(!CanBePiped<char(&&)[10], decltype(std::views::lazy_split)>); 47 static_assert(!CanBePiped<NotAView, decltype(std::views::lazy_split)>); 48 49 static_assert(std::same_as<decltype(std::views::lazy_split), decltype(std::ranges::views::lazy_split)>); 50 51 constexpr bool test() { 52 std::string_view input = "abc"; 53 std::string_view sep = "a"; 54 55 // Test that `std::views::lazy_split` is a range adaptor. 56 57 // Test `views::lazy_split(input, sep)`. 58 { 59 SomeView view(input); 60 61 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>; 62 std::same_as<Result> decltype(auto) result = std::views::lazy_split(view, sep); 63 assert(result.base().begin() == input.begin()); 64 assert(result.base().end() == input.end()); 65 } 66 67 // Test `views::lazy_split(sep)(input)`. 68 { 69 SomeView view(input); 70 71 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>; 72 std::same_as<Result> decltype(auto) result = std::views::lazy_split(sep)(view); 73 assert(result.base().begin() == input.begin()); 74 assert(result.base().end() == input.end()); 75 } 76 77 // Test `view | views::lazy_split`. 78 { 79 SomeView view(input); 80 81 using Result = std::ranges::lazy_split_view<SomeView, std::string_view>; 82 std::same_as<Result> decltype(auto) result = view | std::views::lazy_split(sep); 83 assert(result.base().begin() == input.begin()); 84 assert(result.base().end() == input.end()); 85 } 86 87 // Test `adaptor | views::lazy_split`. 88 { 89 SomeView view(input); 90 auto f = [](char c) { return c; }; 91 auto partial = std::views::transform(f) | std::views::lazy_split(sep); 92 93 using Result = std::ranges::lazy_split_view<std::ranges::transform_view<SomeView, decltype(f)>, std::string_view>; 94 std::same_as<Result> decltype(auto) result = partial(view); 95 assert(result.base().base().begin() == input.begin()); 96 assert(result.base().base().end() == input.end()); 97 } 98 99 // Test `views::lazy_split | adaptor`. 100 { 101 SomeView view(input); 102 auto f = [](auto v) { return v; }; 103 auto partial = std::views::lazy_split(sep) | std::views::transform(f); 104 105 using Result = std::ranges::transform_view<std::ranges::lazy_split_view<SomeView, std::string_view>, decltype(f)>; 106 std::same_as<Result> decltype(auto) result = partial(view); 107 assert(result.base().base().begin() == input.begin()); 108 assert(result.base().base().end() == input.end()); 109 } 110 111 // Test that one can call `std::views::lazy_split` with arbitrary stuff, as long as we 112 // don't try to actually complete the call by passing it a range. 113 // 114 // That makes no sense and we can't do anything with the result, but it's valid. 115 { 116 struct X { }; 117 [[maybe_unused]] auto partial = std::views::lazy_split(X{}); 118 } 119 120 return true; 121 } 122 123 int main(int, char**) { 124 test(); 125 static_assert(test()); 126 127 return 0; 128 } 129