test_sort_by_transform.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 TestSortByTransform
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/algorithm/copy_n.hpp>
  14. #include <boost/compute/algorithm/is_sorted.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include <boost/compute/experimental/sort_by_transform.hpp>
  17. #include "check_macros.hpp"
  18. #include "context_setup.hpp"
  19. namespace compute = boost::compute;
  20. BOOST_AUTO_TEST_CASE(sort_int_by_abs)
  21. {
  22. int data[] = { 1, -2, 4, -3, 0, 5, -8, -9 };
  23. compute::vector<int> vector(data, data + 8, queue);
  24. compute::experimental::sort_by_transform(
  25. vector.begin(),
  26. vector.end(),
  27. compute::abs<int>(),
  28. compute::less<int>(),
  29. queue
  30. );
  31. CHECK_RANGE_EQUAL(int, 8, vector, (0, 1, -2, -3, 4, 5, -8, -9));
  32. }
  33. BOOST_AUTO_TEST_CASE(sort_vectors_by_length)
  34. {
  35. using compute::float4_;
  36. float data[] = {
  37. 1.0f, 0.0f, 0.0f, 0.0f,
  38. 0.0f, 1.0f, 1.0f, 0.0f,
  39. 3.0f, 2.0f, 1.0f, 0.0f,
  40. 0.0f, 0.0f, 0.5f, 0.0f
  41. };
  42. compute::vector<float4_> vector(4, context);
  43. compute::copy_n(
  44. reinterpret_cast<float4_ *>(data), 4, vector.begin(), queue
  45. );
  46. compute::experimental::sort_by_transform(
  47. vector.begin(),
  48. vector.end(),
  49. compute::length<float4_>(),
  50. compute::less<float>(),
  51. queue
  52. );
  53. std::vector<float4_> host_vector(4);
  54. compute::copy(
  55. vector.begin(), vector.end(), host_vector.begin(), queue
  56. );
  57. BOOST_CHECK_EQUAL(host_vector[0], float4_(0.0f, 0.0f, 0.5f, 0.0f));
  58. BOOST_CHECK_EQUAL(host_vector[1], float4_(1.0f, 0.0f, 0.0f, 0.0f));
  59. BOOST_CHECK_EQUAL(host_vector[2], float4_(0.0f, 1.0f, 1.0f, 0.0f));
  60. BOOST_CHECK_EQUAL(host_vector[3], float4_(3.0f, 2.0f, 1.0f, 0.0f));
  61. }
  62. BOOST_AUTO_TEST_CASE(sort_vectors_by_component)
  63. {
  64. using compute::float4_;
  65. float data[] = {
  66. 1.0f, 2.0f, 3.0f, 0.0f,
  67. 9.0f, 8.0f, 7.0f, 0.0f,
  68. 4.0f, 5.0f, 6.0f, 0.0f,
  69. 0.0f, 0.0f, 0.0f, 0.0f
  70. };
  71. compute::vector<float4_> vector(4, context);
  72. compute::copy_n(
  73. reinterpret_cast<float4_ *>(data), 4, vector.begin(), queue
  74. );
  75. // sort by y-component
  76. compute::experimental::sort_by_transform(
  77. vector.begin(),
  78. vector.end(),
  79. compute::get<1>(),
  80. compute::less<float>(),
  81. queue
  82. );
  83. std::vector<float4_> host_vector(4);
  84. compute::copy(
  85. vector.begin(), vector.end(), host_vector.begin(), queue
  86. );
  87. BOOST_CHECK_EQUAL(host_vector[0], float4_(0.0f, 0.0f, 0.0f, 0.0f));
  88. BOOST_CHECK_EQUAL(host_vector[1], float4_(1.0f, 2.0f, 3.0f, 0.0f));
  89. BOOST_CHECK_EQUAL(host_vector[2], float4_(4.0f, 5.0f, 6.0f, 0.0f));
  90. BOOST_CHECK_EQUAL(host_vector[3], float4_(9.0f, 8.0f, 7.0f, 0.0f));
  91. }
  92. BOOST_AUTO_TEST_SUITE_END()