remove_copy.pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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<InputIterator InIter, OutputIterator<auto, InIter::reference> OutIter, class T>
  10. // requires HasEqualTo<InIter::value_type, T>
  11. // constexpr OutIter // constexpr after C++17
  12. // remove_copy(InIter first, InIter last, OutIter result, const T& value);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. #if TEST_STD_VER > 17
  18. TEST_CONSTEXPR bool test_constexpr() {
  19. int ia[] = {1, 3, 5, 2, 5, 6};
  20. int ib[std::size(ia)] = {0};
  21. auto it = std::remove_copy(std::begin(ia), std::end(ia), std::begin(ib), 5);
  22. return std::distance(std::begin(ib), it) == (std::size(ia) - 2) // we removed two elements
  23. && std::none_of(std::begin(ib), it, [](int a) {return a == 5;})
  24. && std::all_of (it, std::end(ib), [](int a) {return a == 0;})
  25. ;
  26. }
  27. #endif
  28. template <class InIter, class OutIter>
  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. int ib[sa];
  35. OutIter r = std::remove_copy(InIter(ia), InIter(ia+sa), OutIter(ib), 2);
  36. assert(base(r) == ib + sa-3);
  37. assert(ib[0] == 0);
  38. assert(ib[1] == 1);
  39. assert(ib[2] == 3);
  40. assert(ib[3] == 4);
  41. assert(ib[4] == 3);
  42. assert(ib[5] == 4);
  43. }
  44. int main(int, char**)
  45. {
  46. test<input_iterator<const int*>, output_iterator<int*> >();
  47. test<input_iterator<const int*>, forward_iterator<int*> >();
  48. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  49. test<input_iterator<const int*>, random_access_iterator<int*> >();
  50. test<input_iterator<const int*>, int*>();
  51. test<forward_iterator<const int*>, output_iterator<int*> >();
  52. test<forward_iterator<const int*>, forward_iterator<int*> >();
  53. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  54. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  55. test<forward_iterator<const int*>, int*>();
  56. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  57. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  58. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  59. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  60. test<bidirectional_iterator<const int*>, int*>();
  61. test<random_access_iterator<const int*>, output_iterator<int*> >();
  62. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  63. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  64. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  65. test<random_access_iterator<const int*>, int*>();
  66. test<const int*, output_iterator<int*> >();
  67. test<const int*, forward_iterator<int*> >();
  68. test<const int*, bidirectional_iterator<int*> >();
  69. test<const int*, random_access_iterator<int*> >();
  70. test<const int*, int*>();
  71. #if TEST_STD_VER > 17
  72. static_assert(test_constexpr());
  73. #endif
  74. return 0;
  75. }