remove.pass.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, class T>
  11. // requires OutputIterator<Iter, RvalueOf<Iter::reference>::type>
  12. // && HasEqualTo<Iter::value_type, T>
  13. // constexpr Iter // constexpr after C++17
  14. // remove(Iter first, Iter last, const T& value);
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <memory>
  18. #include "test_macros.h"
  19. #include "test_iterators.h"
  20. #if TEST_STD_VER > 17
  21. TEST_CONSTEXPR bool test_constexpr() {
  22. int ia[] = {1, 3, 5, 2, 5, 6};
  23. auto it = std::remove(std::begin(ia), std::end(ia), 5);
  24. return (std::begin(ia) + std::size(ia) - 2) == it // we removed two elements
  25. && std::none_of(std::begin(ia), it, [](int a) {return a == 5; })
  26. ;
  27. }
  28. #endif
  29. template <class Iter>
  30. void
  31. test()
  32. {
  33. int ia[] = {0, 1, 2, 3, 4, 2, 3, 4, 2};
  34. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  35. Iter r = std::remove(Iter(ia), Iter(ia+sa), 2);
  36. assert(base(r) == ia + sa-3);
  37. assert(ia[0] == 0);
  38. assert(ia[1] == 1);
  39. assert(ia[2] == 3);
  40. assert(ia[3] == 4);
  41. assert(ia[4] == 3);
  42. assert(ia[5] == 4);
  43. }
  44. #if TEST_STD_VER >= 11
  45. template <class Iter>
  46. void
  47. test1()
  48. {
  49. const unsigned sa = 9;
  50. std::unique_ptr<int> ia[sa];
  51. ia[0].reset(new int(0));
  52. ia[1].reset(new int(1));
  53. ia[3].reset(new int(3));
  54. ia[4].reset(new int(4));
  55. ia[6].reset(new int(3));
  56. ia[7].reset(new int(4));
  57. Iter r = std::remove(Iter(ia), Iter(ia+sa), std::unique_ptr<int>());
  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 // TEST_STD_VER >= 11
  67. int main()
  68. {
  69. test<forward_iterator<int*> >();
  70. test<bidirectional_iterator<int*> >();
  71. test<random_access_iterator<int*> >();
  72. test<int*>();
  73. #if TEST_STD_VER >= 11
  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 // TEST_STD_VER >= 11
  79. #if TEST_STD_VER > 17
  80. static_assert(test_constexpr());
  81. #endif
  82. }