summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/heap/test/stable_heap_tests.hpp
blob: 0b4e7995fe5ee833043908ff228e3ac0f7dfa103 (plain)
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
93
94
95
96
97
98
99
/*=============================================================================
    Copyright (c) 2010 Tim Blechmann

    Use, modification and distribution is subject to 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)
=============================================================================*/

#include <boost/foreach.hpp>
#include "common_heap_tests.hpp"

struct q_tester
{
    q_tester(int i = 0, int j = 0):
        value(i), id(j)
    {}

    bool operator< (q_tester const & rhs) const
    {
        return value < rhs.value;
    }

    bool operator> (q_tester const & rhs) const
    {
        return value > rhs.value;
    }

    bool operator== (q_tester const & rhs) const
    {
        return (value == rhs.value) && (id == rhs.id);
    }

    int value;
    int id;
};

std::ostream& operator<< (std::ostream& out, q_tester const & t)
{
    out << "[" << t.value << " " << t.id << "]";
    return out;
}

typedef std::vector<q_tester> stable_test_data;

stable_test_data make_stable_test_data(int size, int same_count = 3,
                                       int offset = 0, int strive = 1)
{
    stable_test_data ret;

    for (int i = 0; i != size; ++i)
        for (int j = 0; j != same_count; ++j)
            ret.push_back(q_tester(i * strive + offset, j));
    return ret;
}

struct compare_by_id
{
    bool operator()(q_tester const & lhs, q_tester const & rhs)
    {
        return lhs.id > rhs.id;
    }
};

template <typename pri_queue>
void pri_queue_stable_test_sequential_push(void)
{
    stable_test_data data = make_stable_test_data(test_size);

    pri_queue q;
    fill_q(q, data);
    std::stable_sort(data.begin(), data.end(), compare_by_id());
    std::stable_sort(data.begin(), data.end(), std::less<q_tester>());
    check_q(q, data);
}

template <typename pri_queue>
void pri_queue_stable_test_sequential_reverse_push(void)
{
    stable_test_data data = make_stable_test_data(test_size);
    pri_queue q;
    stable_test_data push_data(data);

    std::stable_sort(push_data.begin(), push_data.end(), std::greater<q_tester>());

    fill_q(q, push_data);

    std::stable_sort(data.begin(), data.end(), compare_by_id());
    std::stable_sort(data.begin(), data.end(), std::less<q_tester>());

    check_q(q, data);
}


template <typename pri_queue>
void run_stable_heap_tests(void)
{
    pri_queue_stable_test_sequential_push<pri_queue>();
    pri_queue_stable_test_sequential_reverse_push<pri_queue>();
}