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-has-no-incomplete-ranges
11 
12 // ranges::next(it, n)
13 
14 #include <iterator>
15 
16 #include <cassert>
17 #include <concepts>
18 #include <utility>
19 
20 #include "test_iterators.h"
21 
22 template <typename It>
check(int * first,std::iter_difference_t<It> n,int * expected)23 constexpr void check(int* first, std::iter_difference_t<It> n, int* expected) {
24   It it(first);
25   std::same_as<It> auto result = std::ranges::next(std::move(it), n);
26   assert(base(result) == expected);
27 }
28 
test()29 constexpr bool test() {
30   int range[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
31 
32   // Check next() forward
33   for (int n = 0; n != 10; ++n) {
34     check<cpp17_input_iterator<int*>>(  range, n, range+n);
35     check<cpp20_input_iterator<int*>>(  range, n, range+n);
36     check<forward_iterator<int*>>(      range, n, range+n);
37     check<bidirectional_iterator<int*>>(range, n, range+n);
38     check<random_access_iterator<int*>>(range, n, range+n);
39     check<contiguous_iterator<int*>>(   range, n, range+n);
40     check<int*>(                        range, n, range+n);
41     check<cpp17_output_iterator<int*> >(range, n, range+n);
42   }
43 
44   // Check next() backward
45   for (int n = 0; n != 10; ++n) {
46     check<bidirectional_iterator<int*>>(range+9, -n, range+9 - n);
47     check<random_access_iterator<int*>>(range+9, -n, range+9 - n);
48     check<contiguous_iterator<int*>>(   range+9, -n, range+9 - n);
49     check<int*>(                        range+9, -n, range+9 - n);
50   }
51 
52   return true;
53 }
54 
main(int,char **)55 int main(int, char**) {
56   test();
57   static_assert(test());
58   return 0;
59 }
60