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