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