xref: /aosp_15_r20/external/libchrome/base/metrics/histogram_functions_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1 // Copyright 2016 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/metrics/histogram_functions.h"
6 
7 #include "base/metrics/histogram_macros.h"
8 #include "base/test/metrics/histogram_tester.h"
9 #include "base/time/time.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace base {
14 
15 enum UmaHistogramTestingEnum {
16   UMA_HISTOGRAM_TESTING_ENUM_FIRST,
17   UMA_HISTOGRAM_TESTING_ENUM_SECOND,
18   UMA_HISTOGRAM_TESTING_ENUM_THIRD
19 };
20 
TEST(HistogramFunctionsTest,ExactLinear)21 TEST(HistogramFunctionsTest, ExactLinear) {
22   std::string histogram("Testing.UMA.HistogramExactLinear");
23   HistogramTester tester;
24   UmaHistogramExactLinear(histogram, 10, 100);
25   tester.ExpectUniqueSample(histogram, 10, 1);
26   UmaHistogramExactLinear(histogram, 20, 100);
27   UmaHistogramExactLinear(histogram, 10, 100);
28   tester.ExpectBucketCount(histogram, 10, 2);
29   tester.ExpectBucketCount(histogram, 20, 1);
30   tester.ExpectTotalCount(histogram, 3);
31   // Test linear buckets overflow.
32   UmaHistogramExactLinear(histogram, 200, 100);
33   tester.ExpectBucketCount(histogram, 101, 1);
34   tester.ExpectTotalCount(histogram, 4);
35   // Test linear buckets underflow.
36   UmaHistogramExactLinear(histogram, 0, 100);
37   tester.ExpectBucketCount(histogram, 0, 1);
38   tester.ExpectTotalCount(histogram, 5);
39 }
40 
TEST(HistogramFunctionsTest,Enumeration)41 TEST(HistogramFunctionsTest, Enumeration) {
42   std::string histogram("Testing.UMA.HistogramEnumeration");
43   HistogramTester tester;
44   UmaHistogramEnumeration(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST,
45                           UMA_HISTOGRAM_TESTING_ENUM_THIRD);
46   tester.ExpectUniqueSample(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST, 1);
47 
48   // Verify the overflow & underflow bucket exists.
49   UMA_HISTOGRAM_ENUMERATION(
50       histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 10,
51       static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD));
52   tester.ExpectBucketCount(
53       histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1);
54   tester.ExpectTotalCount(histogram, 2);
55 }
56 
TEST(HistogramFunctionsTest,Boolean)57 TEST(HistogramFunctionsTest, Boolean) {
58   std::string histogram("Testing.UMA.HistogramBoolean");
59   HistogramTester tester;
60   UmaHistogramBoolean(histogram, true);
61   tester.ExpectUniqueSample(histogram, 1, 1);
62   UmaHistogramBoolean(histogram, false);
63   tester.ExpectBucketCount(histogram, 0, 1);
64   tester.ExpectTotalCount(histogram, 2);
65 }
66 
TEST(HistogramFunctionsTest,Percentage)67 TEST(HistogramFunctionsTest, Percentage) {
68   std::string histogram("Testing.UMA.HistogramPercentage");
69   HistogramTester tester;
70   UmaHistogramPercentage(histogram, 50);
71   tester.ExpectUniqueSample(histogram, 50, 1);
72   // Test overflows.
73   UmaHistogramPercentage(histogram, 110);
74   tester.ExpectBucketCount(histogram, 101, 1);
75   tester.ExpectTotalCount(histogram, 2);
76 }
77 
TEST(HistogramFunctionsTest,Counts)78 TEST(HistogramFunctionsTest, Counts) {
79   std::string histogram("Testing.UMA.HistogramCount.Custom");
80   HistogramTester tester;
81   UmaHistogramCustomCounts(histogram, 10, 1, 100, 10);
82   tester.ExpectUniqueSample(histogram, 10, 1);
83   UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
84   UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
85   UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
86   tester.ExpectBucketCount(histogram, 20, 3);
87   tester.ExpectTotalCount(histogram, 4);
88   UmaHistogramCustomCounts(histogram, 110, 1, 100, 10);
89   tester.ExpectBucketCount(histogram, 101, 1);
90   tester.ExpectTotalCount(histogram, 5);
91 }
92 
TEST(HistogramFunctionsTest,Times)93 TEST(HistogramFunctionsTest, Times) {
94   std::string histogram("Testing.UMA.HistogramTimes");
95   HistogramTester tester;
96   UmaHistogramTimes(histogram, TimeDelta::FromSeconds(1));
97   tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(1), 1);
98   tester.ExpectTotalCount(histogram, 1);
99   UmaHistogramTimes(histogram, TimeDelta::FromSeconds(9));
100   tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(9), 1);
101   tester.ExpectTotalCount(histogram, 2);
102   UmaHistogramTimes(histogram, TimeDelta::FromSeconds(10));  // Overflows
103   tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(10), 1);
104   UmaHistogramTimes(histogram, TimeDelta::FromSeconds(20));  // Overflows.
105   // Check the value by picking any overflow time.
106   tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(11), 2);
107   tester.ExpectTotalCount(histogram, 4);
108 }
109 
TEST(HistogramFunctionsTest,Sparse_SupportsLargeRange)110 TEST(HistogramFunctionsTest, Sparse_SupportsLargeRange) {
111   std::string histogram("Testing.UMA.HistogramSparse");
112   HistogramTester tester;
113   UmaHistogramSparse(histogram, 0);
114   UmaHistogramSparse(histogram, 123456789);
115   UmaHistogramSparse(histogram, 123456789);
116   EXPECT_THAT(tester.GetAllSamples(histogram),
117               testing::ElementsAre(Bucket(0, 1), Bucket(123456789, 2)));
118 }
119 
TEST(HistogramFunctionsTest,Sparse_SupportsNegativeValues)120 TEST(HistogramFunctionsTest, Sparse_SupportsNegativeValues) {
121   std::string histogram("Testing.UMA.HistogramSparse");
122   HistogramTester tester;
123   UmaHistogramSparse(histogram, -1);
124   tester.ExpectUniqueSample(histogram, -1, 1);
125 }
126 
127 }  // namespace base.
128