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 TestReverse
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/algorithm/copy.hpp>
17 #include <boost/compute/algorithm/iota.hpp>
18 #include <boost/compute/algorithm/reverse.hpp>
19 #include <boost/compute/algorithm/reverse_copy.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/iterator/counting_iterator.hpp>
22 
23 #include "check_macros.hpp"
24 #include "context_setup.hpp"
25 
26 namespace bc = boost::compute;
27 
BOOST_AUTO_TEST_CASE(reverse_int)28 BOOST_AUTO_TEST_CASE(reverse_int)
29 {
30     bc::vector<int> vector(5, context);
31     bc::iota(vector.begin(), vector.end(), 0, queue);
32     CHECK_RANGE_EQUAL(int, 5, vector, (0, 1, 2, 3, 4));
33 
34     bc::reverse(vector.begin(), vector.end(), queue);
35     CHECK_RANGE_EQUAL(int, 5, vector, (4, 3, 2, 1, 0));
36 
37     bc::reverse(vector.begin() + 1, vector.end(), queue);
38     CHECK_RANGE_EQUAL(int, 5, vector, (4, 0, 1, 2, 3));
39 
40     bc::reverse(vector.begin() + 1, vector.end() - 1, queue);
41     CHECK_RANGE_EQUAL(int, 5, vector, (4, 2, 1, 0, 3));
42 
43     bc::reverse(vector.begin(), vector.end() - 2, queue);
44     CHECK_RANGE_EQUAL(int, 5, vector, (1, 2, 4, 0, 3));
45 
46     vector.resize(6, queue);
47     bc::iota(vector.begin(), vector.end(), 10, queue);
48     CHECK_RANGE_EQUAL(int, 6, vector, (10, 11, 12, 13, 14, 15));
49 
50     bc::reverse(vector.begin(), vector.end(), queue);
51     CHECK_RANGE_EQUAL(int, 6, vector, (15, 14, 13, 12, 11, 10));
52 
53     bc::reverse(vector.begin() + 3, vector.end(), queue);
54     CHECK_RANGE_EQUAL(int, 6, vector, (15, 14, 13, 10, 11, 12));
55 
56     bc::reverse(vector.begin() + 1, vector.end() - 2, queue);
57     CHECK_RANGE_EQUAL(int, 6, vector, (15, 10, 13, 14, 11, 12));
58 }
59 
BOOST_AUTO_TEST_CASE(reverse_copy_int)60 BOOST_AUTO_TEST_CASE(reverse_copy_int)
61 {
62     bc::vector<int> a(5, context);
63     bc::iota(a.begin(), a.end(), 0, queue);
64     CHECK_RANGE_EQUAL(int, 5, a, (0, 1, 2, 3, 4));
65 
66     bc::vector<int> b(5, context);
67     bc::vector<int>::iterator iter =
68         bc::reverse_copy(a.begin(), a.end(), b.begin(), queue);
69     BOOST_CHECK(iter == b.end());
70     CHECK_RANGE_EQUAL(int, 5, b, (4, 3, 2, 1, 0));
71 
72     iter = bc::reverse_copy(b.begin() + 1, b.end(), a.begin() + 1, queue);
73     BOOST_CHECK(iter == a.end());
74     CHECK_RANGE_EQUAL(int, 5, a, (0, 0, 1, 2, 3));
75 
76     iter = bc::reverse_copy(a.begin(), a.end() - 1, b.begin(), queue);
77     BOOST_CHECK(iter == (b.end() - 1));
78     CHECK_RANGE_EQUAL(int, 5, b, (2, 1, 0, 0, 0));
79 }
80 
BOOST_AUTO_TEST_CASE(reverse_copy_counting_iterator)81 BOOST_AUTO_TEST_CASE(reverse_copy_counting_iterator)
82 {
83     bc::vector<int> vector(5, context);
84     bc::reverse_copy(
85         bc::make_counting_iterator(1),
86         bc::make_counting_iterator(6),
87         vector.begin(),
88         queue
89     );
90     CHECK_RANGE_EQUAL(int, 5, vector, (5, 4, 3, 2, 1));
91 }
92 
93 BOOST_AUTO_TEST_SUITE_END()
94