copy_if.pass.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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,
  10. // Predicate<auto, InIter::value_type> Pred>
  11. // requires CopyConstructible<Pred>
  12. // constexpr OutIter // constexpr after C++17
  13. // copy_if(InIter first, InIter last, OutIter result, Pred pred);
  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[] = {2, 4, 6, 8, 6};
  21. // int ic[] = {0, 0, 0, 0, 0, 0};
  22. //
  23. // auto p = std::copy_if(std::begin(ia), std::end(ia), std::begin(ic), is6);
  24. // return std::all_of(std::begin(ic), p, [](int a){return a == 6;})
  25. // && std::all_of(p, std::end(ic), [](int a){return a == 0;})
  26. // ;
  27. // }
  28. // #endif
  29. struct Pred
  30. {
  31. bool operator()(int i) {return i % 3 == 0;}
  32. };
  33. template <class InIter, class OutIter>
  34. void
  35. test()
  36. {
  37. const unsigned N = 1000;
  38. int ia[N];
  39. for (unsigned i = 0; i < N; ++i)
  40. ia[i] = i;
  41. int ib[N] = {0};
  42. OutIter r = std::copy_if(InIter(ia), InIter(ia+N), OutIter(ib), Pred());
  43. assert(base(r) == ib+N/3+1);
  44. for (unsigned i = 0; i < N/3+1; ++i)
  45. assert(ib[i] % 3 == 0);
  46. }
  47. int main(int, char**)
  48. {
  49. test<input_iterator<const int*>, output_iterator<int*> >();
  50. test<input_iterator<const int*>, input_iterator<int*> >();
  51. test<input_iterator<const int*>, forward_iterator<int*> >();
  52. test<input_iterator<const int*>, bidirectional_iterator<int*> >();
  53. test<input_iterator<const int*>, random_access_iterator<int*> >();
  54. test<input_iterator<const int*>, int*>();
  55. test<forward_iterator<const int*>, output_iterator<int*> >();
  56. test<forward_iterator<const int*>, input_iterator<int*> >();
  57. test<forward_iterator<const int*>, forward_iterator<int*> >();
  58. test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
  59. test<forward_iterator<const int*>, random_access_iterator<int*> >();
  60. test<forward_iterator<const int*>, int*>();
  61. test<bidirectional_iterator<const int*>, output_iterator<int*> >();
  62. test<bidirectional_iterator<const int*>, input_iterator<int*> >();
  63. test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
  64. test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
  65. test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
  66. test<bidirectional_iterator<const int*>, int*>();
  67. test<random_access_iterator<const int*>, output_iterator<int*> >();
  68. test<random_access_iterator<const int*>, input_iterator<int*> >();
  69. test<random_access_iterator<const int*>, forward_iterator<int*> >();
  70. test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
  71. test<random_access_iterator<const int*>, random_access_iterator<int*> >();
  72. test<random_access_iterator<const int*>, int*>();
  73. test<const int*, output_iterator<int*> >();
  74. test<const int*, input_iterator<int*> >();
  75. test<const int*, forward_iterator<int*> >();
  76. test<const int*, bidirectional_iterator<int*> >();
  77. test<const int*, random_access_iterator<int*> >();
  78. test<const int*, int*>();
  79. // #if TEST_STD_VER > 17
  80. // static_assert(test_constexpr());
  81. // #endif
  82. return 0;
  83. }