remove_copy.pass.cpp 3.2 KB

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