Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / base / metrics / sample_map_unittest.cc
blob22ce8e1c8e3ab3a347af2e008ce622bb662cd67d
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.
5 #include "base/metrics/sample_map.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace base {
11 namespace {
13 TEST(SampleMapTest, AccumulateTest) {
14 SampleMap samples;
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));
22 EXPECT_EQ(300, samples.sum());
23 EXPECT_EQ(100, samples.TotalCount());
24 EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
27 TEST(SampleMapTest, AddSubtractTest) {
28 SampleMap samples1;
29 SampleMap samples2;
31 samples1.Accumulate(1, 100);
32 samples1.Accumulate(2, 100);
33 samples1.Accumulate(3, 100);
35 samples2.Accumulate(1, 200);
36 samples2.Accumulate(2, 200);
37 samples2.Accumulate(4, 200);
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());
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());
58 TEST(SampleMapIteratorTest, IterateTest) {
59 SampleMap samples;
60 samples.Accumulate(1, 100);
61 samples.Accumulate(2, 200);
62 samples.Accumulate(4, -300);
63 samples.Accumulate(5, 0);
65 scoped_ptr<SampleCountIterator> it = samples.Iterator();
67 HistogramBase::Sample min;
68 HistogramBase::Sample max;
69 HistogramBase::Count count;
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));
77 it->Next();
78 it->Get(&min, &max, &count);
79 EXPECT_EQ(2, min);
80 EXPECT_EQ(3, max);
81 EXPECT_EQ(200, count);
83 it->Next();
84 it->Get(&min, &max, &count);
85 EXPECT_EQ(4, min);
86 EXPECT_EQ(5, max);
87 EXPECT_EQ(-300, count);
89 it->Next();
90 EXPECT_TRUE(it->Done());
93 TEST(SampleMapIteratorTest, SkipEmptyRanges) {
94 SampleMap samples;
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);
101 SampleMap samples2;
102 samples2.Accumulate(5, 1);
103 samples2.Accumulate(20, 4);
104 samples2.Accumulate(25, 5);
106 samples.Subtract(samples2);
108 scoped_ptr<SampleCountIterator> it = samples.Iterator();
109 EXPECT_FALSE(it->Done());
111 HistogramBase::Sample min;
112 HistogramBase::Sample max;
113 HistogramBase::Count count;
115 it->Get(&min, &max, &count);
116 EXPECT_EQ(10, min);
117 EXPECT_EQ(11, max);
118 EXPECT_EQ(2, count);
120 it->Next();
121 EXPECT_FALSE(it->Done());
123 it->Get(&min, &max, &count);
124 EXPECT_EQ(15, min);
125 EXPECT_EQ(16, max);
126 EXPECT_EQ(3, count);
128 it->Next();
129 EXPECT_TRUE(it->Done());
132 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
134 TEST(SampleMapIteratorDeathTest, IterateDoneTest) {
135 SampleMap samples;
137 scoped_ptr<SampleCountIterator> it = samples.Iterator();
139 EXPECT_TRUE(it->Done());
141 HistogramBase::Sample min;
142 HistogramBase::Sample max;
143 HistogramBase::Count count;
144 EXPECT_DEATH(it->Get(&min, &max, &count), "");
146 EXPECT_DEATH(it->Next(), "");
148 samples.Accumulate(1, 100);
149 it = samples.Iterator();
150 EXPECT_FALSE(it->Done());
153 #endif
154 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
156 } // namespace
157 } // namespace base