xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take/range.take.sentinel/ctor.pass.cpp (revision 0b46606ca5b52aa515c5359ed6f24fb18d825b50)
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 // sentinel() = default;
12 // constexpr explicit sentinel(sentinel_t<Base> end);
13 // constexpr sentinel(sentinel<!Const> s)
14 //   requires Const && convertible_to<sentinel_t<V>, sentinel_t<Base>>;
15 
16 #include <ranges>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "test_iterators.h"
21 #include "../types.h"
22 
test()23 constexpr bool test() {
24   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
25 
26   {
27     // Test the default ctor.
28     using TakeView = std::ranges::take_view<MoveOnlyView>;
29     using Sentinel = std::ranges::sentinel_t<TakeView>;
30     Sentinel s;
31     TakeView tv = TakeView(MoveOnlyView(buffer), 4);
32     assert(tv.begin() + 4 == s);
33   }
34 
35   {
36     // Test the conversion from "sentinel" to "sentinel-to-const".
37     using TakeView      = std::ranges::take_view<MoveOnlyView>;
38     using Sentinel      = std::ranges::sentinel_t<TakeView>;
39     using ConstSentinel = std::ranges::sentinel_t<const TakeView>;
40     static_assert(std::is_convertible_v<Sentinel, ConstSentinel>);
41     TakeView tv      = TakeView(MoveOnlyView(buffer), 4);
42     Sentinel s       = tv.end();
43     ConstSentinel cs = s;
44     cs               = s; // test assignment also
45     assert(tv.begin() + 4 == s);
46     assert(tv.begin() + 4 == cs);
47     assert(std::as_const(tv).begin() + 4 == s);
48     assert(std::as_const(tv).begin() + 4 == cs);
49   }
50 
51   {
52     // Test the constructor from "base-sentinel" to "sentinel".
53     using TakeView             = std::ranges::take_view<MoveOnlyView>;
54     using Sentinel             = std::ranges::sentinel_t<TakeView>;
55     sentinel_wrapper<int*> sw1 = MoveOnlyView(buffer).end();
56     static_assert(std::is_constructible_v<Sentinel, sentinel_wrapper<int*>>);
57     static_assert(!std::is_convertible_v<sentinel_wrapper<int*>, Sentinel>);
58     auto s                                        = Sentinel(sw1);
59     std::same_as<sentinel_wrapper<int*>> auto sw2 = s.base();
60     assert(base(sw2) == base(sw1));
61   }
62 
63   return true;
64 }
65 
main(int,char **)66 int main(int, char**) {
67   test();
68   static_assert(test());
69 
70   return 0;
71 }
72