cescape_ut.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <library/cpp/yson_pull/detail/cescape.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NYsonPull::NDetail;
  4. namespace {
  5. void test_roundtrip(const TVector<ui8>& str) {
  6. TStringBuf str_buf(
  7. reinterpret_cast<const char*>(str.data()),
  8. str.size());
  9. auto tmp = NCEscape::encode(str_buf);
  10. auto dest = NCEscape::decode(tmp);
  11. UNIT_ASSERT_VALUES_EQUAL_C(
  12. str_buf, TStringBuf(dest),
  13. "A[" << str.size() << "]: " << str_buf << '\n'
  14. << "B[" << tmp.size() << "]: " << tmp << '\n'
  15. << "C[" << dest.size() << "]: " << dest);
  16. }
  17. template <size_t N>
  18. void test_exhaustive(TVector<ui8>& str) {
  19. for (int i = 0; i < 256; ++i) {
  20. str[str.size() - N] = static_cast<char>(i);
  21. test_exhaustive<N - 1>(str);
  22. }
  23. }
  24. template <>
  25. void test_exhaustive<0>(TVector<ui8>& str) {
  26. test_roundtrip(str);
  27. }
  28. template <size_t N>
  29. void test_exhaustive() {
  30. TVector<ui8> str(N, ' ');
  31. test_exhaustive<N>(str);
  32. }
  33. } // anonymous namespace
  34. Y_UNIT_TEST_SUITE(CEscape) {
  35. Y_UNIT_TEST(ExhaustiveOneChar) {
  36. test_exhaustive<1>();
  37. }
  38. Y_UNIT_TEST(ExhaustiveTwoChars) {
  39. test_exhaustive<2>();
  40. }
  41. Y_UNIT_TEST(ExhaustiveThreeChars) {
  42. test_exhaustive<3>();
  43. }
  44. Y_UNIT_TEST(SpecialEscapeEncode) {
  45. //UNIT_ASSERT_VALUES_EQUAL(R"(\b)", NCEscape::encode("\b"));
  46. //UNIT_ASSERT_VALUES_EQUAL(R"(\f)", NCEscape::encode("\f"));
  47. UNIT_ASSERT_VALUES_EQUAL(R"(\n)", NCEscape::encode("\n"));
  48. UNIT_ASSERT_VALUES_EQUAL(R"(\r)", NCEscape::encode("\r"));
  49. UNIT_ASSERT_VALUES_EQUAL(R"(\t)", NCEscape::encode("\t"));
  50. }
  51. Y_UNIT_TEST(SpecialEscapeDecode) {
  52. UNIT_ASSERT_VALUES_EQUAL("\b", NCEscape::decode(R"(\b)"));
  53. UNIT_ASSERT_VALUES_EQUAL("\f", NCEscape::decode(R"(\f)"));
  54. UNIT_ASSERT_VALUES_EQUAL("\n", NCEscape::decode(R"(\n)"));
  55. UNIT_ASSERT_VALUES_EQUAL("\r", NCEscape::decode(R"(\r)"));
  56. UNIT_ASSERT_VALUES_EQUAL("\t", NCEscape::decode(R"(\t)"));
  57. }
  58. } // Y_UNIT_TEST_SUITE(CEscape)