• 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 TestFunctionalAs
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/copy_n.hpp>
16 #include <boost/compute/algorithm/transform.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/functional/as.hpp>
19 
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22 
23 namespace compute = boost::compute;
24 
BOOST_AUTO_TEST_CASE(roundtrip_int_float)25 BOOST_AUTO_TEST_CASE(roundtrip_int_float)
26 {
27     int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
28     compute::vector<int> input(8, context);
29     compute::copy_n(data, 8, input.begin(), queue);
30 
31     // convert int -> float
32     compute::vector<float> output(8, context);
33     compute::transform(
34         input.begin(),
35         input.end(),
36         output.begin(),
37         compute::as<float>(),
38         queue
39     );
40 
41     // zero out input
42     compute::fill(input.begin(), input.end(), 0, queue);
43 
44     // convert float -> int
45     compute::transform(
46         output.begin(),
47         output.end(),
48         input.begin(),
49         compute::as<int>(),
50         queue
51     );
52 
53     // check values
54     CHECK_RANGE_EQUAL(
55         int, 8, input, (1, 2, 3, 4, 5, 6, 7, 8)
56     );
57 }
58 
59 BOOST_AUTO_TEST_SUITE_END()
60