1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <iterator>
11 
12 // template <BidirectionalIterator Iter>
13 //   Iter prev(Iter x, Iter::difference_type n = 1);
14 
15 #include <iterator>
16 #include <cassert>
17 
18 #include "test_iterators.h"
19 
20 template <class It>
21 void
test(It i,typename std::iterator_traits<It>::difference_type n,It x)22 test(It i, typename std::iterator_traits<It>::difference_type n, It x)
23 {
24     assert(std::prev(i, n) == x);
25 
26     It (*prev)(It, typename std::iterator_traits<It>::difference_type) = std::prev;
27     assert(prev(i, n) == x);
28 }
29 
30 template <class It>
31 void
test(It i,It x)32 test(It i, It x)
33 {
34     assert(std::prev(i) == x);
35 }
36 
37 #if TEST_STD_VER > 14
38 template <class It>
39 constexpr bool
constexpr_test(It i,typename std::iterator_traits<It>::difference_type n,It x)40 constexpr_test(It i, typename std::iterator_traits<It>::difference_type n, It x)
41 {
42     return std::prev(i, n) == x;
43 }
44 
45 template <class It>
46 constexpr bool
constexpr_test(It i,It x)47 constexpr_test(It i, It x)
48 {
49     return std::prev(i) == x;
50 }
51 #endif
52 
main()53 int main()
54 {
55     {
56     const char* s = "1234567890";
57     test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s));
58     test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s));
59     test(s+10, 10, s);
60 
61     test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s));
62     test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s));
63     test(s+1, s);
64     }
65 #if TEST_STD_VER > 14
66     {
67     constexpr const char* s = "1234567890";
68     static_assert( constexpr_test(bidirectional_iterator<const char*>(s+10), 10, bidirectional_iterator<const char*>(s)), "" );
69     static_assert( constexpr_test(random_access_iterator<const char*>(s+10), 10, random_access_iterator<const char*>(s)), "" );
70     static_assert( constexpr_test(s+10, 10, s), "" );
71 
72     static_assert( constexpr_test(bidirectional_iterator<const char*>(s+1), bidirectional_iterator<const char*>(s)), "" );
73     static_assert( constexpr_test(random_access_iterator<const char*>(s+1), random_access_iterator<const char*>(s)), "" );
74     static_assert( constexpr_test(s+1, s), "" );
75     }
76 #endif
77 
78 }
79