1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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 TestAdjacentDifference
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/lambda.hpp>
17 #include <boost/compute/algorithm/iota.hpp>
18 #include <boost/compute/algorithm/copy.hpp>
19 #include <boost/compute/algorithm/fill.hpp>
20 #include <boost/compute/algorithm/all_of.hpp>
21 #include <boost/compute/algorithm/adjacent_difference.hpp>
22 #include <boost/compute/container/vector.hpp>
23
24 #include "check_macros.hpp"
25 #include "context_setup.hpp"
26
27 namespace compute = boost::compute;
28
BOOST_AUTO_TEST_CASE(adjacent_difference_int)29 BOOST_AUTO_TEST_CASE(adjacent_difference_int)
30 {
31 using compute::int_;
32
33 compute::vector<int_> a(5, context);
34 compute::iota(a.begin(), a.end(), 0, queue);
35 CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 2, 3, 4));
36
37 compute::vector<int_> b(5, context);
38 compute::vector<int_>::iterator iter =
39 compute::adjacent_difference(a.begin(), a.end(), b.begin(), queue);
40 BOOST_CHECK(iter == b.end());
41 CHECK_RANGE_EQUAL(int_, 5, b, (0, 1, 1, 1, 1));
42
43 int_ data[] = { 1, 9, 36, 48, 81 };
44 compute::copy(data, data + 5, a.begin(), queue);
45 CHECK_RANGE_EQUAL(int_, 5, a, (1, 9, 36, 48, 81));
46
47 iter = compute::adjacent_difference(a.begin(), a.end(), b.begin(), queue);
48 BOOST_CHECK(iter == b.end());
49 CHECK_RANGE_EQUAL(int_, 5, b, (1, 8, 27, 12, 33));
50 }
51
BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_last)52 BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_last)
53 {
54 using compute::int_;
55
56 compute::vector<int_> a(size_t(5), int_(1), queue);
57 compute::vector<int_> b(size_t(5), int_(0), queue);
58 compute::vector<int_>::iterator iter =
59 compute::adjacent_difference(a.begin(), a.begin(), b.begin(), queue);
60 BOOST_CHECK(iter == b.begin());
61 CHECK_RANGE_EQUAL(int_, 5, b, (0, 0, 0, 0, 0));
62 }
63
BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_result)64 BOOST_AUTO_TEST_CASE(adjacent_difference_first_eq_result)
65 {
66 using compute::int_;
67
68 compute::vector<int_> a(5, context);
69 compute::iota(a.begin(), a.end(), 0, queue);
70 CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 2, 3, 4));
71
72 compute::vector<int_>::iterator iter =
73 compute::adjacent_difference(a.begin(), a.end(), a.begin(), queue);
74 BOOST_CHECK(iter == a.end());
75 CHECK_RANGE_EQUAL(int_, 5, a, (0, 1, 1, 1, 1));
76 }
77
BOOST_AUTO_TEST_CASE(all_same)78 BOOST_AUTO_TEST_CASE(all_same)
79 {
80 using compute::int_;
81
82 compute::vector<int_> input(1000, context);
83 compute::fill(input.begin(), input.end(), 42, queue);
84
85 compute::vector<int_> output(input.size(), context);
86
87 compute::adjacent_difference(
88 input.begin(), input.end(), output.begin(), queue
89 );
90
91 int_ first;
92 compute::copy_n(output.begin(), 1, &first, queue);
93 BOOST_CHECK_EQUAL(first, 42);
94
95 using compute::lambda::_1;
96
97 BOOST_CHECK(
98 compute::all_of(output.begin() + 1, output.end(), _1 == 0, queue)
99 );
100 }
101
102 BOOST_AUTO_TEST_SUITE_END()
103