format_ut.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/string/format.h>
  3. #include <library/cpp/yt/small_containers/compact_vector.h>
  4. #include <limits>
  5. namespace NYT {
  6. namespace {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. // Some compile-time sanity checks.
  9. static_assert(TFormatTraits<int>::HasCustomFormatValue);
  10. static_assert(TFormatTraits<double>::HasCustomFormatValue);
  11. static_assert(TFormatTraits<void*>::HasCustomFormatValue);
  12. static_assert(TFormatTraits<const char*>::HasCustomFormatValue);
  13. static_assert(TFormatTraits<TStringBuf>::HasCustomFormatValue);
  14. static_assert(TFormatTraits<TString>::HasCustomFormatValue);
  15. static_assert(TFormatTraits<std::span<int>>::HasCustomFormatValue);
  16. static_assert(TFormatTraits<std::vector<int>>::HasCustomFormatValue);
  17. // N.B. TCompactVector<int, 1> is not buildable on Windows
  18. static_assert(TFormatTraits<TCompactVector<int, 2>>::HasCustomFormatValue);
  19. static_assert(TFormatTraits<std::set<int>>::HasCustomFormatValue);
  20. static_assert(TFormatTraits<std::map<int, int>>::HasCustomFormatValue);
  21. static_assert(TFormatTraits<std::multimap<int, int>>::HasCustomFormatValue);
  22. static_assert(TFormatTraits<THashSet<int>>::HasCustomFormatValue);
  23. static_assert(TFormatTraits<THashMap<int, int>>::HasCustomFormatValue);
  24. static_assert(TFormatTraits<THashMultiSet<int>>::HasCustomFormatValue);
  25. static_assert(TFormatTraits<std::pair<int, int>>::HasCustomFormatValue);
  26. static_assert(TFormatTraits<std::optional<int>>::HasCustomFormatValue);
  27. static_assert(TFormatTraits<TDuration>::HasCustomFormatValue);
  28. static_assert(TFormatTraits<TInstant>::HasCustomFormatValue);
  29. struct TUnformattable
  30. { };
  31. static_assert(!TFormatTraits<TUnformattable>::HasCustomFormatValue);
  32. ////////////////////////////////////////////////////////////////////////////////
  33. TEST(TFormatTest, Nothing)
  34. {
  35. EXPECT_EQ("abc", Format("a%nb%nc", 1, 2));
  36. }
  37. TEST(TFormatTest, Verbatim)
  38. {
  39. EXPECT_EQ("", Format(""));
  40. EXPECT_EQ("test", Format("test"));
  41. EXPECT_EQ("%", Format("%%"));
  42. EXPECT_EQ("%hello%world%", Format("%%hello%%world%%"));
  43. }
  44. TEST(TFormatTest, MultipleArgs)
  45. {
  46. EXPECT_EQ("2+2=4", Format("%v+%v=%v", 2, 2, 4));
  47. }
  48. TEST(TFormatTest, Strings)
  49. {
  50. EXPECT_EQ("test", Format("%s", "test"));
  51. EXPECT_EQ("test", Format("%s", TStringBuf("test")));
  52. EXPECT_EQ("test", Format("%s", TString("test")));
  53. EXPECT_EQ(" abc", Format("%6s", TString("abc")));
  54. EXPECT_EQ("abc ", Format("%-6s", TString("abc")));
  55. EXPECT_EQ(" abc", Format("%10v", TString("abc")));
  56. EXPECT_EQ("abc ", Format("%-10v", TString("abc")));
  57. EXPECT_EQ("abc", Format("%2s", TString("abc")));
  58. EXPECT_EQ("abc", Format("%-2s", TString("abc")));
  59. EXPECT_EQ("abc", Format("%0s", TString("abc")));
  60. EXPECT_EQ("abc", Format("%-0s", TString("abc")));
  61. EXPECT_EQ(100, std::ssize(Format("%100v", "abc")));
  62. }
  63. TEST(TFormatTest, DecIntegers)
  64. {
  65. EXPECT_EQ("123", Format("%d", 123));
  66. EXPECT_EQ("123", Format("%v", 123));
  67. EXPECT_EQ("042", Format("%03d", 42));
  68. EXPECT_EQ("42", Format("%01d", 42));
  69. EXPECT_EQ("2147483647", Format("%d", std::numeric_limits<i32>::max()));
  70. EXPECT_EQ("-2147483648", Format("%d", std::numeric_limits<i32>::min()));
  71. EXPECT_EQ("0", Format("%u", 0U));
  72. EXPECT_EQ("0", Format("%v", 0U));
  73. EXPECT_EQ("4294967295", Format("%u", std::numeric_limits<ui32>::max()));
  74. EXPECT_EQ("4294967295", Format("%v", std::numeric_limits<ui32>::max()));
  75. EXPECT_EQ("9223372036854775807", Format("%" PRId64, std::numeric_limits<i64>::max()));
  76. EXPECT_EQ("9223372036854775807", Format("%v", std::numeric_limits<i64>::max()));
  77. EXPECT_EQ("-9223372036854775808", Format("%" PRId64, std::numeric_limits<i64>::min()));
  78. EXPECT_EQ("-9223372036854775808", Format("%v", std::numeric_limits<i64>::min()));
  79. EXPECT_EQ("0", Format("%" PRIu64, 0ULL));
  80. EXPECT_EQ("0", Format("%v", 0ULL));
  81. EXPECT_EQ("18446744073709551615", Format("%" PRIu64, std::numeric_limits<ui64>::max()));
  82. EXPECT_EQ("18446744073709551615", Format("%v", std::numeric_limits<ui64>::max()));
  83. }
  84. TEST(TFormatTest, HexIntegers)
  85. {
  86. EXPECT_EQ("7b", Format("%x", 123));
  87. EXPECT_EQ("7B", Format("%X", 123));
  88. EXPECT_EQ("02a", Format("%03x", 42));
  89. EXPECT_EQ("2a", Format("%01x", 42));
  90. EXPECT_EQ("7fffffff", Format("%x", std::numeric_limits<i32>::max()));
  91. EXPECT_EQ("-80000000", Format("%x", std::numeric_limits<i32>::min()));
  92. EXPECT_EQ("0", Format("%x", 0U));
  93. EXPECT_EQ("0", Format("%X", 0U));
  94. EXPECT_EQ("ffffffff", Format("%x", std::numeric_limits<ui32>::max()));
  95. EXPECT_EQ("7fffffffffffffff", Format("%x", std::numeric_limits<i64>::max()));
  96. EXPECT_EQ("-8000000000000000", Format("%x", std::numeric_limits<i64>::min()));
  97. EXPECT_EQ("0", Format("%x", 0ULL));
  98. EXPECT_EQ("ffffffffffffffff", Format("%x", std::numeric_limits<ui64>::max()));
  99. }
  100. TEST(TFormatTest, Floats)
  101. {
  102. EXPECT_EQ("3.14", Format("%.2f", 3.1415F));
  103. EXPECT_EQ("3.14", Format("%.2v", 3.1415F));
  104. EXPECT_EQ("3.14", Format("%.2lf", 3.1415));
  105. EXPECT_EQ("3.14", Format("%.2v", 3.1415));
  106. EXPECT_EQ(TString(std::to_string(std::numeric_limits<double>::max())),
  107. Format("%lF", std::numeric_limits<double>::max()));
  108. }
  109. TEST(TFormatTest, Bool)
  110. {
  111. EXPECT_EQ("True", Format("%v", true));
  112. EXPECT_EQ("False", Format("%v", false));
  113. EXPECT_EQ("true", Format("%lv", true));
  114. EXPECT_EQ("false", Format("%lv", false));
  115. }
  116. TEST(TFormatTest, Quotes)
  117. {
  118. EXPECT_EQ("\"True\"", Format("%Qv", true));
  119. EXPECT_EQ("'False'", Format("%qv", false));
  120. EXPECT_EQ("'\\\'\"'", Format("%qv", "\'\""));
  121. EXPECT_EQ("\"\\x01\"", Format("%Qv", "\x1"));
  122. EXPECT_EQ("'\\x1b'", Format("%qv", '\x1b'));
  123. EXPECT_EQ("'\\\\'", Format("%qv", '\\'));
  124. EXPECT_EQ("'\\n'", Format("%qv", '\n'));
  125. EXPECT_EQ("'\\t'", Format("%qv", '\t'));
  126. EXPECT_EQ("'\\\''", Format("%qv", '\''));
  127. EXPECT_EQ("\"'\"", Format("%Qv", '\''));
  128. EXPECT_EQ("'\"'", Format("%qv", '\"'));
  129. EXPECT_EQ("\"\\\"\"", Format("%Qv", '\"'));
  130. }
  131. TEST(TFormatTest, Escape)
  132. {
  133. EXPECT_EQ("\'\"", Format("%hv", "\'\""));
  134. EXPECT_EQ("\\x01", Format("%hv", "\x1"));
  135. EXPECT_EQ("\\x1b", Format("%hv", '\x1b'));
  136. EXPECT_EQ("\\\\", Format("%hv", '\\'));
  137. EXPECT_EQ("\\n", Format("%hv", '\n'));
  138. EXPECT_EQ("\\t", Format("%hv", '\t'));
  139. EXPECT_EQ("\'", Format("%hv", '\''));
  140. }
  141. TEST(TFormatTest, Nullable)
  142. {
  143. EXPECT_EQ("1", Format("%v", std::make_optional<int>(1)));
  144. EXPECT_EQ("<null>", Format("%v", std::nullopt));
  145. EXPECT_EQ("<null>", Format("%v", std::optional<int>()));
  146. EXPECT_EQ("3.14", Format("%.2f", std::optional<double>(3.1415)));
  147. }
  148. TEST(TFormatTest, Pointers)
  149. {
  150. {
  151. auto ptr = reinterpret_cast<void*>(0x12345678);
  152. EXPECT_EQ("0x12345678", Format("%p", ptr));
  153. EXPECT_EQ("0x12345678", Format("%v", ptr));
  154. EXPECT_EQ("12345678", Format("%x", ptr));
  155. EXPECT_EQ("12345678", Format("%X", ptr));
  156. }
  157. {
  158. auto ptr = reinterpret_cast<void*>(0x12345678abcdefab);
  159. EXPECT_EQ("0x12345678abcdefab", Format("%p", ptr));
  160. EXPECT_EQ("0x12345678abcdefab", Format("%v", ptr));
  161. EXPECT_EQ("12345678abcdefab", Format("%x", ptr));
  162. EXPECT_EQ("12345678ABCDEFAB", Format("%X", ptr));
  163. }
  164. }
  165. TEST(TFormatTest, LazyMultiValueFormatter)
  166. {
  167. int i = 1;
  168. TString s = "hello";
  169. std::vector<int> range{1, 2, 3};
  170. auto lazyFormatter = MakeLazyMultiValueFormatter(
  171. "int: %v, string: %v, range: %v",
  172. i,
  173. s,
  174. MakeFormattableView(range, TDefaultFormatter{}));
  175. EXPECT_EQ("int: 1, string: hello, range: [1, 2, 3]", Format("%v", lazyFormatter));
  176. }
  177. ////////////////////////////////////////////////////////////////////////////////
  178. } // namespace
  179. } // namespace NYT