1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // constexpr iter_difference_t<I> count() const noexcept;
15 #include "test_macros.h"
16 #include "test_iterators.h"
18 struct InputOrOutputArchetype
{
19 using difference_type
= int;
23 constexpr int operator*() { return *ptr
; }
24 constexpr void operator++(int) { ++ptr
; }
25 constexpr InputOrOutputArchetype
& operator++() { ++ptr
; return *this; }
28 constexpr bool test() {
29 int buffer
[8] = {1, 2, 3, 4, 5, 6, 7, 8};
32 std::counted_iterator
iter(cpp20_input_iterator
<int*>{buffer
}, 8);
33 for (int i
= 8; i
!= 0; --i
, ++iter
)
34 assert(iter
.count() == i
);
36 static_assert(noexcept(iter
.count()));
39 std::counted_iterator
iter(forward_iterator
<int*>{buffer
}, 8);
40 for (int i
= 8; i
!= 0; --i
, ++iter
)
41 assert(iter
.count() == i
);
43 static_assert(noexcept(iter
.count()));
46 std::counted_iterator
iter(contiguous_iterator
<int*>{buffer
}, 8);
47 for (int i
= 8; i
!= 0; --i
, ++iter
)
48 assert(iter
.count() == i
);
51 std::counted_iterator
iter(InputOrOutputArchetype
{buffer
+ 2}, 6);
52 assert(iter
.count() == 6);
57 const std::counted_iterator
iter(cpp20_input_iterator
<int*>{buffer
}, 8);
58 assert(iter
.count() == 8);
61 const std::counted_iterator
iter(forward_iterator
<int*>{buffer
+ 1}, 7);
62 assert(iter
.count() == 7);
65 const std::counted_iterator
iter(contiguous_iterator
<int*>{buffer
+ 2}, 6);
66 assert(iter
.count() == 6);
69 const std::counted_iterator
iter(InputOrOutputArchetype
{buffer
+ 2}, 6);
70 assert(iter
.count() == 6);
76 int main(int, char**) {
78 static_assert(test());