test_counting_iterator.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 TestCountingIterator
  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/counting_iterator.hpp>
  18. #include "check_macros.hpp"
  19. #include "context_setup.hpp"
  20. BOOST_AUTO_TEST_CASE(value_type)
  21. {
  22. BOOST_STATIC_ASSERT((
  23. boost::is_same<
  24. boost::compute::counting_iterator<int>::value_type,
  25. int
  26. >::value
  27. ));
  28. BOOST_STATIC_ASSERT((
  29. boost::is_same<
  30. boost::compute::counting_iterator<float>::value_type,
  31. float
  32. >::value
  33. ));
  34. }
  35. BOOST_AUTO_TEST_CASE(distance)
  36. {
  37. BOOST_CHECK_EQUAL(
  38. std::distance(
  39. boost::compute::make_counting_iterator(0),
  40. boost::compute::make_counting_iterator(10)
  41. ),
  42. std::ptrdiff_t(10)
  43. );
  44. BOOST_CHECK_EQUAL(
  45. std::distance(
  46. boost::compute::make_counting_iterator(5),
  47. boost::compute::make_counting_iterator(10)
  48. ),
  49. std::ptrdiff_t(5)
  50. );
  51. BOOST_CHECK_EQUAL(
  52. std::distance(
  53. boost::compute::make_counting_iterator(-5),
  54. boost::compute::make_counting_iterator(5)
  55. ),
  56. std::ptrdiff_t(10)
  57. );
  58. }
  59. BOOST_AUTO_TEST_CASE(copy)
  60. {
  61. boost::compute::vector<int> vector(10, context);
  62. boost::compute::copy(
  63. boost::compute::make_counting_iterator(1),
  64. boost::compute::make_counting_iterator(11),
  65. vector.begin(),
  66. queue
  67. );
  68. CHECK_RANGE_EQUAL(
  69. int, 10, vector,
  70. (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  71. );
  72. }
  73. BOOST_AUTO_TEST_CASE(iota_with_copy_doctest)
  74. {
  75. //! [iota_with_copy]
  76. using boost::compute::make_counting_iterator;
  77. boost::compute::vector<int> result(5, context);
  78. boost::compute::copy(
  79. make_counting_iterator(1), make_counting_iterator(6), result.begin(), queue
  80. );
  81. // result == { 1, 2, 3, 4, 5 }
  82. //! [iota_with_copy]
  83. CHECK_RANGE_EQUAL(int, 5, result, (1, 2, 3, 4, 5));
  84. }
  85. BOOST_AUTO_TEST_SUITE_END()