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 auto size() requires sized_range<V> |
12 | // constexpr auto size() const requires sized_range<const V> |
13 | |
14 | #include <ranges> |
15 | #include <cassert> |
16 | |
17 | #include "test_macros.h" |
18 | #include "test_iterators.h" |
19 | #include "test_range.h" |
20 | #include "types.h" |
21 | |
22 | template<class T> |
23 | concept SizeEnabled = requires(const std::ranges::take_view<T>& tv) { |
24 | tv.size(); |
25 | }; |
26 | |
27 | constexpr bool test() { |
28 | int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
29 | |
30 | { |
31 | static_assert( SizeEnabled<SizedRandomAccessView>); |
32 | static_assert(!SizeEnabled<CopyableView>); |
33 | } |
34 | |
35 | { |
36 | std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView{buffer}, 0); |
37 | assert(tv.size() == 0); |
38 | } |
39 | |
40 | { |
41 | const std::ranges::take_view<SizedRandomAccessView> tv(SizedRandomAccessView{buffer}, 2); |
42 | assert(tv.size() == 2); |
43 | } |
44 | |
45 | { |
46 | std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 4); |
47 | assert(tv.size() == 4); |
48 | } |
49 | |
50 | { |
51 | const std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 6); |
52 | assert(tv.size() == 6); |
53 | } |
54 | |
55 | { |
56 | std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 8); |
57 | assert(tv.size() == 8); |
58 | } |
59 | { |
60 | const std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 8); |
61 | assert(tv.size() == 8); |
62 | } |
63 | |
64 | { |
65 | std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 10); |
66 | assert(tv.size() == 8); |
67 | } |
68 | { |
69 | const std::ranges::take_view<SizedForwardView> tv(SizedForwardView{buffer}, 10); |
70 | assert(tv.size() == 8); |
71 | } |
72 | |
73 | return true; |
74 | } |
75 | |
76 | int main(int, char**) { |
77 | test(); |
78 | static_assert(test()); |
79 | |
80 | return 0; |
81 | } |
82 | |