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::prev(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::prev(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 prev() forward
33   for (int n = 0; n != 10; ++n) {
34     check<bidirectional_iterator<int*>>(range+n, n, range);
35     check<random_access_iterator<int*>>(range+n, n, range);
36     check<contiguous_iterator<int*>>(   range+n, n, range);
37     check<int*>(                        range+n, n, range);
38   }
39 
40   // Check prev() backward
41   for (int n = 0; n != 10; ++n) {
42     check<bidirectional_iterator<int*>>(range, -n, range+n);
43     check<random_access_iterator<int*>>(range, -n, range+n);
44     check<contiguous_iterator<int*>>(   range, -n, range+n);
45     check<int*>(                        range, -n, range+n);
46   }
47 
48   return true;
49 }
50 
main(int,char **)51 int main(int, char**) {
52   test();
53   static_assert(test());
54   return 0;
55 }
56