google-research
120 строк · 5.3 Кб
1// Copyright 2024 The Google Research Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "fec_hashing.h"
16
17#include <limits>
18
19#include "definitions.h"
20#include "gtest/gtest.h"
21
22namespace automl_zero {
23
24using ::std::function;
25using ::std::vector;
26using ::testing::Test;
27
28typedef
29function<size_t(const vector<double>&, const vector<double>&,
30size_t, IntegerT)>
31HashFunction;
32
33class HashFunctionTest : public Test {
34protected:
35void VerifyProducesEqualHashesForEqualVectors(
36const HashFunction& hash_function) {
37EXPECT_EQ(hash_function({0.6}, {0.0}, 0, 1000),
38hash_function({0.6}, {0.0}, 0, 1000));
39EXPECT_EQ(hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000),
40hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000));
41EXPECT_EQ(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
42hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000));
43}
44
45void VerifyDetectsDifferenceInSingleValue(const HashFunction& hash_function) {
46EXPECT_NE(hash_function({0.6}, {0.0}, 0, 1000),
47hash_function({0.601}, {0.0}, 0, 1000));
48EXPECT_NE(hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000),
49hash_function({0.6, 0.2}, {0.81, 2.5}, 1, 1000));
50EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
51hash_function({0.6, 0.2, 0.0001}, {0.8, 2.5, 10.1}, 2, 1000));
52EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
53hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 30, 1000));
54EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
55hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1001));
56}
57
58void VerifyDetectsDifferenceInMultipleValues(
59const HashFunction& hash_function) {
60EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
61hash_function({0.6, 0.1, 0.0}, {0.8, 2.6, 10.1}, 0, 1000));
62EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 1, 1000),
63hash_function({0.6, 0.1, 0.1}, {0.8, 2.5, 10.1}, 1, 1000));
64EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
65hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 2, 1000));
66EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
67hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 4, 1000));
68EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
69hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 4, 10000));
70}
71
72void VerifyDetectsDifferenceInNumberOfValues(
73const HashFunction& hash_function) {
74EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
75hash_function({0.6, 0.2}, {0.8, 2.5, 10.1}, 0, 1000));
76EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
77hash_function({0.6, 0.2, 0.0}, {0.8, 2.5}, 0, 1000));
78EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
79hash_function({0.6, 0.2}, {0.8, 2.5}, 0, 1000));
80}
81
82void VerifyHandlesLargeValues(const HashFunction& hash_function) {
83EXPECT_EQ(hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000),
84hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000));
85EXPECT_NE(hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000),
86hash_function({0.7, 1000000000.0}, {0.8, 2.5}, 0, 1000));
87}
88
89void VerifyHandlesInfinity(const HashFunction& hash_function) {
90const double inf = std::numeric_limits<double>::infinity();
91EXPECT_EQ(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
92hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000));
93EXPECT_NE(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
94hash_function({0.7, inf}, {0.8, 2.5}, 0, 1000));
95EXPECT_NE(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
96hash_function({0.6, 1.0}, {0.8, 2.5}, 0, 1000));
97}
98
99void VerifyHandlesNan(const HashFunction& hash_function) {
100const double nan = std::numeric_limits<double>::quiet_NaN();
101EXPECT_EQ(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
102hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000));
103EXPECT_NE(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
104hash_function({0.7, nan}, {0.8, 2.5}, 0, 1000));
105EXPECT_NE(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
106hash_function({0.6, 1.0}, {0.8, 2.5}, 0, 1000));
107}
108};
109
110TEST_F(HashFunctionTest, WellMixedHashWorksCorrectly) {
111VerifyProducesEqualHashesForEqualVectors(WellMixedHash);
112VerifyDetectsDifferenceInSingleValue(WellMixedHash);
113VerifyDetectsDifferenceInMultipleValues(WellMixedHash);
114VerifyDetectsDifferenceInNumberOfValues(WellMixedHash);
115VerifyHandlesLargeValues(WellMixedHash);
116VerifyHandlesInfinity(WellMixedHash);
117VerifyHandlesNan(WellMixedHash);
118}
119
120} // namespace automl_zero
121