google-research

Форк
0
/
fec_hashing_test.cc 
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

22
namespace automl_zero {
23

24
using ::std::function;
25
using ::std::vector;
26
using ::testing::Test;
27

28
typedef
29
    function<size_t(const vector<double>&, const vector<double>&,
30
                    size_t, IntegerT)>
31
    HashFunction;
32

33
class HashFunctionTest : public Test {
34
 protected:
35
  void VerifyProducesEqualHashesForEqualVectors(
36
      const HashFunction& hash_function) {
37
    EXPECT_EQ(hash_function({0.6}, {0.0}, 0, 1000),
38
              hash_function({0.6}, {0.0}, 0, 1000));
39
    EXPECT_EQ(hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000),
40
              hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000));
41
    EXPECT_EQ(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
42
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000));
43
  }
44

45
  void VerifyDetectsDifferenceInSingleValue(const HashFunction& hash_function) {
46
    EXPECT_NE(hash_function({0.6}, {0.0}, 0, 1000),
47
              hash_function({0.601}, {0.0}, 0, 1000));
48
    EXPECT_NE(hash_function({0.6, 0.2}, {0.8, 2.5}, 1, 1000),
49
              hash_function({0.6, 0.2}, {0.81, 2.5}, 1, 1000));
50
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
51
              hash_function({0.6, 0.2, 0.0001}, {0.8, 2.5, 10.1}, 2, 1000));
52
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
53
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 30, 1000));
54
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
55
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1001));
56
  }
57

58
  void VerifyDetectsDifferenceInMultipleValues(
59
      const HashFunction& hash_function) {
60
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
61
              hash_function({0.6, 0.1, 0.0}, {0.8, 2.6, 10.1}, 0, 1000));
62
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 1, 1000),
63
              hash_function({0.6, 0.1, 0.1}, {0.8, 2.5, 10.1}, 1, 1000));
64
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 2, 1000),
65
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 2, 1000));
66
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
67
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 4, 1000));
68
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 3, 1000),
69
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.6, 10.0}, 4, 10000));
70
  }
71

72
  void VerifyDetectsDifferenceInNumberOfValues(
73
      const HashFunction& hash_function) {
74
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
75
              hash_function({0.6, 0.2}, {0.8, 2.5, 10.1}, 0, 1000));
76
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
77
              hash_function({0.6, 0.2, 0.0}, {0.8, 2.5}, 0, 1000));
78
    EXPECT_NE(hash_function({0.6, 0.2, 0.0}, {0.8, 2.5, 10.1}, 0, 1000),
79
              hash_function({0.6, 0.2}, {0.8, 2.5}, 0, 1000));
80
  }
81

82
  void VerifyHandlesLargeValues(const HashFunction& hash_function) {
83
    EXPECT_EQ(hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000),
84
              hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000));
85
    EXPECT_NE(hash_function({0.6, 1000000000.0}, {0.8, 2.5}, 0, 1000),
86
              hash_function({0.7, 1000000000.0}, {0.8, 2.5}, 0, 1000));
87
  }
88

89
  void VerifyHandlesInfinity(const HashFunction& hash_function) {
90
    const double inf = std::numeric_limits<double>::infinity();
91
    EXPECT_EQ(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
92
              hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000));
93
    EXPECT_NE(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
94
              hash_function({0.7, inf}, {0.8, 2.5}, 0, 1000));
95
    EXPECT_NE(hash_function({0.6, inf}, {0.8, 2.5}, 0, 1000),
96
              hash_function({0.6, 1.0}, {0.8, 2.5}, 0, 1000));
97
  }
98

99
  void VerifyHandlesNan(const HashFunction& hash_function) {
100
    const double nan = std::numeric_limits<double>::quiet_NaN();
101
    EXPECT_EQ(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
102
              hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000));
103
    EXPECT_NE(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
104
              hash_function({0.7, nan}, {0.8, 2.5}, 0, 1000));
105
    EXPECT_NE(hash_function({0.6, nan}, {0.8, 2.5}, 0, 1000),
106
              hash_function({0.6, 1.0}, {0.8, 2.5}, 0, 1000));
107
  }
108
};
109

110
TEST_F(HashFunctionTest, WellMixedHashWorksCorrectly) {
111
  VerifyProducesEqualHashesForEqualVectors(WellMixedHash);
112
  VerifyDetectsDifferenceInSingleValue(WellMixedHash);
113
  VerifyDetectsDifferenceInMultipleValues(WellMixedHash);
114
  VerifyDetectsDifferenceInNumberOfValues(WellMixedHash);
115
  VerifyHandlesLargeValues(WellMixedHash);
116
  VerifyHandlesInfinity(WellMixedHash);
117
  VerifyHandlesNan(WellMixedHash);
118
}
119

120
}  // namespace automl_zero
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.