partition_copy.pass.cpp 2.3 KB

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