aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/string/unittests
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/yt/string/unittests
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/yt/string/unittests')
-rw-r--r--library/cpp/yt/string/unittests/enum_ut.cpp61
-rw-r--r--library/cpp/yt/string/unittests/format_ut.cpp149
-rw-r--r--library/cpp/yt/string/unittests/guid_ut.cpp58
-rw-r--r--library/cpp/yt/string/unittests/string_ut.cpp52
-rw-r--r--library/cpp/yt/string/unittests/ya.make17
5 files changed, 337 insertions, 0 deletions
diff --git a/library/cpp/yt/string/unittests/enum_ut.cpp b/library/cpp/yt/string/unittests/enum_ut.cpp
new file mode 100644
index 00000000000..b8076fd8eed
--- /dev/null
+++ b/library/cpp/yt/string/unittests/enum_ut.cpp
@@ -0,0 +1,61 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/string/enum.h>
+#include <library/cpp/yt/string/format.h>
+
+#include <limits>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Some compile-time sanity checks.
+DEFINE_ENUM(ESample, (One)(Two));
+static_assert(TFormatTraits<ESample>::HasCustomFormatValue);
+static_assert(TFormatTraits<TEnumIndexedVector<ESample, int>>::HasCustomFormatValue);
+
+DEFINE_ENUM(EColor,
+ (Red)
+ (BlackAndWhite)
+);
+
+DEFINE_BIT_ENUM(ELangs,
+ ((None) (0x00))
+ ((Cpp) (0x01))
+ ((Go) (0x02))
+ ((Rust) (0x04))
+ ((Python) (0x08))
+ ((JavaScript) (0x10))
+)
+
+TEST(TFormatTest, Enum)
+{
+ EXPECT_EQ("Red", Format("%v", EColor::Red));
+ EXPECT_EQ("red", Format("%lv", EColor::Red));
+
+ EXPECT_EQ("BlackAndWhite", Format("%v", EColor::BlackAndWhite));
+ EXPECT_EQ("black_and_white", Format("%lv", EColor::BlackAndWhite));
+
+ EXPECT_EQ("EColor(100)", Format("%v", EColor(100)));
+
+ EXPECT_EQ("JavaScript", Format("%v", ELangs::JavaScript));
+ EXPECT_EQ("java_script", Format("%lv", ELangs::JavaScript));
+
+ EXPECT_EQ("None", Format("%v", ELangs::None));
+ EXPECT_EQ("none", Format("%lv", ELangs::None));
+
+ EXPECT_EQ("Cpp | Go", Format("%v", ELangs::Cpp | ELangs::Go));
+ EXPECT_EQ("cpp | go", Format("%lv", ELangs::Cpp | ELangs::Go));
+
+ auto four = ELangs::Cpp | ELangs::Go | ELangs::Python | ELangs::JavaScript;
+ EXPECT_EQ("Cpp | Go | Python | JavaScript", Format("%v", four));
+ EXPECT_EQ("cpp | go | python | java_script", Format("%lv", four));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
+
+
diff --git a/library/cpp/yt/string/unittests/format_ut.cpp b/library/cpp/yt/string/unittests/format_ut.cpp
new file mode 100644
index 00000000000..ee069bb2c09
--- /dev/null
+++ b/library/cpp/yt/string/unittests/format_ut.cpp
@@ -0,0 +1,149 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/string/format.h>
+
+#include <library/cpp/yt/small_containers/compact_vector.h>
+
+#include <limits>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+// Some compile-time sanity checks.
+static_assert(TFormatTraits<int>::HasCustomFormatValue);
+static_assert(TFormatTraits<double>::HasCustomFormatValue);
+static_assert(TFormatTraits<void*>::HasCustomFormatValue);
+static_assert(TFormatTraits<const char*>::HasCustomFormatValue);
+static_assert(TFormatTraits<TStringBuf>::HasCustomFormatValue);
+static_assert(TFormatTraits<TString>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::vector<int>>::HasCustomFormatValue);
+
+// N.B. TCompactVector<int, 1> is not buildable on Windows
+static_assert(TFormatTraits<TCompactVector<int, 2>>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::set<int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::map<int, int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::multimap<int, int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<THashSet<int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<THashMap<int, int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<THashMultiSet<int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::pair<int, int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<std::optional<int>>::HasCustomFormatValue);
+static_assert(TFormatTraits<TDuration>::HasCustomFormatValue);
+static_assert(TFormatTraits<TInstant>::HasCustomFormatValue);
+
+struct TUnformattable
+{ };
+static_assert(!TFormatTraits<TUnformattable>::HasCustomFormatValue);
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TFormatTest, Nothing)
+{
+ EXPECT_EQ("abc", Format("a%nb%nc", 1, 2));
+}
+
+TEST(TFormatTest, Verbatim)
+{
+ EXPECT_EQ("", Format(""));
+ EXPECT_EQ("test", Format("test"));
+ EXPECT_EQ("%", Format("%%"));
+ EXPECT_EQ("%hello%world%", Format("%%hello%%world%%"));
+}
+
+TEST(TFormatTest, MultipleArgs)
+{
+ EXPECT_EQ("2+2=4", Format("%v+%v=%v", 2, 2, 4));
+}
+
+TEST(TFormatTest, Strings)
+{
+ EXPECT_EQ("test", Format("%s", "test"));
+ EXPECT_EQ("test", Format("%s", TStringBuf("test")));
+ EXPECT_EQ("test", Format("%s", TString("test")));
+
+ EXPECT_EQ(" abc", Format("%6s", TString("abc")));
+ EXPECT_EQ("abc ", Format("%-6s", TString("abc")));
+ EXPECT_EQ(" abc", Format("%10v", TString("abc")));
+ EXPECT_EQ("abc ", Format("%-10v", TString("abc")));
+ EXPECT_EQ("abc", Format("%2s", TString("abc")));
+ EXPECT_EQ("abc", Format("%-2s", TString("abc")));
+ EXPECT_EQ("abc", Format("%0s", TString("abc")));
+ EXPECT_EQ("abc", Format("%-0s", TString("abc")));
+ EXPECT_EQ(100, std::ssize(Format("%100v", "abc")));
+}
+
+TEST(TFormatTest, Integers)
+{
+ EXPECT_EQ("123", Format("%d", 123));
+ EXPECT_EQ("123", Format("%v", 123));
+
+ EXPECT_EQ("042", Format("%03d", 42));
+ EXPECT_EQ("42", Format("%01d", 42));
+
+ EXPECT_EQ("2147483647", Format("%d", std::numeric_limits<i32>::max()));
+ EXPECT_EQ("-2147483648", Format("%d", std::numeric_limits<i32>::min()));
+
+ EXPECT_EQ("0", Format("%u", 0U));
+ EXPECT_EQ("0", Format("%v", 0U));
+ EXPECT_EQ("4294967295", Format("%u", std::numeric_limits<ui32>::max()));
+ EXPECT_EQ("4294967295", Format("%v", std::numeric_limits<ui32>::max()));
+
+ EXPECT_EQ("9223372036854775807", Format("%" PRId64, std::numeric_limits<i64>::max()));
+ EXPECT_EQ("9223372036854775807", Format("%v", std::numeric_limits<i64>::max()));
+ EXPECT_EQ("-9223372036854775808", Format("%" PRId64, std::numeric_limits<i64>::min()));
+ EXPECT_EQ("-9223372036854775808", Format("%v", std::numeric_limits<i64>::min()));
+
+ EXPECT_EQ("0", Format("%" PRIu64, 0ULL));
+ EXPECT_EQ("0", Format("%v", 0ULL));
+ EXPECT_EQ("18446744073709551615", Format("%" PRIu64, std::numeric_limits<ui64>::max()));
+ EXPECT_EQ("18446744073709551615", Format("%v", std::numeric_limits<ui64>::max()));
+}
+
+TEST(TFormatTest, Floats)
+{
+ EXPECT_EQ("3.14", Format("%.2f", 3.1415F));
+ EXPECT_EQ("3.14", Format("%.2v", 3.1415F));
+ EXPECT_EQ("3.14", Format("%.2lf", 3.1415));
+ EXPECT_EQ("3.14", Format("%.2v", 3.1415));
+ EXPECT_EQ(TString(std::to_string(std::numeric_limits<double>::max())),
+ Format("%lF", std::numeric_limits<double>::max()));
+}
+
+TEST(TFormatTest, Bool)
+{
+ EXPECT_EQ("True", Format("%v", true));
+ EXPECT_EQ("False", Format("%v", false));
+ EXPECT_EQ("true", Format("%lv", true));
+ EXPECT_EQ("false", Format("%lv", false));
+}
+
+TEST(TFormatTest, Quotes)
+{
+ EXPECT_EQ("\"True\"", Format("%Qv", true));
+ EXPECT_EQ("'False'", Format("%qv", false));
+ EXPECT_EQ("'\\\'\"'", Format("%qv", "\'\""));
+ EXPECT_EQ("\"\\x01\"", Format("%Qv", "\x1"));
+ EXPECT_EQ("'\\x1b'", Format("%qv", '\x1b'));
+}
+
+TEST(TFormatTest, Nullable)
+{
+ EXPECT_EQ("1", Format("%v", std::make_optional<int>(1)));
+ EXPECT_EQ("<null>", Format("%v", std::nullopt));
+ EXPECT_EQ("<null>", Format("%v", std::optional<int>()));
+ EXPECT_EQ("3.14", Format("%.2f", std::optional<double>(3.1415)));
+}
+
+TEST(TFormatTest, Pointers)
+{
+ // No idea if pointer format is standardized, check against Sprintf.
+ auto p = reinterpret_cast<void*>(123);
+ EXPECT_EQ(Sprintf("%p", reinterpret_cast<void*>(123)), Format("%p", p));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
diff --git a/library/cpp/yt/string/unittests/guid_ut.cpp b/library/cpp/yt/string/unittests/guid_ut.cpp
new file mode 100644
index 00000000000..4b5eebea16e
--- /dev/null
+++ b/library/cpp/yt/string/unittests/guid_ut.cpp
@@ -0,0 +1,58 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/string/guid.h>
+#include <library/cpp/yt/string/format.h>
+
+#include <util/string/hex.h>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+static_assert(TFormatTraits<TGuid>::HasCustomFormatValue);
+
+TString CanonicalToString(TGuid value)
+{
+ return Sprintf("%x-%x-%x-%x",
+ value.Parts32[3],
+ value.Parts32[2],
+ value.Parts32[1],
+ value.Parts32[0]);
+}
+
+const ui32 TrickyValues[] = {
+ 0, 0x1, 0x12, 0x123, 0x1234, 0x12345, 0x123456, 0x1234567, 0x12345678
+};
+
+TEST(TGuidTest, FormatAllTricky)
+{
+ for (ui32 a : TrickyValues) {
+ for (ui32 b : TrickyValues) {
+ for (ui32 c : TrickyValues) {
+ for (ui32 d : TrickyValues) {
+ auto value = TGuid(a, b, c, d);
+ EXPECT_EQ(CanonicalToString(value), ToString(value));
+ }
+ }
+ }
+ }
+}
+
+TEST(TGuidTest, FormatAllSymbols)
+{
+ const auto Value = TGuid::FromString("12345678-abcdef01-12345678-abcdef01");
+ EXPECT_EQ(CanonicalToString(Value), ToString(Value));
+}
+
+TEST(TGuidTest, ByteOrder)
+{
+ auto guid = TGuid::FromStringHex32("12345678ABCDEF0112345678ABCDEF01");
+ TString bytes{reinterpret_cast<const char*>(&(guid.Parts32[0])), 16};
+ EXPECT_EQ(HexEncode(bytes), "01EFCDAB7856341201EFCDAB78563412");
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
diff --git a/library/cpp/yt/string/unittests/string_ut.cpp b/library/cpp/yt/string/unittests/string_ut.cpp
new file mode 100644
index 00000000000..3e12312af0b
--- /dev/null
+++ b/library/cpp/yt/string/unittests/string_ut.cpp
@@ -0,0 +1,52 @@
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <library/cpp/yt/string/string.h>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+struct TTestCase
+{
+ const char* UnderCase;
+ const char* CamelCase;
+};
+
+static std::vector<TTestCase> TestCases {
+ { "kenny", "Kenny" },
+ { "south_park", "SouthPark" },
+ { "a", "A" },
+ { "a_b_c", "ABC" },
+ { "reed_solomon_6_3", "ReedSolomon_6_3" },
+ { "lrc_12_2_2", "Lrc_12_2_2" },
+ { "0", "0" },
+ { "0_1_2", "0_1_2" },
+ { "int64", "Int64" }
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TStringTest, UnderscoreCaseToCamelCase)
+{
+ for (const auto& testCase : TestCases) {
+ auto result = UnderscoreCaseToCamelCase(testCase.UnderCase);
+ EXPECT_STREQ(testCase.CamelCase, result.c_str())
+ << "Original: \"" << testCase.UnderCase << '"';
+ }
+}
+
+TEST(TStringTest, CamelCaseToUnderscoreCase)
+{
+ for (const auto& testCase : TestCases) {
+ auto result = CamelCaseToUnderscoreCase(testCase.CamelCase);
+ EXPECT_STREQ(testCase.UnderCase, result.c_str())
+ << "Original: \"" << testCase.CamelCase << '"';
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
+
diff --git a/library/cpp/yt/string/unittests/ya.make b/library/cpp/yt/string/unittests/ya.make
new file mode 100644
index 00000000000..9d539758d12
--- /dev/null
+++ b/library/cpp/yt/string/unittests/ya.make
@@ -0,0 +1,17 @@
+GTEST(unittester-library-string-helpers)
+
+OWNER(g:yt)
+
+SRCS(
+ enum_ut.cpp
+ format_ut.cpp
+ guid_ut.cpp
+ string_ut.cpp
+)
+
+PEERDIR(
+ library/cpp/yt/string
+ library/cpp/testing/gtest
+)
+
+END()