remove_if.pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred>
  10. // requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
  11. // && CopyConstructible<Pred>
  12. // constexpr Iter // constexpr after C++17
  13. // remove_if(Iter first, Iter last, Pred pred);
  14. #include <algorithm>
  15. #include <functional>
  16. #include <cassert>
  17. #include <memory>
  18. #include "test_macros.h"
  19. #include "test_iterators.h"
  20. #include "counting_predicates.h"
  21. TEST_CONSTEXPR bool equal2 ( int i ) { return i == 2; }
  22. #if TEST_STD_VER > 17
  23. TEST_CONSTEXPR bool test_constexpr() {
  24. int ia[] = {1, 3, 5, 2, 5, 6};
  25. auto it = std::remove_if(std::begin(ia), std::end(ia), equal2);
  26. return (std::begin(ia) + std::size(ia) - 1) == it // we removed one element
  27. && std::none_of(std::begin(ia), it, equal2)
  28. ;
  29. }
  30. #endif
  31. template <class Iter>
  32. void
  33. test()
  34. {
  35. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  36. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  37. // int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2));
  38. unary_counting_predicate<bool(*)(int), int> cp(equal2);
  39. int* r = std::remove_if(ia, ia+sa, std::ref(cp));
  40. assert(r == ia + sa-3);
  41. assert(ia[0] == 0);
  42. assert(ia[1] == 1);
  43. assert(ia[2] == 3);
  44. assert(ia[3] == 4);
  45. assert(ia[4] == 3);
  46. assert(ia[5] == 4);
  47. assert(cp.count() == sa);
  48. }
  49. #if TEST_STD_VER >= 11
  50. struct pred
  51. {
  52. bool operator()(const std::unique_ptr<int>& i) {return *i == 2;}
  53. };
  54. template <class Iter>
  55. void
  56. test1()
  57. {
  58. const unsigned sa = 9;
  59. std::unique_ptr<int> ia[sa];
  60. ia[0].reset(new int(0));
  61. ia[1].reset(new int(1));
  62. ia[2].reset(new int(2));
  63. ia[3].reset(new int(3));
  64. ia[4].reset(new int(4));
  65. ia[5].reset(new int(2));
  66. ia[6].reset(new int(3));
  67. ia[7].reset(new int(4));
  68. ia[8].reset(new int(2));
  69. Iter r = std::remove_if(Iter(ia), Iter(ia+sa), pred());
  70. assert(base(r) == ia + sa-3);
  71. assert(*ia[0] == 0);
  72. assert(*ia[1] == 1);
  73. assert(*ia[2] == 3);
  74. assert(*ia[3] == 4);
  75. assert(*ia[4] == 3);
  76. assert(*ia[5] == 4);
  77. }
  78. #endif // TEST_STD_VER >= 11
  79. int main(int, char**)
  80. {
  81. test<forward_iterator<int*> >();
  82. test<bidirectional_iterator<int*> >();
  83. test<random_access_iterator<int*> >();
  84. test<int*>();
  85. #if TEST_STD_VER >= 11
  86. test1<forward_iterator<std::unique_ptr<int>*> >();
  87. test1<bidirectional_iterator<std::unique_ptr<int>*> >();
  88. test1<random_access_iterator<std::unique_ptr<int>*> >();
  89. test1<std::unique_ptr<int>*>();
  90. #endif // TEST_STD_VER >= 11
  91. #if TEST_STD_VER > 17
  92. static_assert(test_constexpr());
  93. #endif
  94. return 0;
  95. }