• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TestSortByTransform
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/algorithm/copy_n.hpp>
16 #include <boost/compute/algorithm/is_sorted.hpp>
17 #include <boost/compute/container/vector.hpp>
18 #include <boost/compute/experimental/sort_by_transform.hpp>
19 
20 #include "check_macros.hpp"
21 #include "context_setup.hpp"
22 
23 namespace compute = boost::compute;
24 
BOOST_AUTO_TEST_CASE(sort_int_by_abs)25 BOOST_AUTO_TEST_CASE(sort_int_by_abs)
26 {
27     int data[] = { 1, -2, 4, -3, 0, 5, -8, -9 };
28     compute::vector<int> vector(data, data + 8, queue);
29 
30     compute::experimental::sort_by_transform(
31         vector.begin(),
32         vector.end(),
33         compute::abs<int>(),
34         compute::less<int>(),
35         queue
36     );
37 
38     CHECK_RANGE_EQUAL(int, 8, vector, (0, 1, -2, -3, 4, 5, -8, -9));
39 }
40 
BOOST_AUTO_TEST_CASE(sort_vectors_by_length)41 BOOST_AUTO_TEST_CASE(sort_vectors_by_length)
42 {
43     using compute::float4_;
44 
45     float data[] = {
46         1.0f, 0.0f, 0.0f, 0.0f,
47         0.0f, 1.0f, 1.0f, 0.0f,
48         3.0f, 2.0f, 1.0f, 0.0f,
49         0.0f, 0.0f, 0.5f, 0.0f
50     };
51 
52     compute::vector<float4_> vector(4, context);
53     compute::copy_n(
54         reinterpret_cast<float4_ *>(data), 4, vector.begin(), queue
55     );
56 
57     compute::experimental::sort_by_transform(
58         vector.begin(),
59         vector.end(),
60         compute::length<float4_>(),
61         compute::less<float>(),
62         queue
63     );
64 
65     std::vector<float4_> host_vector(4);
66     compute::copy(
67         vector.begin(), vector.end(), host_vector.begin(), queue
68     );
69     BOOST_CHECK_EQUAL(host_vector[0], float4_(0.0f, 0.0f, 0.5f, 0.0f));
70     BOOST_CHECK_EQUAL(host_vector[1], float4_(1.0f, 0.0f, 0.0f, 0.0f));
71     BOOST_CHECK_EQUAL(host_vector[2], float4_(0.0f, 1.0f, 1.0f, 0.0f));
72     BOOST_CHECK_EQUAL(host_vector[3], float4_(3.0f, 2.0f, 1.0f, 0.0f));
73 }
74 
BOOST_AUTO_TEST_CASE(sort_vectors_by_component)75 BOOST_AUTO_TEST_CASE(sort_vectors_by_component)
76 {
77     using compute::float4_;
78 
79     float data[] = {
80         1.0f, 2.0f, 3.0f, 0.0f,
81         9.0f, 8.0f, 7.0f, 0.0f,
82         4.0f, 5.0f, 6.0f, 0.0f,
83         0.0f, 0.0f, 0.0f, 0.0f
84     };
85 
86     compute::vector<float4_> vector(4, context);
87     compute::copy_n(
88         reinterpret_cast<float4_ *>(data), 4, vector.begin(), queue
89     );
90 
91     // sort by y-component
92     compute::experimental::sort_by_transform(
93         vector.begin(),
94         vector.end(),
95         compute::get<1>(),
96         compute::less<float>(),
97         queue
98     );
99 
100     std::vector<float4_> host_vector(4);
101     compute::copy(
102         vector.begin(), vector.end(), host_vector.begin(), queue
103     );
104     BOOST_CHECK_EQUAL(host_vector[0], float4_(0.0f, 0.0f, 0.0f, 0.0f));
105     BOOST_CHECK_EQUAL(host_vector[1], float4_(1.0f, 2.0f, 3.0f, 0.0f));
106     BOOST_CHECK_EQUAL(host_vector[2], float4_(4.0f, 5.0f, 6.0f, 0.0f));
107     BOOST_CHECK_EQUAL(host_vector[3], float4_(9.0f, 8.0f, 7.0f, 0.0f));
108 }
109 
110 BOOST_AUTO_TEST_SUITE_END()
111