xref: /llvm-project/libcxx/test/std/iterators/predef.iterators/counted.iterator/minus.eq.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)
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 MinusEqEnabled = 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}, 8));
32     assert(iter.count() == 8);
33 
34     ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&);
35   }
36   {
37     using Counted = std::counted_iterator<contiguous_iterator<int*>>;
38     Counted iter(contiguous_iterator<int*>{buffer + 2}, 6);
39     assert((iter -= 2) == Counted(contiguous_iterator<int*>{buffer}, 8));
40     assert((iter -= 0) == Counted(contiguous_iterator<int*>{buffer}, 8));
41     assert(iter.count() == 8);
42 
43     ASSERT_SAME_TYPE(decltype(iter -= 2), Counted&);
44   }
45   {
46     static_assert( MinusEqEnabled<      std::counted_iterator<random_access_iterator<int*>>>);
47     static_assert(!MinusEqEnabled<const std::counted_iterator<random_access_iterator<int*>>>);
48   }
49 
50 
51   return true;
52 }
53 
main(int,char **)54 int main(int, char**) {
55   test();
56   static_assert(test());
57 
58   return 0;
59 }
60