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// template<borrowed_range R>
12// requires convertible-to-non-slicing<iterator_t<R>, I> &&
13// convertible_to<sentinel_t<R>, S>
14// constexpr subrange(R&& r, make-unsigned-like-t<iter_difference_t<I>> n)
15// requires (K == subrange_kind::sized);
16
17#include <ranges>
18#include <cassert>
19
20struct BorrowedRange {
21 constexpr explicit BorrowedRange(int* b, int* e) : begin_(b), end_(e) { }
22 constexpr int* begin() const { return begin_; }
23 constexpr int* end() const { return end_; }
24
25private:
26 int* begin_;
27 int* end_;
28};
29
30namespace std::ranges {
31 template <>
32 inline constexpr bool enable_borrowed_range<::BorrowedRange> = true;
33}
34
35constexpr bool test() {
36 int buff[] = {1, 2, 3, 4, 5, 6, 7, 8};
37 using Subrange = std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>;
38
39 // Test with an empty range
40 {
41 BorrowedRange range(buff, buff);
42 Subrange subrange(range, 0);
43 assert(subrange.size() == 0);
44 }
45
46 // Test with non-empty ranges
47 {
48 BorrowedRange range(buff, buff + 1);
49 Subrange subrange(range, 1);
50 assert(subrange.size() == 1);
51 }
52 {
53 BorrowedRange range(buff, buff + 2);
54 Subrange subrange(range, 2);
55 assert(subrange[0] == 1);
56 assert(subrange[1] == 2);
57 assert(subrange.size() == 2);
58 }
59 {
60 BorrowedRange range(buff, buff + 8);
61 Subrange subrange(range, 8);
62 assert(subrange[0] == 1);
63 assert(subrange[1] == 2);
64 // ...
65 assert(subrange[7] == 8);
66 assert(subrange.size() == 8);
67 }
68
69 return true;
70}
71
72int main(int, char**) {
73 test();
74 static_assert(test());
75
76 return 0;
77}
78

source code of libcxx/test/std/ranges/range.utility/range.subrange/ctor.range_size.pass.cpp