File: test_stable_sort.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (92 lines) | stat: -rw-r--r-- 3,337 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://boostorg.github.com/compute for more information.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE TestStableSort
#include <boost/test/unit_test.hpp>

#include <boost/compute/system.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/algorithm/stable_sort.hpp>
#include <boost/compute/algorithm/is_sorted.hpp>
#include <boost/compute/container/vector.hpp>

#include "check_macros.hpp"
#include "context_setup.hpp"

namespace compute = boost::compute;

BOOST_AUTO_TEST_CASE(sort_int_vector)
{
    int data[] = { -4, 152, -5000, 963, 75321, -456, 0, 1112 };
    compute::vector<int> vector(data, data + 8, queue);
    BOOST_CHECK_EQUAL(vector.size(), size_t(8));
    BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == false);

    compute::stable_sort(vector.begin(), vector.end(), queue);
    BOOST_CHECK(compute::is_sorted(vector.begin(), vector.end(), queue) == true);
    CHECK_RANGE_EQUAL(int, 8, vector, (-5000, -456, -4, 0, 152, 963, 1112, 75321));

    // sort reversed
    compute::stable_sort(vector.begin(), vector.end(), compute::greater<int>(), queue);
    CHECK_RANGE_EQUAL(int, 8, vector, (75321, 1112, 963, 152, 0, -4, -456, -5000));
}

BOOST_AUTO_TEST_CASE(sort_int2)
{
    using compute::int2_;

    // device vector of int2's
    compute::vector<int2_> vec(context);
    vec.push_back(int2_(2, 1), queue);
    vec.push_back(int2_(2, 2), queue);
    vec.push_back(int2_(1, 2), queue);
    vec.push_back(int2_(1, 1), queue);

    // function comparing the first component of each int2
    BOOST_COMPUTE_FUNCTION(bool, compare_first, (int2_ a, int2_ b),
    {
        return a.x < b.x;
    });

    // ensure vector is not sorted
    BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == false);

    // sort elements based on their first component
    compute::stable_sort(vec.begin(), vec.end(), compare_first, queue);

    // ensure vector is now sorted
    BOOST_CHECK(compute::is_sorted(vec.begin(), vec.end(), compare_first, queue) == true);

    // check sorted vector order
    std::vector<int2_> result(vec.size());
    compute::copy(vec.begin(), vec.end(), result.begin(), queue);
    BOOST_CHECK_EQUAL(result[0], int2_(1, 2));
    BOOST_CHECK_EQUAL(result[1], int2_(1, 1));
    BOOST_CHECK_EQUAL(result[2], int2_(2, 1));
    BOOST_CHECK_EQUAL(result[3], int2_(2, 2));

    // function comparing the second component of each int2
    BOOST_COMPUTE_FUNCTION(bool, compare_second, (int2_ a, int2_ b),
    {
        return a.y < b.y;
    });

    // sort elements based on their second component
    compute::stable_sort(vec.begin(), vec.end(), compare_second, queue);

    // check sorted vector order
    compute::copy(vec.begin(), vec.end(), result.begin(), queue);
    BOOST_CHECK_EQUAL(result[0], int2_(1, 1));
    BOOST_CHECK_EQUAL(result[1], int2_(2, 1));
    BOOST_CHECK_EQUAL(result[2], int2_(1, 2));
    BOOST_CHECK_EQUAL(result[3], int2_(2, 2));
}

BOOST_AUTO_TEST_SUITE_END()