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.
4
5#include "base/memory/scoped_ptr.h"
6#include "base/metrics/sample_map.h"
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace base {
10namespace {
11
12TEST(SampleMapTest, AccumulateTest) {
13  SampleMap samples;
14
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));
20
21  EXPECT_EQ(300, samples.sum());
22  EXPECT_EQ(100, samples.TotalCount());
23  EXPECT_EQ(samples.redundant_count(), samples.TotalCount());
24}
25
26TEST(SampleMapTest, AddSubtractTest) {
27  SampleMap samples1;
28  SampleMap samples2;
29
30  samples1.Accumulate(1, 100);
31  samples1.Accumulate(2, 100);
32  samples1.Accumulate(3, 100);
33
34  samples2.Accumulate(1, 200);
35  samples2.Accumulate(2, 200);
36  samples2.Accumulate(4, 200);
37
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());
46
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());
55}
56
57TEST(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);
63
64  scoped_ptr<SampleCountIterator> it = samples.Iterator();
65
66  HistogramBase::Sample min;
67  HistogramBase::Sample max;
68  HistogramBase::Count count;
69
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));
75
76  it->Next();
77  it->Get(&min, &max, &count);
78  EXPECT_EQ(2, min);
79  EXPECT_EQ(3, max);
80  EXPECT_EQ(200, count);
81
82  it->Next();
83  it->Get(&min, &max, &count);
84  EXPECT_EQ(4, min);
85  EXPECT_EQ(5, max);
86  EXPECT_EQ(-300, count);
87
88  it->Next();
89  it->Get(&min, &max, &count);
90  EXPECT_EQ(5, min);
91  EXPECT_EQ(6, max);
92  EXPECT_EQ(0, count);
93
94  it->Next();
95  EXPECT_TRUE(it->Done());
96}
97
98#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
99
100TEST(SampleMapIteratorDeathTest, IterateDoneTest) {
101  SampleMap samples;
102
103  scoped_ptr<SampleCountIterator> it = samples.Iterator();
104
105  EXPECT_TRUE(it->Done());
106
107  HistogramBase::Sample min;
108  HistogramBase::Sample max;
109  HistogramBase::Count count;
110  EXPECT_DEATH(it->Get(&min, &max, &count), "");
111
112  EXPECT_DEATH(it->Next(), "");
113
114  samples.Accumulate(1, 100);
115  it = samples.Iterator();
116  EXPECT_FALSE(it->Done());
117}
118
119#endif
120// (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST
121
122}  // namespace
123}  // namespace base
124