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