test_conversion.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "library/cpp/json/converter/converter.h"
  2. #include <library/cpp/json/json_reader.h>
  3. #include <library/cpp/json/json_writer.h>
  4. #include "library/cpp/json/writer/json_value.h"
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #include <util/generic/array_ref.h>
  7. #include <util/generic/deque.h>
  8. #include <util/generic/hash.h>
  9. #include <util/generic/list.h>
  10. #include <util/generic/map.h>
  11. #include <util/generic/maybe.h>
  12. namespace NJson {
  13. void AssertJsonsEqual(const TJsonValue& actualJson, const TJsonValue& expectedJson) {
  14. const auto actualString = WriteJson(actualJson, /*formatOutput*/ true, /*sortkeys*/ true, /*validateUtf8*/ true);
  15. const auto expectedString = WriteJson(expectedJson, /*formatOutput*/ true, /*sortkeys*/ true, /*validateUtf8*/ true);
  16. UNIT_ASSERT_NO_DIFF(actualString, expectedString);
  17. }
  18. void AssertJsonsEqual(const TJsonValue& actualJson, TStringBuf expectedString) {
  19. const auto expectedJson = ReadJsonFastTree(expectedString);
  20. AssertJsonsEqual(actualJson, expectedJson);
  21. }
  22. template<typename T>
  23. struct TConverterTester {
  24. using TValues = THashMap<TStringBuf, T>;
  25. static void TestEncoding(const TValues& values) {
  26. for (const auto& [serializedValue, value] : values) {
  27. const auto encodedValue = TConverter<T>::Encode(value);
  28. AssertJsonsEqual(encodedValue, serializedValue);
  29. }
  30. }
  31. static void TestDecoding(const TValues& values) {
  32. for (const auto& [serializedValue, value] : values) {
  33. const auto decodedValue = TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
  34. UNIT_ASSERT_EQUAL(decodedValue, value);
  35. }
  36. }
  37. static void TestDecodingException(TStringBuf serializedValue) {
  38. try {
  39. TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
  40. UNIT_ASSERT(false);
  41. } catch (...) {
  42. }
  43. }
  44. static void Test(const TValues& values) {
  45. TestEncoding(values);
  46. TestDecoding(values);
  47. for (const auto& [serializedValue, value] : values) {
  48. const auto encodedValue = TConverter<T>::Encode(value);
  49. const auto decodedValue = TConverter<T>::Decode(encodedValue);
  50. UNIT_ASSERT_EQUAL(value, decodedValue);
  51. }
  52. }
  53. };
  54. template<typename T>
  55. requires std::is_floating_point_v<T>
  56. struct TConverterTester<T> {
  57. using TValues = THashMap<TStringBuf, T>;
  58. static void TestDecoding(const TValues& values) {
  59. for (const auto& [serializedValue, value] : values) {
  60. {
  61. const auto decodedValue = TConverter<T>::Decode(ReadJsonFastTree(serializedValue));
  62. UNIT_ASSERT_DOUBLES_EQUAL(decodedValue, value, 0.000001);
  63. }
  64. }
  65. }
  66. static void Test(const TValues& values) {
  67. TestDecoding(values);
  68. for (const auto& [serializedValue, value] : values) {
  69. {
  70. const auto encodedValue = TConverter<T>::Encode(value);
  71. const auto decodedValue = TConverter<T>::Decode(encodedValue);
  72. UNIT_ASSERT_DOUBLES_EQUAL(decodedValue, value, 0.000001);
  73. }
  74. }
  75. }
  76. };
  77. Y_UNIT_TEST_SUITE(ConversionTests) {
  78. Y_UNIT_TEST(PrimitivesTest) {
  79. TConverterTester<bool>::Test({{"true", true}, {"false", false}});
  80. TConverterTester<ui8>::Test({{"0", 0}, {"255", 255}});
  81. TConverterTester<i8>::Test({{"-128", -128}, {"127", 127}});
  82. TConverterTester<ui16>::Test({{"0", 0}, {"65535", 65535}});
  83. TConverterTester<i16>::Test({{"-32768", -32768}, {"32767", 32767}});
  84. TConverterTester<ui32>::Test({{"0", 0}, {"4294967295", 4294967295}});
  85. TConverterTester<i32>::Test({{"-2147483648", -2147483648}, {"2147483647", 2147483647}});
  86. TConverterTester<ui64>::Test({{"0", 0}, {"18446744073709551615", 18446744073709551615u}});
  87. TConverterTester<i64>::Test({
  88. {"-9223372036854775808", -9223372036854775808u},
  89. {"9223372036854775807", 9223372036854775807},
  90. });
  91. TConverterTester<i8>::TestDecodingException("128");
  92. TConverterTester<i8>::TestDecodingException("-129");
  93. TConverterTester<ui8>::TestDecodingException("256");
  94. TConverterTester<i16>::TestDecodingException("32768");
  95. TConverterTester<i16>::TestDecodingException("-32769");
  96. TConverterTester<ui16>::TestDecodingException("65536");
  97. TConverterTester<i32>::TestDecodingException("-2147483649");
  98. TConverterTester<i32>::TestDecodingException("2147483649");
  99. TConverterTester<ui32>::TestDecodingException("4294967296");
  100. TConverterTester<unsigned char>::Test({{"0", 0}, {"255", 255}});
  101. TConverterTester<signed char>::Test({{"-128", -128}, {"127", 127}});
  102. TConverterTester<unsigned short int>::Test({{"0", 0}, {"65535", 65535}});
  103. TConverterTester<signed short int>::Test({{"-32768", -32768}, {"32767", 32767}});
  104. TConverterTester<unsigned int>::Test({{"0", 0}, {"65535", 65535}});
  105. TConverterTester<signed int>::Test({{"-32768", -32768}, {"32767", 32767}});
  106. TConverterTester<unsigned long int>::Test({{"0", 0}, {"4294967295", 4294967295}});
  107. TConverterTester<signed long int>::Test({{"-2147483648", -2147483648}, {"2147483647", 2147483647}});
  108. TConverterTester<unsigned long long int>::Test({{"0", 0}, {"18446744073709551615", 18446744073709551615u}});
  109. TConverterTester<signed long long int>::Test({
  110. {"-9223372036854775808", -9223372036854775808u},
  111. {"9223372036854775807", 9223372036854775807},
  112. });
  113. TConverterTester<size_t>::Test({{"0", 0}, {"65535", 65535}});
  114. TConverterTester<float>::Test({{"-1.12312", -1.12312}, {"3434.25674", 3434.25674}});
  115. TConverterTester<double>::Test({{"-1.12312E+42", -1.12312E+42}, {"3.25E+120", 3.25E+120}});
  116. }
  117. Y_UNIT_TEST(StringsTest) {
  118. TConverterTester<TStringBuf>::TestEncoding({
  119. {R"("Let's permit using of Rust in Arcadia")", "Let's permit using of Rust in Arcadia"},
  120. });
  121. TConverterTester<TString>::Test({
  122. {
  123. R"("Всякое непрерывное отображение замкнутого n-мерного шара в себя обладает неподвижной точкой")",
  124. "Всякое непрерывное отображение замкнутого n-мерного шара в себя обладает неподвижной точкой",
  125. },
  126. });
  127. }
  128. Y_UNIT_TEST(MaybeTest) {
  129. TConverterTester<TMaybe<bool>>::Test({
  130. {"true", TMaybe<bool>(true)},
  131. {"null", Nothing()},
  132. {"false", TMaybe<bool>(false)},
  133. });
  134. }
  135. Y_UNIT_TEST(ArraysTest) {
  136. TConverterTester<TVector<bool>>::Test({{"[true, true, false]", {true, true, false}}});
  137. TConverterTester<TList<TString>>::Test({{R"(["a", "b"])", {"a", "b"}}});
  138. TConverterTester<TDeque<bool>>::Test({{"[false, true, false]", {false, true, false}}});
  139. }
  140. Y_UNIT_TEST(MapsTest) {
  141. TConverterTester<THashMap<TStringBuf, bool>>::TestEncoding({
  142. {R"({"a": true, "b": false})", {{"a", true}, {"b", false}}},
  143. });
  144. TConverterTester<THashMap<TString, bool>>::Test({
  145. {R"({"a": true, "b": false})", {{"a", true}, {"b", false}}},
  146. });
  147. TConverterTester<TMap<TStringBuf, TStringBuf>>::TestEncoding({
  148. {R"({"a": "A", "b": "B"})", {{"a", "A"}, {"b", "B"}}},
  149. });
  150. TConverterTester<TMap<TString, TString>>::Test({
  151. {R"({"a": "A", "b": "B"})", {{"a", "A"}, {"b", "B"}}},
  152. });
  153. }
  154. Y_UNIT_TEST(NestedTest) {
  155. TConverterTester<TVector<THashMap<TString, TVector<ui64>>>>::Test({
  156. {
  157. R"([
  158. {
  159. "Three": [0, 1, 2],
  160. "Five": [0, 1, 2, 3, 4]
  161. },
  162. {
  163. "Four": [0, 1, 2, 3],
  164. "Six": [0, 1, 2, 3, 4, 5]
  165. },
  166. ])",
  167. {
  168. {
  169. {"Three", {0, 1, 2}},
  170. {"Five", {0, 1, 2, 3, 4}},
  171. },
  172. {
  173. {"Four", {0, 1, 2, 3}},
  174. {"Six", {0, 1, 2, 3, 4, 5}},
  175. },
  176. },
  177. },
  178. });
  179. }
  180. }
  181. }