1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
media / learning / common / labelled_example_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/learning/common/labelled_example.h"
#include <algorithm>
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace learning {
class LearnerLabelledExampleTest : public testing::Test {};
TEST_F(LearnerLabelledExampleTest, InitListWorks) {
const int kFeature1 = 123;
const int kFeature2 = 456;
std::vector<FeatureValue> features = {FeatureValue(kFeature1),
FeatureValue(kFeature2)};
TargetValue target(789);
LabelledExample example({FeatureValue(kFeature1), FeatureValue(kFeature2)},
target);
EXPECT_EQ(example.features, features);
EXPECT_EQ(example.target_value, target);
}
TEST_F(LearnerLabelledExampleTest, CopyConstructionWorks) {
LabelledExample example_1({FeatureValue(123), FeatureValue(456)},
TargetValue(789));
LabelledExample example_2(example_1);
EXPECT_EQ(example_1, example_2);
}
TEST_F(LearnerLabelledExampleTest, MoveConstructionWorks) {
LabelledExample example_1({FeatureValue(123), FeatureValue(456)},
TargetValue(789));
LabelledExample example_1_copy(example_1);
LabelledExample example_1_move(std::move(example_1));
EXPECT_EQ(example_1_copy, example_1_move);
EXPECT_NE(example_1_copy, example_1);
}
TEST_F(LearnerLabelledExampleTest, EqualExamplesCompareAsEqual) {
const int kFeature1 = 123;
const int kFeature2 = 456;
TargetValue target(789);
LabelledExample example_1({FeatureValue(kFeature1), FeatureValue(kFeature2)},
target);
LabelledExample example_2({FeatureValue(kFeature1), FeatureValue(kFeature2)},
target);
// Verify both that == and != work.
EXPECT_EQ(example_1, example_2);
EXPECT_FALSE(example_1 != example_2);
// Also insist that equal examples are not less.
EXPECT_FALSE(example_1 < example_2);
EXPECT_FALSE(example_2 < example_1);
}
TEST_F(LearnerLabelledExampleTest, UnequalFeaturesCompareAsUnequal) {
const int kFeature1 = 123;
const int kFeature2 = 456;
TargetValue target(789);
LabelledExample example_1({FeatureValue(kFeature1), FeatureValue(kFeature1)},
target);
LabelledExample example_2({FeatureValue(kFeature2), FeatureValue(kFeature2)},
target);
EXPECT_TRUE(example_1 != example_2);
EXPECT_FALSE(example_1 == example_2);
// We don't care which way is <, but we do care that one is less than the
// other but not both.
EXPECT_NE((example_1 < example_2), (example_2 < example_1));
}
TEST_F(LearnerLabelledExampleTest, WeightDoesntChangeExampleEquality) {
const int kFeature1 = 123;
TargetValue target(789);
LabelledExample example_1({FeatureValue(kFeature1)}, target);
LabelledExample example_2 = example_1;
// Set the weights to be unequal. This should not affect the comparison.
example_1.weight = 10u;
example_2.weight = 20u;
// Verify both that == and != ignore weights.
EXPECT_EQ(example_1, example_2);
EXPECT_FALSE(example_1 != example_2);
// Also insist that equal examples are not less.
EXPECT_FALSE(example_1 < example_2);
EXPECT_FALSE(example_2 < example_1);
}
TEST_F(LearnerLabelledExampleTest, ExampleAssignmentCopiesWeights) {
// While comparisons ignore weights, copy / assign should not.
const int kFeature1 = 123;
TargetValue target(789);
LabelledExample example_1({FeatureValue(kFeature1)}, target);
example_1.weight = 10u;
// Copy-assignment.
LabelledExample example_2;
example_2 = example_1;
EXPECT_EQ(example_1, example_2);
EXPECT_EQ(example_1.weight, example_2.weight);
// Copy-construction.
LabelledExample example_3(example_1);
EXPECT_EQ(example_1, example_3);
EXPECT_EQ(example_1.weight, example_3.weight);
// Move-assignment.
LabelledExample example_4;
example_4 = std::move(example_2);
EXPECT_EQ(example_1, example_4);
EXPECT_EQ(example_1.weight, example_4.weight);
// Move-construction.
LabelledExample example_5(std::move(example_3));
EXPECT_EQ(example_1, example_5);
EXPECT_EQ(example_1.weight, example_5.weight);
}
TEST_F(LearnerLabelledExampleTest, UnequalTargetsCompareAsUnequal) {
const int kFeature1 = 123;
const int kFeature2 = 456;
LabelledExample example_1({FeatureValue(kFeature1), FeatureValue(kFeature1)},
TargetValue(789));
LabelledExample example_2({FeatureValue(kFeature2), FeatureValue(kFeature2)},
TargetValue(987));
EXPECT_TRUE(example_1 != example_2);
EXPECT_FALSE(example_1 == example_2);
// Exactly one should be less than the other, but we don't care which one.
EXPECT_TRUE((example_1 < example_2) ^ (example_2 < example_1));
}
TEST_F(LearnerLabelledExampleTest, OrderingIsTransitive) {
// Verify that ordering is transitive. We don't particularly care what the
// ordering is, otherwise.
const FeatureValue kFeature1(123);
const FeatureValue kFeature2(456);
const FeatureValue kTarget1(789);
const FeatureValue kTarget2(987);
std::vector<LabelledExample> examples;
examples.push_back(LabelledExample({kFeature1}, kTarget1));
examples.push_back(LabelledExample({kFeature1}, kTarget2));
examples.push_back(LabelledExample({kFeature2}, kTarget1));
examples.push_back(LabelledExample({kFeature2}, kTarget2));
examples.push_back(LabelledExample({kFeature1, kFeature2}, kTarget1));
examples.push_back(LabelledExample({kFeature1, kFeature2}, kTarget2));
examples.push_back(LabelledExample({kFeature2, kFeature1}, kTarget1));
examples.push_back(LabelledExample({kFeature2, kFeature1}, kTarget2));
// Sort, and make sure that it ends up totally ordered.
std::sort(examples.begin(), examples.end());
for (auto outer = examples.begin(); outer != examples.end(); outer++) {
for (auto inner = outer + 1; inner != examples.end(); inner++) {
EXPECT_TRUE(*outer < *inner);
EXPECT_FALSE(*inner < *outer);
}
}
}
TEST_F(LearnerLabelledExampleTest, UnweightedTrainingDataPushBack) {
// Test that pushing examples from unweighted storage into TrainingData works.
TrainingData training_data;
EXPECT_EQ(training_data.total_weight(), 0u);
EXPECT_TRUE(training_data.empty());
LabelledExample example({FeatureValue(123)}, TargetValue(789));
training_data.push_back(example);
EXPECT_EQ(training_data.total_weight(), 1u);
EXPECT_FALSE(training_data.empty());
EXPECT_TRUE(training_data.is_unweighted());
EXPECT_EQ(training_data[0], example);
}
TEST_F(LearnerLabelledExampleTest, WeightedTrainingDataPushBack) {
// Test that pushing examples from weighted storage into TrainingData works.
TrainingData training_data;
EXPECT_EQ(training_data.total_weight(), 0u);
EXPECT_TRUE(training_data.empty());
LabelledExample example({FeatureValue(123)}, TargetValue(789));
const WeightType weight(10);
example.weight = weight;
training_data.push_back(example);
training_data.push_back(example);
EXPECT_EQ(training_data.total_weight(), weight * 2);
EXPECT_FALSE(training_data.empty());
EXPECT_FALSE(training_data.is_unweighted());
EXPECT_EQ(training_data[0], example);
}
TEST_F(LearnerLabelledExampleTest, TrainingDataDeDuplicate) {
// Make sure that TrainingData::DeDuplicate works properly.
const WeightType weight_0_a(100);
const WeightType weight_0_b(200);
const WeightType weight_1(500);
LabelledExample example_0({FeatureValue(123)}, TargetValue(789));
LabelledExample example_1({FeatureValue(456)}, TargetValue(789));
TrainingData training_data;
example_0.weight = weight_0_a;
training_data.push_back(example_0);
example_1.weight = weight_1;
training_data.push_back(example_1);
example_0.weight = weight_0_b;
training_data.push_back(example_0);
EXPECT_EQ(training_data.total_weight(), weight_0_a + weight_0_b + weight_1);
EXPECT_EQ(training_data.size(), 3u);
EXPECT_EQ(training_data[0].weight, weight_0_a);
EXPECT_EQ(training_data[1].weight, weight_1);
EXPECT_EQ(training_data[2].weight, weight_0_b);
TrainingData dedup = training_data.DeDuplicate();
EXPECT_EQ(dedup.total_weight(), weight_0_a + weight_0_b + weight_1);
EXPECT_EQ(dedup.size(), 2u);
// We don't care which order they're in, so find the index of |example_0|.
size_t idx_0 = (dedup[0] == example_0) ? 0 : 1;
EXPECT_EQ(dedup[idx_0], example_0);
EXPECT_EQ(dedup[idx_0].weight, weight_0_a + weight_0_b);
EXPECT_EQ(dedup[1u - idx_0], example_1);
EXPECT_EQ(dedup[1u - idx_0].weight, weight_1);
}
} // namespace learning
} // namespace media