xref: /llvm-project/libcxx/test/std/ranges/range.adaptors/range.take/ctor.view_count.pass.cpp (revision 40aaa272f145e633b29d5e70a4590cc425801f7e)
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 take_view(V base, range_difference_t<V> count); // explicit since C++23
12 
13 #include <cassert>
14 #include <ranges>
15 
16 #include "test_convertible.h"
17 #include "test_iterators.h"
18 #include "test_macros.h"
19 #include "test_range.h"
20 #include "types.h"
21 
22 // SFINAE tests.
23 
24 #if TEST_STD_VER >= 23
25 
26 static_assert(!test_convertible<std::ranges::take_view<View>, View, std::ranges::range_difference_t<View>>(),
27               "This constructor must be explicit");
28 
29 #else
30 
31 static_assert(test_convertible<std::ranges::take_view<View>, View, std::ranges::range_difference_t<View>>(),
32               "This constructor must be explicit");
33 
34 #endif // TEST_STD_VER >= 23
35 
test()36 constexpr bool test() {
37   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
38 
39   {
40     std::ranges::take_view<CopyableView> tv(CopyableView{buffer}, 0);
41     assert(tv.base().ptr_ == buffer);
42     assert(tv.begin() == tv.end()); // Checking we have correct size.
43   }
44 
45   {
46     std::ranges::take_view<MoveOnlyView> tv(MoveOnlyView{buffer}, 1);
47     assert(std::move(tv).base().ptr_ == buffer);
48     assert(std::ranges::next(tv.begin(), 1) == tv.end()); // Checking we have correct size.
49   }
50 
51   {
52     const std::ranges::take_view<CopyableView> tv(CopyableView{buffer}, 2);
53     assert(tv.base().ptr_ == buffer);
54     assert(std::ranges::next(tv.begin(), 2) == tv.end()); // Checking we have correct size.
55   }
56 
57   return true;
58 }
59 
main(int,char **)60 int main(int, char**) {
61   test();
62   static_assert(test());
63 
64   return 0;
65 }
66