test_find.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 TestFind
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/command_queue.hpp>
  13. #include <boost/compute/lambda.hpp>
  14. #include <boost/compute/algorithm/copy.hpp>
  15. #include <boost/compute/algorithm/find.hpp>
  16. #include <boost/compute/algorithm/find_if.hpp>
  17. #include <boost/compute/algorithm/find_if_not.hpp>
  18. #include <boost/compute/container/vector.hpp>
  19. #include <boost/compute/iterator/constant_buffer_iterator.hpp>
  20. #include "check_macros.hpp"
  21. #include "context_setup.hpp"
  22. namespace bc = boost::compute;
  23. namespace compute = boost::compute;
  24. BOOST_AUTO_TEST_CASE(find_int)
  25. {
  26. int data[] = { 9, 15, 1, 4, 9, 9, 4, 15, 12, 1 };
  27. bc::vector<int> vector(data, data + 10, queue);
  28. bc::vector<int>::iterator iter =
  29. bc::find(vector.begin(), vector.end(), 4, queue);
  30. BOOST_CHECK(iter == vector.begin() + 3);
  31. BOOST_CHECK_EQUAL(*iter, 4);
  32. iter = bc::find(vector.begin(), vector.end(), 12, queue);
  33. BOOST_CHECK(iter == vector.begin() + 8);
  34. BOOST_CHECK_EQUAL(*iter, 12);
  35. iter = bc::find(vector.begin(), vector.end(), 1, queue);
  36. BOOST_CHECK(iter == vector.begin() + 2);
  37. BOOST_CHECK_EQUAL(*iter, 1);
  38. iter = bc::find(vector.begin(), vector.end(), 9, queue);
  39. BOOST_CHECK(iter == vector.begin());
  40. BOOST_CHECK_EQUAL(*iter, 9);
  41. iter = bc::find(vector.begin(), vector.end(), 100, queue);
  42. BOOST_CHECK(iter == vector.end());
  43. }
  44. BOOST_AUTO_TEST_CASE(find_int2)
  45. {
  46. using bc::int2_;
  47. int data[] = { 1, 2, 4, 5, 7, 8 };
  48. bc::vector<int2_> vector(
  49. reinterpret_cast<int2_ *>(data),
  50. reinterpret_cast<int2_ *>(data) + 3,
  51. queue
  52. );
  53. CHECK_RANGE_EQUAL(int2_, 3, vector, (int2_(1, 2), int2_(4, 5), int2_(7, 8)));
  54. bc::vector<int2_>::iterator iter =
  55. bc::find(vector.begin(), vector.end(), int2_(4, 5), queue);
  56. BOOST_CHECK(iter == vector.begin() + 1);
  57. BOOST_CHECK_EQUAL(*iter, int2_(4, 5));
  58. iter = bc::find(vector.begin(), vector.end(), int2_(10, 11), queue);
  59. BOOST_CHECK(iter == vector.end());
  60. }
  61. BOOST_AUTO_TEST_CASE(find_if_not_int)
  62. {
  63. int data[] = { 2, 4, 6, 8, 1, 3, 5, 7, 9 };
  64. bc::vector<int> vector(data, data + 9, queue);
  65. bc::vector<int>::iterator iter =
  66. bc::find_if_not(vector.begin(), vector.end(), bc::_1 == 2, queue);
  67. BOOST_CHECK(iter == vector.begin() + 1);
  68. BOOST_CHECK_EQUAL(*iter, 4);
  69. }
  70. BOOST_AUTO_TEST_CASE(find_point_by_distance)
  71. {
  72. using boost::compute::float2_;
  73. using boost::compute::lambda::_1;
  74. using boost::compute::lambda::distance;
  75. float2_ points[] = {
  76. float2_(0, 0), float2_(2, 2), float2_(4, 4), float2_(8, 8)
  77. };
  78. compute::vector<float2_> vec(points, points + 4, queue);
  79. compute::vector<float2_>::iterator iter =
  80. compute::find_if(vec.begin(), vec.end(), distance(_1, float2_(5, 5)) < 1.5f, queue);
  81. BOOST_CHECK(iter == vec.begin() + 2);
  82. float2_ value;
  83. compute::copy_n(iter, 1, &value, queue);
  84. BOOST_CHECK_EQUAL(value, float2_(4, 4));
  85. }
  86. BOOST_AUTO_TEST_SUITE_END()