• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TestPermutationIterator
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/algorithm/copy.hpp>
21 #include <boost/compute/container/vector.hpp>
22 #include <boost/compute/iterator/buffer_iterator.hpp>
23 #include <boost/compute/iterator/permutation_iterator.hpp>
24 
25 #include "check_macros.hpp"
26 #include "context_setup.hpp"
27 
BOOST_AUTO_TEST_CASE(value_type)28 BOOST_AUTO_TEST_CASE(value_type)
29 {
30     using boost::compute::float4_;
31 
32     BOOST_STATIC_ASSERT((
33         boost::is_same<
34             boost::compute::permutation_iterator<
35                 boost::compute::buffer_iterator<float>,
36                 boost::compute::buffer_iterator<int>
37             >::value_type,
38             float
39         >::value
40     ));
41     BOOST_STATIC_ASSERT((
42         boost::is_same<
43             boost::compute::permutation_iterator<
44                 boost::compute::buffer_iterator<float4_>,
45                 boost::compute::buffer_iterator<short>
46             >::value_type,
47             float4_
48         >::value
49     ));
50 }
51 
BOOST_AUTO_TEST_CASE(base_type)52 BOOST_AUTO_TEST_CASE(base_type)
53 {
54     BOOST_STATIC_ASSERT((
55         boost::is_same<
56             boost::compute::permutation_iterator<
57                 boost::compute::buffer_iterator<int>,
58                 boost::compute::buffer_iterator<int>
59             >::base_type,
60             boost::compute::buffer_iterator<int>
61         >::value
62     ));
63 }
64 
BOOST_AUTO_TEST_CASE(copy)65 BOOST_AUTO_TEST_CASE(copy)
66 {
67     int input_data[] = { 3, 4, 2, 1, 5 };
68     boost::compute::vector<int> input(input_data, input_data + 5, queue);
69 
70     int map_data[] = { 3, 2, 0, 1, 4 };
71     boost::compute::vector<int> map(map_data, map_data + 5, queue);
72 
73     boost::compute::vector<int> output(5, context);
74     boost::compute::copy(
75         boost::compute::make_permutation_iterator(input.begin(), map.begin()),
76         boost::compute::make_permutation_iterator(input.end(), map.end()),
77         output.begin(),
78         queue
79     );
80     CHECK_RANGE_EQUAL(int, 5, output, (1, 2, 3, 4, 5));
81 }
82 
BOOST_AUTO_TEST_CASE(reverse_range_doctest)83 BOOST_AUTO_TEST_CASE(reverse_range_doctest)
84 {
85     int values_data[] = { 10, 20, 30, 40 };
86     int indices_data[] = { 3, 2, 1, 0 };
87 
88     boost::compute::vector<int> values(values_data, values_data + 4, queue);
89     boost::compute::vector<int> indices(indices_data, indices_data + 4, queue);
90 
91     boost::compute::vector<int> result(4, context);
92 
93 //! [reverse_range]
94 // values =  { 10, 20, 30, 40 }
95 // indices = { 3, 2, 1, 0 }
96 
97 boost::compute::copy(
98     boost::compute::make_permutation_iterator(values.begin(), indices.begin()),
99     boost::compute::make_permutation_iterator(values.end(), indices.end()),
100     result.begin(),
101     queue
102 );
103 
104 // result == { 40, 30, 20, 10 }
105 //! [reverse_range]
106 
107     CHECK_RANGE_EQUAL(int, 4, result, (40, 30, 20, 10));
108 }
109 
110 BOOST_AUTO_TEST_SUITE_END()
111