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