reverse_copy.pass.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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<BidirectionalIterator InIter, OutputIterator<auto, InIter::reference> OutIter>
  10. // constexpr OutIter // constexpr after C++17
  11. // reverse_copy(InIter first, InIter last, OutIter result);
  12. #include <algorithm>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "test_iterators.h"
  16. #if TEST_STD_VER > 17
  17. TEST_CONSTEXPR bool test_constexpr() {
  18. int ia[] = {1, 3, 5, 2, 5, 6};
  19. int ib[std::size(ia)] = {0};
  20. auto it = std::reverse_copy(std::begin(ia), std::end(ia), std::begin(ib));
  21. return std::distance(std::begin(ib), it) == std::size(ia)
  22. && std::equal (std::begin(ia), std::end(ia), std::rbegin(ib))
  23. ;
  24. }
  25. #endif
  26. template <class InIter, class OutIter>
  27. void
  28. test()
  29. {
  30. const int ia[] = {0};
  31. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  32. int ja[sa] = {-1};
  33. OutIter r = std::reverse_copy(InIter(ia), InIter(ia), OutIter(ja));
  34. assert(base(r) == ja);
  35. assert(ja[0] == -1);
  36. r = std::reverse_copy(InIter(ia), InIter(ia+sa), OutIter(ja));
  37. assert(ja[0] == 0);
  38. const int ib[] = {0, 1};
  39. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  40. int jb[sb] = {-1};
  41. r = std::reverse_copy(InIter(ib), InIter(ib+sb), OutIter(jb));
  42. assert(base(r) == jb+sb);
  43. assert(jb[0] == 1);
  44. assert(jb[1] == 0);
  45. const int ic[] = {0, 1, 2};
  46. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  47. int jc[sc] = {-1};
  48. r = std::reverse_copy(InIter(ic), InIter(ic+sc), OutIter(jc));
  49. assert(base(r) == jc+sc);
  50. assert(jc[0] == 2);
  51. assert(jc[1] == 1);
  52. assert(jc[2] == 0);
  53. int id[] = {0, 1, 2, 3};
  54. const unsigned sd = sizeof(id)/sizeof(id[0]);
  55. int jd[sd] = {-1};
  56. r = std::reverse_copy(InIter(id), InIter(id+sd), OutIter(jd));
  57. assert(base(r) == jd+sd);
  58. assert(jd[0] == 3);
  59. assert(jd[1] == 2);
  60. assert(jd[2] == 1);
  61. assert(jd[3] == 0);
  62. }
  63. int main(int, char**)
  64. {
  65. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  66. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  67. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  68. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  69. test<bidirectional_iterator<const int*>, int*>();
  70. test<random_access_iterator<const int*>, output_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*, 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. }