]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/test/test_stable_sort.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / compute / test / test_stable_sort.cpp
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 TestStableSort
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/function.hpp>
16 #include <boost/compute/algorithm/stable_sort.hpp>
17 #include <boost/compute/algorithm/is_sorted.hpp>
18 #include <boost/compute/container/vector.hpp>
19
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22
23 namespace compute = boost::compute;
24
25 BOOST_AUTO_TEST_CASE(sort_int_vector)
26 {
27 int data[] = { -4, 152, -5000, 963, 75321, -456, 0, 1112 };
28 compute::vector<int> vector(data, data + 8, queue);
29 BOOST_CHECK_EQUAL(vector.size(), size_t(8));
30 BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == false);
31
32 compute::stable_sort(vector.begin(), vector.end(), queue);
33 BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == true);
34 CHECK_RANGE_EQUAL(int, 8, vector, (-5000, -456, -4, 0, 152, 963, 1112, 75321));
35
36 // sort reversed
37 compute::stable_sort(vector.begin(), vector.end(), compute::greater<int>(), queue);
38 CHECK_RANGE_EQUAL(int, 8, vector, (75321, 1112, 963, 152, 0, -4, -456, -5000));
39 }
40
41 BOOST_AUTO_TEST_CASE(sort_int2)
42 {
43 using compute::int2_;
44
45 // device vector of int2's
46 compute::vector<int2_> vec(context);
47 vec.push_back(int2_(2, 1), queue);
48 vec.push_back(int2_(2, 2), queue);
49 vec.push_back(int2_(1, 2), queue);
50 vec.push_back(int2_(1, 1), queue);
51
52 // function comparing the first component of each int2
53 BOOST_COMPUTE_FUNCTION(bool, compare_first, (int2_ a, int2_ b),
54 {
55 return a.x < b.x;
56 });
57
58 // ensure vector is not sorted
59 BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == false);
60
61 // sort elements based on their first component
62 compute::stable_sort(vec.begin(), vec.end(), compare_first, queue);
63
64 // ensure vector is now sorted
65 BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == true);
66
67 // check sorted vector order
68 std::vector<int2_> result(vec.size());
69 compute::copy(vec.begin(), vec.end(), result.begin(), queue);
70 BOOST_CHECK_EQUAL(result[0], int2_(1, 2));
71 BOOST_CHECK_EQUAL(result[1], int2_(1, 1));
72 BOOST_CHECK_EQUAL(result[2], int2_(2, 1));
73 BOOST_CHECK_EQUAL(result[3], int2_(2, 2));
74
75 // function comparing the second component of each int2
76 BOOST_COMPUTE_FUNCTION(bool, compare_second, (int2_ a, int2_ b),
77 {
78 return a.y < b.y;
79 });
80
81 // sort elements based on their second component
82 compute::stable_sort(vec.begin(), vec.end(), compare_second, queue);
83
84 // check sorted vector order
85 compute::copy(vec.begin(), vec.end(), result.begin(), queue);
86 BOOST_CHECK_EQUAL(result[0], int2_(1, 1));
87 BOOST_CHECK_EQUAL(result[1], int2_(2, 1));
88 BOOST_CHECK_EQUAL(result[2], int2_(1, 2));
89 BOOST_CHECK_EQUAL(result[3], int2_(2, 2));
90 }
91
92 BOOST_AUTO_TEST_SUITE_END()