unary_transform.pass.cpp 3.7 KB

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