remove_if.pass.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. 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. #ifdef _LIBCPP_MOVE
  19. #include <memory>
  20. #endif
  21. #include "../../iterators.h"
  22. template <class Iter>
  23. void
  24. test()
  25. {
  26. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  27. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  28. int* r = std::remove_if(ia, ia+sa, std::bind2nd(std::equal_to<int>(), 2));
  29. assert(r == ia + sa-3);
  30. assert(ia[0] == 0);
  31. assert(ia[1] == 1);
  32. assert(ia[2] == 3);
  33. assert(ia[3] == 4);
  34. assert(ia[4] == 3);
  35. assert(ia[5] == 4);
  36. }
  37. #ifdef _LIBCPP_MOVE
  38. struct pred
  39. {
  40. bool operator()(const std::unique_ptr<int>& i) {return *i == 2;}
  41. };
  42. template <class Iter>
  43. void
  44. test1()
  45. {
  46. const unsigned sa = 9;
  47. std::unique_ptr<int> ia[sa];
  48. ia[0].reset(new int(0));
  49. ia[1].reset(new int(1));
  50. ia[2].reset(new int(2));
  51. ia[3].reset(new int(3));
  52. ia[4].reset(new int(4));
  53. ia[5].reset(new int(2));
  54. ia[6].reset(new int(3));
  55. ia[7].reset(new int(4));
  56. ia[8].reset(new int(2));
  57. Iter r = std::remove_if(Iter(ia), Iter(ia+sa), pred());
  58. assert(base(r) == ia + sa-3);
  59. assert(*ia[0] == 0);
  60. assert(*ia[1] == 1);
  61. assert(*ia[2] == 3);
  62. assert(*ia[3] == 4);
  63. assert(*ia[4] == 3);
  64. assert(*ia[5] == 4);
  65. }
  66. #endif // _LIBCPP_MOVE
  67. int main()
  68. {
  69. test<forward_iterator<int*> >();
  70. test<bidirectional_iterator<int*> >();
  71. test<random_access_iterator<int*> >();
  72. test<int*>();
  73. #ifdef _LIBCPP_MOVE
  74. test1<forward_iterator<std::unique_ptr<int>*> >();
  75. test1<bidirectional_iterator<std::unique_ptr<int>*> >();
  76. test1<random_access_iterator<std::unique_ptr<int>*> >();
  77. test1<std::unique_ptr<int>*>();
  78. #endif // _LIBCPP_MOVE
  79. }