forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranker_example_util_unittest.cc
183 lines (141 loc) · 6.36 KB
/
ranker_example_util_unittest.cc
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/assist_ranker/ranker_example_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace assist_ranker {
using ::testing::ElementsAreArray;
class RankerExampleUtilTest : public ::testing::Test {
protected:
void SetUp() override {
auto& features = *example_.mutable_features();
features[bool_name_].set_bool_value(bool_value_);
features[int32_name_].set_int32_value(int32_value_);
features[float_name_].set_float_value(float_value_);
features[one_hot_name_].set_string_value(one_hot_value_);
}
RankerExample example_;
const std::string bool_name_ = "bool_feature";
const bool bool_value_ = true;
const std::string int32_name_ = "int32_feature";
const int int32_value_ = 2;
const std::string float_name_ = "float_feature";
const float float_value_ = 3.0f;
const std::string one_hot_name_ = "one_hot_feature";
const std::string elem1_ = "elem1";
const std::string elem2_ = "elem2";
const std::string one_hot_value_ = elem1_;
const float epsilon_ = 0.00000001f;
};
TEST_F(RankerExampleUtilTest, CheckFeature) {
EXPECT_TRUE(SafeGetFeature(bool_name_, example_, nullptr));
EXPECT_TRUE(SafeGetFeature(int32_name_, example_, nullptr));
EXPECT_TRUE(SafeGetFeature(float_name_, example_, nullptr));
EXPECT_TRUE(SafeGetFeature(one_hot_name_, example_, nullptr));
EXPECT_FALSE(SafeGetFeature("", example_, nullptr));
EXPECT_FALSE(SafeGetFeature("foo", example_, nullptr));
}
TEST_F(RankerExampleUtilTest, SafeGetFeature) {
Feature feature;
EXPECT_TRUE(SafeGetFeature(bool_name_, example_, &feature));
EXPECT_TRUE(feature.bool_value());
feature.Clear();
EXPECT_TRUE(SafeGetFeature(int32_name_, example_, &feature));
EXPECT_EQ(int32_value_, feature.int32_value());
feature.Clear();
EXPECT_TRUE(SafeGetFeature(float_name_, example_, &feature));
EXPECT_NEAR(float_value_, feature.float_value(), epsilon_);
feature.Clear();
EXPECT_TRUE(SafeGetFeature(one_hot_name_, example_, &feature));
EXPECT_EQ(one_hot_value_, feature.string_value());
feature.Clear();
EXPECT_FALSE(SafeGetFeature("", example_, &feature));
EXPECT_FALSE(SafeGetFeature("foo", example_, &feature));
}
TEST_F(RankerExampleUtilTest, GetFeatureValueAsFloat) {
float value;
EXPECT_TRUE(GetFeatureValueAsFloat(bool_name_, example_, &value));
EXPECT_NEAR(1.0f, value, epsilon_);
EXPECT_TRUE(GetFeatureValueAsFloat(int32_name_, example_, &value));
EXPECT_NEAR(2.0f, value, epsilon_);
EXPECT_TRUE(GetFeatureValueAsFloat(float_name_, example_, &value));
EXPECT_NEAR(3.0f, value, epsilon_);
EXPECT_FALSE(GetFeatureValueAsFloat(one_hot_name_, example_, &value));
// Value remains unchanged if GetFeatureValueAsFloat returns false.
EXPECT_NEAR(3.0f, value, epsilon_);
EXPECT_FALSE(GetFeatureValueAsFloat("", example_, &value));
EXPECT_FALSE(GetFeatureValueAsFloat("foo", example_, &value));
}
TEST_F(RankerExampleUtilTest, GetOneHotValue) {
std::string value;
EXPECT_FALSE(GetOneHotValue(bool_name_, example_, &value));
EXPECT_FALSE(GetOneHotValue(int32_name_, example_, &value));
EXPECT_FALSE(GetOneHotValue(float_name_, example_, &value));
EXPECT_TRUE(GetOneHotValue(one_hot_name_, example_, &value));
EXPECT_EQ(one_hot_value_, value);
EXPECT_FALSE(GetOneHotValue("", example_, &value));
EXPECT_FALSE(GetOneHotValue("foo", example_, &value));
}
TEST_F(RankerExampleUtilTest, ScalarFeatureInt64Conversion) {
Feature feature;
int64_t int64_value;
feature.set_bool_value(true);
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 72057594037927937LL);
feature.set_int32_value(std::numeric_limits<int32_t>::max());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 216172784261267455LL);
feature.set_int32_value(std::numeric_limits<int32_t>::lowest());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 216172784261267456LL);
feature.set_string_value("foo");
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 288230377439557724LL);
}
TEST_F(RankerExampleUtilTest, FloatFeatureInt64Conversion) {
Feature feature;
int64_t int64_value;
feature.set_float_value(std::numeric_limits<float>::epsilon());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 144115188948271104LL);
feature.set_float_value(-std::numeric_limits<float>::epsilon());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 144115191095754752LL);
feature.set_float_value(std::numeric_limits<float>::max());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 144115190214950911LL);
feature.set_float_value(std::numeric_limits<float>::lowest());
EXPECT_TRUE(FeatureToInt64(feature, &int64_value));
EXPECT_EQ(int64_value, 144115192362434559LL);
}
TEST_F(RankerExampleUtilTest, StringListInt64Conversion) {
Feature feature;
int64_t int64_value;
feature.mutable_string_list()->add_string_value("");
feature.mutable_string_list()->add_string_value("TEST");
EXPECT_TRUE(FeatureToInt64(feature, &int64_value, 1));
EXPECT_EQ(int64_value, 360287974776690660LL);
}
TEST_F(RankerExampleUtilTest, HashExampleFeatureNames) {
auto hashed_example = HashExampleFeatureNames(example_);
// Hashed example has the same number of features.
EXPECT_EQ(example_.features().size(), hashed_example.features().size());
// But the feature names have changed.
EXPECT_FALSE(SafeGetFeature(bool_name_, hashed_example, nullptr));
EXPECT_FALSE(SafeGetFeature(int32_name_, hashed_example, nullptr));
EXPECT_FALSE(SafeGetFeature(float_name_, hashed_example, nullptr));
EXPECT_FALSE(SafeGetFeature(one_hot_name_, hashed_example, nullptr));
EXPECT_TRUE(
SafeGetFeature(HashFeatureName(bool_name_), hashed_example, nullptr));
// Values have not changed.
float float_value;
EXPECT_TRUE(GetFeatureValueAsFloat(HashFeatureName(float_name_),
hashed_example, &float_value));
EXPECT_EQ(float_value_, float_value);
std::string string_value;
EXPECT_TRUE(GetOneHotValue(HashFeatureName(one_hot_name_), hashed_example,
&string_value));
EXPECT_EQ(one_hot_value_, string_value);
}
} // namespace assist_ranker