summaryrefslogtreecommitdiffstats
path: root/src/dmclock/support/test/test_intrusive_heap.cc
blob: 18d770f317ab6bdff36d2fbf2576618cbb19bfd7 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

/*
 * Copyright (C) 2016 Red Hat Inc.
 *
 * Author: J. Eric Ivancich <ivancich@redhat.com>
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 2.1, as published by the Free Software Foundation.  See file
 * COPYING.
 */


#include <string>
#include <iostream>

#include "intrusive_heap.h"


struct TestCompare;
struct TestIntruData;


class Test1 {
    friend TestCompare;
    friend TestIntruData;

    int data;
    crimson::IntruHeapData heap_data;

public:
    explicit Test1(int _data) : data(_data) {}

    friend std::ostream& operator<<(std::ostream& out, const Test1& d) {
        out << d.data << " (" << d.heap_data << ")";
        return out;
    }

    int& the_data() { return data; }
};


struct TestCompare {
    bool operator()(const Test1& d1, const Test1& d2) {
        return d1.data < d2.data;
    }
};


struct TestIntruData {
    crimson::IntruHeapData& operator()(Test1& d) {
        return d.heap_data;
    }
};


int main(int argc, char** argv) {
    Test1 d1(2);
    Test1 d2(3);
    Test1 d3(1);
    Test1 d4(-5);

    crimson::IntruHeap<Test1, TestIntruData, TestCompare> my_heap;

    my_heap.push(d1);
    my_heap.push(d2);
    my_heap.push(d3);
    my_heap.push(d4);
    my_heap.push(Test1(-9));
    my_heap.push(Test1(99));
    my_heap.push(Test1(0));

    std::cout << my_heap << std::endl;

    auto& t = my_heap.top();
    t.the_data() = 17;
    my_heap.adjust_down(t);

    std::cout << my_heap << std::endl;

    my_heap.display_sorted(std::cout);

    while (!my_heap.empty()) {
        auto& top = my_heap.top();
        std::cout << top << std::endl;
        my_heap.pop();
        std::cout << my_heap << std::endl;
    }

    return 0;
}