1/*
2 *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "testing/gtest/include/gtest/gtest.h"
12
13#include "webrtc/system_wrappers/include/metrics.h"
14#include "webrtc/test/histogram.h"
15
16namespace webrtc {
17namespace {
18const int kSample = 22;
19const std::string kName = "Name";
20
21void AddSparseSample(const std::string& name, int sample) {
22  RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample);
23}
24#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
25void AddSample(const std::string& name, int sample) {
26  RTC_HISTOGRAM_COUNTS_100(name, sample);
27}
28#endif  // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
29}  // namespace
30
31TEST(MetricsTest, InitiallyNoSamples) {
32  test::ClearHistograms();
33  EXPECT_EQ(0, test::NumHistogramSamples(kName));
34  EXPECT_EQ(-1, test::LastHistogramSample(kName));
35}
36
37TEST(MetricsTest, RtcHistogramPercent_AddSample) {
38  test::ClearHistograms();
39  RTC_HISTOGRAM_PERCENTAGE(kName, kSample);
40  EXPECT_EQ(1, test::NumHistogramSamples(kName));
41  EXPECT_EQ(kSample, test::LastHistogramSample(kName));
42}
43
44TEST(MetricsTest, RtcHistogramEnumeration_AddSample) {
45  test::ClearHistograms();
46  RTC_HISTOGRAM_ENUMERATION(kName, kSample, kSample + 1);
47  EXPECT_EQ(1, test::NumHistogramSamples(kName));
48  EXPECT_EQ(kSample, test::LastHistogramSample(kName));
49}
50
51TEST(MetricsTest, RtcHistogramCountsSparse_AddSample) {
52  test::ClearHistograms();
53  RTC_HISTOGRAM_COUNTS_SPARSE_100(kName, kSample);
54  EXPECT_EQ(1, test::NumHistogramSamples(kName));
55  EXPECT_EQ(kSample, test::LastHistogramSample(kName));
56}
57
58TEST(MetricsTest, RtcHistogramCounts_AddSample) {
59  test::ClearHistograms();
60  RTC_HISTOGRAM_COUNTS_100(kName, kSample);
61  EXPECT_EQ(1, test::NumHistogramSamples(kName));
62  EXPECT_EQ(kSample, test::LastHistogramSample(kName));
63}
64
65TEST(MetricsTest, RtcHistogramCounts_AddMultipleSamples) {
66  test::ClearHistograms();
67  const int kNumSamples = 10;
68  for (int i = 0; i < kNumSamples; ++i) {
69    RTC_HISTOGRAM_COUNTS_100(kName, i);
70  }
71  EXPECT_EQ(kNumSamples, test::NumHistogramSamples(kName));
72  EXPECT_EQ(kNumSamples - 1, test::LastHistogramSample(kName));
73}
74
75TEST(MetricsTest, RtcHistogramSparse_NonConstantNameWorks) {
76  test::ClearHistograms();
77  AddSparseSample("Name1", kSample);
78  AddSparseSample("Name2", kSample);
79  EXPECT_EQ(1, test::NumHistogramSamples("Name1"));
80  EXPECT_EQ(1, test::NumHistogramSamples("Name2"));
81}
82
83#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
84TEST(MetricsTest, RtcHistogram_FailsForNonConstantName) {
85  test::ClearHistograms();
86  AddSample("Name1", kSample);
87  EXPECT_DEATH(AddSample("Name2", kSample), "");
88}
89#endif  // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
90
91}  // namespace webrtc
92