| 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 | |
| 11 | #define BOOST_TEST_MODULE TestTransformIterator |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | |
| 14 | #include <iterator> |
| 15 | |
| 16 | #include <boost/type_traits.hpp> |
| 17 | #include <boost/static_assert.hpp> |
| 18 | |
| 19 | #include <boost/compute/types.hpp> |
| 20 | #include <boost/compute/functional.hpp> |
| 21 | #include <boost/compute/algorithm/copy.hpp> |
| 22 | #include <boost/compute/container/vector.hpp> |
| 23 | #include <boost/compute/iterator/buffer_iterator.hpp> |
| 24 | #include <boost/compute/iterator/transform_iterator.hpp> |
| 25 | |
| 26 | #include "check_macros.hpp" |
| 27 | #include "context_setup.hpp" |
| 28 | |
| 29 | BOOST_AUTO_TEST_CASE(value_type) |
| 30 | { |
| 31 | using boost::compute::float4_; |
| 32 | |
| 33 | BOOST_STATIC_ASSERT(( |
| 34 | boost::is_same< |
| 35 | boost::compute::transform_iterator< |
| 36 | boost::compute::buffer_iterator<float>, |
| 37 | boost::compute::sqrt<float> |
| 38 | >::value_type, |
| 39 | float |
| 40 | >::value |
| 41 | )); |
| 42 | BOOST_STATIC_ASSERT(( |
| 43 | boost::is_same< |
| 44 | boost::compute::transform_iterator< |
| 45 | boost::compute::buffer_iterator<float4_>, |
| 46 | boost::compute::length<float4_> |
| 47 | >::value_type, |
| 48 | float |
| 49 | >::value |
| 50 | )); |
| 51 | } |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(base_type) |
| 54 | { |
| 55 | BOOST_STATIC_ASSERT(( |
| 56 | boost::is_same< |
| 57 | boost::compute::transform_iterator< |
| 58 | boost::compute::buffer_iterator<int>, boost::compute::abs<int> |
| 59 | >::base_type, |
| 60 | boost::compute::buffer_iterator<int> |
| 61 | >::value |
| 62 | )); |
| 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(copy) |
| 66 | { |
| 67 | int data[] = { 1, -2, 3, -4, 5 }; |
| 68 | boost::compute::vector<int> a(data, data + 5, queue); |
| 69 | |
| 70 | boost::compute::vector<int> b(5, context); |
| 71 | boost::compute::copy( |
| 72 | boost::compute::make_transform_iterator( |
| 73 | a.begin(), |
| 74 | boost::compute::abs<int>() |
| 75 | ), |
| 76 | boost::compute::make_transform_iterator( |
| 77 | a.end(), |
| 78 | boost::compute::abs<int>() |
| 79 | ), |
| 80 | b.begin(), |
| 81 | queue |
| 82 | ); |
| 83 | CHECK_RANGE_EQUAL(int, 5, b, (1, 2, 3, 4, 5)); |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(copy_abs_doctest) |
| 87 | { |
| 88 | int data[] = { -1, -2, -3, -4 }; |
| 89 | boost::compute::vector<int> input(data, data + 4, queue); |
| 90 | boost::compute::vector<int> output(4, context); |
| 91 | |
| 92 | //! [copy_abs] |
| 93 | // use abs() from boost.compute |
| 94 | using boost::compute::abs; |
| 95 | |
| 96 | // copy the absolute value for each element in input to output |
| 97 | boost::compute::copy( |
| 98 | first: boost::compute::make_transform_iterator(iterator: input.begin(), transform: abs<int>()), |
| 99 | last: boost::compute::make_transform_iterator(iterator: input.end(), transform: abs<int>()), |
| 100 | result: output.begin(), |
| 101 | queue |
| 102 | ); |
| 103 | //! [copy_abs] |
| 104 | |
| 105 | CHECK_RANGE_EQUAL(int, 4, output, (1, 2, 3, 4)); |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_SUITE_END() |
| 109 | |