1 
2 // Copyright 2018 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include "media/learning/common/target_histogram.h"
7 
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace media {
11 namespace learning {
12 
13 class TargetHistogramTest : public testing::Test {
14  public:
TargetHistogramTest()15   TargetHistogramTest() : value_1(123), value_2(456), value_3(789) {}
16 
17   TargetHistogram histogram_;
18 
19   TargetValue value_1;
20   const size_t counts_1 = 100;
21 
22   TargetValue value_2;
23   const size_t counts_2 = 10;
24 
25   TargetValue value_3;
26 };
27 
TEST_F(TargetHistogramTest,EmptyTargetHistogramHasZeroCounts)28 TEST_F(TargetHistogramTest, EmptyTargetHistogramHasZeroCounts) {
29   EXPECT_EQ(histogram_.total_counts(), 0u);
30 }
31 
TEST_F(TargetHistogramTest,AddingCountsWorks)32 TEST_F(TargetHistogramTest, AddingCountsWorks) {
33   histogram_[value_1] = counts_1;
34   EXPECT_EQ(histogram_.total_counts(), counts_1);
35   EXPECT_EQ(histogram_[value_1], counts_1);
36   histogram_[value_1] += counts_1;
37   EXPECT_EQ(histogram_.total_counts(), counts_1 * 2u);
38   EXPECT_EQ(histogram_[value_1], counts_1 * 2u);
39 }
40 
TEST_F(TargetHistogramTest,MultipleValuesAreSeparate)41 TEST_F(TargetHistogramTest, MultipleValuesAreSeparate) {
42   histogram_[value_1] = counts_1;
43   histogram_[value_2] = counts_2;
44   EXPECT_EQ(histogram_.total_counts(), counts_1 + counts_2);
45   EXPECT_EQ(histogram_[value_1], counts_1);
46   EXPECT_EQ(histogram_[value_2], counts_2);
47 }
48 
TEST_F(TargetHistogramTest,AddingTargetValues)49 TEST_F(TargetHistogramTest, AddingTargetValues) {
50   histogram_ += value_1;
51   EXPECT_EQ(histogram_.total_counts(), 1u);
52   EXPECT_EQ(histogram_[value_1], 1u);
53   EXPECT_EQ(histogram_[value_2], 0u);
54 
55   histogram_ += value_1;
56   EXPECT_EQ(histogram_.total_counts(), 2u);
57   EXPECT_EQ(histogram_[value_1], 2u);
58   EXPECT_EQ(histogram_[value_2], 0u);
59 
60   histogram_ += value_2;
61   EXPECT_EQ(histogram_.total_counts(), 3u);
62   EXPECT_EQ(histogram_[value_1], 2u);
63   EXPECT_EQ(histogram_[value_2], 1u);
64 }
65 
TEST_F(TargetHistogramTest,AddingTargetHistograms)66 TEST_F(TargetHistogramTest, AddingTargetHistograms) {
67   histogram_[value_1] = counts_1;
68 
69   TargetHistogram rhs;
70   rhs[value_2] = counts_2;
71 
72   histogram_ += rhs;
73 
74   EXPECT_EQ(histogram_.total_counts(), counts_1 + counts_2);
75   EXPECT_EQ(histogram_[value_1], counts_1);
76   EXPECT_EQ(histogram_[value_2], counts_2);
77 }
78 
TEST_F(TargetHistogramTest,FindSingularMaxFindsTheSingularMax)79 TEST_F(TargetHistogramTest, FindSingularMaxFindsTheSingularMax) {
80   histogram_[value_1] = counts_1;
81   histogram_[value_2] = counts_2;
82   ASSERT_TRUE(counts_1 > counts_2);
83 
84   TargetValue max_value(0);
85   double max_counts = 0;
86   EXPECT_TRUE(histogram_.FindSingularMax(&max_value, &max_counts));
87   EXPECT_EQ(max_value, value_1);
88   EXPECT_EQ(max_counts, counts_1);
89 }
90 
TEST_F(TargetHistogramTest,FindSingularMaxFindsTheSingularMaxAlternateOrder)91 TEST_F(TargetHistogramTest, FindSingularMaxFindsTheSingularMaxAlternateOrder) {
92   // Switch the order, to handle sorting in different directions.
93   histogram_[value_1] = counts_2;
94   histogram_[value_2] = counts_1;
95   ASSERT_TRUE(counts_1 > counts_2);
96 
97   TargetValue max_value(0);
98   double max_counts = 0;
99   EXPECT_TRUE(histogram_.FindSingularMax(&max_value, &max_counts));
100   EXPECT_EQ(max_value, value_2);
101   EXPECT_EQ(max_counts, counts_1);
102 }
103 
TEST_F(TargetHistogramTest,FindSingularMaxReturnsFalsForNonSingularMax)104 TEST_F(TargetHistogramTest, FindSingularMaxReturnsFalsForNonSingularMax) {
105   histogram_[value_1] = counts_1;
106   histogram_[value_2] = counts_1;
107 
108   TargetValue max_value(0);
109   double max_counts = 0;
110   EXPECT_FALSE(histogram_.FindSingularMax(&max_value, &max_counts));
111 }
112 
TEST_F(TargetHistogramTest,FindSingularMaxIgnoresNonSingularNonMax)113 TEST_F(TargetHistogramTest, FindSingularMaxIgnoresNonSingularNonMax) {
114   histogram_[value_1] = counts_1;
115   // |value_2| and |value_3| are tied, but not the max.
116   histogram_[value_2] = counts_2;
117   histogram_[value_3] = counts_2;
118   ASSERT_TRUE(counts_1 > counts_2);
119 
120   TargetValue max_value(0);
121   double max_counts = 0;
122   EXPECT_TRUE(histogram_.FindSingularMax(&max_value, &max_counts));
123   EXPECT_EQ(max_value, value_1);
124   EXPECT_EQ(max_counts, counts_1);
125 }
126 
TEST_F(TargetHistogramTest,FindSingularMaxDoesntRequireCounts)127 TEST_F(TargetHistogramTest, FindSingularMaxDoesntRequireCounts) {
128   histogram_[value_1] = counts_1;
129 
130   TargetValue max_value(0);
131   EXPECT_TRUE(histogram_.FindSingularMax(&max_value));
132   EXPECT_EQ(max_value, value_1);
133 }
134 
TEST_F(TargetHistogramTest,EqualDistributionsCompareAsEqual)135 TEST_F(TargetHistogramTest, EqualDistributionsCompareAsEqual) {
136   histogram_[value_1] = counts_1;
137   TargetHistogram histogram_2;
138   histogram_2[value_1] = counts_1;
139 
140   EXPECT_TRUE(histogram_ == histogram_2);
141 }
142 
TEST_F(TargetHistogramTest,UnequalDistributionsCompareAsNotEqual)143 TEST_F(TargetHistogramTest, UnequalDistributionsCompareAsNotEqual) {
144   histogram_[value_1] = counts_1;
145   TargetHistogram histogram_2;
146   histogram_2[value_2] = counts_2;
147 
148   EXPECT_FALSE(histogram_ == histogram_2);
149 }
150 
TEST_F(TargetHistogramTest,WeightedLabelledExamplesCountCorrectly)151 TEST_F(TargetHistogramTest, WeightedLabelledExamplesCountCorrectly) {
152   LabelledExample example = {{}, value_1};
153   example.weight = counts_1;
154   histogram_ += example;
155 
156   TargetHistogram histogram_2;
157   for (size_t i = 0; i < counts_1; i++)
158     histogram_2 += value_1;
159 
160   EXPECT_EQ(histogram_, histogram_2);
161 }
162 
TEST_F(TargetHistogramTest,Normalize)163 TEST_F(TargetHistogramTest, Normalize) {
164   histogram_[value_1] = counts_1;
165   histogram_[value_2] = counts_2;
166   histogram_.Normalize();
167   EXPECT_EQ(histogram_[value_1],
168             counts_1 / static_cast<double>(counts_1 + counts_2));
169   EXPECT_EQ(histogram_[value_2],
170             counts_2 / static_cast<double>(counts_1 + counts_2));
171 }
172 
TEST_F(TargetHistogramTest,NormalizeEmptyDistribution)173 TEST_F(TargetHistogramTest, NormalizeEmptyDistribution) {
174   // Normalizing an empty distribution should result in an empty distribution.
175   histogram_.Normalize();
176   EXPECT_EQ(histogram_.total_counts(), 0);
177 }
178 
179 }  // namespace learning
180 }  // namespace media
181