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 
14 class TestStatHist : public CPPUNIT_NS::TestFixture
15 {
22 
23 public:
24 protected:
27  void testStatHistLog();
28  void testStatHistSum();
29 };
31 
32 typedef enum {
34 } number ;
35 
37 {
38 public:
39  bool operator==(const InspectingStatHist &);
40  bins_type counter(double v) {
41  return bins[findBin(v)];
42  }
43 };
44 
45 bool
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 
59 void
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 
70 void
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 
82 void
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 
101 void
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);
115  InspectingStatHist ts3;
116  ts3.logInit(30,1.0,100.0);
117  ts3.count(3);
118  ts3.count(30);
119  CPPUNIT_ASSERT(ts3 == ts1);
120 
121 }
122 
123 int
124 main(int argc, char *argv[])
125 {
126  return TestProgram().run(argc, argv);
127 }
128 
double scale_
scaling factor when looking for a bin
Definition: StatHist.h:111
@ s1
Definition: cf_gen.cc:55
double max_
value of the maximum counter in the histogram
Definition: StatHist.h:108
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
hbase_f * val_out
Definition: StatHist.h:113
@ ONE
Definition: testStatHist.cc:33
const A & max(A const &lhs, A const &rhs)
bins_type counter(double v)
Definition: testStatHist.cc:40
CPPUNIT_TEST_SUITE_END()
unsigned int findBin(double v)
Definition: StatHist.cc:64
number
Definition: testStatHist.cc:32
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
void testStatHistBaseEquality()
Definition: testStatHist.cc:60
double min_
minimum value to be stored, corresponding to the first bin
Definition: StatHist.h:105
void testStatHistLog()
Definition: testStatHist.cc:83
void enumInit(unsigned int last_enum)
Definition: StatHist.cc:235
@ THREE
Definition: testStatHist.cc:33
void count(double val)
Definition: StatHist.cc:55
#define assert(EX)
Definition: assert.h:17
void testStatHistBaseAssignment()
Definition: testStatHist.cc:71
@ TWO
Definition: testStatHist.cc:33
unsigned int capacity_
Definition: StatHist.h:102
@ FOUR
Definition: testStatHist.cc:33
int main(int argc, char *argv[])
bool operator==(const InspectingStatHist &)
Definition: testStatHist.cc:46
@ FIVE
Definition: testStatHist.cc:33
void logInit(unsigned int capacity, double min, double max)
Definition: StatHist.cc:221
CPPUNIT_TEST(testStatHistBaseEquality)
@ ZERO
Definition: testStatHist.cc:33
CPPUNIT_TEST_SUITE(TestStatHist)
hbase_f * val_in
Definition: StatHist.h:112
bins_type * bins
the histogram counters
Definition: StatHist.h:101
CPPUNIT_TEST_SUITE_REGISTRATION(TestStatHist)
const A & min(A const &lhs, A const &rhs)
uint64_t bins_type
Definition: StatHist.h:44
void testStatHistSum()

 

Introduction

Documentation

Support

Miscellaneous