test_stable_sort.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 TestStableSort
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/function.hpp>
  14. #include <boost/compute/algorithm/stable_sort.hpp>
  15. #include <boost/compute/algorithm/is_sorted.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace compute = boost::compute;
  20. BOOST_AUTO_TEST_CASE(sort_int_vector)
  21. {
  22. int data[] = { -4, 152, -5000, 963, 75321, -456, 0, 1112 };
  23. compute::vector<int> vector(data, data + 8, queue);
  24. BOOST_CHECK_EQUAL(vector.size(), size_t(8));
  25. BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == false);
  26. compute::stable_sort(vector.begin(), vector.end(), queue);
  27. BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == true);
  28. CHECK_RANGE_EQUAL(int, 8, vector, (-5000, -456, -4, 0, 152, 963, 1112, 75321));
  29. // sort reversed
  30. compute::stable_sort(vector.begin(), vector.end(), compute::greater<int>(), queue);
  31. CHECK_RANGE_EQUAL(int, 8, vector, (75321, 1112, 963, 152, 0, -4, -456, -5000));
  32. }
  33. BOOST_AUTO_TEST_CASE(sort_int2)
  34. {
  35. using compute::int2_;
  36. // device vector of int2's
  37. compute::vector<int2_> vec(context);
  38. vec.push_back(int2_(2, 1), queue);
  39. vec.push_back(int2_(2, 2), queue);
  40. vec.push_back(int2_(1, 2), queue);
  41. vec.push_back(int2_(1, 1), queue);
  42. // function comparing the first component of each int2
  43. BOOST_COMPUTE_FUNCTION(bool, compare_first, (int2_ a, int2_ b),
  44. {
  45. return a.x < b.x;
  46. });
  47. // ensure vector is not sorted
  48. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == false);
  49. // sort elements based on their first component
  50. compute::stable_sort(vec.begin(), vec.end(), compare_first, queue);
  51. // ensure vector is now sorted
  52. BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == true);
  53. // check sorted vector order
  54. std::vector<int2_> result(vec.size());
  55. compute::copy(vec.begin(), vec.end(), result.begin(), queue);
  56. BOOST_CHECK_EQUAL(result[0], int2_(1, 2));
  57. BOOST_CHECK_EQUAL(result[1], int2_(1, 1));
  58. BOOST_CHECK_EQUAL(result[2], int2_(2, 1));
  59. BOOST_CHECK_EQUAL(result[3], int2_(2, 2));
  60. // function comparing the second component of each int2
  61. BOOST_COMPUTE_FUNCTION(bool, compare_second, (int2_ a, int2_ b),
  62. {
  63. return a.y < b.y;
  64. });
  65. // sort elements based on their second component
  66. compute::stable_sort(vec.begin(), vec.end(), compare_second, queue);
  67. // check sorted vector order
  68. compute::copy(vec.begin(), vec.end(), result.begin(), queue);
  69. BOOST_CHECK_EQUAL(result[0], int2_(1, 1));
  70. BOOST_CHECK_EQUAL(result[1], int2_(2, 1));
  71. BOOST_CHECK_EQUAL(result[2], int2_(1, 2));
  72. BOOST_CHECK_EQUAL(result[3], int2_(2, 2));
  73. }
  74. BOOST_AUTO_TEST_SUITE_END()