xref: /llvm-project/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.size.pass.cpp (revision d2baefae6846765eef6a6dd69d4fdf1082ce29ad)
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 counted_iterator operator-(iter_difference_t<I> n) const
12 //   requires random_access_iterator<I>;
13 
14 #include <iterator>
15 
16 #include "test_macros.h"
17 #include "test_iterators.h"
18 
19 template<class Iter>
20 concept MinusEnabled = requires(Iter& iter) {
21   iter - 1;
22 };
23 
test()24 constexpr bool test() {
25   int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
26 
27   {
28     using Counted = std::counted_iterator<random_access_iterator<int*>>;
29     Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
30     assert(iter - 2 == Counted(random_access_iterator<int*>{buffer}, 8));
31     assert(iter - 0 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
32     assert(iter.count() == 6);
33 
34     ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
35   }
36   {
37     using Counted = std::counted_iterator<random_access_iterator<int*>>;
38     const Counted iter(random_access_iterator<int*>{buffer + 2}, 6);
39     assert(iter - 2 == Counted(random_access_iterator<int*>{buffer}, 8));
40     assert(iter - 0 == Counted(random_access_iterator<int*>{buffer + 2}, 6));
41     assert(iter.count() == 6);
42 
43     ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
44   }
45   {
46     using Counted = std::counted_iterator<contiguous_iterator<int*>>;
47     Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
48     assert(iter - 2 == Counted(contiguous_iterator<int*>{buffer}, 8));
49     assert(iter - 0 == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
50     assert(iter.count() == 6);
51 
52     ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
53   }
54   {
55     using Counted = std::counted_iterator<contiguous_iterator<int*>>;
56     const Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
57     assert(iter - 2 == Counted(contiguous_iterator<int*>{buffer}, 8));
58     assert(iter - 0 == Counted(contiguous_iterator<int*>{buffer + 2}, 6));
59     assert(iter.count() == 6);
60 
61     ASSERT_SAME_TYPE(decltype(iter - 2), Counted);
62   }
63 
64   {
65     static_assert( MinusEnabled<std::counted_iterator<random_access_iterator<int*>>>);
66     static_assert(!MinusEnabled<std::counted_iterator<bidirectional_iterator<int*>>>);
67   }
68 
69   return true;
70 }
71 
main(int,char **)72 int main(int, char**) {
73   test();
74   static_assert(test());
75 
76   return 0;
77 }
78