• 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 TestFind
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/command_queue.hpp>
15 #include <boost/compute/lambda.hpp>
16 #include <boost/compute/algorithm/copy.hpp>
17 #include <boost/compute/algorithm/find.hpp>
18 #include <boost/compute/algorithm/find_if.hpp>
19 #include <boost/compute/algorithm/find_if_not.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/iterator/constant_buffer_iterator.hpp>
22 
23 #include "check_macros.hpp"
24 #include "context_setup.hpp"
25 
26 namespace bc = boost::compute;
27 namespace compute = boost::compute;
28 
BOOST_AUTO_TEST_CASE(find_int)29 BOOST_AUTO_TEST_CASE(find_int)
30 {
31     int data[] = { 9, 15, 1, 4, 9, 9, 4, 15, 12, 1 };
32     bc::vector<int> vector(data, data + 10, queue);
33 
34     bc::vector<int>::iterator iter =
35         bc::find(vector.begin(), vector.end(), 4, queue);
36     BOOST_CHECK(iter == vector.begin() + 3);
37     BOOST_CHECK_EQUAL(*iter, 4);
38 
39     iter = bc::find(vector.begin(), vector.end(), 12, queue);
40     BOOST_CHECK(iter == vector.begin() + 8);
41     BOOST_CHECK_EQUAL(*iter, 12);
42 
43     iter = bc::find(vector.begin(), vector.end(), 1, queue);
44     BOOST_CHECK(iter == vector.begin() + 2);
45     BOOST_CHECK_EQUAL(*iter, 1);
46 
47     iter = bc::find(vector.begin(), vector.end(), 9, queue);
48     BOOST_CHECK(iter == vector.begin());
49     BOOST_CHECK_EQUAL(*iter, 9);
50 
51     iter = bc::find(vector.begin(), vector.end(), 100, queue);
52     BOOST_CHECK(iter == vector.end());
53 }
54 
BOOST_AUTO_TEST_CASE(find_int2)55 BOOST_AUTO_TEST_CASE(find_int2)
56 {
57     using bc::int2_;
58 
59     int data[] = { 1, 2, 4, 5, 7, 8 };
60     bc::vector<int2_> vector(
61         reinterpret_cast<int2_ *>(data),
62         reinterpret_cast<int2_ *>(data) + 3,
63         queue
64     );
65     CHECK_RANGE_EQUAL(int2_, 3, vector, (int2_(1, 2), int2_(4, 5), int2_(7, 8)));
66 
67     bc::vector<int2_>::iterator iter =
68         bc::find(vector.begin(), vector.end(), int2_(4, 5), queue);
69     BOOST_CHECK(iter == vector.begin() + 1);
70     BOOST_CHECK_EQUAL(*iter, int2_(4, 5));
71 
72     iter = bc::find(vector.begin(), vector.end(), int2_(10, 11), queue);
73     BOOST_CHECK(iter == vector.end());
74 }
75 
BOOST_AUTO_TEST_CASE(find_if_not_int)76 BOOST_AUTO_TEST_CASE(find_if_not_int)
77 {
78     int data[] = { 2, 4, 6, 8, 1, 3, 5, 7, 9 };
79     bc::vector<int> vector(data, data + 9, queue);
80 
81     bc::vector<int>::iterator iter =
82         bc::find_if_not(vector.begin(), vector.end(), bc::_1 == 2, queue);
83     BOOST_CHECK(iter == vector.begin() + 1);
84     BOOST_CHECK_EQUAL(*iter, 4);
85 }
86 
BOOST_AUTO_TEST_CASE(find_point_by_distance)87 BOOST_AUTO_TEST_CASE(find_point_by_distance)
88 {
89     using boost::compute::float2_;
90     using boost::compute::lambda::_1;
91     using boost::compute::lambda::distance;
92 
93     float2_ points[] = {
94         float2_(0, 0), float2_(2, 2), float2_(4, 4), float2_(8, 8)
95     };
96     compute::vector<float2_> vec(points, points + 4, queue);
97 
98     compute::vector<float2_>::iterator iter =
99         compute::find_if(vec.begin(), vec.end(), distance(_1, float2_(5, 5)) < 1.5f, queue);
100     BOOST_CHECK(iter == vec.begin() + 2);
101 
102     float2_ value;
103     compute::copy_n(iter, 1, &value, queue);
104     BOOST_CHECK_EQUAL(value, float2_(4, 4));
105 }
106 
107 BOOST_AUTO_TEST_SUITE_END()
108