aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/unittests/compare_ut.cpp
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2024-11-06 13:24:38 +0300
committerbabenko <babenko@yandex-team.com>2024-11-06 13:54:01 +0300
commitb60a78031c047a8c8543bf6a3dc4f828d104dd3c (patch)
treee8ff58e6fe24f57d35a6cc9a91ca61522706d088 /library/cpp/yt/misc/unittests/compare_ut.cpp
parent658682dbe663353ecdf6cb50a846c54b3dd24481 (diff)
downloadydb-b60a78031c047a8c8543bf6a3dc4f828d104dd3c.tar.gz
NaN-safe comparison and hashing
commit_hash:46d59ab3acbd313753d3e46f3a6f10a8ebc424d8
Diffstat (limited to 'library/cpp/yt/misc/unittests/compare_ut.cpp')
-rw-r--r--library/cpp/yt/misc/unittests/compare_ut.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/unittests/compare_ut.cpp b/library/cpp/yt/misc/unittests/compare_ut.cpp
new file mode 100644
index 0000000000..da149a0690
--- /dev/null
+++ b/library/cpp/yt/misc/unittests/compare_ut.cpp
@@ -0,0 +1,62 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/misc/compare.h>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TCompareTest, TernaryCompare)
+{
+ EXPECT_EQ(TernaryCompare(123, 123), 0);
+ EXPECT_EQ(TernaryCompare(10, 20), -1);
+ EXPECT_EQ(TernaryCompare(20, 10), +1);
+ EXPECT_EQ(TernaryCompare(std::nan("1"), std::nan("1")), +1);
+ EXPECT_EQ(TernaryCompare(std::nan("1"), std::nan("2")), +1);
+ EXPECT_EQ(TernaryCompare(std::nan("1"), 123.0), +1);
+ EXPECT_EQ(TernaryCompare(123.0, std::nan("1")), +1);
+}
+
+TEST(TCompareTest, NaNSafeTernaryCompare)
+{
+ EXPECT_EQ(NaNSafeTernaryCompare(std::nan("1"), std::nan("1")), 0);
+ EXPECT_EQ(NaNSafeTernaryCompare(std::nan("1"), std::nan("2")), 0);
+ EXPECT_EQ(NaNSafeTernaryCompare(123.0, std::nan("1")), -1);
+ EXPECT_EQ(NaNSafeTernaryCompare(std::nan("1"), 123.0), +1);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <class T>
+class TTernaryCompareStringTest
+ : public ::testing::Test
+{ };
+
+TYPED_TEST_SUITE_P(TTernaryCompareStringTest);
+
+TYPED_TEST_P(TTernaryCompareStringTest, Compare)
+{
+ EXPECT_EQ(TernaryCompare(TypeParam("abc"), TypeParam("abc")), 0);
+ EXPECT_EQ(TernaryCompare(TypeParam("x"), TypeParam("y")), -1);
+ EXPECT_EQ(TernaryCompare(TypeParam("y"), TypeParam("x")), +1);
+}
+
+REGISTER_TYPED_TEST_SUITE_P(TTernaryCompareStringTest, Compare);
+
+using TTernaryCompareStringTestTypes = ::testing::Types<
+ TString,
+ TStringBuf,
+ std::string,
+ std::string_view
+>;
+
+INSTANTIATE_TYPED_TEST_SUITE_P(
+ TypeParametrized,
+ TTernaryCompareStringTest,
+ TTernaryCompareStringTestTypes);
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT