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-no-concepts
11 // UNSUPPORTED: libcpp-has-no-incomplete-ranges
12 
13 // constexpr auto end();
14 // constexpr auto end() const requires range<const V>;
15 
16 #include <ranges>
17 
18 #include <cassert>
19 #include <concepts>
20 #include <utility>
21 
22 #include "test_macros.h"
23 #include "test_iterators.h"
24 #include "types.h"
25 
26 constexpr bool test() {
27   int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
28 
29   {
30     SizedRandomAccessView view{buf, buf + 8};
31     std::ranges::common_view<SizedRandomAccessView> common(view);
32     std::same_as<RandomAccessIter> auto end = common.end(); // Note this should NOT be the sentinel type.
33     assert(end.base() == buf + 8);
34   }
35 
36   // const version
37   {
38     SizedRandomAccessView view{buf, buf + 8};
39     std::ranges::common_view<SizedRandomAccessView> const common(view);
40     std::same_as<RandomAccessIter> auto end = common.end(); // Note this should NOT be the sentinel type.
41     assert(end.base() == buf + 8);
42   }
43 
44   return true;
45 }
46 
47 int main(int, char**) {
48   test();
49   static_assert(test());
50 
51   {
52     int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
53 
54     using CommonForwardIter = std::common_iterator<ForwardIter, sized_sentinel<ForwardIter>>;
55     using CommonIntIter = std::common_iterator<int*, sentinel_wrapper<int*>>;
56 
57     {
58       SizedForwardView view{buf, buf + 8};
59       std::ranges::common_view<SizedForwardView> common(view);
60       std::same_as<CommonForwardIter> auto end = common.end();
61       assert(end == CommonForwardIter(std::ranges::end(view)));
62     }
63     {
64       CopyableView view{buf, buf + 8};
65       std::ranges::common_view<CopyableView> common(view);
66       std::same_as<CommonIntIter> auto end = common.end();
67       assert(end == CommonIntIter(std::ranges::end(view)));
68     }
69 
70     // const versions
71     {
72       SizedForwardView view{buf, buf + 8};
73       std::ranges::common_view<SizedForwardView> const common(view);
74       std::same_as<CommonForwardIter> auto end = common.end();
75       assert(end == CommonForwardIter(std::ranges::end(view)));
76     }
77     {
78       CopyableView view{buf, buf + 8};
79       std::ranges::common_view<CopyableView> const common(view);
80       std::same_as<CommonIntIter> auto end = common.end();
81       assert(end == CommonIntIter(std::ranges::end(view)));
82     }
83   }
84 
85   return 0;
86 }
87