unary_transform.pass.cpp 3.7 KB

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