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