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 // template <InputIterator Iter>
12 // Iter prev(Iter x, Iter::difference_type n = 1); // constexpr in C++17
16 #include <type_traits>
18 #include "test_macros.h"
19 #include "test_iterators.h"
22 TEST_CONSTEXPR_CXX17
void
23 check_prev_n(It it
, typename
std::iterator_traits
<It
>::difference_type n
, It result
)
25 static_assert(std::is_same
<decltype(std::prev(it
, n
)), It
>::value
, "");
26 assert(std::prev(it
, n
) == result
);
28 It (*prev_ptr
)(It
, typename
std::iterator_traits
<It
>::difference_type
) = std::prev
;
29 assert(prev_ptr(it
, n
) == result
);
33 TEST_CONSTEXPR_CXX17
void
34 check_prev_1(It it
, It result
)
36 static_assert(std::is_same
<decltype(std::prev(it
)), It
>::value
, "");
37 assert(std::prev(it
) == result
);
40 TEST_CONSTEXPR_CXX17
bool tests()
42 const char* s
= "1234567890";
43 check_prev_n(forward_iterator
<const char*>(s
), -10, forward_iterator
<const char*>(s
+10));
44 check_prev_n(bidirectional_iterator
<const char*>(s
+10), 10, bidirectional_iterator
<const char*>(s
));
45 check_prev_n(bidirectional_iterator
<const char*>(s
), -10, bidirectional_iterator
<const char*>(s
+10));
46 check_prev_n(random_access_iterator
<const char*>(s
+10), 10, random_access_iterator
<const char*>(s
));
47 check_prev_n(random_access_iterator
<const char*>(s
), -10, random_access_iterator
<const char*>(s
+10));
48 check_prev_n(s
+10, 10, s
);
50 check_prev_1(bidirectional_iterator
<const char*>(s
+1), bidirectional_iterator
<const char*>(s
));
51 check_prev_1(random_access_iterator
<const char*>(s
+1), random_access_iterator
<const char*>(s
));
60 #if TEST_STD_VER >= 17
61 static_assert(tests(), "");