]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testStatHist.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / tests / testStatHist.cc
1 /*
2 * Copyright (C) 1996-2021 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 "StatHist.h"
11 #include "testStatHist.h"
12 #include "unitTestMain.h"
13
14 CPPUNIT_TEST_SUITE_REGISTRATION(testStatHist);
15
16 typedef enum {
17 ZERO, ONE, TWO, THREE, FOUR, FIVE
18 } number ;
19
20 class InspectingStatHist : public StatHist
21 {
22 public:
23 bool operator==(const InspectingStatHist &);
24 bins_type counter(double v) {
25 return bins[findBin(v)];
26 }
27 };
28
29 bool
30 InspectingStatHist::operator ==(const InspectingStatHist & src)
31 {
32 assert(bins != NULL && src.bins != NULL); // TODO: remove after initializing bins at construction time
33 if (capacity_ != src.capacity_ ||
34 min_!=src.min_ ||
35 max_!=src.max_ ||
36 scale_!=src.scale_ ||
37 val_in!=src.val_in ||
38 val_out!=src.val_out)
39 return false;
40 return (memcmp(bins,src.bins,capacity_*sizeof(*bins))==0);
41 }
42
43 void
44 testStatHist::testStatHistBaseEquality()
45 {
46 InspectingStatHist raw, test;
47 raw.enumInit(FIVE);
48 test.enumInit(FIVE);
49 CPPUNIT_ASSERT(raw==test);
50 test.count(ZERO);
51 CPPUNIT_ASSERT_ASSERTION_FAIL(CPPUNIT_ASSERT(raw==test));
52 }
53
54 void
55 testStatHist::testStatHistBaseAssignment()
56 {
57 InspectingStatHist raw, test;
58 raw.enumInit(FIVE);
59 test.enumInit(FIVE);
60 test.count(ZERO);
61 CPPUNIT_ASSERT_ASSERTION_FAIL(CPPUNIT_ASSERT(raw==test));
62 test=raw;
63 CPPUNIT_ASSERT(raw==test);
64 }
65
66 void
67 testStatHist::testStatHistLog()
68 {
69 const double min=0.0, max=10000.0;
70 const int capacity=10;
71 InspectingStatHist raw, test;
72 raw.logInit(capacity,min,max);
73 test=raw;
74 CPPUNIT_ASSERT(test.counter(min)==0);
75 test.count(min);
76 CPPUNIT_ASSERT(test.counter(min)==1);
77 CPPUNIT_ASSERT(test.counter(max)==0);
78 test.count(max);
79 CPPUNIT_ASSERT(test.counter(max)==1);
80 test=raw;
81 test.count(max);
82 //CPPUNIT_ASSERT(test.val(capacity-1)==1); // XXX: val() returns a density
83 }
84
85 void
86 testStatHist::testStatHistSum()
87 {
88 InspectingStatHist s1, s2;
89 s1.logInit(30,1.0,100.0);
90 s2.logInit(30,1.0,100.0);
91 s1.count(3);
92 s2.count(30);
93 InspectingStatHist ts1, ts2;
94 ts1=s1;
95 ts1+=s2;
96 ts2=s2;
97 ts2+=s1;
98 CPPUNIT_ASSERT(ts1 == ts2);
99 InspectingStatHist ts3;
100 ts3.logInit(30,1.0,100.0);
101 ts3.count(3);
102 ts3.count(30);
103 CPPUNIT_ASSERT(ts3 == ts1);
104
105 }
106