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/memory/scoped_ptr.h"
6 #include "base/metrics/sample_map.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 TEST(SampleMapTest
, AccumulateTest
) {
15 samples
.Accumulate(1, 100);
16 samples
.Accumulate(2, 200);
17 samples
.Accumulate(1, -200);
18 EXPECT_EQ(-100, samples
.GetCount(1));
19 EXPECT_EQ(200, samples
.GetCount(2));
21 EXPECT_EQ(300, samples
.sum());
22 EXPECT_EQ(100, samples
.TotalCount());
23 EXPECT_EQ(samples
.redundant_count(), samples
.TotalCount());
26 TEST(SampleMapTest
, AddSubtractTest
) {
30 samples1
.Accumulate(1, 100);
31 samples1
.Accumulate(2, 100);
32 samples1
.Accumulate(3, 100);
34 samples2
.Accumulate(1, 200);
35 samples2
.Accumulate(2, 200);
36 samples2
.Accumulate(4, 200);
38 samples1
.Add(samples2
);
39 EXPECT_EQ(300, samples1
.GetCount(1));
40 EXPECT_EQ(300, samples1
.GetCount(2));
41 EXPECT_EQ(100, samples1
.GetCount(3));
42 EXPECT_EQ(200, samples1
.GetCount(4));
43 EXPECT_EQ(2000, samples1
.sum());
44 EXPECT_EQ(900, samples1
.TotalCount());
45 EXPECT_EQ(samples1
.redundant_count(), samples1
.TotalCount());
47 samples1
.Subtract(samples2
);
48 EXPECT_EQ(100, samples1
.GetCount(1));
49 EXPECT_EQ(100, samples1
.GetCount(2));
50 EXPECT_EQ(100, samples1
.GetCount(3));
51 EXPECT_EQ(0, samples1
.GetCount(4));
52 EXPECT_EQ(600, samples1
.sum());
53 EXPECT_EQ(300, samples1
.TotalCount());
54 EXPECT_EQ(samples1
.redundant_count(), samples1
.TotalCount());
57 TEST(SampleMapIteratorTest
, IterateTest
) {
59 samples
.Accumulate(1, 100);
60 samples
.Accumulate(2, 200);
61 samples
.Accumulate(4, -300);
62 samples
.Accumulate(5, 0);
64 scoped_ptr
<SampleCountIterator
> it
= samples
.Iterator();
66 HistogramBase::Sample min
;
67 HistogramBase::Sample max
;
68 HistogramBase::Count count
;
70 it
->Get(&min
, &max
, &count
);
73 EXPECT_EQ(100, count
);
74 EXPECT_FALSE(it
->GetBucketIndex(NULL
));
77 it
->Get(&min
, &max
, &count
);
80 EXPECT_EQ(200, count
);
83 it
->Get(&min
, &max
, &count
);
86 EXPECT_EQ(-300, count
);
89 it
->Get(&min
, &max
, &count
);
95 EXPECT_TRUE(it
->Done());
98 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
100 TEST(SampleMapIteratorDeathTest
, IterateDoneTest
) {
103 scoped_ptr
<SampleCountIterator
> it
= samples
.Iterator();
105 EXPECT_TRUE(it
->Done());
107 HistogramBase::Sample min
;
108 HistogramBase::Sample max
;
109 HistogramBase::Count count
;
110 EXPECT_DEATH(it
->Get(&min
, &max
, &count
), "");
112 EXPECT_DEATH(it
->Next(), "");
114 samples
.Accumulate(1, 100);
115 it
= samples
.Iterator();
116 EXPECT_FALSE(it
->Done());
120 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST