| 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 |
// <algorithm> |
| 10 |
|
| 11 |
// template<InputIterator InIter, class OutIter, |
| 12 |
// Callable<auto, const InIter::value_type&> Op> |
| 13 |
// requires OutputIterator<OutIter, Op::result_type> && CopyConstructible<Op> |
| 14 |
// constexpr OutIter // constexpr after C++17 |
| 15 |
// transform(InIter first, InIter last, OutIter result, Op op); |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <functional> |
| 19 |
#include <cassert> |
| 20 |
|
| 21 |
#include "test_macros.h" |
| 22 |
#include "test_iterators.h" |
| 23 |
|
| 24 |
TEST_CONSTEXPR int plusOne(int v) { return v + 1; } |
| 25 |
|
| 26 |
|
| 27 |
#if TEST_STD_VER > 17 |
| 28 |
TEST_CONSTEXPR bool test_constexpr() { |
| 29 |
int ia[] = {1, 3, 6, 7}; |
| 30 |
int ib[] = {0, 0, 0, 0, 0}; // one bigger |
| 31 |
const int expected[] = {2, 4, 7, 8}; |
| 32 |
|
| 33 |
auto it = std::transform(std::begin(ia), std::end(ia), std::begin(ib), plusOne); |
| 34 |
|
| 35 |
return it == (std::begin(ib) + std::size(ia)) |
| 36 |
&& *it == 0 // don't overwrite the last value in the output array |
| 37 |
&& std::equal(std::begin(ib), it, std::begin(expected), std::end(expected)) |
| 38 |
; |
| 39 |
} |
| 40 |
#endif |
| 41 |
|
| 42 |
|
| 43 |
template <class InIter, class OutIter> |
| 44 |
void |
| 45 |
test() |
| 46 |
{ |
| 47 |
int ia[] = {0, 1, 2, 3, 4}; |
| 48 |
const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
| 49 |
int ib[sa] = {0}; |
| 50 |
OutIter r = std::transform(InIter(ia), InIter(ia+sa), |
| 51 |
OutIter(ib), plusOne); |
| 52 |
assert(base(r) == ib + sa); |
| 53 |
assert(ib[0] == 1); |
| 54 |
assert(ib[1] == 2); |
| 55 |
assert(ib[2] == 3); |
| 56 |
assert(ib[3] == 4); |
| 57 |
assert(ib[4] == 5); |
| 58 |
} |
| 59 |
|
| 60 |
int main(int, char**) |
| 61 |
{ |
| 62 |
test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 63 |
test<cpp17_input_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 64 |
test<cpp17_input_iterator<const int*>, forward_iterator<int*> >(); |
| 65 |
test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 66 |
test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >(); |
| 67 |
test<cpp17_input_iterator<const int*>, int*>(); |
| 68 |
|
| 69 |
test<forward_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 70 |
test<forward_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 71 |
test<forward_iterator<const int*>, forward_iterator<int*> >(); |
| 72 |
test<forward_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 73 |
test<forward_iterator<const int*>, random_access_iterator<int*> >(); |
| 74 |
test<forward_iterator<const int*>, int*>(); |
| 75 |
|
| 76 |
test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 77 |
test<bidirectional_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 78 |
test<bidirectional_iterator<const int*>, forward_iterator<int*> >(); |
| 79 |
test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 80 |
test<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); |
| 81 |
test<bidirectional_iterator<const int*>, int*>(); |
| 82 |
|
| 83 |
test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 84 |
test<random_access_iterator<const int*>, cpp17_input_iterator<int*> >(); |
| 85 |
test<random_access_iterator<const int*>, forward_iterator<int*> >(); |
| 86 |
test<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 87 |
test<random_access_iterator<const int*>, random_access_iterator<int*> >(); |
| 88 |
test<random_access_iterator<const int*>, int*>(); |
| 89 |
|
| 90 |
test<const int*, cpp17_output_iterator<int*> >(); |
| 91 |
test<const int*, cpp17_input_iterator<int*> >(); |
| 92 |
test<const int*, forward_iterator<int*> >(); |
| 93 |
test<const int*, bidirectional_iterator<int*> >(); |
| 94 |
test<const int*, random_access_iterator<int*> >(); |
| 95 |
test<const int*, int*>(); |
| 96 |
|
| 97 |
#if TEST_STD_VER > 17 |
| 98 |
static_assert(test_constexpr()); |
| 99 |
#endif |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |