test_binary_search.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 TestBinarySearch
  11. #include <boost/test/unit_test.hpp>
  12. #include <iterator>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/algorithm/binary_search.hpp>
  15. #include <boost/compute/algorithm/fill.hpp>
  16. #include <boost/compute/algorithm/lower_bound.hpp>
  17. #include <boost/compute/algorithm/upper_bound.hpp>
  18. #include <boost/compute/container/vector.hpp>
  19. #include "context_setup.hpp"
  20. BOOST_AUTO_TEST_CASE(binary_search_int)
  21. {
  22. // test data = { 1, ..., 2, ..., 4, 4, 5, 7, ..., 9, ..., 10 }
  23. boost::compute::vector<int> vector(size_t(4096), int(1), queue);
  24. boost::compute::vector<int>::iterator first = vector.begin() + 128;
  25. boost::compute::vector<int>::iterator last = first + (1024 - 128);
  26. boost::compute::fill(first, last, int(2), queue);
  27. last.write(4, queue); last++;
  28. last.write(4, queue); last++;
  29. last.write(5, queue); last++;
  30. first = last;
  31. last = first + 127;
  32. boost::compute::fill(first, last, 7, queue);
  33. first = last;
  34. last = vector.end() - 1;
  35. boost::compute::fill(first, last, 9, queue);
  36. last.write(10, queue);
  37. queue.finish();
  38. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(0), queue) == false);
  39. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(1), queue) == true);
  40. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(2), queue) == true);
  41. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(3), queue) == false);
  42. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(4), queue) == true);
  43. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(5), queue) == true);
  44. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(6), queue) == false);
  45. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(7), queue) == true);
  46. BOOST_CHECK(boost::compute::binary_search(vector.begin(), vector.end(), int(8), queue) == false);
  47. }
  48. BOOST_AUTO_TEST_CASE(range_bounds_int)
  49. {
  50. // test data = { 1, ..., 2, ..., 4, 4, 5, 7, ..., 9, ..., 10 }
  51. boost::compute::vector<int> vector(size_t(4096), int(1), queue);
  52. boost::compute::vector<int>::iterator first = vector.begin() + 128;
  53. boost::compute::vector<int>::iterator last = first + (1024 - 128);
  54. boost::compute::fill(first, last, int(2), queue);
  55. last.write(4, queue); last++; // 1024
  56. last.write(4, queue); last++; // 1025
  57. last.write(5, queue); last++; // 1026
  58. first = last;
  59. last = first + 127;
  60. boost::compute::fill(first, last, 7, queue);
  61. first = last;
  62. last = vector.end() - 1;
  63. boost::compute::fill(first, last, 9, queue);
  64. last.write(10, queue);
  65. queue.finish();
  66. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(0), queue) == vector.begin());
  67. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(0), queue) == vector.begin());
  68. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(1), queue) == vector.begin());
  69. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(1), queue) == vector.begin() + 128);
  70. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(2), queue) == vector.begin() + 128);
  71. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(2), queue) == vector.begin() + 1024);
  72. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(4), queue) == vector.begin() + 1024);
  73. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(4), queue) == vector.begin() + 1026);
  74. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(5), queue) == vector.begin() + 1026);
  75. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(5), queue) == vector.begin() + 1027);
  76. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(6), queue) == vector.begin() + 1027);
  77. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(6), queue) == vector.begin() + 1027);
  78. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(7), queue) == vector.begin() + 1027);
  79. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(7), queue) == vector.begin() + (1027 + 127));
  80. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(9), queue) == vector.begin() + (1027 + 127));
  81. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(9), queue) == vector.end() - 1);
  82. BOOST_CHECK(boost::compute::lower_bound(vector.begin(), vector.end(), int(10), queue) == vector.end() - 1);
  83. BOOST_CHECK(boost::compute::upper_bound(vector.begin(), vector.end(), int(10), queue) == vector.end());
  84. }
  85. BOOST_AUTO_TEST_SUITE_END()