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 TestCount
12 #include <boost/test/unit_test.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/function.hpp>
18 #include <boost/compute/lambda.hpp>
19 #include <boost/compute/system.hpp>
20 #include <boost/compute/algorithm/copy.hpp>
21 #include <boost/compute/algorithm/count.hpp>
22 #include <boost/compute/algorithm/count_if.hpp>
23 #include <boost/compute/algorithm/iota.hpp>
24 #include <boost/compute/container/vector.hpp>
25 #include <boost/compute/iterator/constant_iterator.hpp>
27 #include "context_setup.hpp"
29 namespace bc
= boost::compute
;
30 namespace compute
= boost::compute
;
32 BOOST_AUTO_TEST_CASE(count_int
)
34 int data
[] = { 1, 2, 1, 2, 3 };
35 bc::vector
<int> vector(data
, data
+ 5, queue
);
36 BOOST_CHECK_EQUAL(bc::count(vector
.begin(), vector
.end(), 1, queue
), size_t(2));
37 BOOST_CHECK_EQUAL(bc::count(vector
.begin(), vector
.end(), 2, queue
), size_t(2));
38 BOOST_CHECK_EQUAL(bc::count(vector
.begin(), vector
.end(), 3, queue
), size_t(1));
39 BOOST_CHECK_EQUAL(bc::count(vector
.begin() + 1, vector
.end(), 1, queue
), size_t(1));
40 BOOST_CHECK_EQUAL(bc::count(vector
.begin() + 1, vector
.end() - 1, 3, queue
), size_t(0));
41 BOOST_CHECK_EQUAL(bc::count(vector
.begin() + 1, vector
.end() - 1, 2, queue
), size_t(2));
44 BOOST_AUTO_TEST_CASE(count_constant_int_range
)
47 bc::count(bc::make_constant_iterator(18, 0),
48 bc::make_constant_iterator(18, 5),
55 bc::count(bc::make_constant_iterator(19, 0),
56 bc::make_constant_iterator(19, 5),
63 BOOST_AUTO_TEST_CASE(count_if_greater_than_two
)
65 float data
[] = { 1.0f
, 2.5f
, -1.0f
, 3.0f
, 5.0f
, -8.0f
};
66 bc::vector
<float> vector(data
, data
+ 5, queue
);
69 bc::count_if(vector
.begin(), vector
.end(), bc::_1
> 2.0f
, queue
),
74 BOOST_AUTO_TEST_CASE(count_int4
)
76 int data
[] = { 1, 2, 3, 4,
82 bc::vector
<bc::int4_
> vector(reinterpret_cast<bc::int4_
*>(data
),
83 reinterpret_cast<bc::int4_
*>(data
) + 6,
85 BOOST_CHECK_EQUAL(vector
.size(), size_t(6));
88 bc::count(vector
.begin(), vector
.end(), bc::int4_(1, 2, 3, 4), queue
),
92 bc::count(vector
.begin(), vector
.end(), bc::int4_(4, 5, 6, 7), queue
),
96 bc::count(vector
.begin(), vector
.end(), bc::int4_(7, 8, 9, 1), queue
),
100 bc::count(vector
.begin(), vector
.end(), bc::int4_(0, 3, 2, 2), queue
),
104 bc::count(vector
.begin(), vector
.end(), bc::int4_(3, 4, 4, 5), queue
),
108 bc::count(vector
.begin(), vector
.end(), bc::int4_(1, 2, 3, 0), queue
),
112 bc::count(vector
.begin(), vector
.end(), bc::int4_(1, 9, 8, 7), queue
),
117 BOOST_AUTO_TEST_CASE(count_newlines
)
119 std::string string
= "abcdefg\nhijklmn\nopqrs\ntuv\nwxyz\n";
120 compute::vector
<char> data(string
.size(), context
);
121 compute::copy(string
.begin(), string
.end(), data
.begin(), queue
);
124 compute::count(data
.begin(), data
.end(), '\n', queue
),
129 BOOST_AUTO_TEST_CASE(count_uchar
)
131 using boost::compute::uchar_
;
133 unsigned char data
[] = { 0x00, 0x10, 0x2F, 0x10, 0x01, 0x00, 0x01, 0x00 };
134 compute::vector
<uchar_
> vector(8, context
);
135 compute::copy(data
, data
+ 8, vector
.begin(), queue
);
138 compute::count(vector
.begin(), vector
.end(), 0x00, queue
),
142 compute::count(vector
.begin(), vector
.end(), 0x10, queue
),
146 compute::count(vector
.begin(), vector
.end(), 0x2F, queue
),
150 compute::count(vector
.begin(), vector
.end(), 0x01, queue
),
154 compute::count(vector
.begin(), vector
.end(), 0xFF, queue
),
159 BOOST_AUTO_TEST_CASE(count_vector_component
)
168 using boost::compute::int2_
;
170 compute::vector
<int2_
> vector(4, context
);
172 reinterpret_cast<int2_
*>(data
),
173 reinterpret_cast<int2_
*>(data
) + 4,
178 using boost::compute::lambda::_1
;
179 using boost::compute::lambda::get
;
182 compute::count_if(vector
.begin(), vector
.end(), get
<0>(_1
) < 4, queue
),
186 compute::count_if(vector
.begin(), vector
.end(), get
<1>(_1
) > 3, queue
),
191 BOOST_AUTO_TEST_CASE(count_if_odd
)
193 compute::vector
<int> vec(2048, context
);
194 compute::iota(vec
.begin(), vec
.end(), 0, queue
);
196 BOOST_COMPUTE_FUNCTION(bool, is_odd
, (int x
),
202 compute::count_if(vec
.begin(), vec
.end(), is_odd
, queue
), vec
.size() / 2
206 BOOST_AUTO_TEST_CASE(count_if_with_reduce
)
208 compute::vector
<int> vec(2048, context
);
209 compute::iota(vec
.begin(), vec
.end(), 0, queue
);
211 using boost::compute::lambda::_1
;
214 compute::detail::count_if_with_reduce(
215 vec
.begin(), vec
.end(), _1
> 1024, queue
221 BOOST_AUTO_TEST_SUITE_END()