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