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 iter_difference_t<I> count() const noexcept;
12
13#include <iterator>
14
15#include "test_macros.h"
16#include "test_iterators.h"
17
18struct InputOrOutputArchetype {
19 using difference_type = int;
20
21 int *ptr;
22
23 constexpr int operator*() { return *ptr; }
24 constexpr void operator++(int) { ++ptr; }
25 constexpr InputOrOutputArchetype& operator++() { ++ptr; return *this; }
26};
27
28constexpr bool test() {
29 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
30
31 {
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);
35
36 static_assert(noexcept(iter.count()));
37 }
38 {
39 std::counted_iterator iter(forward_iterator<int*>{buffer}, 8);
40 for (int i = 8; i != 0; --i, ++iter)
41 assert(iter.count() == i);
42
43 static_assert(noexcept(iter.count()));
44 }
45 {
46 std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8);
47 for (int i = 8; i != 0; --i, ++iter)
48 assert(iter.count() == i);
49 }
50 {
51 std::counted_iterator iter(InputOrOutputArchetype{.ptr: buffer + 2}, 6);
52 assert(iter.count() == 6);
53 }
54
55 // Const tests.
56 {
57 const std::counted_iterator iter(cpp20_input_iterator<int*>{buffer}, 8);
58 assert(iter.count() == 8);
59 }
60 {
61 const std::counted_iterator iter(forward_iterator<int*>{buffer + 1}, 7);
62 assert(iter.count() == 7);
63 }
64 {
65 const std::counted_iterator iter(contiguous_iterator<int*>{buffer + 2}, 6);
66 assert(iter.count() == 6);
67 }
68 {
69 const std::counted_iterator iter(InputOrOutputArchetype{buffer + 2}, 6);
70 assert(iter.count() == 6);
71 }
72
73 return true;
74}
75
76int main(int, char**) {
77 test();
78 static_assert(test());
79
80 return 0;
81}
82

source code of libcxx/test/std/iterators/predef.iterators/counted.iterator/count.pass.cpp