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"
13 TEST(SampleMapTest
, AccumulateTest
) {
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
) {
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
) {
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
);
74 EXPECT_EQ(100, count
);
75 EXPECT_FALSE(it
->GetBucketIndex(NULL
));
78 it
->Get(&min
, &max
, &count
);
81 EXPECT_EQ(200, count
);
84 it
->Get(&min
, &max
, &count
);
87 EXPECT_EQ(-300, count
);
90 EXPECT_TRUE(it
->Done());
93 TEST(SampleMapIteratorTest
, SkipEmptyRanges
) {
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);
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
);
121 EXPECT_FALSE(it
->Done());
123 it
->Get(&min
, &max
, &count
);
129 EXPECT_TRUE(it
->Done());
132 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
134 TEST(SampleMapIteratorDeathTest
, IterateDoneTest
) {
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());
154 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST