1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
base / metrics / sample_map_unittest.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/sample_map.h"
#include <memory>
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
TEST(SampleMapTest, AccumulateTest) {
SampleMap samples(1);
samples.Accumulate(1, 100);
samples.Accumulate(2, 200);
samples.Accumulate(1, -200);
EXPECT_EQ(-100, samples.GetCount(1));
EXPECT_EQ(200, samples.GetCount(2));
EXPECT_EQ(300, samples.sum());
EXPECT_EQ(100, samples.TotalCount());
EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
}
TEST(SampleMapTest, Accumulate_LargeValuesDontOverflow) {
SampleMap samples(1);
samples.Accumulate(250000000, 100);
samples.Accumulate(500000000, 200);
samples.Accumulate(250000000, -200);
EXPECT_EQ(-100, samples.GetCount(250000000));
EXPECT_EQ(200, samples.GetCount(500000000));
EXPECT_EQ(75000000000LL, samples.sum());
EXPECT_EQ(100, samples.TotalCount());
EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
}
TEST(SampleMapTest, AddSubtractTest) {
SampleMap samples1(1);
SampleMap samples2(2);
samples1.Accumulate(1, 100);
samples1.Accumulate(2, 100);
samples1.Accumulate(3, 100);
samples2.Accumulate(1, 200);
samples2.Accumulate(2, 200);
samples2.Accumulate(4, 200);
samples1.Add(samples2);
EXPECT_EQ(300, samples1.GetCount(1));
EXPECT_EQ(300, samples1.GetCount(2));
EXPECT_EQ(100, samples1.GetCount(3));
EXPECT_EQ(200, samples1.GetCount(4));
EXPECT_EQ(2000, samples1.sum());
EXPECT_EQ(900, samples1.TotalCount());
EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
samples1.Subtract(samples2);
EXPECT_EQ(100, samples1.GetCount(1));
EXPECT_EQ(100, samples1.GetCount(2));
EXPECT_EQ(100, samples1.GetCount(3));
EXPECT_EQ(0, samples1.GetCount(4));
EXPECT_EQ(600, samples1.sum());
EXPECT_EQ(300, samples1.TotalCount());
EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
}
TEST(SampleMapIteratorTest, IterateTest) {
SampleMap samples(1);
samples.Accumulate(1, 100);
samples.Accumulate(2, 200);
samples.Accumulate(4, -300);
samples.Accumulate(5, 0);
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
it->Get(&min, &max, &count);
EXPECT_EQ(1, min);
EXPECT_EQ(2, max);
EXPECT_EQ(100, count);
EXPECT_FALSE(it->GetBucketIndex(nullptr));
it->Next();
it->Get(&min, &max, &count);
EXPECT_EQ(2, min);
EXPECT_EQ(3, max);
EXPECT_EQ(200, count);
it->Next();
it->Get(&min, &max, &count);
EXPECT_EQ(4, min);
EXPECT_EQ(5, max);
EXPECT_EQ(-300, count);
it->Next();
EXPECT_TRUE(it->Done());
}
TEST(SampleMapIteratorTest, SkipEmptyRanges) {
SampleMap samples(1);
samples.Accumulate(5, 1);
samples.Accumulate(10, 2);
samples.Accumulate(15, 3);
samples.Accumulate(20, 4);
samples.Accumulate(25, 5);
SampleMap samples2(2);
samples2.Accumulate(5, 1);
samples2.Accumulate(20, 4);
samples2.Accumulate(25, 5);
samples.Subtract(samples2);
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
EXPECT_FALSE(it->Done());
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
it->Get(&min, &max, &count);
EXPECT_EQ(10, min);
EXPECT_EQ(11, max);
EXPECT_EQ(2, count);
it->Next();
EXPECT_FALSE(it->Done());
it->Get(&min, &max, &count);
EXPECT_EQ(15, min);
EXPECT_EQ(16, max);
EXPECT_EQ(3, count);
it->Next();
EXPECT_TRUE(it->Done());
}
TEST(SampleMapIteratorDeathTest, IterateDoneTest) {
SampleMap samples(1);
std::unique_ptr<SampleCountIterator> it = samples.Iterator();
EXPECT_TRUE(it->Done());
HistogramBase::Sample min;
int64_t max;
HistogramBase::Count count;
EXPECT_DCHECK_DEATH(it->Get(&min, &max, &count));
EXPECT_DCHECK_DEATH(it->Next());
samples.Accumulate(1, 100);
it = samples.Iterator();
EXPECT_FALSE(it->Done());
}
} // namespace
} // namespace base