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 TestPair
12 #include <boost/test/unit_test.hpp>
13 
14 #include <iostream>
15 
16 #include <boost/compute/algorithm/copy.hpp>
17 #include <boost/compute/algorithm/fill.hpp>
18 #include <boost/compute/algorithm/find.hpp>
19 #include <boost/compute/algorithm/transform.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/functional/get.hpp>
22 #include <boost/compute/functional/field.hpp>
23 #include <boost/compute/types/pair.hpp>
24 
25 #include "quirks.hpp"
26 #include "check_macros.hpp"
27 #include "context_setup.hpp"
28 
BOOST_AUTO_TEST_CASE(vector_pair_int_float)29 BOOST_AUTO_TEST_CASE(vector_pair_int_float)
30 {
31     boost::compute::vector<std::pair<int, float> > vector(context);
32     vector.push_back(std::make_pair(1, 1.1f), queue);
33     vector.push_back(std::make_pair(2, 2.2f), queue);
34     vector.push_back(std::make_pair(3, 3.3f), queue);
35     queue.finish();
36     BOOST_CHECK_EQUAL(vector.size(), size_t(3));
37     BOOST_CHECK(vector[0] == std::make_pair(1, 1.1f));
38     BOOST_CHECK(vector[1] == std::make_pair(2, 2.2f));
39     BOOST_CHECK(vector[2] == std::make_pair(3, 3.3f));
40 }
41 
BOOST_AUTO_TEST_CASE(copy_pair_vector)42 BOOST_AUTO_TEST_CASE(copy_pair_vector)
43 {
44     boost::compute::vector<std::pair<int, float> > input(context);
45     input.push_back(std::make_pair(1, 2.0f), queue);
46     input.push_back(std::make_pair(3, 4.0f), queue);
47     input.push_back(std::make_pair(5, 6.0f), queue);
48     input.push_back(std::make_pair(7, 8.0f), queue);
49     queue.finish();
50     BOOST_CHECK_EQUAL(input.size(), size_t(4));
51 
52     boost::compute::vector<std::pair<int, float> > output(4, context);
53     boost::compute::copy(input.begin(), input.end(), output.begin(), queue);
54     queue.finish();
55     BOOST_CHECK(output[0] == std::make_pair(1, 2.0f));
56     BOOST_CHECK(output[1] == std::make_pair(3, 4.0f));
57     BOOST_CHECK(output[2] == std::make_pair(5, 6.0f));
58     BOOST_CHECK(output[3] == std::make_pair(7, 8.0f));
59 }
60 
BOOST_AUTO_TEST_CASE(fill_pair_vector)61 BOOST_AUTO_TEST_CASE(fill_pair_vector)
62 {
63     if(bug_in_struct_assignment(device)){
64         std::cerr << "skipping fill_pair_vector test" << std::endl;
65         return;
66     }
67 
68     boost::compute::vector<std::pair<int, float> > vector(5, context);
69     boost::compute::fill(vector.begin(), vector.end(), std::make_pair(4, 2.0f), queue);
70     queue.finish();
71     BOOST_CHECK(vector[0] == std::make_pair(4, 2.0f));
72     BOOST_CHECK(vector[1] == std::make_pair(4, 2.0f));
73     BOOST_CHECK(vector[2] == std::make_pair(4, 2.0f));
74     BOOST_CHECK(vector[3] == std::make_pair(4, 2.0f));
75     BOOST_CHECK(vector[4] == std::make_pair(4, 2.0f));
76 }
77 
BOOST_AUTO_TEST_CASE(fill_char_pair_vector)78 BOOST_AUTO_TEST_CASE(fill_char_pair_vector)
79 {
80     if(bug_in_struct_assignment(device)){
81         std::cerr << "skipping fill_char_pair_vector test" << std::endl;
82         return;
83     }
84 
85     std::pair<char, unsigned char> value('c', static_cast<unsigned char>(127));
86     boost::compute::vector<std::pair<char, unsigned char> > vector(5, context);
87     boost::compute::fill(vector.begin(), vector.end(), value, queue);
88     queue.finish();
89     BOOST_CHECK(vector[0] == value);
90     BOOST_CHECK(vector[1] == value);
91     BOOST_CHECK(vector[2] == value);
92     BOOST_CHECK(vector[3] == value);
93     BOOST_CHECK(vector[4] == value);
94 }
95 
BOOST_AUTO_TEST_CASE(transform_pair_get)96 BOOST_AUTO_TEST_CASE(transform_pair_get)
97 {
98     boost::compute::vector<std::pair<int, float> > input(context);
99     input.push_back(std::make_pair(1, 2.0f), queue);
100     input.push_back(std::make_pair(3, 4.0f), queue);
101     input.push_back(std::make_pair(5, 6.0f), queue);
102     input.push_back(std::make_pair(7, 8.0f), queue);
103     queue.finish();
104 
105     boost::compute::vector<int> first_output(4, context);
106     boost::compute::transform(
107         input.begin(),
108         input.end(),
109         first_output.begin(),
110         ::boost::compute::get<0>(),
111         queue
112     );
113     CHECK_RANGE_EQUAL(int, 4, first_output, (1, 3, 5, 7));
114 
115     boost::compute::vector<float> second_output(4, context);
116     boost::compute::transform(
117         input.begin(),
118         input.end(),
119         second_output.begin(),
120         ::boost::compute::get<1>(),
121         queue
122     );
123     CHECK_RANGE_EQUAL(float, 4, second_output, (2.0f, 4.0f, 6.0f, 8.0f));
124 }
125 
BOOST_AUTO_TEST_CASE(transform_pair_field)126 BOOST_AUTO_TEST_CASE(transform_pair_field)
127 {
128     boost::compute::vector<std::pair<int, float> > input(context);
129     input.push_back(std::make_pair(1, 2.0f), queue);
130     input.push_back(std::make_pair(3, 4.0f), queue);
131     input.push_back(std::make_pair(5, 6.0f), queue);
132     input.push_back(std::make_pair(7, 8.0f), queue);
133 
134     boost::compute::vector<int> first_output(4, context);
135     boost::compute::transform(
136         input.begin(),
137         input.end(),
138         first_output.begin(),
139         boost::compute::field<int>("first"),
140         queue
141     );
142     CHECK_RANGE_EQUAL(int, 4, first_output, (1, 3, 5, 7));
143 
144     boost::compute::vector<float> second_output(4, context);
145     boost::compute::transform(
146         input.begin(),
147         input.end(),
148         second_output.begin(),
149         boost::compute::field<float>("second"),
150         queue
151     );
152     CHECK_RANGE_EQUAL(float, 4, second_output, (2.0f, 4.0f, 6.0f, 8.0f));
153 }
154 
BOOST_AUTO_TEST_CASE(find_vector_pair)155 BOOST_AUTO_TEST_CASE(find_vector_pair)
156 {
157     boost::compute::vector<std::pair<int, float> > vector(context);
158     vector.push_back(std::make_pair(1, 1.1f), queue);
159     vector.push_back(std::make_pair(2, 2.2f), queue);
160     vector.push_back(std::make_pair(3, 3.3f), queue);
161     BOOST_CHECK_EQUAL(vector.size(), size_t(3));
162 
163     BOOST_CHECK(
164         boost::compute::find(
165             boost::compute::make_transform_iterator(
166                 vector.begin(),
167                 boost::compute::get<0>()
168             ),
169             boost::compute::make_transform_iterator(
170                 vector.end(),
171                 boost::compute::get<0>()
172             ),
173             int(2),
174             queue
175         ).base() == vector.begin() + 1
176     );
177 
178     BOOST_CHECK(
179         boost::compute::find(
180             boost::compute::make_transform_iterator(
181                 vector.begin(),
182                 boost::compute::get<1>()
183             ),
184             boost::compute::make_transform_iterator(
185                 vector.end(),
186                 boost::compute::get<1>()
187             ),
188             float(3.3f),
189             queue
190         ).base() == vector.begin() + 2
191     );
192 }
193 
194 BOOST_AUTO_TEST_SUITE_END()
195