xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.lazy.split/ctor.view.pass.cpp (revision 40aaa272f145e633b29d5e70a4590cc425801f7e)
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 lazy_split_view(View base, Pattern pattern); // explicit since C++23
12 
13 #include <cassert>
14 #include <ranges>
15 #include <string_view>
16 #include <utility>
17 
18 #include "test_convertible.h"
19 #include "types.h"
20 
21 struct ViewWithCounting : std::ranges::view_base {
22   int* times_copied = nullptr;
23   int* times_moved = nullptr;
24 
ViewWithCountingViewWithCounting25   constexpr ViewWithCounting(int& copies_ctr, int& moves_ctr) : times_copied(&copies_ctr), times_moved(&moves_ctr) {}
26 
ViewWithCountingViewWithCounting27   constexpr ViewWithCounting(const ViewWithCounting& rhs)
28     : times_copied(rhs.times_copied)
29     , times_moved(rhs.times_moved) {
30     ++(*times_copied);
31   }
ViewWithCountingViewWithCounting32   constexpr ViewWithCounting(ViewWithCounting&& rhs)
33     : times_copied(rhs.times_copied)
34     , times_moved(rhs.times_moved) {
35     ++(*times_moved);
36   }
37 
beginViewWithCounting38   constexpr const char* begin() const { return nullptr; }
endViewWithCounting39   constexpr const char* end() const { return nullptr; }
40 
41   constexpr ViewWithCounting& operator=(const ViewWithCounting&) = default;
42   constexpr ViewWithCounting& operator=(ViewWithCounting&&) = default;
operator ==ViewWithCounting43   constexpr bool operator==(const ViewWithCounting&) const { return true; }
44 };
45 
46 static_assert(std::ranges::forward_range<ViewWithCounting>);
47 static_assert(std::ranges::view<ViewWithCounting>);
48 
49 using View = ViewWithCounting;
50 using Pattern = ViewWithCounting;
51 
52 // SFINAE tests.
53 
54 #if TEST_STD_VER >= 23
55 
56 static_assert(!test_convertible<std::ranges::lazy_split_view<View, Pattern>, View, Pattern>(),
57               "This constructor must be explicit");
58 
59 #else
60 
61 static_assert( test_convertible<std::ranges::lazy_split_view<View, Pattern>, View, Pattern>(),
62               "This constructor must not be explicit");
63 
64 #endif // TEST_STD_VER >= 23
65 
test()66 constexpr bool test() {
67   // Calling the constructor with `(ForwardView, ForwardView)`.
68   {
69     CopyableView input = "abc def";
70     std::ranges::lazy_split_view<CopyableView, CopyableView> v(input, " ");
71     assert(v.base() == input);
72   }
73 
74   // Calling the constructor with `(InputView, TinyView)`.
75   {
76     InputView input = "abc def";
77     std::ranges::lazy_split_view<InputView, ForwardTinyView> v(input, ' ');
78     // Note: `InputView` isn't equality comparable.
79     (void)v;
80   }
81 
82   // Make sure the arguments are moved, not copied.
83   {
84     // Arguments are lvalues.
85     {
86       int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;
87       View view(view_copied, view_moved);
88       Pattern pattern(pattern_copied, pattern_moved);
89 
90       std::ranges::lazy_split_view<View, Pattern> v(view, pattern);
91       assert(view_copied == 1); // The local variable is copied into the argument.
92       assert(view_moved == 1);
93       assert(pattern_copied == 1);
94       assert(pattern_moved == 1);
95     }
96 
97     // Arguments are rvalues.
98     {
99       int view_copied = 0, view_moved = 0, pattern_copied = 0, pattern_moved = 0;
100       std::ranges::lazy_split_view<View, Pattern> v(
101           View(view_copied, view_moved), Pattern(pattern_copied, pattern_moved));
102       assert(view_copied == 0);
103       assert(view_moved == 1);
104       assert(pattern_copied == 0);
105       assert(pattern_moved == 1);
106     }
107   }
108 
109   return true;
110 }
111 
main(int,char **)112 int main(int, char**) {
113   test();
114   static_assert(test());
115 
116   return 0;
117 }
118