copy_if.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <range/v3/algorithm/copy_if.hpp>
  12. #include "../array.hpp"
  13. #include "../simple_test.hpp"
  14. constexpr bool is_even(int i)
  15. {
  16. return i % 2 == 0;
  17. }
  18. constexpr bool test_constexpr()
  19. {
  20. using namespace ranges;
  21. test::array<int, 4> a{{1, 2, 3, 4}};
  22. test::array<int, 4> b{{0, 0, 0, 0}};
  23. const auto res = copy_if(a, ranges::begin(b), is_even);
  24. STATIC_CHECK_RETURN(res.in == end(a));
  25. STATIC_CHECK_RETURN(res.out == begin(b) + 2);
  26. STATIC_CHECK_RETURN(a[0] == 1);
  27. STATIC_CHECK_RETURN(a[1] == 2);
  28. STATIC_CHECK_RETURN(a[2] == 3);
  29. STATIC_CHECK_RETURN(a[3] == 4);
  30. STATIC_CHECK_RETURN(b[0] == 2);
  31. STATIC_CHECK_RETURN(b[1] == 4);
  32. STATIC_CHECK_RETURN(b[2] == 0);
  33. STATIC_CHECK_RETURN(b[3] == 0);
  34. return true;
  35. }
  36. int main()
  37. {
  38. STATIC_CHECK(test_constexpr());
  39. return test_result();
  40. }