reverse_copy.pass.cpp 3.2 KB

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