1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be 3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file. 4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/metrics/sample_vector.h" 6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 7cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include <limits.h> 8cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko#include <stddef.h> 9cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko 10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <vector> 11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 12b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/memory/scoped_ptr.h" 13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/metrics/bucket_ranges.h" 14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/metrics/histogram.h" 15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "testing/gtest/include/gtest/gtest.h" 16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base { 18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace { 19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorTest, AccumulateTest) { 21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Custom buckets: [1, 5) [5, 10) 22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges(3); 23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(0, 1); 24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(1, 5); 25b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(2, 10); 26cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples(1, &ranges); 27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(1, 200); 29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(2, -300); 30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(-100, samples.GetCountAtIndex(0)); 31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 32b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(5, 200); 33b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(200, samples.GetCountAtIndex(1)); 34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(600, samples.sum()); 36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples.redundant_count()); 37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); 38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(5, -100); 40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples.GetCountAtIndex(1)); 41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples.sum()); 43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(0, samples.redundant_count()); 44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples.TotalCount(), samples.redundant_count()); 45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorTest, AddSubtractTest) { 48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Custom buckets: [0, 1) [1, 2) [2, 3) [3, INT_MAX) 49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges(5); 50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(0, 0); 51b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(1, 1); 52b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(2, 2); 53b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(3, 3); 54b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(4, INT_MAX); 55b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 56cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples1(1, &ranges); 57b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Accumulate(0, 100); 58b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Accumulate(2, 100); 59b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Accumulate(4, 100); 60b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(600, samples1.sum()); 61b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(300, samples1.TotalCount()); 62b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 64cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples2(2, &ranges); 65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(1, 200); 66b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(2, 200); 67b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(4, 200); 68b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(1400, samples2.sum()); 69b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(600, samples2.TotalCount()); 70b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); 71b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 72b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Add(samples2); 73b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 74b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(200, samples1.GetCountAtIndex(1)); 75b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(300, samples1.GetCountAtIndex(2)); 76b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(300, samples1.GetCountAtIndex(3)); 77b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2000, samples1.sum()); 78b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(900, samples1.TotalCount()); 79b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 80b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 81b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Subtract(samples2); 82b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 83b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(0, samples1.GetCountAtIndex(1)); 84b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples1.GetCountAtIndex(2)); 85b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples1.GetCountAtIndex(3)); 86b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(600, samples1.sum()); 87b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(300, samples1.TotalCount()); 88b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); 89b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 90b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 91b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 92b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorDeathTest, BucketIndexTest) { 93b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // 8 buckets with exponential layout: 94b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // [0, 1) [1, 2) [2, 4) [4, 8) [8, 16) [16, 32) [32, 64) [64, INT_MAX) 95b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges(9); 96b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat Histogram::InitializeBucketRanges(1, 64, &ranges); 97cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples(1, &ranges); 98b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 99b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Normal case 100b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(0, 1); 101b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(3, 2); 102b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(64, 3); 103b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(1, samples.GetCount(0)); 104b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2, samples.GetCount(2)); 105b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(3, samples.GetCount(65)); 106b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 107b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Extreme case. 108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples.Accumulate(INT_MIN, 100), ""); 109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples.Accumulate(-1, 100), ""); 110b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples.Accumulate(INT_MAX, 100), ""); 111b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 112b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Custom buckets: [1, 5) [5, 10) 113b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Note, this is not a valid BucketRanges for Histogram because it does not 114b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // have overflow buckets. 115b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges2(3); 116b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(0, 1); 117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(1, 5); 118b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(2, 10); 119cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples2(2, &ranges2); 120b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 121b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Normal case. 122b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(1, 1); 123b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(4, 1); 124b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(5, 2); 125b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(9, 2); 126b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2, samples2.GetCount(1)); 127b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(4, samples2.GetCount(5)); 128b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 129b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Extreme case. 130b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples2.Accumulate(0, 100), ""); 131b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples2.Accumulate(10, 100), ""); 132b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 133b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 134b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorDeathTest, AddSubtractBucketNotMatchTest) { 135b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Custom buckets 1: [1, 3) [3, 5) 136b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges1(3); 137b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges1.set_range(0, 1); 138b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges1.set_range(1, 3); 139b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges1.set_range(2, 5); 140cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples1(1, &ranges1); 141b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 142b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Custom buckets 2: [0, 1) [1, 3) [3, 6) [6, 7) 143b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges2(5); 144b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(0, 0); 145b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(1, 1); 146b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(2, 3); 147b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(3, 6); 148b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges2.set_range(4, 7); 149cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples2(2, &ranges2); 150b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 151b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(1, 100); 152b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples1.Add(samples2); 153b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(100, samples1.GetCountAtIndex(0)); 154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Extra bucket in the beginning. 156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(0, 100); 157b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Add(samples2), ""); 158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Subtract(samples2), ""); 159b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 160b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Extra bucket in the end. 161b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(0, -100); 162b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(6, 100); 163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Add(samples2), ""); 164b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Subtract(samples2), ""); 165b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 166b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Bucket not match: [3, 5) VS [3, 6) 167b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(6, -100); 168b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples2.Accumulate(3, 100); 169b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Add(samples2), ""); 170b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(samples1.Subtract(samples2), ""); 171b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 172b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 173b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif 174b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 176b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorIteratorTest, IterateTest) { 177b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges(5); 178b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(0, 0); 179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(1, 1); 180b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(2, 2); 181b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(3, 3); 182b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(4, 4); 183b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 184b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat std::vector<HistogramBase::Count> counts(3); 185b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat counts[0] = 1; 186b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat counts[1] = 0; // Iterator will bypass this empty bucket. 187b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat counts[2] = 2; 188b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // BucketRanges can have larger size than counts. 190b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat SampleVectorIterator it(&counts, &ranges); 191b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat size_t index; 192b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 193b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Sample min; 194b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Sample max; 195b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Count count; 196b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it.Get(&min, &max, &count); 197b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(0, min); 198b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(1, max); 199b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(1, count); 200b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_TRUE(it.GetBucketIndex(&index)); 201b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(0u, index); 202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 203b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it.Next(); 204b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it.Get(&min, &max, &count); 205b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2, min); 206b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(3, max); 207b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2, count); 208b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_TRUE(it.GetBucketIndex(&index)); 209b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(2u, index); 210b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 211b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it.Next(); 212b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_TRUE(it.Done()); 213b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 214b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat // Create iterator from SampleVector. 215cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples(1, &ranges); 216b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(0, 0); 217b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(1, 1); 218b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(2, 2); 219b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(3, 3); 220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat scoped_ptr<SampleCountIterator> it2 = samples.Iterator(); 221b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 222b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat int i; 223b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat for (i = 1; !it2->Done(); i++, it2->Next()) { 224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it2->Get(&min, &max, &count); 225b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(i, min); 226b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(i + 1, max); 227b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(i, count); 228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 229b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat size_t index; 230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_TRUE(it2->GetBucketIndex(&index)); 231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(static_cast<size_t>(i), index); 232b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat } 233b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_EQ(4, i); 234b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 235b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 236b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 237b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 238b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTEST(SampleVectorIteratorDeathTest, IterateDoneTest) { 239b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat BucketRanges ranges(5); 240b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(0, 0); 241b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(1, 1); 242b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(2, 2); 243b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(3, 3); 244b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat ranges.set_range(4, INT_MAX); 245cce46a0c214b37e8da48c522c83037e8ffa4f9fdAlex Vakulenko SampleVector samples(1, &ranges); 246b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 247b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat scoped_ptr<SampleCountIterator> it = samples.Iterator(); 248b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 249b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_TRUE(it->Done()); 250b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 251b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Sample min; 252b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Sample max; 253b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat HistogramBase::Count count; 254b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(it->Get(&min, &max, &count), ""); 255b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 256b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_DEATH(it->Next(), ""); 257b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 258b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat samples.Accumulate(2, 100); 259b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat it = samples.Iterator(); 260b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat EXPECT_FALSE(it->Done()); 261b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} 262b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 263b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif 264b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST 265b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat 266b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} // namespace 267b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat} // namespace base 268