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 "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace base {
11 namespace {
12
TEST(SampleMapTest,AccumulateTest)13 TEST(SampleMapTest, AccumulateTest) {
14 SampleMap samples(1);
15
16 samples.Accumulate(1, 100);
17 samples.Accumulate(2, 200);
18 samples.Accumulate(1, -200);
19 EXPECT_EQ(-100, samples.GetCount(1));
20 EXPECT_EQ(200, samples.GetCount(2));
21
22 EXPECT_EQ(300, samples.sum());
23 EXPECT_EQ(100, samples.TotalCount());
24 EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
25 }
26
TEST(SampleMapTest,AddSubtractTest)27 TEST(SampleMapTest, AddSubtractTest) {
28 SampleMap samples1(1);
29 SampleMap samples2(2);
30
31 samples1.Accumulate(1, 100);
32 samples1.Accumulate(2, 100);
33 samples1.Accumulate(3, 100);
34
35 samples2.Accumulate(1, 200);
36 samples2.Accumulate(2, 200);
37 samples2.Accumulate(4, 200);
38
39 samples1.Add(samples2);
40 EXPECT_EQ(300, samples1.GetCount(1));
41 EXPECT_EQ(300, samples1.GetCount(2));
42 EXPECT_EQ(100, samples1.GetCount(3));
43 EXPECT_EQ(200, samples1.GetCount(4));
44 EXPECT_EQ(2000, samples1.sum());
45 EXPECT_EQ(900, samples1.TotalCount());
46 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
47
48 samples1.Subtract(samples2);
49 EXPECT_EQ(100, samples1.GetCount(1));
50 EXPECT_EQ(100, samples1.GetCount(2));
51 EXPECT_EQ(100, samples1.GetCount(3));
52 EXPECT_EQ(0, samples1.GetCount(4));
53 EXPECT_EQ(600, samples1.sum());
54 EXPECT_EQ(300, samples1.TotalCount());
55 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
56 }
57
TEST(SampleMapIteratorTest,IterateTest)58 TEST(SampleMapIteratorTest, IterateTest) {
59 SampleMap samples(1);
60 samples.Accumulate(1, 100);
61 samples.Accumulate(2, 200);
62 samples.Accumulate(4, -300);
63 samples.Accumulate(5, 0);
64
65 scoped_ptr<SampleCountIterator> it = samples.Iterator();
66
67 HistogramBase::Sample min;
68 HistogramBase::Sample max;
69 HistogramBase::Count count;
70
71 it->Get(&min, &max, &count);
72 EXPECT_EQ(1, min);
73 EXPECT_EQ(2, max);
74 EXPECT_EQ(100, count);
75 EXPECT_FALSE(it->GetBucketIndex(NULL));
76
77 it->Next();
78 it->Get(&min, &max, &count);
79 EXPECT_EQ(2, min);
80 EXPECT_EQ(3, max);
81 EXPECT_EQ(200, count);
82
83 it->Next();
84 it->Get(&min, &max, &count);
85 EXPECT_EQ(4, min);
86 EXPECT_EQ(5, max);
87 EXPECT_EQ(-300, count);
88
89 it->Next();
90 EXPECT_TRUE(it->Done());
91 }
92
TEST(SampleMapIteratorTest,SkipEmptyRanges)93 TEST(SampleMapIteratorTest, SkipEmptyRanges) {
94 SampleMap samples(1);
95 samples.Accumulate(5, 1);
96 samples.Accumulate(10, 2);
97 samples.Accumulate(15, 3);
98 samples.Accumulate(20, 4);
99 samples.Accumulate(25, 5);
100
101 SampleMap samples2(2);
102 samples2.Accumulate(5, 1);
103 samples2.Accumulate(20, 4);
104 samples2.Accumulate(25, 5);
105
106 samples.Subtract(samples2);
107
108 scoped_ptr<SampleCountIterator> it = samples.Iterator();
109 EXPECT_FALSE(it->Done());
110
111 HistogramBase::Sample min;
112 HistogramBase::Sample max;
113 HistogramBase::Count count;
114
115 it->Get(&min, &max, &count);
116 EXPECT_EQ(10, min);
117 EXPECT_EQ(11, max);
118 EXPECT_EQ(2, count);
119
120 it->Next();
121 EXPECT_FALSE(it->Done());
122
123 it->Get(&min, &max, &count);
124 EXPECT_EQ(15, min);
125 EXPECT_EQ(16, max);
126 EXPECT_EQ(3, count);
127
128 it->Next();
129 EXPECT_TRUE(it->Done());
130 }
131
132 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
133
TEST(SampleMapIteratorDeathTest,IterateDoneTest)134 TEST(SampleMapIteratorDeathTest, IterateDoneTest) {
135 SampleMap samples(1);
136
137 scoped_ptr<SampleCountIterator> it = samples.Iterator();
138
139 EXPECT_TRUE(it->Done());
140
141 HistogramBase::Sample min;
142 HistogramBase::Sample max;
143 HistogramBase::Count count;
144 EXPECT_DEATH(it->Get(&min, &max, &count), "");
145
146 EXPECT_DEATH(it->Next(), "");
147
148 samples.Accumulate(1, 100);
149 it = samples.Iterator();
150 EXPECT_FALSE(it->Done());
151 }
152
153 #endif
154 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
155
156 } // namespace
157 } // namespace base
158