testStatHist.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "compat/cppunit.h"
11#include "StatHist.h"
12#include "unitTestMain.h"
13
14class TestStatHist : public CPPUNIT_NS::TestFixture
15{
22
23public:
24protected:
27 void testStatHistLog();
28 void testStatHistSum();
29};
31
32typedef enum {
35
37{
38public:
39 bool operator==(const InspectingStatHist &);
40 bins_type counter(double v) {
41 return bins[findBin(v)];
42 }
43};
44
45bool
47{
48 assert(bins != nullptr && src.bins != nullptr); // TODO: remove after initializing bins at construction time
49 if (capacity_ != src.capacity_ ||
50 min_!=src.min_ ||
51 max_!=src.max_ ||
52 scale_!=src.scale_ ||
53 val_in!=src.val_in ||
54 val_out!=src.val_out)
55 return false;
56 return (memcmp(bins,src.bins,capacity_*sizeof(*bins))==0);
57}
58
59void
61{
62 InspectingStatHist raw, test;
63 raw.enumInit(FIVE);
64 test.enumInit(FIVE);
65 CPPUNIT_ASSERT(raw==test);
66 test.count(ZERO);
67 CPPUNIT_ASSERT_ASSERTION_FAIL(CPPUNIT_ASSERT(raw==test));
68}
69
70void
72{
73 InspectingStatHist raw, test;
74 raw.enumInit(FIVE);
75 test.enumInit(FIVE);
76 test.count(ZERO);
77 CPPUNIT_ASSERT_ASSERTION_FAIL(CPPUNIT_ASSERT(raw==test));
78 test=raw;
79 CPPUNIT_ASSERT(raw==test);
80}
81
82void
84{
85 const double min=0.0, max=10000.0;
86 const int capacity=10;
87 InspectingStatHist raw, test;
88 raw.logInit(capacity,min,max);
89 test=raw;
90 CPPUNIT_ASSERT(test.counter(min)==0);
91 test.count(min);
92 CPPUNIT_ASSERT(test.counter(min)==1);
93 CPPUNIT_ASSERT(test.counter(max)==0);
94 test.count(max);
95 CPPUNIT_ASSERT(test.counter(max)==1);
96 test=raw;
97 test.count(max);
98 //CPPUNIT_ASSERT(test.val(capacity-1)==1); // XXX: val() returns a density
99}
100
101void
103{
105 s1.logInit(30,1.0,100.0);
106 s2.logInit(30,1.0,100.0);
107 s1.count(3);
108 s2.count(30);
109 InspectingStatHist ts1, ts2;
110 ts1=s1;
111 ts1+=s2;
112 ts2=s2;
113 ts2+=s1;
114 CPPUNIT_ASSERT(ts1 == ts2);
116 ts3.logInit(30,1.0,100.0);
117 ts3.count(3);
118 ts3.count(30);
119 CPPUNIT_ASSERT(ts3 == ts1);
120
121}
122
123int
124main(int argc, char *argv[])
125{
126 return TestProgram().run(argc, argv);
127}
128
#define assert(EX)
Definition: assert.h:17
@ s1
Definition: cf_gen.cc:55
bool operator==(const InspectingStatHist &)
Definition: testStatHist.cc:46
bins_type counter(double v)
Definition: testStatHist.cc:40
uint64_t bins_type
Definition: StatHist.h:44
hbase_f * val_in
Definition: StatHist.h:112
double max_
value of the maximum counter in the histogram
Definition: StatHist.h:108
double scale_
scaling factor when looking for a bin
Definition: StatHist.h:111
void logInit(unsigned int capacity, double min, double max)
Definition: StatHist.cc:221
unsigned int capacity_
Definition: StatHist.h:102
unsigned int findBin(double v)
Definition: StatHist.cc:64
bins_type * bins
the histogram counters
Definition: StatHist.h:101
void enumInit(unsigned int last_enum)
Definition: StatHist.cc:235
void count(double val)
Definition: StatHist.cc:55
double min_
minimum value to be stored, corresponding to the first bin
Definition: StatHist.h:105
hbase_f * val_out
Definition: StatHist.h:113
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testStatHistBaseEquality)
CPPUNIT_TEST(testStatHistLog)
void testStatHistLog()
Definition: testStatHist.cc:83
void testStatHistBaseAssignment()
Definition: testStatHist.cc:71
CPPUNIT_TEST(testStatHistBaseAssignment)
void testStatHistBaseEquality()
Definition: testStatHist.cc:60
CPPUNIT_TEST(testStatHistSum)
void testStatHistSum()
CPPUNIT_TEST_SUITE(TestStatHist)
A const & max(A const &lhs, A const &rhs)
A const & min(A const &lhs, A const &rhs)
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestStatHist)
number
Definition: testStatHist.cc:32
@ TWO
Definition: testStatHist.cc:33
@ THREE
Definition: testStatHist.cc:33
@ FOUR
Definition: testStatHist.cc:33
@ FIVE
Definition: testStatHist.cc:33
@ ONE
Definition: testStatHist.cc:33
@ ZERO
Definition: testStatHist.cc:33

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors