Re-land: content: Refactor GPU memory buffer framework.
[chromium-blink-merge.git] / base / metrics / sample_map_unittest.cc
blob1a53ee7f546b38525c8c4b2836fb3649ca74cc38
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"
9 namespace base {
10 namespace {
12 TEST(SampleMapTest, AccumulateTest) {
13 SampleMap samples;
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) {
27 SampleMap samples1;
28 SampleMap samples2;
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) {
58 SampleMap samples;
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);
71 EXPECT_EQ(1, min);
72 EXPECT_EQ(2, max);
73 EXPECT_EQ(100, count);
74 EXPECT_FALSE(it->GetBucketIndex(NULL));
76 it->Next();
77 it->Get(&min, &max, &count);
78 EXPECT_EQ(2, min);
79 EXPECT_EQ(3, max);
80 EXPECT_EQ(200, count);
82 it->Next();
83 it->Get(&min, &max, &count);
84 EXPECT_EQ(4, min);
85 EXPECT_EQ(5, max);
86 EXPECT_EQ(-300, count);
88 it->Next();
89 it->Get(&min, &max, &count);
90 EXPECT_EQ(5, min);
91 EXPECT_EQ(6, max);
92 EXPECT_EQ(0, count);
94 it->Next();
95 EXPECT_TRUE(it->Done());
98 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
100 TEST(SampleMapIteratorDeathTest, IterateDoneTest) {
101 SampleMap samples;
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());
119 #endif
120 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
122 } // namespace
123 } // namespace base