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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/metrics/histogram_base.h" | |
8 #include "base/metrics/sample_map.h" | 9 #include "base/metrics/sample_map.h" |
9 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
11 #include "base/pickle.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace base { | 14 namespace base { |
13 | 15 |
14 class SparseHistogramTest : public testing::Test { | 16 class SparseHistogramTest : public testing::Test { |
15 protected: | 17 protected: |
16 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) { | 18 scoped_ptr<SparseHistogram> NewSparseHistogram(const std::string& name) { |
17 return scoped_ptr<SparseHistogram>(new SparseHistogram(name)); | 19 return scoped_ptr<SparseHistogram>(new SparseHistogram(name)); |
18 } | 20 } |
19 }; | 21 }; |
20 | 22 |
21 TEST_F(SparseHistogramTest, BasicTest) { | 23 TEST_F(SparseHistogramTest, BasicTest) { |
22 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse1")); | 24 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
23 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); | 25 scoped_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); |
24 EXPECT_EQ(0, snapshot->TotalCount()); | 26 EXPECT_EQ(0, snapshot->TotalCount()); |
25 EXPECT_EQ(0, snapshot->sum()); | 27 EXPECT_EQ(0, snapshot->sum()); |
26 | 28 |
27 histogram->Add(100); | 29 histogram->Add(100); |
28 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); | 30 scoped_ptr<HistogramSamples> snapshot1(histogram->SnapshotSamples()); |
29 EXPECT_EQ(1, snapshot1->TotalCount()); | 31 EXPECT_EQ(1, snapshot1->TotalCount()); |
30 EXPECT_EQ(1, snapshot1->GetCount(100)); | 32 EXPECT_EQ(1, snapshot1->GetCount(100)); |
31 | 33 |
32 histogram->Add(100); | 34 histogram->Add(100); |
33 histogram->Add(101); | 35 histogram->Add(101); |
34 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); | 36 scoped_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); |
35 EXPECT_EQ(3, snapshot2->TotalCount()); | 37 EXPECT_EQ(3, snapshot2->TotalCount()); |
36 EXPECT_EQ(2, snapshot2->GetCount(100)); | 38 EXPECT_EQ(2, snapshot2->GetCount(100)); |
37 EXPECT_EQ(1, snapshot2->GetCount(101)); | 39 EXPECT_EQ(1, snapshot2->GetCount(101)); |
38 } | 40 } |
39 | 41 |
42 TEST_F(SparseHistogramTest, Serialize) { | |
43 scoped_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); | |
44 histogram->SetFlags(HistogramBase::kIPCSerializationSourceFlag); | |
45 | |
46 Pickle pickle; | |
47 histogram->SerializeInfo(&pickle); | |
48 | |
49 PickleIterator iter(pickle); | |
50 | |
51 int type; | |
52 EXPECT_TRUE(iter.ReadInt(&type)); | |
53 EXPECT_EQ(SPARSE_HISTOGRAM, type); | |
54 | |
55 std::string name; | |
56 EXPECT_TRUE(iter.ReadString(&name)); | |
57 EXPECT_EQ("Sparse", name); | |
58 | |
59 int flag; | |
60 EXPECT_TRUE(iter.ReadInt(&flag)); | |
61 EXPECT_EQ(HistogramBase::kIPCSerializationSourceFlag, flag); | |
62 | |
63 // No more data in the pickle. | |
64 EXPECT_FALSE(iter.SkipBytes(1)); | |
65 } | |
Ilya Sherman
2012/12/29 00:17:30
It seems like a more fruitful test would be to tes
kaiwang
2013/01/08 00:51:40
All deserializing tests are in histogram_base_unit
| |
66 | |
40 } // namespace base | 67 } // namespace base |
OLD | NEW |