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