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 TestMerge
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/types/pair.hpp>
16 #include <boost/compute/algorithm/copy_n.hpp>
17 #include <boost/compute/algorithm/merge.hpp>
18 #include <boost/compute/container/vector.hpp>
19 #include <boost/compute/lambda.hpp>
20
21 #include "check_macros.hpp"
22 #include "context_setup.hpp"
23
BOOST_AUTO_TEST_CASE(simple_merge_int)24 BOOST_AUTO_TEST_CASE(simple_merge_int)
25 {
26 int data1[] = { 1, 3, 5, 7 };
27 int data2[] = { 2, 4, 6, 8 };
28
29 boost::compute::vector<int> v1(4, context);
30 boost::compute::vector<int> v2(4, context);
31 boost::compute::vector<int> v3(8, context);
32
33 boost::compute::copy_n(data1, 4, v1.begin(), queue);
34 boost::compute::copy_n(data2, 4, v2.begin(), queue);
35 boost::compute::fill(v3.begin(), v3.end(), 0, queue);
36
37 // merge v1 with v2 into v3
38 boost::compute::merge(
39 v1.begin(), v1.end(),
40 v2.begin(), v2.end(),
41 v3.begin(),
42 queue
43 );
44 CHECK_RANGE_EQUAL(int, 8, v3, (1, 2, 3, 4, 5, 6, 7, 8));
45
46 // merge v2 with v1 into v3
47 boost::compute::merge(
48 v2.begin(), v2.end(),
49 v1.begin(), v1.end(),
50 v3.begin(),
51 queue
52 );
53 CHECK_RANGE_EQUAL(int, 8, v3, (1, 2, 3, 4, 5, 6, 7, 8));
54
55 // merge v1 with v1 into v3
56 boost::compute::merge(
57 v1.begin(), v1.end(),
58 v1.begin(), v1.end(),
59 v3.begin(),
60 queue
61 );
62 CHECK_RANGE_EQUAL(int, 8, v3, (1, 1, 3, 3, 5, 5, 7, 7));
63
64 // merge v2 with v2 into v3
65 boost::compute::merge(
66 v2.begin(), v2.end(),
67 v2.begin(), v2.end(),
68 v3.begin(),
69 queue
70 );
71 CHECK_RANGE_EQUAL(int, 8, v3, (2, 2, 4, 4, 6, 6, 8, 8));
72
73 // merge v1 with empty range into v3
74 boost::compute::merge(
75 v1.begin(), v1.end(),
76 v1.begin(), v1.begin(),
77 v3.begin(),
78 queue
79 );
80 CHECK_RANGE_EQUAL(int, 4, v3, (1, 3, 5, 7));
81
82 // merge v2 with empty range into v3
83 boost::compute::merge(
84 v1.begin(), v1.begin(),
85 v2.begin(), v2.end(),
86 v3.begin(),
87 queue
88 );
89 CHECK_RANGE_EQUAL(int, 4, v3, (2, 4, 6, 8));
90 }
91
BOOST_AUTO_TEST_CASE(merge_pairs)92 BOOST_AUTO_TEST_CASE(merge_pairs)
93 {
94 std::vector<std::pair<int, float> > data1;
95 std::vector<std::pair<int, float> > data2;
96
97 data1.push_back(std::make_pair(0, 0.1f));
98 data1.push_back(std::make_pair(2, 2.1f));
99 data1.push_back(std::make_pair(4, 4.1f));
100 data1.push_back(std::make_pair(6, 6.1f));
101 data2.push_back(std::make_pair(1, 1.1f));
102 data2.push_back(std::make_pair(3, 3.1f));
103 data2.push_back(std::make_pair(5, 5.1f));
104 data2.push_back(std::make_pair(7, 7.1f));
105
106 std::vector<std::pair<int, float> > data3(data1.size() + data2.size());
107 std::fill(data3.begin(), data3.end(), std::make_pair(-1, -1.f));
108
109 boost::compute::vector<std::pair<int, float> > v1(data1.size(), context);
110 boost::compute::vector<std::pair<int, float> > v2(data2.size(), context);
111 boost::compute::vector<std::pair<int, float> > v3(data3.size(), context);
112
113 boost::compute::copy(data1.begin(), data1.end(), v1.begin(), queue);
114 boost::compute::copy(data2.begin(), data2.end(), v2.begin(), queue);
115
116 using ::boost::compute::lambda::_1;
117 using ::boost::compute::lambda::_2;
118 using ::boost::compute::lambda::get;
119
120 boost::compute::merge(
121 v1.begin(), v1.end(),
122 v2.begin(), v2.end(),
123 v3.begin(),
124 get<0>(_1) < get<0>(_2),
125 queue
126 );
127
128 boost::compute::copy(v3.begin(), v3.end(), data3.begin(), queue);
129
130 BOOST_CHECK(v3[0] == std::make_pair(0, 0.1f));
131 BOOST_CHECK(v3[1] == std::make_pair(1, 1.1f));
132 BOOST_CHECK(v3[2] == std::make_pair(2, 2.1f));
133 BOOST_CHECK(v3[3] == std::make_pair(3, 3.1f));
134 BOOST_CHECK(v3[4] == std::make_pair(4, 4.1f));
135 BOOST_CHECK(v3[5] == std::make_pair(5, 5.1f));
136 BOOST_CHECK(v3[6] == std::make_pair(6, 6.1f));
137 BOOST_CHECK(v3[7] == std::make_pair(7, 7.1f));
138 }
139
BOOST_AUTO_TEST_CASE(merge_floats)140 BOOST_AUTO_TEST_CASE(merge_floats)
141 {
142 float data1[] = { 1.1f, 2.2f, 3.3f, 4.4f,
143 5.5f, 6.6f, 7.7f, 8.8f };
144 float data2[] = { 1.0f, 2.0f, 3.9f, 4.9f,
145 6.8f, 6.9f, 7.0f, 7.1f };
146
147 boost::compute::vector<float> v1(8, context);
148 boost::compute::vector<float> v2(8, context);
149 boost::compute::vector<float> v3(v1.size() + v2.size(), context);
150
151 boost::compute::copy_n(data1, 8, v1.begin(), queue);
152 boost::compute::copy_n(data2, 8, v2.begin(), queue);
153 boost::compute::fill(v3.begin(), v3.end(), 0.f, queue);
154
155 boost::compute::merge(
156 v1.begin(), v1.end(),
157 v2.begin(), v2.end(),
158 v3.begin(),
159 queue
160 );
161 CHECK_RANGE_EQUAL(float, 16, v3,
162 (1.0f, 1.1f, 2.0f, 2.2f, 3.3f, 3.9f, 4.4f, 4.9f,
163 5.5f, 6.6f, 6.8f, 6.9f, 7.0f, 7.1f, 7.7f, 8.8f)
164 );
165 }
166
167 BOOST_AUTO_TEST_SUITE_END()
168