xref: /llvm-project/libcxx/test/std/ranges/range.utility/range.subrange/primitives.pass.cpp (revision b8cb1dc9ea87faa8e8e9ab7a31710a8c0bb8b084)
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 // class std::ranges::subrange;
12 
13 #include <ranges>
14 
15 #include <cassert>
16 #include "test_iterators.h"
17 #include "types.h"
18 
test()19 constexpr bool test() {
20   int buff[] = {1, 2, 3, 4, 5};
21 
22   {
23     std::ranges::subrange<MoveOnlyForwardIter, int*> a(MoveOnlyForwardIter(buff), buff + 5, 5);
24     assert(base(a.begin()) == buff);
25     assert(!a.empty());
26     assert(a.size() == 5);
27   }
28 
29   {
30     std::ranges::subrange<ForwardIter> b(ForwardIter(nullptr), ForwardIter(nullptr));
31     assert(b.empty());
32   }
33 
34   {
35     std::ranges::subrange<ForwardIter> c{ForwardIter(buff), ForwardIter(buff)};
36     assert(c.empty());
37   }
38 
39   {
40     std::ranges::subrange<ForwardIter> d(ForwardIter(buff), ForwardIter(buff + 1));
41     assert(!d.empty());
42   }
43 
44   {
45     bool minusWasCalled = false;
46     SizedSentinelForwardIter beg(buff, &minusWasCalled), end(buff + 5, &minusWasCalled);
47     std::ranges::subrange<SizedSentinelForwardIter> e(beg, end, 5);
48     assert(!e.empty());
49 
50     // Make sure that operator- is used to calculate size when possible.
51     minusWasCalled = false;
52     assert(e.size() == 5);
53     assert(minusWasCalled);
54   }
55 
56   return true;
57 }
58 
59 
main(int,char **)60 int main(int, char**) {
61   test();
62   static_assert(test());
63 
64   return 0;
65 }
66