remove_if.pass.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Iter
  14. // remove_if(Iter first, Iter last, Pred pred);
  15. #include <algorithm>
  16. #include <functional>
  17. #include <cassert>
  18. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  19. #include <memory>
  20. #endif
  21. #include "test_iterators.h"
  22. #include "counting_predicates.hpp"
  23. bool equal2 ( int i ) { return i == 2; }
  24. template <class Iter>
  25. void
  26. test()
  27. {
  28. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  29. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  30. // int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2));
  31. unary_counting_predicate<bool(*)(int), int> cp(equal2);
  32. int* r = std::remove_if(ia, ia+sa, std::ref(cp));
  33. assert(r == ia + sa-3);
  34. assert(ia[0] == 0);
  35. assert(ia[1] == 1);
  36. assert(ia[2] == 3);
  37. assert(ia[3] == 4);
  38. assert(ia[4] == 3);
  39. assert(ia[5] == 4);
  40. assert(cp.count() == sa);
  41. }
  42. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  43. struct pred
  44. {
  45. bool operator()(const std::unique_ptr<int>& i) {return *i == 2;}
  46. };
  47. template <class Iter>
  48. void
  49. test1()
  50. {
  51. const unsigned sa = 9;
  52. std::unique_ptr<int> ia[sa];
  53. ia[0].reset(new int(0));
  54. ia[1].reset(new int(1));
  55. ia[2].reset(new int(2));
  56. ia[3].reset(new int(3));
  57. ia[4].reset(new int(4));
  58. ia[5].reset(new int(2));
  59. ia[6].reset(new int(3));
  60. ia[7].reset(new int(4));
  61. ia[8].reset(new int(2));
  62. Iter r = std::remove_if(Iter(ia), Iter(ia+sa), pred());
  63. assert(base(r) == ia + sa-3);
  64. assert(*ia[0] == 0);
  65. assert(*ia[1] == 1);
  66. assert(*ia[2] == 3);
  67. assert(*ia[3] == 4);
  68. assert(*ia[4] == 3);
  69. assert(*ia[5] == 4);
  70. }
  71. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  72. int main()
  73. {
  74. test<forward_iterator<int*> >();
  75. test<bidirectional_iterator<int*> >();
  76. test<random_access_iterator<int*> >();
  77. test<int*>();
  78. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  79. test1<forward_iterator<std::unique_ptr<int>*> >();
  80. test1<bidirectional_iterator<std::unique_ptr<int>*> >();
  81. test1<random_access_iterator<std::unique_ptr<int>*> >();
  82. test1<std::unique_ptr<int>*>();
  83. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  84. }