test_copy_if.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #define BOOST_TEST_MODULE TestCopyIf
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/lambda.hpp>
  13. #include <boost/compute/algorithm/copy_if.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. #include "check_macros.hpp"
  16. #include "context_setup.hpp"
  17. namespace bc = boost::compute;
  18. namespace compute = boost::compute;
  19. BOOST_AUTO_TEST_CASE(copy_if_int)
  20. {
  21. int data[] = { 1, 6, 3, 5, 8, 2, 4 };
  22. bc::vector<int> input(data, data + 7, queue);
  23. bc::vector<int> output(input.size(), context);
  24. bc::fill(output.begin(), output.end(), -1, queue);
  25. using ::boost::compute::_1;
  26. bc::vector<int>::iterator iter =
  27. bc::copy_if(input.begin(), input.end(),
  28. output.begin(), _1 < 5, queue);
  29. BOOST_VERIFY(iter == output.begin() + 4);
  30. CHECK_RANGE_EQUAL(int, 7, output, (1, 3, 2, 4, -1, -1, -1));
  31. bc::fill(output.begin(), output.end(), 42, queue);
  32. iter =
  33. bc::copy_if(input.begin(), input.end(),
  34. output.begin(), _1 * 2 >= 10, queue);
  35. BOOST_VERIFY(iter == output.begin() + 3);
  36. CHECK_RANGE_EQUAL(int, 7, output, (6, 5, 8, 42, 42, 42, 42));
  37. }
  38. BOOST_AUTO_TEST_CASE(copy_if_odd)
  39. {
  40. int data[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
  41. bc::vector<int> input(data, data + 10, queue);
  42. using ::boost::compute::_1;
  43. bc::vector<int> odds(input.size(), context);
  44. bc::vector<int>::iterator odds_end =
  45. bc::copy_if(input.begin(), input.end(),
  46. odds.begin(), _1 % 2 == 1, queue);
  47. BOOST_CHECK(odds_end == odds.begin() + 6);
  48. CHECK_RANGE_EQUAL(int, 6, odds, (1, 3, 5, 1, 3, 5));
  49. bc::vector<int> evens(input.size(), context);
  50. bc::vector<int>::iterator evens_end =
  51. bc::copy_if(input.begin(), input.end(),
  52. evens.begin(), _1 % 2 == 0, queue);
  53. BOOST_CHECK(evens_end == evens.begin() + 4);
  54. CHECK_RANGE_EQUAL(int, 4, evens, (2, 4, 2, 4));
  55. }
  56. BOOST_AUTO_TEST_CASE(clip_points_below_plane)
  57. {
  58. float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
  59. -1.0f, 2.0f, 3.0f, 0.0f,
  60. -2.0f, -3.0f, 4.0f, 0.0f,
  61. 4.0f, -3.0f, 2.0f, 0.0f };
  62. bc::vector<bc::float4_> points(reinterpret_cast<bc::float4_ *>(data),
  63. reinterpret_cast<bc::float4_ *>(data) + 4,
  64. queue);
  65. // create output vector filled with (0, 0, 0, 0)
  66. bc::vector<bc::float4_> output(points.size(), context);
  67. bc::fill(output.begin(), output.end(),
  68. bc::float4_(0.0f, 0.0f, 0.0f, 0.0f), queue);
  69. // define the plane (at origin, +X normal)
  70. bc::float4_ plane_origin(0.0f, 0.0f, 0.0f, 0.0f);
  71. bc::float4_ plane_normal(1.0f, 0.0f, 0.0f, 0.0f);
  72. using ::boost::compute::_1;
  73. using ::boost::compute::lambda::dot;
  74. bc::vector<bc::float4_>::const_iterator iter =
  75. bc::copy_if(points.begin(),
  76. points.end(),
  77. output.begin(),
  78. dot(_1 - plane_origin, plane_normal) > 0.0f,
  79. queue);
  80. BOOST_CHECK(iter == output.begin() + 2);
  81. }
  82. BOOST_AUTO_TEST_CASE(copy_index_if_int)
  83. {
  84. int data[] = { 1, 6, 3, 5, 8, 2, 4 };
  85. compute::vector<int> input(data, data + 7, queue);
  86. compute::vector<int> output(input.size(), context);
  87. compute::fill(output.begin(), output.end(), -1, queue);
  88. using ::boost::compute::_1;
  89. using ::boost::compute::detail::copy_index_if;
  90. compute::vector<int>::iterator iter =
  91. copy_index_if(input.begin(), input.end(), output.begin(),
  92. _1 < 5, queue);
  93. BOOST_VERIFY(iter == output.begin() + 4);
  94. CHECK_RANGE_EQUAL(int, 7, output, (0, 2, 5, 6, -1, -1, -1));
  95. }
  96. BOOST_AUTO_TEST_SUITE_END()