test_adjacent_difference.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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 TestAdjacentDifference
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/lambda.hpp>
  15. #include <boost/compute/algorithm/iota.hpp>
  16. #include <boost/compute/algorithm/copy.hpp>
  17. #include <boost/compute/algorithm/fill.hpp>
  18. #include <boost/compute/algorithm/all_of.hpp>
  19. #include <boost/compute/algorithm/adjacent_difference.hpp>
  20. #include <boost/compute/container/vector.hpp>
  21. #include "check_macros.hpp"
  22. #include "context_setup.hpp"
  23. namespace compute = boost::compute;
  24. BOOST_AUTO_TEST_CASE(adjacent_difference_int)
  25. {
  26. using compute::int_;
  27. compute::vector<int_> a(5, context);
  28. compute::iota(a.begin(), a.end(), 0, queue);
  29. CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 2, 3, 4));
  30. compute::vector<int_> b(5, context);
  31. compute::vector<int_>::iterator iter =
  32. compute::adjacent_difference(a.begin(), a.end(), b.begin(), queue);
  33. BOOST_CHECK(iter == b.end());
  34. CHECK_RANGE_EQUAL(int_, 5, b, (0, 1, 1, 1, 1));
  35. int_ data[] = { 1, 9, 36, 48, 81 };
  36. compute::copy(data, data + 5, a.begin(), queue);
  37. CHECK_RANGE_EQUAL(int_, 5, a, (1, 9, 36, 48, 81));
  38. iter = compute::adjacent_difference(a.begin(), a.end(), b.begin(), queue);
  39. BOOST_CHECK(iter == b.end());
  40. CHECK_RANGE_EQUAL(int_, 5, b, (1, 8, 27, 12, 33));
  41. }
  42. BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_last)
  43. {
  44. using compute::int_;
  45. compute::vector<int_> a(size_t(5), int_(1), queue);
  46. compute::vector<int_> b(size_t(5), int_(0), queue);
  47. compute::vector<int_>::iterator iter =
  48. compute::adjacent_difference(a.begin(), a.begin(), b.begin(), queue);
  49. BOOST_CHECK(iter == b.begin());
  50. CHECK_RANGE_EQUAL(int_, 5, b, (0, 0, 0, 0, 0));
  51. }
  52. BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_result)
  53. {
  54. using compute::int_;
  55. compute::vector<int_> a(5, context);
  56. compute::iota(a.begin(), a.end(), 0, queue);
  57. CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 2, 3, 4));
  58. compute::vector<int_>::iterator iter =
  59. compute::adjacent_difference(a.begin(), a.end(), a.begin(), queue);
  60. BOOST_CHECK(iter == a.end());
  61. CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 1, 1, 1));
  62. }
  63. BOOST_AUTO_TEST_CASE(all_same)
  64. {
  65. using compute::int_;
  66. compute::vector<int_> input(1000, context);
  67. compute::fill(input.begin(), input.end(), 42, queue);
  68. compute::vector<int_> output(input.size(), context);
  69. compute::adjacent_difference(
  70. input.begin(), input.end(), output.begin(), queue
  71. );
  72. int_ first;
  73. compute::copy_n(output.begin(), 1, &first, queue);
  74. BOOST_CHECK_EQUAL(first, 42);
  75. using compute::lambda::_1;
  76. BOOST_CHECK(
  77. compute::all_of(output.begin() + 1, output.end(), _1 == 0, queue)
  78. );
  79. }
  80. BOOST_AUTO_TEST_SUITE_END()