partition_point.pass.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 ForwardIterator, class Predicate>
  10. // constpexr ForwardIterator // constexpr after C++17
  11. // partition_point(ForwardIterator first, ForwardIterator last, Predicate pred);
  12. #include <algorithm>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "test_iterators.h"
  16. struct is_odd
  17. {
  18. TEST_CONSTEXPR bool operator()(const int& i) const {return i & 1;}
  19. };
  20. #if TEST_STD_VER > 17
  21. TEST_CONSTEXPR bool test_constexpr() {
  22. int ia[] = {1, 3, 5, 2, 4, 6};
  23. int ib[] = {1, 2, 3, 4, 5, 6};
  24. return (std::partition_point(std::begin(ia), std::end(ia), is_odd()) == ia+3)
  25. && (std::partition_point(std::begin(ib), std::end(ib), is_odd()) == ib+1)
  26. ;
  27. }
  28. #endif
  29. int main(int, char**)
  30. {
  31. {
  32. const int ia[] = {2, 4, 6, 8, 10};
  33. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  34. forward_iterator<const int*>(std::end(ia)),
  35. is_odd()) == forward_iterator<const int*>(ia));
  36. }
  37. {
  38. const int ia[] = {1, 2, 4, 6, 8};
  39. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  40. forward_iterator<const int*>(std::end(ia)),
  41. is_odd()) == forward_iterator<const int*>(ia + 1));
  42. }
  43. {
  44. const int ia[] = {1, 3, 2, 4, 6};
  45. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  46. forward_iterator<const int*>(std::end(ia)),
  47. is_odd()) == forward_iterator<const int*>(ia + 2));
  48. }
  49. {
  50. const int ia[] = {1, 3, 5, 2, 4, 6};
  51. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  52. forward_iterator<const int*>(std::end(ia)),
  53. is_odd()) == forward_iterator<const int*>(ia + 3));
  54. }
  55. {
  56. const int ia[] = {1, 3, 5, 7, 2, 4};
  57. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  58. forward_iterator<const int*>(std::end(ia)),
  59. is_odd()) == forward_iterator<const int*>(ia + 4));
  60. }
  61. {
  62. const int ia[] = {1, 3, 5, 7, 9, 2};
  63. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  64. forward_iterator<const int*>(std::end(ia)),
  65. is_odd()) == forward_iterator<const int*>(ia + 5));
  66. }
  67. {
  68. const int ia[] = {1, 3, 5, 7, 9, 11};
  69. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  70. forward_iterator<const int*>(std::end(ia)),
  71. is_odd()) == forward_iterator<const int*>(ia + 6));
  72. }
  73. {
  74. const int ia[] = {1, 3, 5, 2, 4, 6, 7};
  75. assert(std::partition_point(forward_iterator<const int*>(std::begin(ia)),
  76. forward_iterator<const int*>(std::begin(ia)),
  77. is_odd()) == forward_iterator<const int*>(ia));
  78. }
  79. #if TEST_STD_VER > 17
  80. static_assert(test_constexpr());
  81. #endif
  82. return 0;
  83. }