} BOOST_AUTO_TEST_CASE(min_and_max) { using boost::compute::int2_; int data[] = { 5, 3, 1, 6, 4, 2 }; boost::compute::vector<int> vector(data, data + 6, queue); BOOST_COMPUTE_FUNCTION(int2_, min_and_max, (int2_ accumulator, const int value), { return (int2)(min(accumulator.x, value), max(accumulator.y, value)); }); int2_ result = boost::compute::accumulate( vector.begin(), vector.end(), int2_(100, -100), min_and_max, queue ); BOOST_CHECK_EQUAL(result[0], 1); BOOST_CHECK_EQUAL(result[1], 6); } BOOST_AUTO_TEST_CASE(min_max) { float data[] = { 1.2f, 5.5f, 0.1f, 9.6f, 4.2f, 6.7f, 9.0f, 3.4f }; boost::compute::vector<float> vec(data, data + 8, queue); using ::boost::compute::min; using ::boost::compute::max; float min_value = boost::compute::accumulate( vec.begin(), vec.end(), std::numeric_limits<float>::max(), min<float>(), queue
return a.x < b.x; }); // ensure vector is not sorted BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == false); // sort elements based on their first component compute::stable_sort(vec.begin(), vec.end(), compare_first, queue); // ensure vector is now sorted BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == true); // check sorted vector order std::vector<int2_> result(vec.size()); compute::copy(vec.begin(), vec.end(), result.begin(), queue); BOOST_CHECK_EQUAL(result[0], int2_(1, 2)); BOOST_CHECK_EQUAL(result[1], int2_(1, 1)); BOOST_CHECK_EQUAL(result[2], int2_(2, 1)); BOOST_CHECK_EQUAL(result[3], int2_(2, 2)); // function comparing the second component of each int2 BOOST_COMPUTE_FUNCTION(bool, compare_second, (int2_ a, int2_ b), { return a.y < b.y; }); // sort elements based on their second component compute::stable_sort(vec.begin(), vec.end(), compare_second, queue); // check sorted vector order compute::copy(vec.begin(), vec.end(), result.begin(), queue);