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