OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/sample_map.h" | 5 #include "base/metrics/sample_map.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace { | 11 namespace { |
12 | 12 |
13 TEST(SampleMapTest, AccumulateTest) { | 13 TEST(SampleMapTest, AccumulateTest) { |
14 SampleMap samples; | 14 SampleMap samples(1); |
15 | 15 |
16 samples.Accumulate(1, 100); | 16 samples.Accumulate(1, 100); |
17 samples.Accumulate(2, 200); | 17 samples.Accumulate(2, 200); |
18 samples.Accumulate(1, -200); | 18 samples.Accumulate(1, -200); |
19 EXPECT_EQ(-100, samples.GetCount(1)); | 19 EXPECT_EQ(-100, samples.GetCount(1)); |
20 EXPECT_EQ(200, samples.GetCount(2)); | 20 EXPECT_EQ(200, samples.GetCount(2)); |
21 | 21 |
22 EXPECT_EQ(300, samples.sum()); | 22 EXPECT_EQ(300, samples.sum()); |
23 EXPECT_EQ(100, samples.TotalCount()); | 23 EXPECT_EQ(100, samples.TotalCount()); |
24 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); | 24 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); |
25 } | 25 } |
26 | 26 |
27 TEST(SampleMapTest, AddSubtractTest) { | 27 TEST(SampleMapTest, AddSubtractTest) { |
28 SampleMap samples1; | 28 SampleMap samples1(1); |
29 SampleMap samples2; | 29 SampleMap samples2(2); |
30 | 30 |
31 samples1.Accumulate(1, 100); | 31 samples1.Accumulate(1, 100); |
32 samples1.Accumulate(2, 100); | 32 samples1.Accumulate(2, 100); |
33 samples1.Accumulate(3, 100); | 33 samples1.Accumulate(3, 100); |
34 | 34 |
35 samples2.Accumulate(1, 200); | 35 samples2.Accumulate(1, 200); |
36 samples2.Accumulate(2, 200); | 36 samples2.Accumulate(2, 200); |
37 samples2.Accumulate(4, 200); | 37 samples2.Accumulate(4, 200); |
38 | 38 |
39 samples1.Add(samples2); | 39 samples1.Add(samples2); |
40 EXPECT_EQ(300, samples1.GetCount(1)); | 40 EXPECT_EQ(300, samples1.GetCount(1)); |
41 EXPECT_EQ(300, samples1.GetCount(2)); | 41 EXPECT_EQ(300, samples1.GetCount(2)); |
42 EXPECT_EQ(100, samples1.GetCount(3)); | 42 EXPECT_EQ(100, samples1.GetCount(3)); |
43 EXPECT_EQ(200, samples1.GetCount(4)); | 43 EXPECT_EQ(200, samples1.GetCount(4)); |
44 EXPECT_EQ(2000, samples1.sum()); | 44 EXPECT_EQ(2000, samples1.sum()); |
45 EXPECT_EQ(900, samples1.TotalCount()); | 45 EXPECT_EQ(900, samples1.TotalCount()); |
46 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); | 46 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
47 | 47 |
48 samples1.Subtract(samples2); | 48 samples1.Subtract(samples2); |
49 EXPECT_EQ(100, samples1.GetCount(1)); | 49 EXPECT_EQ(100, samples1.GetCount(1)); |
50 EXPECT_EQ(100, samples1.GetCount(2)); | 50 EXPECT_EQ(100, samples1.GetCount(2)); |
51 EXPECT_EQ(100, samples1.GetCount(3)); | 51 EXPECT_EQ(100, samples1.GetCount(3)); |
52 EXPECT_EQ(0, samples1.GetCount(4)); | 52 EXPECT_EQ(0, samples1.GetCount(4)); |
53 EXPECT_EQ(600, samples1.sum()); | 53 EXPECT_EQ(600, samples1.sum()); |
54 EXPECT_EQ(300, samples1.TotalCount()); | 54 EXPECT_EQ(300, samples1.TotalCount()); |
55 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); | 55 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
56 } | 56 } |
57 | 57 |
58 TEST(SampleMapIteratorTest, IterateTest) { | 58 TEST(SampleMapIteratorTest, IterateTest) { |
59 SampleMap samples; | 59 SampleMap samples(1); |
60 samples.Accumulate(1, 100); | 60 samples.Accumulate(1, 100); |
61 samples.Accumulate(2, 200); | 61 samples.Accumulate(2, 200); |
62 samples.Accumulate(4, -300); | 62 samples.Accumulate(4, -300); |
63 samples.Accumulate(5, 0); | 63 samples.Accumulate(5, 0); |
64 | 64 |
65 scoped_ptr<SampleCountIterator> it = samples.Iterator(); | 65 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
66 | 66 |
67 HistogramBase::Sample min; | 67 HistogramBase::Sample min; |
68 HistogramBase::Sample max; | 68 HistogramBase::Sample max; |
69 HistogramBase::Count count; | 69 HistogramBase::Count count; |
(...skipping 14 matching lines...) Expand all Loading... |
84 it->Get(&min, &max, &count); | 84 it->Get(&min, &max, &count); |
85 EXPECT_EQ(4, min); | 85 EXPECT_EQ(4, min); |
86 EXPECT_EQ(5, max); | 86 EXPECT_EQ(5, max); |
87 EXPECT_EQ(-300, count); | 87 EXPECT_EQ(-300, count); |
88 | 88 |
89 it->Next(); | 89 it->Next(); |
90 EXPECT_TRUE(it->Done()); | 90 EXPECT_TRUE(it->Done()); |
91 } | 91 } |
92 | 92 |
93 TEST(SampleMapIteratorTest, SkipEmptyRanges) { | 93 TEST(SampleMapIteratorTest, SkipEmptyRanges) { |
94 SampleMap samples; | 94 SampleMap samples(1); |
95 samples.Accumulate(5, 1); | 95 samples.Accumulate(5, 1); |
96 samples.Accumulate(10, 2); | 96 samples.Accumulate(10, 2); |
97 samples.Accumulate(15, 3); | 97 samples.Accumulate(15, 3); |
98 samples.Accumulate(20, 4); | 98 samples.Accumulate(20, 4); |
99 samples.Accumulate(25, 5); | 99 samples.Accumulate(25, 5); |
100 | 100 |
101 SampleMap samples2; | 101 SampleMap samples2(2); |
102 samples2.Accumulate(5, 1); | 102 samples2.Accumulate(5, 1); |
103 samples2.Accumulate(20, 4); | 103 samples2.Accumulate(20, 4); |
104 samples2.Accumulate(25, 5); | 104 samples2.Accumulate(25, 5); |
105 | 105 |
106 samples.Subtract(samples2); | 106 samples.Subtract(samples2); |
107 | 107 |
108 scoped_ptr<SampleCountIterator> it = samples.Iterator(); | 108 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
109 EXPECT_FALSE(it->Done()); | 109 EXPECT_FALSE(it->Done()); |
110 | 110 |
111 HistogramBase::Sample min; | 111 HistogramBase::Sample min; |
(...skipping 13 matching lines...) Expand all Loading... |
125 EXPECT_EQ(16, max); | 125 EXPECT_EQ(16, max); |
126 EXPECT_EQ(3, count); | 126 EXPECT_EQ(3, count); |
127 | 127 |
128 it->Next(); | 128 it->Next(); |
129 EXPECT_TRUE(it->Done()); | 129 EXPECT_TRUE(it->Done()); |
130 } | 130 } |
131 | 131 |
132 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 132 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
133 | 133 |
134 TEST(SampleMapIteratorDeathTest, IterateDoneTest) { | 134 TEST(SampleMapIteratorDeathTest, IterateDoneTest) { |
135 SampleMap samples; | 135 SampleMap samples(1); |
136 | 136 |
137 scoped_ptr<SampleCountIterator> it = samples.Iterator(); | 137 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
138 | 138 |
139 EXPECT_TRUE(it->Done()); | 139 EXPECT_TRUE(it->Done()); |
140 | 140 |
141 HistogramBase::Sample min; | 141 HistogramBase::Sample min; |
142 HistogramBase::Sample max; | 142 HistogramBase::Sample max; |
143 HistogramBase::Count count; | 143 HistogramBase::Count count; |
144 EXPECT_DEATH(it->Get(&min, &max, &count), ""); | 144 EXPECT_DEATH(it->Get(&min, &max, &count), ""); |
145 | 145 |
146 EXPECT_DEATH(it->Next(), ""); | 146 EXPECT_DEATH(it->Next(), ""); |
147 | 147 |
148 samples.Accumulate(1, 100); | 148 samples.Accumulate(1, 100); |
149 it = samples.Iterator(); | 149 it = samples.Iterator(); |
150 EXPECT_FALSE(it->Done()); | 150 EXPECT_FALSE(it->Done()); |
151 } | 151 } |
152 | 152 |
153 #endif | 153 #endif |
154 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 154 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
155 | 155 |
156 } // namespace | 156 } // namespace |
157 } // namespace base | 157 } // namespace base |
OLD | NEW |