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 // <iterator>
10 
11 // template <BidirectionalIterator Iter>
12 //   Iter prev(Iter x, Iter::difference_type n = 1);
13 
14 #include <iterator>
15 #include <cassert>
16 
17 #include "test_iterators.h"
18 
19 template <class It>
20 void
21 test(It i, typename std::iterator_traits<It>::difference_type n, It x)
22 {
23     assert(std::prev(i, n) == x);
24 
25     It (*prev)(It, typename std::iterator_traits<It>::difference_type) = std::prev;
26     assert(prev(i, n) == x);
27 }
28 
29 template <class It>
30 void
31 test(It i, It x)
32 {
33     assert(std::prev(i) == x);
34 }
35 
36 #if TEST_STD_VER > 14
37 template <class It>
38 constexpr bool
39 constexpr_test(It i, typename std::iterator_traits<It>::difference_type n, It x)
40 {
41     return std::prev(i, n) == x;
42 }
43 
44 template <class It>
45 constexpr bool
46 constexpr_test(It i, It x)
47 {
48     return std::prev(i) == x;
49 }
50 #endif
51 
52 int main(int, char**)
53 {
54     {
55     const char* s = "1234567890";
56     test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s));
57     test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s));
58     test(s+10, 10, s);
59 
60     test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s));
61     test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s));
62     test(s+1, s);
63     }
64 #if TEST_STD_VER > 14
65     {
66     constexpr const char* s = "1234567890";
67     static_assert( constexpr_test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s)), "" );
68     static_assert( constexpr_test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s)), "" );
69     static_assert( constexpr_test(s+10, 10, s), "" );
70 
71     static_assert( constexpr_test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s)), "" );
72     static_assert( constexpr_test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s)), "" );
73     static_assert( constexpr_test(s+1, s), "" );
74     }
75 #endif
76 
77 
78   return 0;
79 }
80