Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / dmclock / support / test / test_intrusive_heap.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 /*
5  * Copyright (C) 2016 Red Hat Inc.
6  */
7
8
9 #include <string>
10 #include <iostream>
11
12 #include "intrusive_heap.h"
13
14
15 struct TestCompare;
16 struct TestIntruData;
17
18
19 class Test1 {
20     friend TestCompare;
21     friend TestIntruData;
22
23     int data;
24     crimson::IntruHeapData heap_data;
25
26 public:
27     Test1(int _data) : data(_data) {}
28
29     friend std::ostream& operator<<(std::ostream& out, const Test1& d) {
30         out << d.data << " (" << d.heap_data << ")";
31         return out;
32     }
33
34     int& the_data() { return data; }
35 };
36
37
38 struct TestCompare {
39     bool operator()(const Test1& d1, const Test1& d2) {
40         return d1.data < d2.data;
41     }
42 };
43
44
45 struct TestIntruData {
46     crimson::IntruHeapData& operator()(Test1& d) {
47         return d.heap_data;
48     }
49 };
50
51
52 int main(int argc, char** argv) {
53     Test1 d1(2);
54     Test1 d2(3);
55     Test1 d3(1);
56     Test1 d4(-5);
57
58     crimson::IntruHeap<Test1, TestIntruData, TestCompare> my_heap;
59
60     my_heap.push(d1);
61     my_heap.push(d2);
62     my_heap.push(d3);
63     my_heap.push(d4);
64     my_heap.push(Test1(-9));
65     my_heap.push(Test1(99));
66     my_heap.push(Test1(0));
67
68     std::cout << my_heap << std::endl;
69
70     auto& t = my_heap.top();
71     t.the_data() = 17;
72     my_heap.adjust_down(t);
73
74     std::cout << my_heap << std::endl;
75
76     my_heap.display_sorted(std::cout);
77
78     while (!my_heap.empty()) {
79         auto& top = my_heap.top();
80         std::cout << top << std::endl;
81         my_heap.pop();
82         std::cout << my_heap << std::endl;
83     }
84
85     return 0;
86 }