|
15 | 15 | #include <boost/compute/algorithm/sort_by_key.hpp>
|
16 | 16 | #include <boost/compute/algorithm/is_sorted.hpp>
|
17 | 17 | #include <boost/compute/container/vector.hpp>
|
| 18 | +#include <boost/compute/types/struct.hpp> |
| 19 | + |
| 20 | +struct custom_struct |
| 21 | +{ |
| 22 | + boost::compute::int_ x; |
| 23 | + boost::compute::int_ y; |
| 24 | + boost::compute::float2_ zw; |
| 25 | +}; |
| 26 | + |
| 27 | +BOOST_COMPUTE_ADAPT_STRUCT(custom_struct, custom_struct, (x, y, zw)) |
18 | 28 |
|
19 | 29 | #include "check_macros.hpp"
|
20 | 30 | #include "context_setup.hpp"
|
@@ -69,15 +79,15 @@ BOOST_AUTO_TEST_CASE(sort_int_2)
|
69 | 79 | BOOST_AUTO_TEST_CASE(sort_char_by_int)
|
70 | 80 | {
|
71 | 81 | int keys_data[] = { 6, 2, 1, 3, 4, 7, 5, 0 };
|
72 |
| - char values_data[] = { 'g', 'c', 'b', 'd', 'e', 'h', 'f', 'a' }; |
| 82 | + compute::char_ values_data[] = { 'g', 'c', 'b', 'd', 'e', 'h', 'f', 'a' }; |
73 | 83 |
|
74 | 84 | compute::vector<int> keys(keys_data, keys_data + 8, queue);
|
75 |
| - compute::vector<char> values(values_data, values_data + 8, queue); |
| 85 | + compute::vector<compute::char_> values(values_data, values_data + 8, queue); |
76 | 86 |
|
77 | 87 | compute::sort_by_key(keys.begin(), keys.end(), values.begin(), queue);
|
78 | 88 |
|
79 | 89 | CHECK_RANGE_EQUAL(int, 8, keys, (0, 1, 2, 3, 4, 5, 6, 7));
|
80 |
| - CHECK_RANGE_EQUAL(char, 8, values, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')); |
| 90 | + CHECK_RANGE_EQUAL(compute::char_, 8, values, ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')); |
81 | 91 | }
|
82 | 92 |
|
83 | 93 | BOOST_AUTO_TEST_CASE(sort_int_and_float)
|
@@ -132,4 +142,66 @@ BOOST_AUTO_TEST_CASE(sort_int_and_float_custom_comparison_func)
|
132 | 142 | BOOST_CHECK(compute::is_sorted(values.begin(), values.end(), queue) == true);
|
133 | 143 | }
|
134 | 144 |
|
| 145 | +BOOST_AUTO_TEST_CASE(sort_int_and_float2) |
| 146 | +{ |
| 147 | + using boost::compute::int_; |
| 148 | + using boost::compute::float2_; |
| 149 | + |
| 150 | + int n = 1024; |
| 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); |
| 156 | + } |
| 157 | + |
| 158 | + BOOST_COMPUTE_FUNCTION(bool, sort_float2, (float2_ a, float2_ b), |
| 159 | + { |
| 160 | + return a.x < b.x; |
| 161 | + }); |
| 162 | + |
| 163 | + compute::vector<int_> keys(host_keys.begin(), host_keys.end(), queue); |
| 164 | + compute::vector<float2_> values(host_values.begin(), host_values.end(), queue); |
| 165 | + |
| 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); |
| 168 | + |
| 169 | + compute::sort_by_key(keys.begin(), keys.end(), values.begin(), queue); |
| 170 | + |
| 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); |
| 173 | +} |
| 174 | + |
| 175 | +BOOST_AUTO_TEST_CASE(sort_custom_struct_by_int) |
| 176 | +{ |
| 177 | + using boost::compute::int_; |
| 178 | + using boost::compute::float2_; |
| 179 | + |
| 180 | + int_ n = 1024; |
| 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); |
| 188 | + } |
| 189 | + |
| 190 | + BOOST_COMPUTE_FUNCTION(bool, sort_custom_struct, (custom_struct a, custom_struct b), |
| 191 | + { |
| 192 | + return a.x < b.x; |
| 193 | + }); |
| 194 | + |
| 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); |
| 197 | + |
| 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); |
| 200 | + |
| 201 | + compute::sort_by_key(keys.begin(), keys.end(), values.begin(), queue); |
| 202 | + |
| 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); |
| 205 | +} |
| 206 | + |
135 | 207 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments