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 TestCopyIf
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/lambda.hpp>
15 #include <boost/compute/algorithm/copy_if.hpp>
16 #include <boost/compute/container/vector.hpp>
17
18 #include "check_macros.hpp"
19 #include "context_setup.hpp"
20
21 namespace bc = boost::compute;
22 namespace compute = boost::compute;
23
BOOST_AUTO_TEST_CASE(copy_if_int)24 BOOST_AUTO_TEST_CASE(copy_if_int)
25 {
26 int data[] = { 1, 6, 3, 5, 8, 2, 4 };
27 bc::vector<int> input(data, data + 7, queue);
28
29 bc::vector<int> output(input.size(), context);
30 bc::fill(output.begin(), output.end(), -1, queue);
31
32 using ::boost::compute::_1;
33
34 bc::vector<int>::iterator iter =
35 bc::copy_if(input.begin(), input.end(),
36 output.begin(), _1 < 5, queue);
37 BOOST_VERIFY(iter == output.begin() + 4);
38 CHECK_RANGE_EQUAL(int, 7, output, (1, 3, 2, 4, -1, -1, -1));
39
40 bc::fill(output.begin(), output.end(), 42, queue);
41 iter =
42 bc::copy_if(input.begin(), input.end(),
43 output.begin(), _1 * 2 >= 10, queue);
44 BOOST_VERIFY(iter == output.begin() + 3);
45 CHECK_RANGE_EQUAL(int, 7, output, (6, 5, 8, 42, 42, 42, 42));
46 }
47
BOOST_AUTO_TEST_CASE(copy_if_odd)48 BOOST_AUTO_TEST_CASE(copy_if_odd)
49 {
50 int data[] = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
51 bc::vector<int> input(data, data + 10, queue);
52
53 using ::boost::compute::_1;
54
55 bc::vector<int> odds(input.size(), context);
56 bc::vector<int>::iterator odds_end =
57 bc::copy_if(input.begin(), input.end(),
58 odds.begin(), _1 % 2 == 1, queue);
59 BOOST_CHECK(odds_end == odds.begin() + 6);
60 CHECK_RANGE_EQUAL(int, 6, odds, (1, 3, 5, 1, 3, 5));
61
62 bc::vector<int> evens(input.size(), context);
63 bc::vector<int>::iterator evens_end =
64 bc::copy_if(input.begin(), input.end(),
65 evens.begin(), _1 % 2 == 0, queue);
66 BOOST_CHECK(evens_end == evens.begin() + 4);
67 CHECK_RANGE_EQUAL(int, 4, evens, (2, 4, 2, 4));
68 }
69
BOOST_AUTO_TEST_CASE(clip_points_below_plane)70 BOOST_AUTO_TEST_CASE(clip_points_below_plane)
71 {
72 float data[] = { 1.0f, 2.0f, 3.0f, 0.0f,
73 -1.0f, 2.0f, 3.0f, 0.0f,
74 -2.0f, -3.0f, 4.0f, 0.0f,
75 4.0f, -3.0f, 2.0f, 0.0f };
76 bc::vector<bc::float4_> points(reinterpret_cast<bc::float4_ *>(data),
77 reinterpret_cast<bc::float4_ *>(data) + 4,
78 queue);
79
80 // create output vector filled with (0, 0, 0, 0)
81 bc::vector<bc::float4_> output(points.size(), context);
82 bc::fill(output.begin(), output.end(),
83 bc::float4_(0.0f, 0.0f, 0.0f, 0.0f), queue);
84
85 // define the plane (at origin, +X normal)
86 bc::float4_ plane_origin(0.0f, 0.0f, 0.0f, 0.0f);
87 bc::float4_ plane_normal(1.0f, 0.0f, 0.0f, 0.0f);
88
89 using ::boost::compute::_1;
90 using ::boost::compute::lambda::dot;
91
92 bc::vector<bc::float4_>::const_iterator iter =
93 bc::copy_if(points.begin(),
94 points.end(),
95 output.begin(),
96 dot(_1 - plane_origin, plane_normal) > 0.0f,
97 queue);
98 BOOST_CHECK(iter == output.begin() + 2);
99 }
100
BOOST_AUTO_TEST_CASE(copy_index_if_int)101 BOOST_AUTO_TEST_CASE(copy_index_if_int)
102 {
103 int data[] = { 1, 6, 3, 5, 8, 2, 4 };
104 compute::vector<int> input(data, data + 7, queue);
105
106 compute::vector<int> output(input.size(), context);
107 compute::fill(output.begin(), output.end(), -1, queue);
108
109 using ::boost::compute::_1;
110 using ::boost::compute::detail::copy_index_if;
111
112 compute::vector<int>::iterator iter =
113 copy_index_if(input.begin(), input.end(), output.begin(),
114 _1 < 5, queue);
115 BOOST_VERIFY(iter == output.begin() + 4);
116 CHECK_RANGE_EQUAL(int, 7, output, (0, 2, 5, 6, -1, -1, -1));
117 }
118
119 BOOST_AUTO_TEST_SUITE_END()
120