diff options
author | swarmer <swarmer@yandex-team.com> | 2024-03-21 23:48:00 +0300 |
---|---|---|
committer | swarmer <swarmer@yandex-team.com> | 2024-03-22 00:01:35 +0300 |
commit | 44ea9939d4ab7a714648ed97a9ed972629e374a7 (patch) | |
tree | 10e5cbdf93e0c4536724d1d1d2a137da2190ee1d /util/generic | |
parent | 120d7867596b83d6d954905dabd53fa97bfcdabf (diff) | |
download | ydb-44ea9939d4ab7a714648ed97a9ed972629e374a7.tar.gz |
[util] Handle the negative zero value in the NumericHash
Two equal numbers must produce the same hash value.
35bb6ef6de3ccbb3b5cacd09950a21063814b499
Diffstat (limited to 'util/generic')
-rw-r--r-- | util/generic/hash_ut.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/util/generic/hash_ut.cpp b/util/generic/hash_ut.cpp index f8bf4e8eac..78fc5657ce 100644 --- a/util/generic/hash_ut.cpp +++ b/util/generic/hash_ut.cpp @@ -62,6 +62,7 @@ class THashTest: public TTestBase { UNIT_TEST(TestHSetInsertInitializerList); UNIT_TEST(TestTupleHash); UNIT_TEST(TestStringHash); + UNIT_TEST(TestFloatingPointHash); UNIT_TEST_SUITE_END(); using hmset = THashMultiSet<char, hash<char>, TEqualTo<char>>; @@ -115,6 +116,7 @@ protected: void TestHSetInsertInitializerList(); void TestTupleHash(); void TestStringHash(); + void TestFloatingPointHash(); }; UNIT_TEST_SUITE_REGISTRATION(THashTest); @@ -1334,3 +1336,24 @@ void THashTest::TestStringHash() { UNIT_ASSERT_VALUES_EQUAL(ComputeHash(TStringBuf("hehe")), expected); // TStringBuf UNIT_ASSERT_VALUES_EQUAL(ComputeHash<const char*>("hehe"), expected); // const char* } + +template <class TFloat> +static void TestFloatingPointHashImpl() { + const TFloat f = 0; + Y_ASSERT(f == -f); + THashSet<TFloat> set; + set.insert(f); + UNIT_ASSERT_C(set.contains(-f), TypeName<TFloat>()); + UNIT_ASSERT_VALUES_EQUAL_C(ComputeHash(f), ComputeHash(-f), TypeName<TFloat>()); + for (int i = 0; i < 5; ++i) { + set.insert(-TFloat(i)); + set.insert(+TFloat(i)); + } + UNIT_ASSERT_VALUES_EQUAL_C(set.size(), 9, TypeName<TFloat>()); +} + +void THashTest::TestFloatingPointHash() { + TestFloatingPointHashImpl<float>(); + TestFloatingPointHashImpl<double>(); + // TestFloatingPointHashImpl<long double>(); +} |