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::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
24struct SomeView : std::ranges::view_base {
25 const std::string_view* v_;
26 constexpr SomeView(const std::string_view& v) : v_(&v) {}
27 constexpr auto begin() const { return v_->begin(); }
28 constexpr auto end() const { return v_->end(); }
29};
30
31struct NotAView { };
32
33static_assert(!std::is_invocable_v<decltype(std::views::split)>);
34static_assert(!std::is_invocable_v<decltype(std::views::split), SomeView, NotAView>);
35static_assert(!std::is_invocable_v<decltype(std::views::split), NotAView, SomeView>);
36static_assert( std::is_invocable_v<decltype(std::views::split), SomeView, SomeView>);
37
38// Regression test for #75002, views::split shouldn't be a range adaptor closure
39static_assert(!CanBePiped<SomeView&, decltype(std::views::split)>);
40static_assert(!CanBePiped<char (&)[10], decltype(std::views::split)>);
41static_assert(!CanBePiped<char (&&)[10], decltype(std::views::split)>);
42static_assert(!CanBePiped<NotAView, decltype(std::views::split)>);
43
44static_assert(CanBePiped<SomeView&, decltype(std::views::split('x'))>);
45static_assert(CanBePiped<char (&)[10], decltype(std::views::split('x'))>);
46static_assert(!CanBePiped<char (&&)[10], decltype(std::views::split('x'))>);
47static_assert(!CanBePiped<NotAView, decltype(std::views::split('x'))>);
48
49static_assert(std::same_as<decltype(std::views::split), decltype(std::ranges::views::split)>);
50
51constexpr bool test() {
52 std::string_view input = "abc";
53 std::string_view sep = "a";
54
55 // Test that `std::views::split` is a range adaptor.
56
57 // Test `views::split(input, sep)`.
58 {
59 SomeView view(input);
60
61 using Result = std::ranges::split_view<SomeView, std::string_view>;
62 std::same_as<Result> decltype(auto) result = std::views::split(view, sep);
63 assert(result.base().begin() == input.begin());
64 assert(result.base().end() == input.end());
65 }
66
67 // Test `views::split(sep)(input)`.
68 {
69 SomeView view(input);
70
71 using Result = std::ranges::split_view<SomeView, std::string_view>;
72 std::same_as<Result> decltype(auto) result = std::views::split(sep)(view);
73 assert(result.base().begin() == input.begin());
74 assert(result.base().end() == input.end());
75 }
76
77 // Test `view | views::split`.
78 {
79 SomeView view(input);
80
81 using Result = std::ranges::split_view<SomeView, std::string_view>;
82 std::same_as<Result> decltype(auto) result = view | std::views::split(sep);
83 assert(result.base().begin() == input.begin());
84 assert(result.base().end() == input.end());
85 }
86
87 // Test `adaptor | views::split`.
88 {
89 SomeView view(input);
90 auto f = [](char c) { return c; };
91 auto partial = std::views::transform(f) | std::views::split(sep);
92
93 using Result = std::ranges::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::split | adaptor`.
100 {
101 SomeView view(input);
102 auto f = [](auto v) { return v; };
103 auto partial = std::views::split(sep) | std::views::transform(f);
104
105 using Result = std::ranges::transform_view<std::ranges::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::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::split(X{});
118 }
119
120 return true;
121}
122
123int main(int, char**) {
124 test();
125 static_assert(test());
126
127 return 0;
128}
129

source code of libcxx/test/std/ranges/range.adaptors/range.split/adaptor.pass.cpp