test_zip_iterator.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 TestZipIterator
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/type_traits.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/tuple/tuple_io.hpp>
  15. #include <boost/tuple/tuple_comparison.hpp>
  16. #include <boost/compute/functional.hpp>
  17. #include <boost/compute/algorithm/copy.hpp>
  18. #include <boost/compute/algorithm/transform.hpp>
  19. #include <boost/compute/container/vector.hpp>
  20. #include <boost/compute/iterator/constant_iterator.hpp>
  21. #include <boost/compute/iterator/zip_iterator.hpp>
  22. #include <boost/compute/types/tuple.hpp>
  23. #include "check_macros.hpp"
  24. #include "context_setup.hpp"
  25. namespace compute = boost::compute;
  26. BOOST_AUTO_TEST_CASE(value_type)
  27. {
  28. BOOST_STATIC_ASSERT((
  29. boost::is_same<
  30. boost::compute::zip_iterator<
  31. boost::tuple<
  32. boost::compute::buffer_iterator<float>,
  33. boost::compute::buffer_iterator<int>
  34. >
  35. >::value_type,
  36. boost::tuple<float, int>
  37. >::value
  38. ));
  39. }
  40. BOOST_AUTO_TEST_CASE(distance)
  41. {
  42. boost::compute::vector<char> char_vector(5, context);
  43. boost::compute::vector<int> int_vector(5, context);
  44. BOOST_CHECK_EQUAL(
  45. std::distance(
  46. boost::compute::make_zip_iterator(
  47. boost::make_tuple(
  48. char_vector.begin(),
  49. int_vector.begin()
  50. )
  51. ),
  52. boost::compute::make_zip_iterator(
  53. boost::make_tuple(
  54. char_vector.end(),
  55. int_vector.end()
  56. )
  57. )
  58. ),
  59. ptrdiff_t(5)
  60. );
  61. BOOST_CHECK_EQUAL(
  62. std::distance(
  63. boost::compute::make_zip_iterator(
  64. boost::make_tuple(
  65. char_vector.begin(),
  66. int_vector.begin()
  67. )
  68. ) + 1,
  69. boost::compute::make_zip_iterator(
  70. boost::make_tuple(
  71. char_vector.end(),
  72. int_vector.end()
  73. )
  74. ) - 1
  75. ),
  76. ptrdiff_t(3)
  77. );
  78. BOOST_CHECK_EQUAL(
  79. std::distance(
  80. boost::compute::make_zip_iterator(
  81. boost::make_tuple(
  82. char_vector.begin() + 2,
  83. int_vector.begin() + 2
  84. )
  85. ),
  86. boost::compute::make_zip_iterator(
  87. boost::make_tuple(
  88. char_vector.end() - 1,
  89. int_vector.end() - 1
  90. )
  91. )
  92. ),
  93. ptrdiff_t(2)
  94. );
  95. }
  96. BOOST_AUTO_TEST_CASE(copy)
  97. {
  98. // create three separate vectors of three different types
  99. char char_data[] = { 'x', 'y', 'z' };
  100. boost::compute::vector<char> char_vector(char_data, char_data + 3, queue);
  101. int int_data[] = { 4, 7, 9 };
  102. boost::compute::vector<int> int_vector(int_data, int_data + 3, queue);
  103. float float_data[] = { 3.2f, 4.5f, 7.6f };
  104. boost::compute::vector<float> float_vector(float_data, float_data + 3, queue);
  105. // zip all three vectors into a single tuple vector
  106. boost::compute::vector<boost::tuple<char, int, float> > tuple_vector(3, context);
  107. boost::compute::copy(
  108. boost::compute::make_zip_iterator(
  109. boost::make_tuple(
  110. char_vector.begin(),
  111. int_vector.begin(),
  112. float_vector.begin()
  113. )
  114. ),
  115. boost::compute::make_zip_iterator(
  116. boost::make_tuple(
  117. char_vector.end(),
  118. int_vector.end(),
  119. float_vector.end()
  120. )
  121. ),
  122. tuple_vector.begin(),
  123. queue
  124. );
  125. // copy tuple vector to host
  126. std::vector<boost::tuple<char, int, float> > host_vector(3);
  127. boost::compute::copy(
  128. tuple_vector.begin(),
  129. tuple_vector.end(),
  130. host_vector.begin(),
  131. queue
  132. );
  133. // check tuple values
  134. BOOST_CHECK_EQUAL(host_vector[0], boost::make_tuple('x', 4, 3.2f));
  135. BOOST_CHECK_EQUAL(host_vector[1], boost::make_tuple('y', 7, 4.5f));
  136. BOOST_CHECK_EQUAL(host_vector[2], boost::make_tuple('z', 9, 7.6f));
  137. }
  138. BOOST_AUTO_TEST_CASE(zip_iterator_get)
  139. {
  140. int data1[] = { 0, 2, 4, 6, 8 };
  141. int data2[] = { 1, 3, 5, 7, 9 };
  142. compute::vector<int> input1(data1, data1 + 5, queue);
  143. compute::vector<int> input2(data2, data2 + 5, queue);
  144. compute::vector<int> output(5, context);
  145. // extract first component from (input1)
  146. compute::transform(
  147. compute::make_zip_iterator(
  148. boost::make_tuple(input1.begin())
  149. ),
  150. compute::make_zip_iterator(
  151. boost::make_tuple(input1.end())
  152. ),
  153. output.begin(),
  154. compute::get<0>(),
  155. queue
  156. );
  157. CHECK_RANGE_EQUAL(int, 5, output, (0, 2, 4, 6, 8));
  158. // extract first component from (input2, input1)
  159. compute::transform(
  160. compute::make_zip_iterator(
  161. boost::make_tuple(input2.begin(), input1.begin())
  162. ),
  163. compute::make_zip_iterator(
  164. boost::make_tuple(input2.end(), input1.end())
  165. ),
  166. output.begin(),
  167. compute::get<0>(),
  168. queue
  169. );
  170. CHECK_RANGE_EQUAL(int, 5, output, (1, 3, 5, 7, 9));
  171. // extract second component from (input1, input2, input1)
  172. compute::transform(
  173. compute::make_zip_iterator(
  174. boost::make_tuple(input1.begin(), input2.begin(), input1.begin())
  175. ),
  176. compute::make_zip_iterator(
  177. boost::make_tuple(input1.end(), input2.end(), input1.end())
  178. ),
  179. output.begin(),
  180. compute::get<1>(),
  181. queue
  182. );
  183. CHECK_RANGE_EQUAL(int, 5, output, (1, 3, 5, 7, 9));
  184. }
  185. BOOST_AUTO_TEST_CASE(zip_constant_iterator)
  186. {
  187. compute::vector<int> result(4, context);
  188. compute::transform(
  189. compute::make_zip_iterator(
  190. boost::make_tuple(
  191. compute::make_constant_iterator(7)
  192. )
  193. ),
  194. compute::make_zip_iterator(
  195. boost::make_tuple(
  196. compute::make_constant_iterator(7, result.size())
  197. )
  198. ),
  199. result.begin(),
  200. compute::get<0>(),
  201. queue
  202. );
  203. CHECK_RANGE_EQUAL(int, 4, result, (7, 7, 7, 7));
  204. }
  205. BOOST_AUTO_TEST_SUITE_END()