1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // All of these became constexpr in C++17
13 // template <InputIterator Iter, class Distance>
14 // constexpr void advance(Iter& i, Distance n);
16 // template <BidirectionalIterator Iter, class Distance>
17 // constexpr void advance(Iter& i, Distance n);
19 // template <RandomAccessIterator Iter, class Distance>
20 // constexpr void advance(Iter& i, Distance n);
23 // TODO: test_iterators.h includes <ranges>, and <ranges> includes <chrono> and <atomic>.
24 // Lots of implementation headers under <__chrono/> and <atomic> has signed to unsigned conversion,
25 // which will trigger the -Wsign-conversion warning.
26 // Once those headers are fixed, enable the -Wsign-conversion for this test by removing
27 // <TODO:Remove brackets> below
29 // Make sure we catch forced conversions to the difference_type if they happen.
30 // ADDITIONAL_COMPILE_FLAGS<TODO:Remove brackets>(gcc-style-warnings): -Wsign-conversion
35 #include <type_traits>
37 #include "test_macros.h"
38 #include "test_iterators.h"
40 template <class Distance
, class It
>
42 void check_advance(It it
, Distance n
, It result
)
44 static_assert(std::is_same
<decltype(std::advance(it
, n
)), void>::value
, "");
49 TEST_CONSTEXPR_CXX17
bool tests()
51 const char* s
= "1234567890";
53 // Check with iterator_traits::difference_type
55 typedef std::iterator_traits
<const char*>::difference_type Distance
;
56 check_advance
<Distance
>(cpp17_input_iterator
<const char*>(s
), 10, cpp17_input_iterator
<const char*>(s
+10));
57 check_advance
<Distance
>(forward_iterator
<const char*>(s
), 10, forward_iterator
<const char*>(s
+10));
58 check_advance
<Distance
>(bidirectional_iterator
<const char*>(s
+5), 5, bidirectional_iterator
<const char*>(s
+10));
59 check_advance
<Distance
>(bidirectional_iterator
<const char*>(s
+5), -5, bidirectional_iterator
<const char*>(s
));
60 check_advance
<Distance
>(random_access_iterator
<const char*>(s
+5), 5, random_access_iterator
<const char*>(s
+10));
61 check_advance
<Distance
>(random_access_iterator
<const char*>(s
+5), -5, random_access_iterator
<const char*>(s
));
62 check_advance
<Distance
>(s
+5, 5, s
+10);
63 check_advance
<Distance
>(s
+5, -5, s
);
66 // Also check with other distance types
69 check_advance
<Distance
>(cpp17_input_iterator
<const char*>(s
), 10, cpp17_input_iterator
<const char*>(s
+10));
70 check_advance
<Distance
>(forward_iterator
<const char*>(s
), 10, forward_iterator
<const char*>(s
+10));
71 check_advance
<Distance
>(bidirectional_iterator
<const char*>(s
), 10, bidirectional_iterator
<const char*>(s
+10));
72 check_advance
<Distance
>(random_access_iterator
<const char*>(s
), 10, random_access_iterator
<const char*>(s
+10));
75 // Check with unsigned distance types to catch signedness-change issues
77 typedef std::size_t Distance
;
78 check_advance
<Distance
>(cpp17_input_iterator
<const char*>(s
), 10u, cpp17_input_iterator
<const char*>(s
+10));
79 check_advance
<Distance
>(forward_iterator
<const char*>(s
), 10u, forward_iterator
<const char*>(s
+10));
80 check_advance
<Distance
>(bidirectional_iterator
<const char*>(s
), 10u, bidirectional_iterator
<const char*>(s
+10));
81 check_advance
<Distance
>(random_access_iterator
<const char*>(s
), 10u, random_access_iterator
<const char*>(s
+10));
90 #if TEST_STD_VER >= 17
91 static_assert(tests(), "");