py_string_ut.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "ut3/py_test_engine.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NPython;
  4. Y_UNIT_TEST_SUITE(TPyStringTest) {
  5. template <typename TStringType>
  6. void TestStringCasts() {
  7. TStringType testStr1(TStringBuf("test string"));
  8. TStringBuf strBuf1 = testStr1;
  9. TPyObjectPtr str1 = PyBytes_FromString(strBuf1.data());
  10. const auto value = PyCast<TStringType>(str1.Get());
  11. UNIT_ASSERT_STRINGS_EQUAL(value, testStr1);
  12. TStringType testStr2(TStringBuf("another test string"));
  13. TStringBuf strBuf2 = testStr2;
  14. TPyObjectPtr str2 = PyCast<TStringType>(testStr2);
  15. Py_ssize_t size = 0U;
  16. char* buf = nullptr;
  17. const auto rc = PyBytes_AsStringAndSize(str2.Get(), &buf, &size);
  18. UNIT_ASSERT(rc >= 0);
  19. UNIT_ASSERT(buf != nullptr);
  20. UNIT_ASSERT_EQUAL(static_cast<size_t>(size), strBuf2.size());
  21. UNIT_ASSERT_STRINGS_EQUAL(buf, testStr2);
  22. }
  23. template <typename TStringType>
  24. void TestBinaryStringCasts() {
  25. TStringType testStr1(TStringBuf("\xa0\xa1"sv));
  26. TStringBuf strBuf1 = testStr1;
  27. TPyObjectPtr str1 = PyBytes_FromString(strBuf1.data());
  28. const auto value = PyCast<TStringType>(str1.Get());
  29. UNIT_ASSERT_STRINGS_EQUAL(value, testStr1);
  30. TStringType testStr2(TStringBuf("\xf0\x90\x28\xbc"sv));
  31. TStringBuf strBuf2 = testStr2;
  32. TPyObjectPtr str2 = PyCast<TStringType>(testStr2);
  33. Py_ssize_t size = 0U;
  34. char* buf = nullptr;
  35. const auto rc = PyBytes_AsStringAndSize(str2.Get(), &buf, &size);
  36. UNIT_ASSERT(rc >= 0);
  37. UNIT_ASSERT(buf != nullptr);
  38. UNIT_ASSERT_EQUAL(static_cast<size_t>(size), strBuf2.size());
  39. UNIT_ASSERT_STRINGS_EQUAL(buf, testStr2);
  40. }
  41. template <typename TStringType>
  42. void TestUtf8StringCasts() {
  43. const TStringType testStr1(TStringBuf("тестовая строка"));
  44. TStringBuf strBuf1 = testStr1;
  45. const TPyObjectPtr str1 = PyUnicode_FromString(strBuf1.data());
  46. const TPyObjectPtr utf8 = PyUnicode_AsUTF8String(str1.Get());
  47. const auto value = PyCast<TStringType>(utf8.Get());
  48. UNIT_ASSERT_STRINGS_EQUAL(value, testStr1);
  49. const TStringType testStr2(TStringBuf("еще одна тестовая строка"));
  50. TStringBuf strBuf2 = testStr2;
  51. const auto str2 = ToPyUnicode<TStringType>(testStr2);
  52. UNIT_ASSERT(PyUnicode_Check(str2.Get()));
  53. Py_ssize_t size = 0U;
  54. #if PY_MAJOR_VERSION >= 3
  55. const auto buf = PyUnicode_AsUTF8AndSize(str2.Get(), &size);
  56. #else
  57. char* buf = nullptr;
  58. const TPyObjectPtr pyUtf8Str = PyUnicode_AsUTF8String(str2.Get());
  59. const auto rc = PyBytes_AsStringAndSize(pyUtf8Str.Get(), &buf, &size);
  60. UNIT_ASSERT(rc >= 0);
  61. #endif
  62. UNIT_ASSERT(buf != nullptr);
  63. UNIT_ASSERT_EQUAL(static_cast<size_t>(size), strBuf2.size());
  64. UNIT_ASSERT_STRINGS_EQUAL(buf, testStr2);
  65. }
  66. Y_UNIT_TEST(Simple) {
  67. TestStringCasts<TString>();
  68. TestStringCasts<TStringBuf>();
  69. TestStringCasts<NUdf::TStringRef>();
  70. }
  71. Y_UNIT_TEST(Utf8) {
  72. TestUtf8StringCasts<TString>();
  73. TestUtf8StringCasts<TStringBuf>();
  74. TestUtf8StringCasts<NUdf::TStringRef>();
  75. }
  76. Y_UNIT_TEST(Binary) {
  77. TestBinaryStringCasts<TString>();
  78. TestBinaryStringCasts<TStringBuf>();
  79. TestBinaryStringCasts<NUdf::TStringRef>();
  80. }
  81. }