| 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 prev(Iter x, Iter::difference_type n = 1); // constexpr in C++17 |
| 13 |
|
| 14 |
#include <iterator> |
| 15 |
#include <cassert> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
#include "test_macros.h" |
| 19 |
#include "test_iterators.h" |
| 20 |
|
| 21 |
template <class It> |
| 22 |
TEST_CONSTEXPR_CXX17 void |
| 23 |
check_prev_n(It it, typename std::iterator_traits<It>::difference_type n, It result) |
| 24 |
{ |
| 25 |
static_assert(std::is_same<decltype(std::prev(it, n)), It>::value, ""); |
| 26 |
assert(std::prev(it, n) == result); |
| 27 |
|
| 28 |
It (*prev_ptr)(It, typename std::iterator_traits<It>::difference_type) = std::prev; |
| 29 |
assert(prev_ptr(it, n) == result); |
| 30 |
} |
| 31 |
|
| 32 |
template <class It> |
| 33 |
TEST_CONSTEXPR_CXX17 void |
| 34 |
check_prev_1(It it, It result) |
| 35 |
{ |
| 36 |
static_assert(std::is_same<decltype(std::prev(it)), It>::value, ""); |
| 37 |
assert(std::prev(it) == result); |
| 38 |
} |
| 39 |
|
| 40 |
TEST_CONSTEXPR_CXX17 bool tests() |
| 41 |
{ |
| 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); |
| 49 |
|
| 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)); |
| 52 |
check_prev_1(s+1, s); |
| 53 |
|
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
int main(int, char**) |
| 58 |
{ |
| 59 |
tests(); |
| 60 |
#if TEST_STD_VER >= 17 |
| 61 |
static_assert(tests(), ""); |
| 62 |
#endif |
| 63 |
return 0; |
| 64 |
} |