1a2b3ab8fSHui //===----------------------------------------------------------------------===//
2a2b3ab8fSHui //
3a2b3ab8fSHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a2b3ab8fSHui // See https://llvm.org/LICENSE.txt for license information.
5a2b3ab8fSHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a2b3ab8fSHui //
7a2b3ab8fSHui //===----------------------------------------------------------------------===//
8a2b3ab8fSHui
9a2b3ab8fSHui // UNSUPPORTED: c++03, c++11, c++14, c++17
10a2b3ab8fSHui
11a2b3ab8fSHui // template <input_range Range>
12a2b3ab8fSHui // requires constructible_from<View, views::all_t<Range>> &&
13a2b3ab8fSHui // constructible_from<Pattern, single_view<range_value_t<Range>>>
14*40aaa272SHristo Hristov // constexpr split_view(Range&& r, range_value_t<Range> e); // explicit since C++23
15a2b3ab8fSHui
16a2b3ab8fSHui #include <algorithm>
17a2b3ab8fSHui #include <cassert>
18a2b3ab8fSHui #include <ranges>
19a2b3ab8fSHui #include <string>
20a2b3ab8fSHui #include <string_view>
21a2b3ab8fSHui #include <type_traits>
22a2b3ab8fSHui #include <utility>
23a2b3ab8fSHui
24*40aaa272SHristo Hristov #include "test_convertible.h"
25*40aaa272SHristo Hristov #include "test_macros.h"
26*40aaa272SHristo Hristov
27a2b3ab8fSHui struct Counting {
28a2b3ab8fSHui int* times_copied = nullptr;
29a2b3ab8fSHui int* times_moved = nullptr;
30a2b3ab8fSHui
CountingCounting31a2b3ab8fSHui constexpr Counting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {}
32a2b3ab8fSHui
CountingCounting33a2b3ab8fSHui constexpr Counting(const Counting& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {
34a2b3ab8fSHui ++(*times_copied);
35a2b3ab8fSHui }
CountingCounting36a2b3ab8fSHui constexpr Counting(Counting&& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {
37a2b3ab8fSHui ++(*times_moved);
38a2b3ab8fSHui }
39a2b3ab8fSHui
40a2b3ab8fSHui constexpr Counting& operator=(const Counting&) = default;
41a2b3ab8fSHui constexpr Counting& operator=(Counting&&) = default;
42a2b3ab8fSHui };
43a2b3ab8fSHui
44a2b3ab8fSHui struct ElementWithCounting : Counting {
45a2b3ab8fSHui using Counting::Counting;
46a2b3ab8fSHui
operator ==ElementWithCounting47a2b3ab8fSHui constexpr bool operator==(const ElementWithCounting&) const { return true; }
48a2b3ab8fSHui };
49a2b3ab8fSHui
50a2b3ab8fSHui struct RangeWithCounting : Counting {
51a2b3ab8fSHui using Counting::Counting;
52a2b3ab8fSHui
beginRangeWithCounting53a2b3ab8fSHui constexpr const ElementWithCounting* begin() const { return nullptr; }
endRangeWithCounting54a2b3ab8fSHui constexpr const ElementWithCounting* end() const { return nullptr; }
55a2b3ab8fSHui
operator ==RangeWithCounting56a2b3ab8fSHui constexpr bool operator==(const RangeWithCounting&) const { return true; }
57a2b3ab8fSHui };
58a2b3ab8fSHui static_assert(std::ranges::forward_range<RangeWithCounting>);
59a2b3ab8fSHui static_assert(!std::ranges::view<RangeWithCounting>);
60a2b3ab8fSHui
61a2b3ab8fSHui struct StrView : std::ranges::view_base {
62a2b3ab8fSHui std::string buffer_;
63a2b3ab8fSHui
64a2b3ab8fSHui template <std::ranges::range R>
StrViewStrView65a2b3ab8fSHui constexpr StrView(R&& r) : buffer_(std::ranges::begin(r), std::ranges::end(r)) {}
beginStrView66a2b3ab8fSHui constexpr const char* begin() const { return buffer_.data(); }
endStrView67a2b3ab8fSHui constexpr const char* end() const { return buffer_.data() + buffer_.size(); }
operator ==StrView68a2b3ab8fSHui constexpr bool operator==(const StrView& rhs) const { return buffer_ == rhs.buffer_; }
69a2b3ab8fSHui };
70a2b3ab8fSHui static_assert(std::ranges::random_access_range<StrView>);
71a2b3ab8fSHui static_assert(std::ranges::view<StrView>);
72a2b3ab8fSHui static_assert(std::is_copy_constructible_v<StrView>);
73a2b3ab8fSHui
74*40aaa272SHristo Hristov // SFINAE tests.
75*40aaa272SHristo Hristov
76*40aaa272SHristo Hristov #if TEST_STD_VER >= 23
77*40aaa272SHristo Hristov
78*40aaa272SHristo Hristov static_assert(
79*40aaa272SHristo Hristov !test_convertible<std::ranges::split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(),
80*40aaa272SHristo Hristov "This constructor must be explicit");
81*40aaa272SHristo Hristov
82*40aaa272SHristo Hristov # else
83*40aaa272SHristo Hristov
84*40aaa272SHristo Hristov static_assert(
85*40aaa272SHristo Hristov test_convertible<std::ranges::split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(),
86*40aaa272SHristo Hristov "This constructor must not be explicit");
87*40aaa272SHristo Hristov
88*40aaa272SHristo Hristov #endif // TEST_STD_VER >= 23
89*40aaa272SHristo Hristov
test()90a2b3ab8fSHui constexpr bool test() {
91a2b3ab8fSHui {
92a2b3ab8fSHui using V = std::ranges::split_view<StrView, StrView>;
93a2b3ab8fSHui
94a2b3ab8fSHui // Calling the constructor with `(std::string, range_value_t)`.
95a2b3ab8fSHui {
96a2b3ab8fSHui std::string input("abc def");
97a2b3ab8fSHui V v(input, ' ');
98a2b3ab8fSHui assert(v.base() == input);
99a2b3ab8fSHui assert(std::ranges::equal(*v.begin(), std::string_view("abc")));
100a2b3ab8fSHui }
101a2b3ab8fSHui
102a2b3ab8fSHui // Calling the constructor with `(StrView, range_value_t)`.
103a2b3ab8fSHui {
104a2b3ab8fSHui StrView input("abc def");
105a2b3ab8fSHui V v(input, ' ');
106a2b3ab8fSHui assert(v.base() == input);
107a2b3ab8fSHui assert(std::ranges::equal(*v.begin(), std::string_view("abc")));
108a2b3ab8fSHui }
109a2b3ab8fSHui
110a2b3ab8fSHui struct Empty {};
111a2b3ab8fSHui static_assert(!std::is_constructible_v<V, Empty, std::string_view>);
112a2b3ab8fSHui static_assert(!std::is_constructible_v<V, std::string_view, Empty>);
113a2b3ab8fSHui }
114a2b3ab8fSHui
115a2b3ab8fSHui // Make sure the arguments are moved, not copied.
116a2b3ab8fSHui {
117a2b3ab8fSHui using Range = RangeWithCounting;
118a2b3ab8fSHui using Element = ElementWithCounting;
119a2b3ab8fSHui using Pattern = std::ranges::single_view<Element>;
120a2b3ab8fSHui
121a2b3ab8fSHui // Arguments are lvalues.
122a2b3ab8fSHui {
123a2b3ab8fSHui using View = std::ranges::ref_view<Range>;
124a2b3ab8fSHui
125a2b3ab8fSHui int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0;
126a2b3ab8fSHui Range range(range_copied, range_moved);
127a2b3ab8fSHui Element element(element_copied, element_moved);
128a2b3ab8fSHui
129a2b3ab8fSHui std::ranges::split_view<View, Pattern> v(range, element);
130a2b3ab8fSHui assert(range_copied == 0); // `ref_view` does neither copy...
131a2b3ab8fSHui assert(range_moved == 0); // ...nor move the element.
132a2b3ab8fSHui assert(element_copied == 1); // The element is copied into the argument...
133a2b3ab8fSHui assert(element_moved == 1); // ...and moved into the member variable.
134a2b3ab8fSHui }
135a2b3ab8fSHui
136a2b3ab8fSHui // Arguments are rvalues.
137a2b3ab8fSHui {
138a2b3ab8fSHui using View = std::ranges::owning_view<Range>;
139a2b3ab8fSHui
140a2b3ab8fSHui int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0;
141a2b3ab8fSHui std::ranges::split_view<View, Pattern> v(
142a2b3ab8fSHui Range(range_copied, range_moved), Element(element_copied, element_moved));
143a2b3ab8fSHui assert(range_copied == 0);
144a2b3ab8fSHui assert(range_moved == 1); // `owning_view` moves the given argument.
145a2b3ab8fSHui assert(element_copied == 0);
146a2b3ab8fSHui assert(element_moved == 1);
147a2b3ab8fSHui }
148a2b3ab8fSHui }
149a2b3ab8fSHui
150a2b3ab8fSHui return true;
151a2b3ab8fSHui }
152a2b3ab8fSHui
main(int,char **)153a2b3ab8fSHui int main(int, char**) {
154a2b3ab8fSHui test();
155a2b3ab8fSHui static_assert(test());
156a2b3ab8fSHui
157a2b3ab8fSHui return 0;
158a2b3ab8fSHui }
159