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 // template <input_range Range> 12 // requires constructible_from<View, views::all_t<Range>> && 13 // constructible_from<Pattern, single_view<range_value_t<Range>>> 14 // constexpr lazy_split_view(Range&& r, range_value_t<Range> e); // explicit since C++23 15 16 #include <ranges> 17 18 #include <cassert> 19 #include <string> 20 #include <string_view> 21 #include <type_traits> 22 #include <utility> 23 24 #include "test_convertible.h" 25 #include "types.h" 26 27 struct ElementWithCounting { 28 int* times_copied = nullptr; 29 int* times_moved = nullptr; 30 31 constexpr ElementWithCounting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {} 32 33 constexpr ElementWithCounting(const ElementWithCounting& rhs) 34 : times_copied(rhs.times_copied) 35 , times_moved(rhs.times_moved) { 36 ++(*times_copied); 37 } 38 constexpr ElementWithCounting(ElementWithCounting&& rhs) 39 : times_copied(rhs.times_copied) 40 , times_moved(rhs.times_moved) { 41 ++(*times_moved); 42 } 43 44 constexpr bool operator==(const ElementWithCounting&) const { return true; } 45 }; 46 47 struct RangeWithCounting { 48 using value_type = ElementWithCounting; 49 50 int* times_copied = nullptr; 51 int* times_moved = nullptr; 52 53 constexpr RangeWithCounting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {} 54 55 constexpr RangeWithCounting(const RangeWithCounting& rhs) 56 : times_copied(rhs.times_copied) 57 , times_moved(rhs.times_moved) { 58 ++(*times_copied); 59 } 60 constexpr RangeWithCounting(RangeWithCounting&& rhs) 61 : times_copied(rhs.times_copied) 62 , times_moved(rhs.times_moved) { 63 ++(*times_moved); 64 } 65 66 constexpr const ElementWithCounting* begin() const { return nullptr; } 67 constexpr const ElementWithCounting* end() const { return nullptr; } 68 69 constexpr RangeWithCounting& operator=(const RangeWithCounting&) = default; 70 constexpr RangeWithCounting& operator=(RangeWithCounting&&) = default; 71 constexpr bool operator==(const RangeWithCounting&) const { return true; } 72 }; 73 static_assert( std::ranges::forward_range<RangeWithCounting>); 74 static_assert(!std::ranges::view<RangeWithCounting>); 75 76 struct StrView : std::ranges::view_base { 77 std::string_view buffer_; 78 constexpr explicit StrView() = default; 79 constexpr StrView(const char* ptr) : buffer_(ptr) {} 80 // Intentionally don't forward to range constructor for std::string_view since 81 // this test needs to work on C++20 as well and the range constructor is only for 82 // C++23 and later. 83 template <std::ranges::range R> 84 constexpr StrView(R&& r) : buffer_(r.begin(), r.end()) {} 85 constexpr std::string_view::const_iterator begin() const { return buffer_.begin(); } 86 constexpr std::string_view::const_iterator end() const { return buffer_.end(); } 87 constexpr bool operator==(const StrView& rhs) const { return buffer_ == rhs.buffer_; } 88 }; 89 static_assert( std::ranges::random_access_range<StrView>); 90 static_assert( std::ranges::view<StrView>); 91 static_assert( std::is_copy_constructible_v<StrView>); 92 93 // SFINAE tests. 94 95 #if TEST_STD_VER >= 23 96 97 static_assert( 98 !test_convertible<std::ranges::lazy_split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(), 99 "This constructor must be explicit"); 100 101 #else 102 103 static_assert( 104 test_convertible<std::ranges::lazy_split_view<StrView, StrView>, StrView, std::ranges::range_value_t<StrView>>(), 105 "This constructor must not be explicit"); 106 107 #endif // TEST_STD_VER >= 23 108 109 constexpr bool test() { 110 { 111 using V = std::ranges::lazy_split_view<StrView, StrView>; 112 113 // Calling the constructor with `(std::string, range_value_t)`. 114 { 115 std::string input; 116 V v(input, ' '); 117 assert(v.base() == input); 118 } 119 120 // Calling the constructor with `(StrView, range_value_t)`. 121 { 122 StrView input("abc def"); 123 V v(input, ' '); 124 assert(v.base() == input); 125 } 126 127 struct Empty {}; 128 static_assert(!std::is_constructible_v<V, Empty, std::string_view>); 129 static_assert(!std::is_constructible_v<V, std::string_view, Empty>); 130 } 131 132 // Make sure the arguments are moved, not copied. 133 { 134 using Range = RangeWithCounting; 135 using Element = ElementWithCounting; 136 using Pattern = std::ranges::single_view<Element>; 137 138 // Arguments are lvalues. 139 { 140 using View = std::ranges::ref_view<Range>; 141 142 int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0; 143 Range range(range_copied, range_moved); 144 Element element(element_copied, element_moved); 145 146 std::ranges::lazy_split_view<View, Pattern> v(range, element); 147 assert(range_copied == 0); // `ref_view` does neither copy... 148 assert(range_moved == 0); // ...nor move the element. 149 assert(element_copied == 1); // The element is copied into the argument... 150 assert(element_moved == 1); // ...and moved into the member variable. 151 } 152 153 // Arguments are rvalues. 154 { 155 using View = std::ranges::owning_view<Range>; 156 157 int range_copied = 0, range_moved = 0, element_copied = 0, element_moved = 0; 158 std::ranges::lazy_split_view<View, Pattern> v( 159 Range(range_copied, range_moved), Element(element_copied, element_moved)); 160 assert(range_copied == 0); 161 assert(range_moved == 1); // `owning_view` moves the given argument. 162 assert(element_copied == 0); 163 assert(element_moved == 1); 164 } 165 } 166 167 return true; 168 } 169 170 int main(int, char**) { 171 test(); 172 static_assert(test()); 173 174 return 0; 175 } 176