test_nth_element.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 TestNthElement
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/command_queue.hpp>
  13. #include <boost/compute/algorithm/copy_n.hpp>
  14. #include <boost/compute/algorithm/is_partitioned.hpp>
  15. #include <boost/compute/algorithm/nth_element.hpp>
  16. #include <boost/compute/algorithm/partition_point.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "check_macros.hpp"
  19. #include "context_setup.hpp"
  20. BOOST_AUTO_TEST_CASE(nth_element_int)
  21. {
  22. int data[] = { 9, 15, 1, 4, 9, 9, 4, 15, 12, 1 };
  23. boost::compute::vector<int> vector(10, context);
  24. boost::compute::copy_n(data, 10, vector.begin(), queue);
  25. boost::compute::nth_element(
  26. vector.begin(), vector.begin() + 5, vector.end(), queue
  27. );
  28. BOOST_CHECK_EQUAL(vector[5], 9);
  29. BOOST_VERIFY(boost::compute::is_partitioned(
  30. vector.begin(), vector.end(), boost::compute::_1 <= 9, queue
  31. ));
  32. BOOST_VERIFY(boost::compute::partition_point(
  33. vector.begin(), vector.end(), boost::compute::_1 <= 9, queue
  34. ) > vector.begin() + 5);
  35. boost::compute::copy_n(data, 10, vector.begin(), queue);
  36. boost::compute::nth_element(
  37. vector.begin(), vector.end(), vector.end(), queue
  38. );
  39. CHECK_RANGE_EQUAL(int, 10, vector, (9, 15, 1, 4, 9, 9, 4, 15, 12, 1));
  40. }
  41. BOOST_AUTO_TEST_CASE(nth_element_median)
  42. {
  43. int data[] = { 5, 6, 4, 3, 2, 6, 7, 9, 3 };
  44. boost::compute::vector<int> v(9, context);
  45. boost::compute::copy_n(data, 9, v.begin(), queue);
  46. boost::compute::nth_element(v.begin(), v.begin() + 4, v.end(), queue);
  47. BOOST_CHECK_EQUAL(v[4], 5);
  48. BOOST_VERIFY(boost::compute::is_partitioned(
  49. v.begin(), v.end(), boost::compute::_1 <= 5, queue
  50. ));
  51. BOOST_VERIFY(boost::compute::partition_point(
  52. v.begin(), v.end(), boost::compute::_1 <= 5, queue
  53. ) > v.begin() + 4);
  54. }
  55. BOOST_AUTO_TEST_CASE(nth_element_second_largest)
  56. {
  57. int data[] = { 5, 6, 4, 3, 2, 6, 7, 9, 3 };
  58. boost::compute::vector<int> v(9, context);
  59. boost::compute::copy_n(data, 9, v.begin(), queue);
  60. boost::compute::nth_element(v.begin(), v.begin() + 1, v.end(), queue);
  61. BOOST_CHECK_EQUAL(v[1], 3);
  62. BOOST_VERIFY(boost::compute::is_partitioned(
  63. v.begin(), v.end(), boost::compute::_1 <= 3, queue
  64. ));
  65. BOOST_VERIFY(boost::compute::partition_point(
  66. v.begin(), v.end(), boost::compute::_1 <= 3, queue
  67. ) > v.begin() + 1);
  68. }
  69. BOOST_AUTO_TEST_CASE(nth_element_comparator)
  70. {
  71. int data[] = { 9, 15, 1, 4, 9, 9, 4, 15, 12, 1 };
  72. boost::compute::vector<int> vector(10, context);
  73. boost::compute::less<int> less_than;
  74. boost::compute::copy_n(data, 10, vector.begin(), queue);
  75. boost::compute::nth_element(
  76. vector.begin(), vector.begin() + 5, vector.end(), less_than, queue
  77. );
  78. BOOST_CHECK_EQUAL(vector[5], 9);
  79. BOOST_VERIFY(boost::compute::is_partitioned(
  80. vector.begin(), vector.end(), boost::compute::_1 <= 9, queue
  81. ));
  82. BOOST_VERIFY(boost::compute::partition_point(
  83. vector.begin(), vector.end(), boost::compute::_1 <= 9, queue
  84. ) > vector.begin() + 5);
  85. boost::compute::copy_n(data, 10, vector.begin(), queue);
  86. boost::compute::nth_element(
  87. vector.begin(), vector.end(), vector.end(), less_than, queue
  88. );
  89. CHECK_RANGE_EQUAL(int, 10, vector, (9, 15, 1, 4, 9, 9, 4, 15, 12, 1));
  90. }
  91. BOOST_AUTO_TEST_SUITE_END()