writer_ut.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <library/cpp/yson_pull/scalar.h>
  2. #include <library/cpp/yson_pull/detail/writer.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/generic/string.h>
  5. #include <climits>
  6. #include <limits>
  7. using namespace std::string_view_literals;
  8. namespace {
  9. template <typename Writer, typename Function>
  10. TString with_writer(Function&& function) {
  11. TString result;
  12. auto writer = NYsonPull::NDetail::make_writer<Writer>(
  13. NYsonPull::NOutput::FromString(&result),
  14. NYsonPull::EStreamType::Node);
  15. function(writer);
  16. return result;
  17. }
  18. template <typename Writer>
  19. TString to_yson_string(const NYsonPull::TScalar& value) {
  20. return with_writer<Writer>([&](NYsonPull::TWriter& writer) {
  21. writer.BeginStream().Scalar(value).EndStream();
  22. });
  23. }
  24. template <typename T>
  25. TString to_yson_binary_string(T&& value) {
  26. return to_yson_string<NYsonPull::NDetail::TBinaryWriterImpl>(std::forward<T>(value));
  27. }
  28. template <typename T>
  29. TString to_yson_text_string(T&& value) {
  30. return to_yson_string<NYsonPull::NDetail::TTextWriterImpl>(std::forward<T>(value));
  31. }
  32. } // anonymous namespace
  33. // =================== Text format =====================
  34. Y_UNIT_TEST_SUITE(Writer) {
  35. Y_UNIT_TEST(TextEntity) {
  36. UNIT_ASSERT_VALUES_EQUAL(
  37. "#",
  38. to_yson_text_string(NYsonPull::TScalar{}));
  39. }
  40. Y_UNIT_TEST(TextBoolean) {
  41. UNIT_ASSERT_VALUES_EQUAL(
  42. "%false",
  43. to_yson_text_string(NYsonPull::TScalar{false}));
  44. UNIT_ASSERT_VALUES_EQUAL(
  45. "%true",
  46. to_yson_text_string(NYsonPull::TScalar{true}));
  47. }
  48. Y_UNIT_TEST(TextInt64) {
  49. UNIT_ASSERT_VALUES_EQUAL(
  50. "0",
  51. to_yson_text_string(NYsonPull::TScalar{i64{0}}));
  52. UNIT_ASSERT_VALUES_EQUAL(
  53. "200",
  54. to_yson_text_string(NYsonPull::TScalar{i64{200}}));
  55. UNIT_ASSERT_VALUES_EQUAL(
  56. "20000",
  57. to_yson_text_string(NYsonPull::TScalar{i64{20000}}));
  58. UNIT_ASSERT_VALUES_EQUAL(
  59. "200000000",
  60. to_yson_text_string(NYsonPull::TScalar{i64{200000000}}));
  61. UNIT_ASSERT_VALUES_EQUAL(
  62. "20000000000000000",
  63. to_yson_text_string(NYsonPull::TScalar{i64{20000000000000000}}));
  64. UNIT_ASSERT_VALUES_EQUAL(
  65. "9223372036854775807",
  66. to_yson_text_string(NYsonPull::TScalar{i64{INT64_MAX}}));
  67. UNIT_ASSERT_VALUES_EQUAL(
  68. "-200",
  69. to_yson_text_string(NYsonPull::TScalar{i64{-200}}));
  70. UNIT_ASSERT_VALUES_EQUAL(
  71. "-20000",
  72. to_yson_text_string(NYsonPull::TScalar{i64{-20000}}));
  73. UNIT_ASSERT_VALUES_EQUAL(
  74. "-200000000",
  75. to_yson_text_string(NYsonPull::TScalar{i64{-200000000}}));
  76. UNIT_ASSERT_VALUES_EQUAL(
  77. "-20000000000000000",
  78. to_yson_text_string(NYsonPull::TScalar{i64{-20000000000000000}}));
  79. UNIT_ASSERT_VALUES_EQUAL(
  80. "-9223372036854775808",
  81. to_yson_text_string(NYsonPull::TScalar{i64{INT64_MIN}}));
  82. }
  83. Y_UNIT_TEST(TextUInt64) {
  84. UNIT_ASSERT_VALUES_EQUAL(
  85. "0u",
  86. to_yson_text_string(NYsonPull::TScalar{ui64{0}}));
  87. UNIT_ASSERT_VALUES_EQUAL(
  88. "200u",
  89. to_yson_text_string(NYsonPull::TScalar{ui64{200}}));
  90. UNIT_ASSERT_VALUES_EQUAL(
  91. "20000u",
  92. to_yson_text_string(NYsonPull::TScalar{ui64{20000}}));
  93. UNIT_ASSERT_VALUES_EQUAL(
  94. "200000000u",
  95. to_yson_text_string(NYsonPull::TScalar{ui64{200000000}}));
  96. UNIT_ASSERT_VALUES_EQUAL(
  97. "20000000000000000u",
  98. to_yson_text_string(NYsonPull::TScalar{ui64{20000000000000000}}));
  99. UNIT_ASSERT_VALUES_EQUAL(
  100. "9223372036854775807u",
  101. to_yson_text_string(NYsonPull::TScalar{ui64{INT64_MAX}}));
  102. UNIT_ASSERT_VALUES_EQUAL(
  103. "18446744073709551615u",
  104. to_yson_text_string(NYsonPull::TScalar{ui64{UINT64_MAX}}));
  105. }
  106. Y_UNIT_TEST(TextFloat64) {
  107. UNIT_ASSERT_VALUES_EQUAL(
  108. "%inf",
  109. to_yson_text_string(NYsonPull::TScalar{std::numeric_limits<double>::infinity()}));
  110. UNIT_ASSERT_VALUES_EQUAL(
  111. "%-inf",
  112. to_yson_text_string(NYsonPull::TScalar{-std::numeric_limits<double>::infinity()}));
  113. UNIT_ASSERT_VALUES_EQUAL(
  114. "%nan",
  115. to_yson_text_string(NYsonPull::TScalar{std::numeric_limits<double>::quiet_NaN()}));
  116. }
  117. Y_UNIT_TEST(TextString) {
  118. UNIT_ASSERT_VALUES_EQUAL(
  119. R"("")",
  120. to_yson_text_string(NYsonPull::TScalar{""}));
  121. UNIT_ASSERT_VALUES_EQUAL(
  122. R"("hello")",
  123. to_yson_text_string(NYsonPull::TScalar{"hello"}));
  124. UNIT_ASSERT_VALUES_EQUAL(
  125. R"("hello\nworld")",
  126. to_yson_text_string(NYsonPull::TScalar{"hello\nworld"}));
  127. }
  128. // =================== Binary format =====================
  129. Y_UNIT_TEST(BinaryEntity) {
  130. UNIT_ASSERT_VALUES_EQUAL(
  131. "#",
  132. to_yson_binary_string(NYsonPull::TScalar{}));
  133. }
  134. Y_UNIT_TEST(BinaryBoolean) {
  135. UNIT_ASSERT_VALUES_EQUAL(
  136. TStringBuf("\x4"),
  137. to_yson_binary_string(NYsonPull::TScalar{false}));
  138. UNIT_ASSERT_VALUES_EQUAL(
  139. TStringBuf("\x5"),
  140. to_yson_binary_string(NYsonPull::TScalar{true}));
  141. }
  142. Y_UNIT_TEST(BinaryInt64) {
  143. UNIT_ASSERT_VALUES_EQUAL(
  144. TStringBuf("\x2\0"sv),
  145. to_yson_binary_string(NYsonPull::TScalar{i64{0}}));
  146. UNIT_ASSERT_VALUES_EQUAL(
  147. TStringBuf("\x2\x90\x3"),
  148. to_yson_binary_string(NYsonPull::TScalar{i64{200}}));
  149. UNIT_ASSERT_VALUES_EQUAL(
  150. TStringBuf("\x2\xC0\xB8\x2"),
  151. to_yson_binary_string(NYsonPull::TScalar{i64{20000}}));
  152. UNIT_ASSERT_VALUES_EQUAL(
  153. TStringBuf("\x2\x80\x88\xDE\xBE\x1"),
  154. to_yson_binary_string(NYsonPull::TScalar{i64{200000000}}));
  155. UNIT_ASSERT_VALUES_EQUAL(
  156. TStringBuf("\x2\x80\x80\x90\xF8\x9B\xF9\x86G"),
  157. to_yson_binary_string(NYsonPull::TScalar{i64{20000000000000000}}));
  158. UNIT_ASSERT_VALUES_EQUAL(
  159. TStringBuf("\x2\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
  160. to_yson_binary_string(NYsonPull::TScalar{i64{INT64_MAX}}));
  161. UNIT_ASSERT_VALUES_EQUAL(
  162. TStringBuf("\x2\x8F\x3"),
  163. to_yson_binary_string(NYsonPull::TScalar{i64{-200}}));
  164. UNIT_ASSERT_VALUES_EQUAL(
  165. TStringBuf("\x2\xBF\xB8\x2"),
  166. to_yson_binary_string(NYsonPull::TScalar{i64{-20000}}));
  167. UNIT_ASSERT_VALUES_EQUAL(
  168. TStringBuf("\x2\xFF\x87\xDE\xBE\x1"),
  169. to_yson_binary_string(NYsonPull::TScalar{i64{-200000000}}));
  170. UNIT_ASSERT_VALUES_EQUAL(
  171. TStringBuf("\x2\xFF\xFF\x8F\xF8\x9B\xF9\x86G"),
  172. to_yson_binary_string(NYsonPull::TScalar{i64{-20000000000000000}}));
  173. UNIT_ASSERT_VALUES_EQUAL(
  174. TStringBuf("\x2\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
  175. to_yson_binary_string(NYsonPull::TScalar{i64{INT64_MIN}}));
  176. }
  177. Y_UNIT_TEST(BinaryUInt64) {
  178. UNIT_ASSERT_VALUES_EQUAL(
  179. TStringBuf("\x6\0"sv),
  180. to_yson_binary_string(NYsonPull::TScalar{ui64{0}}));
  181. UNIT_ASSERT_VALUES_EQUAL(
  182. TStringBuf("\x6\xC8\x1"),
  183. to_yson_binary_string(NYsonPull::TScalar{ui64{200}}));
  184. UNIT_ASSERT_VALUES_EQUAL(
  185. TStringBuf("\x6\xA0\x9C\x1"),
  186. to_yson_binary_string(NYsonPull::TScalar{ui64{20000}}));
  187. UNIT_ASSERT_VALUES_EQUAL(
  188. TStringBuf("\x6\x80\x84\xAF_"),
  189. to_yson_binary_string(NYsonPull::TScalar{ui64{200000000}}));
  190. UNIT_ASSERT_VALUES_EQUAL(
  191. TStringBuf("\x6\x80\x80\x88\xFC\xCD\xBC\xC3#"),
  192. to_yson_binary_string(NYsonPull::TScalar{ui64{20000000000000000}}));
  193. UNIT_ASSERT_VALUES_EQUAL(
  194. TStringBuf("\x6\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F"),
  195. to_yson_binary_string(NYsonPull::TScalar{ui64{INT64_MAX}}));
  196. UNIT_ASSERT_VALUES_EQUAL(
  197. TStringBuf("\x6\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1"),
  198. to_yson_binary_string(NYsonPull::TScalar{ui64{UINT64_MAX}}));
  199. }
  200. Y_UNIT_TEST(BinaryFloat64) {
  201. UNIT_ASSERT_VALUES_EQUAL(
  202. TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf0\x7f"sv),
  203. to_yson_binary_string(NYsonPull::TScalar{std::numeric_limits<double>::infinity()}));
  204. UNIT_ASSERT_VALUES_EQUAL(
  205. TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf0\xff"sv),
  206. to_yson_binary_string(NYsonPull::TScalar{-std::numeric_limits<double>::infinity()}));
  207. UNIT_ASSERT_VALUES_EQUAL(
  208. TStringBuf("\x03\x00\x00\x00\x00\x00\x00\xf8\x7f"sv),
  209. to_yson_binary_string(NYsonPull::TScalar{std::numeric_limits<double>::quiet_NaN()}));
  210. UNIT_ASSERT_VALUES_EQUAL(
  211. TStringBuf("\x03\x9a\x99\x99\x99\x99\x99\xf1\x3f"),
  212. to_yson_binary_string(NYsonPull::TScalar{double{1.1}}));
  213. UNIT_ASSERT_VALUES_EQUAL(
  214. TStringBuf("\x03\x9a\x99\x99\x99\x99\x99\xf1\xbf"),
  215. to_yson_binary_string(NYsonPull::TScalar{double{-1.1}}));
  216. }
  217. Y_UNIT_TEST(BinaryString) {
  218. UNIT_ASSERT_VALUES_EQUAL(
  219. TStringBuf("\x1\0"sv),
  220. to_yson_binary_string(NYsonPull::TScalar{""}));
  221. UNIT_ASSERT_VALUES_EQUAL(
  222. TStringBuf("\x1\nhello"),
  223. to_yson_binary_string(NYsonPull::TScalar{"hello"}));
  224. UNIT_ASSERT_VALUES_EQUAL(
  225. TStringBuf("\x1\x16hello\nworld"),
  226. to_yson_binary_string(NYsonPull::TScalar{"hello\nworld"}));
  227. }
  228. } // Y_UNIT_TEST_SUITE(Writer)