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 <InputIterator Iter>
12 //   Iter next(Iter x, Iter::difference_type n = 1); // constexpr in C++17
13 
14 // LWG #2353 relaxed the requirement on next from ForwardIterator to InputIterator
15 
16 #include <iterator>
17 #include <cassert>
18 #include <type_traits>
19 
20 #include "test_macros.h"
21 #include "test_iterators.h"
22 
23 template <class It>
24 TEST_CONSTEXPR_CXX17 void
check_next_n(It it,typename std::iterator_traits<It>::difference_type n,It result)25 check_next_n(It it, typename std::iterator_traits<It>::difference_type n, It result)
26 {
27     static_assert(std::is_same<decltype(std::next(it, n)), It>::value, "");
28     assert(std::next(it, n) == result);
29 
30     It (*next_ptr)(It, typename std::iterator_traits<It>::difference_type) = std::next;
31     assert(next_ptr(it, n) == result);
32 }
33 
34 template <class It>
35 TEST_CONSTEXPR_CXX17 void
check_next_1(It it,It result)36 check_next_1(It it, It result)
37 {
38     static_assert(std::is_same<decltype(std::next(it)), It>::value, "");
39     assert(std::next(it) == result);
40 }
41 
tests()42 TEST_CONSTEXPR_CXX17 bool tests()
43 {
44     const char* s = "1234567890";
45     check_next_n(cpp17_input_iterator<const char*>(s),             10, cpp17_input_iterator<const char*>(s+10));
46     check_next_n(forward_iterator<const char*>(s),           10, forward_iterator<const char*>(s+10));
47     check_next_n(bidirectional_iterator<const char*>(s),     10, bidirectional_iterator<const char*>(s+10));
48     check_next_n(bidirectional_iterator<const char*>(s+10), -10, bidirectional_iterator<const char*>(s));
49     check_next_n(random_access_iterator<const char*>(s),     10, random_access_iterator<const char*>(s+10));
50     check_next_n(random_access_iterator<const char*>(s+10), -10, random_access_iterator<const char*>(s));
51     check_next_n(s, 10, s+10);
52 
53     check_next_1(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1));
54     check_next_1(forward_iterator<const char*>(s), forward_iterator<const char*>(s+1));
55     check_next_1(bidirectional_iterator<const char*>(s), bidirectional_iterator<const char*>(s+1));
56     check_next_1(random_access_iterator<const char*>(s), random_access_iterator<const char*>(s+1));
57     check_next_1(s, s+1);
58 
59     return true;
60 }
61 
main(int,char **)62 int main(int, char**)
63 {
64     tests();
65 #if TEST_STD_VER >= 17
66     static_assert(tests(), "");
67 #endif
68     return 0;
69 }
70