test_reverse.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 TestReverse
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/compute/system.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/algorithm/copy.hpp>
  15. #include <boost/compute/algorithm/iota.hpp>
  16. #include <boost/compute/algorithm/reverse.hpp>
  17. #include <boost/compute/algorithm/reverse_copy.hpp>
  18. #include <boost/compute/container/vector.hpp>
  19. #include <boost/compute/iterator/counting_iterator.hpp>
  20. #include "check_macros.hpp"
  21. #include "context_setup.hpp"
  22. namespace bc = boost::compute;
  23. BOOST_AUTO_TEST_CASE(reverse_int)
  24. {
  25. bc::vector<int> vector(5, context);
  26. bc::iota(vector.begin(), vector.end(), 0, queue);
  27. CHECK_RANGE_EQUAL(int, 5, vector, (0, 1, 2, 3, 4));
  28. bc::reverse(vector.begin(), vector.end(), queue);
  29. CHECK_RANGE_EQUAL(int, 5, vector, (4, 3, 2, 1, 0));
  30. bc::reverse(vector.begin() + 1, vector.end(), queue);
  31. CHECK_RANGE_EQUAL(int, 5, vector, (4, 0, 1, 2, 3));
  32. bc::reverse(vector.begin() + 1, vector.end() - 1, queue);
  33. CHECK_RANGE_EQUAL(int, 5, vector, (4, 2, 1, 0, 3));
  34. bc::reverse(vector.begin(), vector.end() - 2, queue);
  35. CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 4, 0, 3));
  36. vector.resize(6, queue);
  37. bc::iota(vector.begin(), vector.end(), 10, queue);
  38. CHECK_RANGE_EQUAL(int, 6, vector, (10, 11, 12, 13, 14, 15));
  39. bc::reverse(vector.begin(), vector.end(), queue);
  40. CHECK_RANGE_EQUAL(int, 6, vector, (15, 14, 13, 12, 11, 10));
  41. bc::reverse(vector.begin() + 3, vector.end(), queue);
  42. CHECK_RANGE_EQUAL(int, 6, vector, (15, 14, 13, 10, 11, 12));
  43. bc::reverse(vector.begin() + 1, vector.end() - 2, queue);
  44. CHECK_RANGE_EQUAL(int, 6, vector, (15, 10, 13, 14, 11, 12));
  45. }
  46. BOOST_AUTO_TEST_CASE(reverse_copy_int)
  47. {
  48. bc::vector<int> a(5, context);
  49. bc::iota(a.begin(), a.end(), 0, queue);
  50. CHECK_RANGE_EQUAL(int, 5, a, (0, 1, 2, 3, 4));
  51. bc::vector<int> b(5, context);
  52. bc::vector<int>::iterator iter =
  53. bc::reverse_copy(a.begin(), a.end(), b.begin(), queue);
  54. BOOST_CHECK(iter == b.end());
  55. CHECK_RANGE_EQUAL(int, 5, b, (4, 3, 2, 1, 0));
  56. iter = bc::reverse_copy(b.begin() + 1, b.end(), a.begin() + 1, queue);
  57. BOOST_CHECK(iter == a.end());
  58. CHECK_RANGE_EQUAL(int, 5, a, (0, 0, 1, 2, 3));
  59. iter = bc::reverse_copy(a.begin(), a.end() - 1, b.begin(), queue);
  60. BOOST_CHECK(iter == (b.end() - 1));
  61. CHECK_RANGE_EQUAL(int, 5, b, (2, 1, 0, 0, 0));
  62. }
  63. BOOST_AUTO_TEST_CASE(reverse_copy_counting_iterator)
  64. {
  65. bc::vector<int> vector(5, context);
  66. bc::reverse_copy(
  67. bc::make_counting_iterator(1),
  68. bc::make_counting_iterator(6),
  69. vector.begin(),
  70. queue
  71. );
  72. CHECK_RANGE_EQUAL(int, 5, vector, (5, 4, 3, 2, 1));
  73. }
  74. BOOST_AUTO_TEST_SUITE_END()