]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/test/test_stable_sort.cpp
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 TestStableSort
12 #include <boost/test/unit_test.hpp>
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>
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
23 namespace compute
= boost::compute
;
25 BOOST_AUTO_TEST_CASE(sort_int_vector
)
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);
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));
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));
41 BOOST_AUTO_TEST_CASE(sort_int2
)
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
);
52 // function comparing the first component of each int2
53 BOOST_COMPUTE_FUNCTION(bool, compare_first
, (int2_ a
, int2_ b
),
58 // ensure vector is not sorted
59 BOOST_CHECK(compute::is_sorted(vec
.begin(), vec
.end(), compare_first
, queue
) == false);
61 // sort elements based on their first component
62 compute::stable_sort(vec
.begin(), vec
.end(), compare_first
, queue
);
64 // ensure vector is now sorted
65 BOOST_CHECK(compute::is_sorted(vec
.begin(), vec
.end(), compare_first
, queue
) == true);
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));
75 // function comparing the second component of each int2
76 BOOST_COMPUTE_FUNCTION(bool, compare_second
, (int2_ a
, int2_ b
),
81 // sort elements based on their second component
82 compute::stable_sort(vec
.begin(), vec
.end(), compare_second
, queue
);
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));
92 BOOST_AUTO_TEST_SUITE_END()