copy_backward.pass.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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, BidirectionalIterator OutIter>
  11. // requires OutputIterator<OutIter, InIter::reference>
  12. // constexpr OutIter // constexpr after C++17
  13. // copy_backward(InIter first, InIter last, OutIter result);
  14. #include <algorithm>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "test_iterators.h"
  18. #include "user_defined_integral.hpp"
  19. // #if TEST_STD_VER > 17
  20. // TEST_CONSTEXPR bool test_constexpr() {
  21. // int ia[] = {1, 2, 3, 4, 5};
  22. // int ic[] = {6, 6, 6, 6, 6, 6, 6};
  23. //
  24. // size_t N = std::size(ia);
  25. // auto p = std::copy_backward(std::begin(ia), std::end(ia), std::begin(ic) + N);
  26. // return std::equal(std::begin(ic), p, std::begin(ia))
  27. // && std::all_of(p, std::end(ic), [](int a){return a == 6;})
  28. // ;
  29. // }
  30. // #endif
  31. template <class InIter, class OutIter>
  32. void
  33. test()
  34. {
  35. const unsigned N = 1000;
  36. int ia[N];
  37. for (unsigned i = 0; i < N; ++i)
  38. ia[i] = i;
  39. int ib[N] = {0};
  40. OutIter r = std::copy_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
  41. assert(base(r) == ib);
  42. for (unsigned i = 0; i < N; ++i)
  43. assert(ia[i] == ib[i]);
  44. }
  45. int main()
  46. {
  47. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  48. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  49. test<bidirectional_iterator<const int*>, int*>();
  50. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  51. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  52. test<random_access_iterator<const int*>, int*>();
  53. test<const int*, bidirectional_iterator<int*> >();
  54. test<const int*, random_access_iterator<int*> >();
  55. test<const int*, int*>();
  56. // #if TEST_STD_VER > 17
  57. // static_assert(test_constexpr());
  58. // #endif
  59. }