xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.split/ctor.view.pass.cpp (revision 881718857f6d0189eecd1d7391aa1c3778959a80)
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 // constexpr split_view(View base, Pattern pattern); // explicit since C++23
12 
13 #include <algorithm>
14 #include <cassert>
15 #include <ranges>
16 #include <string_view>
17 #include <utility>
18 
19 #include "test_convertible.h"
20 #include "test_macros.h"
21 
22 struct ViewWithCounting : std::ranges::view_base {
23   int* times_copied = nullptr;
24   int* times_moved  = nullptr;
25 
ViewWithCountingViewWithCounting26   constexpr ViewWithCounting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {}
27 
ViewWithCountingViewWithCounting28   constexpr ViewWithCounting(const ViewWithCounting& rhs)
29       : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {
30     ++(*times_copied);
31   }
ViewWithCountingViewWithCounting32   constexpr ViewWithCounting(ViewWithCounting&& rhs) : times_copied(rhs.times_copied), times_moved(rhs.times_moved) {
33     ++(*times_moved);
34   }
35 
beginViewWithCounting36   constexpr const char* begin() const { return nullptr; }
endViewWithCounting37   constexpr const char* end() const { return nullptr; }
38 
39   constexpr ViewWithCounting& operator=(const ViewWithCounting&) = default;
40   constexpr ViewWithCounting& operator=(ViewWithCounting&&)      = default;
operator ==ViewWithCounting41   constexpr bool operator==(const ViewWithCounting&) const { return true; }
42 };
43 
44 using View    = ViewWithCounting;
45 using Pattern = ViewWithCounting;
46 
47 // SFINAE tests.
48 
49 #if TEST_STD_VER >= 23
50 
51 static_assert(!test_convertible<std::ranges::split_view<View, Pattern>, View, Pattern>(),
52               "This constructor must be explicit");
53 
54 #else
55 
56 static_assert( test_convertible<std::ranges::split_view<View, Pattern>, View, Pattern>(),
57               "This constructor must not be explicit");
58 
59 #endif // TEST_STD_VER >= 23
60 
test()61 constexpr bool test() {
62   {
63     std::string_view input = "abc def";
64     std::ranges::split_view<std::string_view, std::string_view> v(input, " ");
65     assert(v.base() == input);
66     assert(std::ranges::equal(*v.begin(), std::string_view{"abc"}));
67   }
68 
69   // Make sure the arguments are moved, not copied.
70   {
71     // Arguments are lvalues.
72     {
73       int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;
74       View view(view_copied, view_moved);
75       Pattern pattern(pattern_copied, pattern_moved);
76 
77       std::ranges::split_view<View, Pattern> v(view, pattern);
78       assert(view_copied == 1); // The local variable is copied into the argument.
79       assert(view_moved == 1);
80       assert(pattern_copied == 1);
81       assert(pattern_moved == 1);
82     }
83 
84     // Arguments are rvalues.
85     {
86       int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;
87       std::ranges::split_view<View, Pattern> v(View(view_copied, view_moved), Pattern(pattern_copied, pattern_moved));
88       assert(view_copied == 0);
89       assert(view_moved == 1);
90       assert(pattern_copied == 0);
91       assert(pattern_moved == 1);
92     }
93   }
94 
95   return true;
96 }
97 
main(int,char **)98 int main(int, char**) {
99   test();
100   static_assert(test());
101 
102   return 0;
103 }
104