test_unique.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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 TestUnique
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/function.hpp>
  13. #include <boost/compute/algorithm/iota.hpp>
  14. #include <boost/compute/algorithm/none_of.hpp>
  15. #include <boost/compute/algorithm/transform.hpp>
  16. #include <boost/compute/algorithm/unique.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include "check_macros.hpp"
  19. #include "context_setup.hpp"
  20. namespace compute = boost::compute;
  21. BOOST_AUTO_TEST_CASE(unique_int)
  22. {
  23. int data[] = {1, 6, 6, 4, 2, 2, 4};
  24. compute::vector<int> input(data, data + 7, queue);
  25. compute::vector<int>::iterator iter =
  26. compute::unique(input.begin(), input.end(), queue);
  27. BOOST_VERIFY(iter == input.begin() + 5);
  28. CHECK_RANGE_EQUAL(int, 7, input, (1, 6, 4, 2, 4, 2, 4));
  29. }
  30. BOOST_AUTO_TEST_CASE(all_same_float)
  31. {
  32. compute::vector<float> vec(1024, context);
  33. compute::fill(vec.begin(), vec.end(), 3.14f, queue);
  34. compute::vector<float>::iterator iter =
  35. compute::unique(vec.begin(), vec.end(), queue);
  36. BOOST_VERIFY(iter == vec.begin() + 1);
  37. float first;
  38. compute::copy_n(vec.begin(), 1, &first, queue);
  39. BOOST_CHECK_EQUAL(first, 3.14f);
  40. }
  41. BOOST_AUTO_TEST_CASE(unique_even_uints)
  42. {
  43. using compute::uint_;
  44. // create vector filled with [0, 1, 2, ...]
  45. compute::vector<uint_> vec(1024, context);
  46. compute::iota(vec.begin(), vec.end(), 0, queue);
  47. // all should be unique
  48. compute::vector<uint_>::iterator iter = compute::unique(
  49. vec.begin(), vec.end(), queue
  50. );
  51. BOOST_VERIFY(iter == vec.end());
  52. // if odd, return the prior even number, else return the number
  53. BOOST_COMPUTE_FUNCTION(uint_, odd_to_even, (uint_ x),
  54. {
  55. if(x & 1){
  56. return x - 1;
  57. }
  58. else {
  59. return x;
  60. }
  61. });
  62. // set all odd numbers the previous even number
  63. compute::transform(
  64. vec.begin(), vec.end(), vec.begin(), odd_to_even, queue
  65. );
  66. // now the vector should contain [0, 0, 2, 2, 4, 4, ...]
  67. iter = compute::unique(vec.begin(), vec.end(), queue);
  68. BOOST_VERIFY(iter == vec.begin() + (vec.size() / 2));
  69. // ensure all of the values are even
  70. BOOST_COMPUTE_FUNCTION(bool, is_odd, (uint_ x),
  71. {
  72. return x & 1;
  73. });
  74. BOOST_VERIFY(compute::none_of(vec.begin(), vec.end(), is_odd, queue));
  75. }
  76. BOOST_AUTO_TEST_SUITE_END()