Update V8 to version 4.7.21.
[chromium-blink-merge.git] / base / metrics / sample_vector_unittest.cc
blobfd4237666997ea96150fe1c4c4ab18b76d3918f6
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_vector.h"
7 #include <vector>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/bucket_ranges.h"
11 #include "base/metrics/histogram.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace base {
15 namespace {
17 TEST(SampleVectorTest, AccumulateTest) {
18 // Custom buckets: [1, 5) [5, 10)
19 BucketRanges ranges(3);
20 ranges.set_range(0, 1);
21 ranges.set_range(1, 5);
22 ranges.set_range(2, 10);
23 SampleVector samples(&ranges);
25 samples.Accumulate(1, 200);
26 samples.Accumulate(2, -300);
27 EXPECT_EQ(-100, samples.GetCountAtIndex(0));
29 samples.Accumulate(5, 200);
30 EXPECT_EQ(200, samples.GetCountAtIndex(1));
32 EXPECT_EQ(600, samples.sum());
33 EXPECT_EQ(100, samples.redundant_count());
34 EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
36 samples.Accumulate(5, -100);
37 EXPECT_EQ(100, samples.GetCountAtIndex(1));
39 EXPECT_EQ(100, samples.sum());
40 EXPECT_EQ(0, samples.redundant_count());
41 EXPECT_EQ(samples.TotalCount(), samples.redundant_count());
44 TEST(SampleVectorTest, AddSubtractTest) {
45 // Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX)
46 BucketRanges ranges(5);
47 ranges.set_range(0, 0);
48 ranges.set_range(1, 1);
49 ranges.set_range(2, 2);
50 ranges.set_range(3, 3);
51 ranges.set_range(4, INT_MAX);
53 SampleVector samples1(&ranges);
54 samples1.Accumulate(0, 100);
55 samples1.Accumulate(2, 100);
56 samples1.Accumulate(4, 100);
57 EXPECT_EQ(600, samples1.sum());
58 EXPECT_EQ(300, samples1.TotalCount());
59 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
61 SampleVector samples2(&ranges);
62 samples2.Accumulate(1, 200);
63 samples2.Accumulate(2, 200);
64 samples2.Accumulate(4, 200);
65 EXPECT_EQ(1400, samples2.sum());
66 EXPECT_EQ(600, samples2.TotalCount());
67 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount());
69 samples1.Add(samples2);
70 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
71 EXPECT_EQ(200, samples1.GetCountAtIndex(1));
72 EXPECT_EQ(300, samples1.GetCountAtIndex(2));
73 EXPECT_EQ(300, samples1.GetCountAtIndex(3));
74 EXPECT_EQ(2000, samples1.sum());
75 EXPECT_EQ(900, samples1.TotalCount());
76 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
78 samples1.Subtract(samples2);
79 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
80 EXPECT_EQ(0, samples1.GetCountAtIndex(1));
81 EXPECT_EQ(100, samples1.GetCountAtIndex(2));
82 EXPECT_EQ(100, samples1.GetCountAtIndex(3));
83 EXPECT_EQ(600, samples1.sum());
84 EXPECT_EQ(300, samples1.TotalCount());
85 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount());
88 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
89 TEST(SampleVectorDeathTest, BucketIndexTest) {
90 // 8 buckets with exponential layout:
91 // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX)
92 BucketRanges ranges(9);
93 Histogram::InitializeBucketRanges(1, 64, &ranges);
94 SampleVector samples(&ranges);
96 // Normal case
97 samples.Accumulate(0, 1);
98 samples.Accumulate(3, 2);
99 samples.Accumulate(64, 3);
100 EXPECT_EQ(1, samples.GetCount(0));
101 EXPECT_EQ(2, samples.GetCount(2));
102 EXPECT_EQ(3, samples.GetCount(65));
104 // Extreme case.
105 EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), "");
106 EXPECT_DEATH(samples.Accumulate(-1, 100), "");
107 EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), "");
109 // Custom buckets: [1, 5) [5, 10)
110 // Note, this is not a valid BucketRanges for Histogram because it does not
111 // have overflow buckets.
112 BucketRanges ranges2(3);
113 ranges2.set_range(0, 1);
114 ranges2.set_range(1, 5);
115 ranges2.set_range(2, 10);
116 SampleVector samples2(&ranges2);
118 // Normal case.
119 samples2.Accumulate(1, 1);
120 samples2.Accumulate(4, 1);
121 samples2.Accumulate(5, 2);
122 samples2.Accumulate(9, 2);
123 EXPECT_EQ(2, samples2.GetCount(1));
124 EXPECT_EQ(4, samples2.GetCount(5));
126 // Extreme case.
127 EXPECT_DEATH(samples2.Accumulate(0, 100), "");
128 EXPECT_DEATH(samples2.Accumulate(10, 100), "");
131 TEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) {
132 // Custom buckets 1: [1, 3) [3, 5)
133 BucketRanges ranges1(3);
134 ranges1.set_range(0, 1);
135 ranges1.set_range(1, 3);
136 ranges1.set_range(2, 5);
137 SampleVector samples1(&ranges1);
139 // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7)
140 BucketRanges ranges2(5);
141 ranges2.set_range(0, 0);
142 ranges2.set_range(1, 1);
143 ranges2.set_range(2, 3);
144 ranges2.set_range(3, 6);
145 ranges2.set_range(4, 7);
146 SampleVector samples2(&ranges2);
148 samples2.Accumulate(1, 100);
149 samples1.Add(samples2);
150 EXPECT_EQ(100, samples1.GetCountAtIndex(0));
152 // Extra bucket in the beginning.
153 samples2.Accumulate(0, 100);
154 EXPECT_DEATH(samples1.Add(samples2), "");
155 EXPECT_DEATH(samples1.Subtract(samples2), "");
157 // Extra bucket in the end.
158 samples2.Accumulate(0, -100);
159 samples2.Accumulate(6, 100);
160 EXPECT_DEATH(samples1.Add(samples2), "");
161 EXPECT_DEATH(samples1.Subtract(samples2), "");
163 // Bucket not match: [3, 5) VS [3, 6)
164 samples2.Accumulate(6, -100);
165 samples2.Accumulate(3, 100);
166 EXPECT_DEATH(samples1.Add(samples2), "");
167 EXPECT_DEATH(samples1.Subtract(samples2), "");
170 #endif
171 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
173 TEST(SampleVectorIteratorTest, IterateTest) {
174 BucketRanges ranges(5);
175 ranges.set_range(0, 0);
176 ranges.set_range(1, 1);
177 ranges.set_range(2, 2);
178 ranges.set_range(3, 3);
179 ranges.set_range(4, 4);
181 std::vector<HistogramBase::Count> counts(3);
182 counts[0] = 1;
183 counts[1] = 0; // Iterator will bypass this empty bucket.
184 counts[2] = 2;
186 // BucketRanges can have larger size than counts.
187 SampleVectorIterator it(&counts, &ranges);
188 size_t index;
190 HistogramBase::Sample min;
191 HistogramBase::Sample max;
192 HistogramBase::Count count;
193 it.Get(&min, &max, &count);
194 EXPECT_EQ(0, min);
195 EXPECT_EQ(1, max);
196 EXPECT_EQ(1, count);
197 EXPECT_TRUE(it.GetBucketIndex(&index));
198 EXPECT_EQ(0u, index);
200 it.Next();
201 it.Get(&min, &max, &count);
202 EXPECT_EQ(2, min);
203 EXPECT_EQ(3, max);
204 EXPECT_EQ(2, count);
205 EXPECT_TRUE(it.GetBucketIndex(&index));
206 EXPECT_EQ(2u, index);
208 it.Next();
209 EXPECT_TRUE(it.Done());
211 // Create iterator from SampleVector.
212 SampleVector samples(&ranges);
213 samples.Accumulate(0, 0);
214 samples.Accumulate(1, 1);
215 samples.Accumulate(2, 2);
216 samples.Accumulate(3, 3);
217 scoped_ptr<SampleCountIterator> it2 = samples.Iterator();
219 int i;
220 for (i = 1; !it2->Done(); i++, it2->Next()) {
221 it2->Get(&min, &max, &count);
222 EXPECT_EQ(i, min);
223 EXPECT_EQ(i + 1, max);
224 EXPECT_EQ(i, count);
226 size_t index;
227 EXPECT_TRUE(it2->GetBucketIndex(&index));
228 EXPECT_EQ(static_cast<size_t>(i), index);
230 EXPECT_EQ(4, i);
233 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
235 TEST(SampleVectorIteratorDeathTest, IterateDoneTest) {
236 BucketRanges ranges(5);
237 ranges.set_range(0, 0);
238 ranges.set_range(1, 1);
239 ranges.set_range(2, 2);
240 ranges.set_range(3, 3);
241 ranges.set_range(4, INT_MAX);
242 SampleVector samples(&ranges);
244 scoped_ptr<SampleCountIterator> it = samples.Iterator();
246 EXPECT_TRUE(it->Done());
248 HistogramBase::Sample min;
249 HistogramBase::Sample max;
250 HistogramBase::Count count;
251 EXPECT_DEATH(it->Get(&min, &max, &count), "");
253 EXPECT_DEATH(it->Next(), "");
255 samples.Accumulate(2, 100);
256 it = samples.Iterator();
257 EXPECT_FALSE(it->Done());
260 #endif
261 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
263 } // namespace
264 } // namespace base