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