remove.pass.cpp 2.5 KB

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