Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / histogram.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2014 Inktank <info@inktank.com>
7  *
8  * LGPL2.1 (see COPYING-LGPL2.1) or later
9  */
10
11 #include <iostream>
12 #include <gtest/gtest.h>
13
14 #include "common/histogram.h"
15 #include "include/stringify.h"
16
17 TEST(Histogram, Basic) {
18   pow2_hist_t h;
19
20   h.add(0);
21   h.add(0);
22   h.add(0);
23   ASSERT_EQ(3, h.h[0]);
24   ASSERT_EQ(1u, h.h.size());
25
26   h.add(1);
27   ASSERT_EQ(3, h.h[0]);
28   ASSERT_EQ(1, h.h[1]);
29   ASSERT_EQ(2u, h.h.size());
30
31   h.add(2);
32   h.add(2);
33   ASSERT_EQ(3, h.h[0]);
34   ASSERT_EQ(1, h.h[1]);
35   ASSERT_EQ(2, h.h[2]);
36   ASSERT_EQ(3u, h.h.size());
37 }
38
39 TEST(Histogram, Set) {
40   pow2_hist_t h;
41   h.set_bin(0, 12);
42   h.set_bin(2, 12);
43   ASSERT_EQ(12, h.h[0]);
44   ASSERT_EQ(0, h.h[1]);
45   ASSERT_EQ(12, h.h[2]);
46   ASSERT_EQ(3u, h.h.size());
47 }
48
49 TEST(Histogram, Position) {
50   pow2_hist_t h;
51   uint64_t lb, ub;
52   h.add(0);
53   ASSERT_EQ(-1, h.get_position_micro(-20, &lb, &ub));
54 }
55
56 TEST(Histogram, Position1) {
57   pow2_hist_t h;
58   h.add(0);
59   uint64_t lb, ub;
60   h.get_position_micro(0, &lb, &ub);
61   ASSERT_EQ(0u, lb);
62   ASSERT_EQ(1000000u, ub);
63   h.add(0);
64   h.add(0);
65   h.add(0);
66   h.get_position_micro(0, &lb, &ub);
67   ASSERT_EQ(0u, lb);
68   ASSERT_EQ(1000000u, ub);
69 }
70
71 TEST(Histogram, Position2) {
72   pow2_hist_t h;
73   h.add(1);
74   h.add(1);
75   uint64_t lb, ub;
76   h.get_position_micro(0, &lb, &ub);
77   ASSERT_EQ(0u, lb);
78   ASSERT_EQ(0u, ub);
79   h.add(0);
80   h.get_position_micro(0, &lb, &ub);
81   ASSERT_EQ(0u, lb);
82   ASSERT_EQ(333333u, ub);
83   h.get_position_micro(1, &lb, &ub);
84   ASSERT_EQ(333333u, lb);
85   ASSERT_EQ(1000000u, ub);
86 }
87
88 TEST(Histogram, Position3) {
89   pow2_hist_t h;
90   h.h.resize(10, 0);
91   h.h[0] = 1;
92   h.h[5] = 1;
93   uint64_t lb, ub;
94   h.get_position_micro(4, &lb, &ub);
95   ASSERT_EQ(500000u, lb);
96   ASSERT_EQ(500000u, ub);
97 }
98
99 TEST(Histogram, Position4) {
100   pow2_hist_t h;
101   h.h.resize(10, 0);
102   h.h[0] = UINT_MAX;
103   h.h[5] = UINT_MAX;
104   uint64_t lb, ub;
105   h.get_position_micro(4, &lb, &ub);
106   ASSERT_EQ(0u, lb);
107   ASSERT_EQ(0u, ub);
108 }
109
110 TEST(Histogram, Decay) {
111   pow2_hist_t h;
112   h.set_bin(0, 123);
113   h.set_bin(3, 12);
114   h.set_bin(5, 1);
115   h.decay(1);
116   ASSERT_EQ(61, h.h[0]);
117   ASSERT_EQ(6, h.h[3]);
118   ASSERT_EQ(4u, h.h.size());
119 }
120
121 /*
122  * Local Variables:
123  * compile-command: "cd ../.. ; make -j4 &&
124  *   make unittest_histogram &&
125  *   valgrind --tool=memcheck --leak-check=full \
126  *      ./unittest_histogram
127  *   "
128  * End:
129  */