test_strided_iterator.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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 TestStridedIterator
  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/algorithm/copy.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/iterator/buffer_iterator.hpp>
  18. #include <boost/compute/iterator/strided_iterator.hpp>
  19. #include "check_macros.hpp"
  20. #include "context_setup.hpp"
  21. namespace bc = boost::compute;
  22. BOOST_AUTO_TEST_CASE(value_type)
  23. {
  24. BOOST_STATIC_ASSERT((
  25. boost::is_same<
  26. boost::compute::strided_iterator<
  27. boost::compute::buffer_iterator<int>
  28. >::value_type,
  29. int
  30. >::value
  31. ));
  32. BOOST_STATIC_ASSERT((
  33. boost::is_same<
  34. boost::compute::strided_iterator<
  35. boost::compute::buffer_iterator<float>
  36. >::value_type,
  37. float
  38. >::value
  39. ));
  40. }
  41. BOOST_AUTO_TEST_CASE(base_type)
  42. {
  43. BOOST_STATIC_ASSERT((
  44. boost::is_same<
  45. boost::compute::strided_iterator<
  46. boost::compute::buffer_iterator<int>
  47. >::base_type,
  48. boost::compute::buffer_iterator<int>
  49. >::value
  50. ));
  51. }
  52. BOOST_AUTO_TEST_CASE(distance)
  53. {
  54. int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  55. boost::compute::vector<int> vec(data, data + 8, queue);
  56. BOOST_CHECK_EQUAL(
  57. std::distance(
  58. boost::compute::make_strided_iterator(vec.begin(), 1),
  59. boost::compute::make_strided_iterator(vec.end(), 1)
  60. ),
  61. std::ptrdiff_t(8)
  62. );
  63. BOOST_CHECK_EQUAL(
  64. std::distance(
  65. boost::compute::make_strided_iterator(vec.begin(), 2),
  66. boost::compute::make_strided_iterator(vec.end(), 2)
  67. ),
  68. std::ptrdiff_t(4)
  69. );
  70. BOOST_CHECK_EQUAL(
  71. std::distance(
  72. boost::compute::make_strided_iterator(vec.begin(), 3),
  73. boost::compute::make_strided_iterator(vec.begin()+6, 3)
  74. ),
  75. std::ptrdiff_t(2)
  76. );
  77. }
  78. BOOST_AUTO_TEST_CASE(copy)
  79. {
  80. boost::compute::int_ data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  81. boost::compute::vector<boost::compute::int_> vec(data, data + 8, queue);
  82. boost::compute::vector<boost::compute::int_> result(4, context);
  83. // copy every other element to result
  84. boost::compute::copy(
  85. boost::compute::make_strided_iterator(vec.begin(), 2),
  86. boost::compute::make_strided_iterator(vec.end(), 2),
  87. result.begin(),
  88. queue
  89. );
  90. CHECK_RANGE_EQUAL(boost::compute::int_, 4, result, (1, 3, 5, 7));
  91. // copy every 3rd element to result
  92. boost::compute::copy(
  93. boost::compute::make_strided_iterator(vec.begin(), 3),
  94. boost::compute::make_strided_iterator(vec.begin()+9, 3),
  95. result.begin(),
  96. queue
  97. );
  98. CHECK_RANGE_EQUAL(boost::compute::int_, 3, result, (1, 4, 7));
  99. }
  100. BOOST_AUTO_TEST_CASE(make_strided_iterator_end)
  101. {
  102. boost::compute::int_ data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  103. boost::compute::vector<boost::compute::int_> vec(data, data + 8, queue);
  104. // stride equals 3
  105. typedef boost::compute::vector<boost::compute::int_>::iterator IterType;
  106. boost::compute::strided_iterator<IterType> end =
  107. boost::compute::make_strided_iterator_end(vec.begin(),
  108. vec.end(),
  109. 3);
  110. // end should be vec.begin() + 9 which is one step after last element
  111. // accessible through strided_iterator, i.e. vec.begin()+6
  112. BOOST_CHECK(boost::compute::make_strided_iterator(vec.begin()+9, 3) ==
  113. end);
  114. // stride equals 2
  115. end = boost::compute::make_strided_iterator_end(vec.begin(),
  116. vec.end(),
  117. 2);
  118. // end should be vec.end(), because vector size is divisible by 2
  119. BOOST_CHECK(boost::compute::make_strided_iterator(vec.end(), 2) == end);
  120. // stride equals 1000
  121. end = boost::compute::make_strided_iterator_end(vec.begin(),
  122. vec.end(),
  123. 1000);
  124. // end should be vec.begin() + 1000, because stride > vector size
  125. BOOST_CHECK(boost::compute::make_strided_iterator(vec.begin()+1000, 1000) ==
  126. end);
  127. // test boost::compute::make_strided_iterator_end with copy(..)
  128. boost::compute::vector<boost::compute::int_> result(4, context);
  129. // copy every other element to result
  130. boost::compute::copy(
  131. boost::compute::make_strided_iterator(vec.begin()+1, 2),
  132. boost::compute::make_strided_iterator_end(vec.begin()+1, vec.end(), 2),
  133. result.begin(),
  134. queue
  135. );
  136. CHECK_RANGE_EQUAL(boost::compute::int_, 4, result, (2, 4, 6, 8));
  137. }
  138. BOOST_AUTO_TEST_CASE(iterator_tag)
  139. {
  140. typedef bc::buffer_iterator<bc::float_> i_type;
  141. BOOST_STATIC_ASSERT((
  142. boost::is_same<
  143. std::iterator_traits<
  144. i_type
  145. >::iterator_category,
  146. std::iterator_traits<
  147. bc::strided_iterator<i_type>
  148. >::iterator_category
  149. >::value
  150. ));
  151. }
  152. BOOST_AUTO_TEST_CASE(std_distance)
  153. {
  154. bc::vector<bc::float_> vec(
  155. size_t(300),
  156. bc::float_(1.1f),
  157. queue
  158. );
  159. bc::strided_iterator<bc::buffer_iterator<bc::float_> > begin(vec.begin(), 1);
  160. bc::strided_iterator<bc::buffer_iterator<bc::float_> > end(vec.end(), 1);
  161. BOOST_CHECK_EQUAL(std::distance(begin, end), 300);
  162. BOOST_CHECK_EQUAL(std::distance(end, begin), -300);
  163. }
  164. BOOST_AUTO_TEST_SUITE_END()