rippled
Histogram_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2017 Ripple Labs Inc
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/beast/unit_test.h>
21 #include <test/csf/Histogram.h>
22 
23 namespace ripple {
24 namespace test {
25 
26 class Histogram_test : public beast::unit_test::suite
27 {
28 public:
29  void
30  run() override
31  {
32  using namespace csf;
33  Histogram<int> hist;
34 
35  BEAST_EXPECT(hist.size() == 0);
36  BEAST_EXPECT(hist.numBins() == 0);
37  BEAST_EXPECT(hist.minValue() == 0);
38  BEAST_EXPECT(hist.maxValue() == 0);
39  BEAST_EXPECT(hist.avg() == 0);
40  BEAST_EXPECT(hist.percentile(0.0f) == hist.minValue());
41  BEAST_EXPECT(hist.percentile(0.5f) == 0);
42  BEAST_EXPECT(hist.percentile(0.9f) == 0);
43  BEAST_EXPECT(hist.percentile(1.0f) == hist.maxValue());
44 
45  hist.insert(1);
46 
47  BEAST_EXPECT(hist.size() == 1);
48  BEAST_EXPECT(hist.numBins() == 1);
49  BEAST_EXPECT(hist.minValue() == 1);
50  BEAST_EXPECT(hist.maxValue() == 1);
51  BEAST_EXPECT(hist.avg() == 1);
52  BEAST_EXPECT(hist.percentile(0.0f) == hist.minValue());
53  BEAST_EXPECT(hist.percentile(0.5f) == 1);
54  BEAST_EXPECT(hist.percentile(0.9f) == 1);
55  BEAST_EXPECT(hist.percentile(1.0f) == hist.maxValue());
56 
57  hist.insert(9);
58 
59  BEAST_EXPECT(hist.size() == 2);
60  BEAST_EXPECT(hist.numBins() == 2);
61  BEAST_EXPECT(hist.minValue() == 1);
62  BEAST_EXPECT(hist.maxValue() == 9);
63  BEAST_EXPECT(hist.avg() == 5);
64  BEAST_EXPECT(hist.percentile(0.0f) == hist.minValue());
65  BEAST_EXPECT(hist.percentile(0.5f) == 1);
66  BEAST_EXPECT(hist.percentile(0.9f) == 9);
67  BEAST_EXPECT(hist.percentile(1.0f) == hist.maxValue());
68 
69  hist.insert(1);
70 
71  BEAST_EXPECT(hist.size() == 3);
72  BEAST_EXPECT(hist.numBins() == 2);
73  BEAST_EXPECT(hist.minValue() == 1);
74  BEAST_EXPECT(hist.maxValue() == 9);
75  BEAST_EXPECT(hist.avg() == 11 / 3);
76  BEAST_EXPECT(hist.percentile(0.0f) == hist.minValue());
77  BEAST_EXPECT(hist.percentile(0.5f) == 1);
78  BEAST_EXPECT(hist.percentile(0.9f) == 9);
79  BEAST_EXPECT(hist.percentile(1.0f) == hist.maxValue());
80  }
81 };
82 
83 BEAST_DEFINE_TESTSUITE(Histogram, test, ripple);
84 
85 } // namespace test
86 } // namespace ripple
ripple::test::Histogram_test
Definition: Histogram_test.cpp:26
ripple::test::Histogram_test::run
void run() override
Definition: Histogram_test.cpp:30
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)