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 //   All of these became constexpr in C++17
12 //
13 // template <InputIterator Iter>
14 //   constexpr void advance(Iter& i, Iter::difference_type n);
15 //
16 // template <BidirectionalIterator Iter>
17 //   constexpr void advance(Iter& i, Iter::difference_type n);
18 //
19 // template <RandomAccessIterator Iter>
20 //   constexpr void advance(Iter& i, Iter::difference_type n);
21 
22 #include <iterator>
23 #include <cassert>
24 #include <type_traits>
25 
26 #include "test_macros.h"
27 #include "test_iterators.h"
28 
29 template <class It>
30 TEST_CONSTEXPR_CXX17
31 void check_advance(It it, typename std::iterator_traits<It>::difference_type n, It result)
32 {
33     static_assert(std::is_same<decltype(std::advance(it, n)), void>::value, "");
34     std::advance(it, n);
35     assert(it == result);
36 }
37 
38 TEST_CONSTEXPR_CXX17 bool tests()
39 {
40     const char* s = "1234567890";
41     check_advance(input_iterator<const char*>(s), 10, input_iterator<const char*>(s+10));
42     check_advance(forward_iterator<const char*>(s), 10, forward_iterator<const char*>(s+10));
43     check_advance(bidirectional_iterator<const char*>(s+5), 5, bidirectional_iterator<const char*>(s+10));
44     check_advance(bidirectional_iterator<const char*>(s+5), -5, bidirectional_iterator<const char*>(s));
45     check_advance(random_access_iterator<const char*>(s+5), 5, random_access_iterator<const char*>(s+10));
46     check_advance(random_access_iterator<const char*>(s+5), -5, random_access_iterator<const char*>(s));
47     check_advance(s+5, 5, s+10);
48     check_advance(s+5, -5, s);
49 
50     return true;
51 }
52 
53 int main(int, char**)
54 {
55     tests();
56 #if TEST_STD_VER >= 17
57     static_assert(tests(), "");
58 #endif
59     return 0;
60 }
61