• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@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 TestUnique
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/function.hpp>
15 #include <boost/compute/algorithm/iota.hpp>
16 #include <boost/compute/algorithm/none_of.hpp>
17 #include <boost/compute/algorithm/transform.hpp>
18 #include <boost/compute/algorithm/unique.hpp>
19 #include <boost/compute/container/vector.hpp>
20 
21 #include "check_macros.hpp"
22 #include "context_setup.hpp"
23 
24 namespace compute = boost::compute;
25 
BOOST_AUTO_TEST_CASE(unique_int)26 BOOST_AUTO_TEST_CASE(unique_int)
27 {
28     int data[] = {1, 6, 6, 4, 2, 2, 4};
29 
30     compute::vector<int> input(data, data + 7, queue);
31 
32     compute::vector<int>::iterator iter =
33         compute::unique(input.begin(), input.end(), queue);
34 
35     BOOST_VERIFY(iter == input.begin() + 5);
36     CHECK_RANGE_EQUAL(int, 7, input, (1, 6, 4, 2, 4, 2, 4));
37 }
38 
BOOST_AUTO_TEST_CASE(all_same_float)39 BOOST_AUTO_TEST_CASE(all_same_float)
40 {
41     compute::vector<float> vec(1024, context);
42     compute::fill(vec.begin(), vec.end(), 3.14f, queue);
43 
44     compute::vector<float>::iterator iter =
45         compute::unique(vec.begin(), vec.end(), queue);
46 
47     BOOST_VERIFY(iter == vec.begin() + 1);
48 
49     float first;
50     compute::copy_n(vec.begin(), 1, &first, queue);
51     BOOST_CHECK_EQUAL(first, 3.14f);
52 }
53 
BOOST_AUTO_TEST_CASE(unique_even_uints)54 BOOST_AUTO_TEST_CASE(unique_even_uints)
55 {
56     using compute::uint_;
57 
58     // create vector filled with [0, 1, 2, ...]
59     compute::vector<uint_> vec(1024, context);
60     compute::iota(vec.begin(), vec.end(), 0, queue);
61 
62     // all should be unique
63     compute::vector<uint_>::iterator iter = compute::unique(
64         vec.begin(), vec.end(), queue
65     );
66     BOOST_VERIFY(iter == vec.end());
67 
68     // if odd, return the prior even number, else return the number
69     BOOST_COMPUTE_FUNCTION(uint_, odd_to_even, (uint_ x),
70     {
71         if(x & 1){
72             return x - 1;
73         }
74         else {
75             return x;
76         }
77     });
78 
79     // set all odd numbers the previous even number
80     compute::transform(
81         vec.begin(), vec.end(), vec.begin(), odd_to_even, queue
82     );
83 
84     // now the vector should contain [0, 0, 2, 2, 4, 4, ...]
85     iter = compute::unique(vec.begin(), vec.end(), queue);
86     BOOST_VERIFY(iter == vec.begin() + (vec.size() / 2));
87 
88     // ensure all of the values are even
89     BOOST_COMPUTE_FUNCTION(bool, is_odd, (uint_ x),
90     {
91         return x & 1;
92     });
93     BOOST_VERIFY(compute::none_of(vec.begin(), vec.end(), is_odd, queue));
94 }
95 
96 BOOST_AUTO_TEST_SUITE_END()
97