1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #define BOOST_TEST_MODULE TestSortByKey
12 #include <boost/test/unit_test.hpp>
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/sort_by_key.hpp>
16 #include <boost/compute/algorithm/is_sorted.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/types/struct.hpp>
22 boost::compute::int_ x
;
23 boost::compute::int_ y
;
24 boost::compute::float2_ zw
;
27 BOOST_COMPUTE_ADAPT_STRUCT(custom_struct
, custom_struct
, (x
, y
, zw
))
29 #include "check_macros.hpp"
30 #include "context_setup.hpp"
32 namespace compute
= boost::compute
;
34 // test trivial sorting of zero element vectors
35 BOOST_AUTO_TEST_CASE(sort_int_0
)
37 compute::vector
<int> keys(context
);
38 compute::vector
<int> values(context
);
39 BOOST_CHECK_EQUAL(keys
.size(), size_t(0));
40 BOOST_CHECK_EQUAL(values
.size(), size_t(0));
41 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end()) == true);
42 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end()) == true);
43 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
46 // test trivial sorting of one element vectors
47 BOOST_AUTO_TEST_CASE(sort_int_1
)
49 int keys_data
[] = { 11 };
50 int values_data
[] = { 100 };
52 compute::vector
<int> keys(keys_data
, keys_data
+ 1, queue
);
53 compute::vector
<int> values(values_data
, values_data
+ 1, queue
);
55 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == true);
56 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == true);
58 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
61 // test trivial sorting of two element vectors
62 BOOST_AUTO_TEST_CASE(sort_int_2
)
64 int keys_data
[] = { 4, 2 };
65 int values_data
[] = { 42, 24 };
67 compute::vector
<int> keys(keys_data
, keys_data
+ 2, queue
);
68 compute::vector
<int> values(values_data
, values_data
+ 2, queue
);
70 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == false);
71 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == false);
73 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
75 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == true);
76 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == true);
79 BOOST_AUTO_TEST_CASE(sort_char_by_int
)
81 int keys_data
[] = { 6, 2, 1, 3, 4, 7, 5, 0 };
82 compute::char_ values_data
[] = { 'g', 'c', 'b', 'd', 'e', 'h', 'f', 'a' };
84 compute::vector
<int> keys(keys_data
, keys_data
+ 8, queue
);
85 compute::vector
<compute::char_
> values(values_data
, values_data
+ 8, queue
);
87 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
89 CHECK_RANGE_EQUAL(int, 8, keys
, (0, 1, 2, 3, 4, 5, 6, 7));
90 CHECK_RANGE_EQUAL(compute::char_
, 8, values
, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'));
93 BOOST_AUTO_TEST_CASE(sort_int_and_float
)
96 std::vector
<int> host_keys(n
);
97 std::vector
<float> host_values(n
);
98 for(int i
= 0; i
< n
; i
++){
100 host_values
[i
] = (n
- i
) / 2.f
;
103 compute::vector
<int> keys(host_keys
.begin(), host_keys
.end(), queue
);
104 compute::vector
<float> values(host_values
.begin(), host_values
.end(), queue
);
106 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == false);
107 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == false);
109 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
111 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == true);
112 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == true);
115 BOOST_AUTO_TEST_CASE(sort_int_and_float_custom_comparison_func
)
117 using boost::compute::int_
;
118 using boost::compute::float_
;
121 std::vector
<int_
> host_keys(n
);
122 std::vector
<float_
> host_values(n
);
123 for(int i
= 0; i
< n
; i
++){
124 host_keys
[i
] = n
- i
;
125 host_values
[i
] = (n
- i
) / 2.f
;
128 BOOST_COMPUTE_FUNCTION(bool, sort_int
, (int_ a
, int_ b
),
133 compute::vector
<int_
> keys(host_keys
.begin(), host_keys
.end(), queue
);
134 compute::vector
<float_
> values(host_values
.begin(), host_values
.end(), queue
);
136 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), sort_int
, queue
) == false);
137 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == false);
139 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), sort_int
, queue
);
141 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), sort_int
, queue
) == true);
142 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), queue
) == true);
145 BOOST_AUTO_TEST_CASE(sort_int_and_float2
)
147 using boost::compute::int_
;
148 using boost::compute::float2_
;
151 std::vector
<int_
> host_keys(n
);
152 std::vector
<float2_
> host_values(n
);
153 for(int i
= 0; i
< n
; i
++){
154 host_keys
[i
] = n
- i
;
155 host_values
[i
] = float2_((n
- i
) / 2.f
);
158 BOOST_COMPUTE_FUNCTION(bool, sort_float2
, (float2_ a
, float2_ b
),
163 compute::vector
<int_
> keys(host_keys
.begin(), host_keys
.end(), queue
);
164 compute::vector
<float2_
> values(host_values
.begin(), host_values
.end(), queue
);
166 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == false);
167 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), sort_float2
, queue
) == false);
169 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
171 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == true);
172 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), sort_float2
, queue
) == true);
175 BOOST_AUTO_TEST_CASE(sort_custom_struct_by_int
)
177 using boost::compute::int_
;
178 using boost::compute::float2_
;
181 std::vector
<int_
> host_keys(n
);
182 std::vector
<custom_struct
> host_values(n
);
183 for(int_ i
= 0; i
< n
; i
++){
184 host_keys
[i
] = n
- i
;
185 host_values
[i
].x
= n
- i
;
186 host_values
[i
].y
= n
- i
;
187 host_values
[i
].zw
= float2_((n
- i
) / 0.5f
);
190 BOOST_COMPUTE_FUNCTION(bool, sort_custom_struct
, (custom_struct a
, custom_struct b
),
195 compute::vector
<int_
> keys(host_keys
.begin(), host_keys
.end(), queue
);
196 compute::vector
<custom_struct
> values(host_values
.begin(), host_values
.end(), queue
);
198 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == false);
199 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), sort_custom_struct
, queue
) == false);
201 compute::sort_by_key(keys
.begin(), keys
.end(), values
.begin(), queue
);
203 BOOST_CHECK(compute::is_sorted(keys
.begin(), keys
.end(), queue
) == true);
204 BOOST_CHECK(compute::is_sorted(values
.begin(), values
.end(), sort_custom_struct
, queue
) == true);
207 BOOST_AUTO_TEST_SUITE_END()