remove_if.pass.cpp 2.9 KB

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