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 TestGenerate
12 #include <boost/test/unit_test.hpp>
13 
14 #include <iostream>
15 
16 #include <boost/compute/function.hpp>
17 #include <boost/compute/algorithm/generate.hpp>
18 #include <boost/compute/algorithm/generate_n.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/types/pair.hpp>
21 
22 #include "quirks.hpp"
23 #include "check_macros.hpp"
24 #include "context_setup.hpp"
25 
26 namespace bc = boost::compute;
27 
BOOST_AUTO_TEST_CASE(generate4)28 BOOST_AUTO_TEST_CASE(generate4)
29 {
30     bc::vector<int> vector(4, context);
31     bc::fill(vector.begin(), vector.end(), 2, queue);
32     CHECK_RANGE_EQUAL(int, 4, vector, (2, 2, 2, 2));
33 
34     BOOST_COMPUTE_FUNCTION(int, ret4, (void),
35     {
36         return 4;
37     });
38 
39     bc::generate(vector.begin(), vector.end(), ret4, queue);
40     CHECK_RANGE_EQUAL(int, 4, vector, (4, 4, 4, 4));
41 }
42 
BOOST_AUTO_TEST_CASE(generate_pair)43 BOOST_AUTO_TEST_CASE(generate_pair)
44 {
45     if(bug_in_struct_assignment(device)){
46         std::cerr << "skipping generate_pair test" << std::endl;
47         return;
48     }
49 
50     // in order to use std::pair<T1, T2> with BOOST_COMPUTE_FUNCTION() macro we
51     // need a typedef. otherwise the commas in the type declaration screw it up.
52     typedef std::pair<int, float> pair_type;
53 
54     bc::vector<pair_type> vector(3, context);
55 
56     BOOST_COMPUTE_FUNCTION(pair_type, generate_pair, (void),
57     {
58         return boost_make_pair(int, 2, float, 3.14f);
59     });
60 
61     bc::generate(vector.begin(), vector.end(), generate_pair, queue);
62 
63     // check results
64     std::vector<pair_type> host_vector(3);
65     bc::copy(vector.begin(), vector.end(), host_vector.begin(), queue);
66     BOOST_CHECK(host_vector[0] == std::make_pair(2, 3.14f));
67     BOOST_CHECK(host_vector[1] == std::make_pair(2, 3.14f));
68     BOOST_CHECK(host_vector[2] == std::make_pair(2, 3.14f));
69 }
70 
71 BOOST_AUTO_TEST_SUITE_END()
72