partition_copy.pass.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <class InputIterator, class OutputIterator1,
  11. // class OutputIterator2, class Predicate>
  12. // constexpr pair<OutputIterator1, OutputIterator2> // constexpr after C++17
  13. // partition_copy(InputIterator first, InputIterator last,
  14. // OutputIterator1 out_true, OutputIterator2 out_false,
  15. // Predicate pred);
  16. #include <algorithm>
  17. #include <cassert>
  18. #include "test_macros.h"
  19. #include "test_iterators.h"
  20. struct is_odd
  21. {
  22. TEST_CONSTEXPR bool operator()(const int& i) const {return i & 1;}
  23. };
  24. #if TEST_STD_VER > 17
  25. TEST_CONSTEXPR bool test_constexpr() {
  26. int ia[] = {1, 3, 5, 2, 4, 6};
  27. int r1[10] = {0};
  28. int r2[10] = {0};
  29. auto p = std::partition_copy(std::begin(ia), std::end(ia),
  30. std::begin(r1), std::begin(r2), is_odd());
  31. return std::all_of(std::begin(r1), p.first, is_odd())
  32. && std::all_of(p.first, std::end(r1), [](int a){return a == 0;})
  33. && std::none_of(std::begin(r2), p.second, is_odd())
  34. && std::all_of(p.second, std::end(r2), [](int a){return a == 0;})
  35. ;
  36. }
  37. #endif
  38. int main()
  39. {
  40. {
  41. const int ia[] = {1, 2, 3, 4, 6, 8, 5, 7};
  42. int r1[10] = {0};
  43. int r2[10] = {0};
  44. typedef std::pair<output_iterator<int*>, int*> P;
  45. P p = std::partition_copy(input_iterator<const int*>(std::begin(ia)),
  46. input_iterator<const int*>(std::end(ia)),
  47. output_iterator<int*>(r1), r2, is_odd());
  48. assert(p.first.base() == r1 + 4);
  49. assert(r1[0] == 1);
  50. assert(r1[1] == 3);
  51. assert(r1[2] == 5);
  52. assert(r1[3] == 7);
  53. assert(p.second == r2 + 4);
  54. assert(r2[0] == 2);
  55. assert(r2[1] == 4);
  56. assert(r2[2] == 6);
  57. assert(r2[3] == 8);
  58. }
  59. #if TEST_STD_VER > 17
  60. static_assert(test_constexpr());
  61. #endif
  62. }