test_permutation_iterator.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 TestPermutationIterator
  11. #include <boost/test/unit_test.hpp>
  12. #include <iterator>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/compute/types.hpp>
  16. #include <boost/compute/algorithm/copy.hpp>
  17. #include <boost/compute/container/vector.hpp>
  18. #include <boost/compute/iterator/buffer_iterator.hpp>
  19. #include <boost/compute/iterator/permutation_iterator.hpp>
  20. #include "check_macros.hpp"
  21. #include "context_setup.hpp"
  22. BOOST_AUTO_TEST_CASE(value_type)
  23. {
  24. using boost::compute::float4_;
  25. BOOST_STATIC_ASSERT((
  26. boost::is_same<
  27. boost::compute::permutation_iterator<
  28. boost::compute::buffer_iterator<float>,
  29. boost::compute::buffer_iterator<int>
  30. >::value_type,
  31. float
  32. >::value
  33. ));
  34. BOOST_STATIC_ASSERT((
  35. boost::is_same<
  36. boost::compute::permutation_iterator<
  37. boost::compute::buffer_iterator<float4_>,
  38. boost::compute::buffer_iterator<short>
  39. >::value_type,
  40. float4_
  41. >::value
  42. ));
  43. }
  44. BOOST_AUTO_TEST_CASE(base_type)
  45. {
  46. BOOST_STATIC_ASSERT((
  47. boost::is_same<
  48. boost::compute::permutation_iterator<
  49. boost::compute::buffer_iterator<int>,
  50. boost::compute::buffer_iterator<int>
  51. >::base_type,
  52. boost::compute::buffer_iterator<int>
  53. >::value
  54. ));
  55. }
  56. BOOST_AUTO_TEST_CASE(copy)
  57. {
  58. int input_data[] = { 3, 4, 2, 1, 5 };
  59. boost::compute::vector<int> input(input_data, input_data + 5, queue);
  60. int map_data[] = { 3, 2, 0, 1, 4 };
  61. boost::compute::vector<int> map(map_data, map_data + 5, queue);
  62. boost::compute::vector<int> output(5, context);
  63. boost::compute::copy(
  64. boost::compute::make_permutation_iterator(input.begin(), map.begin()),
  65. boost::compute::make_permutation_iterator(input.end(), map.end()),
  66. output.begin(),
  67. queue
  68. );
  69. CHECK_RANGE_EQUAL(int, 5, output, (1, 2, 3, 4, 5));
  70. }
  71. BOOST_AUTO_TEST_CASE(reverse_range_doctest)
  72. {
  73. int values_data[] = { 10, 20, 30, 40 };
  74. int indices_data[] = { 3, 2, 1, 0 };
  75. boost::compute::vector<int> values(values_data, values_data + 4, queue);
  76. boost::compute::vector<int> indices(indices_data, indices_data + 4, queue);
  77. boost::compute::vector<int> result(4, context);
  78. //! [reverse_range]
  79. // values = { 10, 20, 30, 40 }
  80. // indices = { 3, 2, 1, 0 }
  81. boost::compute::copy(
  82. boost::compute::make_permutation_iterator(values.begin(), indices.begin()),
  83. boost::compute::make_permutation_iterator(values.end(), indices.end()),
  84. result.begin(),
  85. queue
  86. );
  87. // result == { 40, 30, 20, 10 }
  88. //! [reverse_range]
  89. CHECK_RANGE_EQUAL(int, 4, result, (40, 30, 20, 10));
  90. }
  91. BOOST_AUTO_TEST_SUITE_END()