iconv_ut.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "wide.h"
  2. #include "recyr.hh"
  3. #include "codepage.h"
  4. #include <library/cpp/testing/unittest/registar.h>
  5. static void TestIconv(const TString& utf8, const TString& other, ECharset enc) {
  6. TUtf16String wide0 = CharToWide(utf8, CODES_UTF8);
  7. TUtf16String wide1 = CharToWide(other, enc);
  8. UNIT_ASSERT(wide0 == wide1);
  9. TString temp = WideToUTF8(wide0);
  10. UNIT_ASSERT(temp == utf8);
  11. temp = WideToChar(wide0, enc);
  12. UNIT_ASSERT(temp == other);
  13. temp = Recode(enc, CODES_UTF8, other);
  14. UNIT_ASSERT(temp == utf8);
  15. temp = Recode(CODES_UTF8, enc, utf8);
  16. UNIT_ASSERT(temp == other);
  17. size_t read = 0;
  18. size_t written = 0;
  19. RECODE_RESULT res = RecodeToUnicode(enc, other.c_str(), wide1.begin(), other.size(), wide1.size(), read, written);
  20. UNIT_ASSERT(res == RECODE_OK);
  21. UNIT_ASSERT(read == other.size());
  22. UNIT_ASSERT(written == wide1.size());
  23. UNIT_ASSERT(wide0 == wide1);
  24. res = RecodeFromUnicode(enc, wide0.c_str(), temp.begin(), wide0.size(), temp.size(), read, written);
  25. UNIT_ASSERT(res == RECODE_OK);
  26. UNIT_ASSERT(read == wide0.size());
  27. UNIT_ASSERT(written == other.size());
  28. UNIT_ASSERT(temp == other);
  29. }
  30. class TIconvTest: public TTestBase {
  31. static void TestSurrogates(const char* str, const wchar16* wide, size_t wideSize) {
  32. size_t sSize = strlen(str);
  33. size_t wSize = sSize * 2;
  34. TArrayHolder<wchar16> w(new wchar16[wSize]);
  35. size_t read = 0;
  36. size_t written = 0;
  37. NICONVPrivate::RecodeToUnicode(CODES_UTF8, str, w.Get(), sSize, wSize, read, written);
  38. UNIT_ASSERT(read == sSize);
  39. UNIT_ASSERT(written == wideSize);
  40. UNIT_ASSERT(!memcmp(w.Get(), wide, wideSize));
  41. TArrayHolder<char> s(new char[sSize]);
  42. NICONVPrivate::RecodeFromUnicode(CODES_UTF8, w.Get(), s.Get(), wideSize, sSize, read, written);
  43. UNIT_ASSERT(read == wideSize);
  44. UNIT_ASSERT(written == sSize);
  45. UNIT_ASSERT(!memcmp(s.Get(), str, sSize));
  46. }
  47. private:
  48. UNIT_TEST_SUITE(TIconvTest);
  49. UNIT_TEST(TestBig5);
  50. UNIT_TEST(TestSurrogatePairs);
  51. UNIT_TEST_SUITE_END();
  52. public:
  53. void TestBig5() {
  54. UNIT_ASSERT(!NCodepagePrivate::NativeCodepage(CODES_BIG5));
  55. const char* UTF8 = "\xe5\xad\xb8\xe7\x94\x9f\xe7\xb8\xbd\xe4\xba\xba\xe6\x95\xb8\xe6\x99\xae\xe9\x80\x9a\xe7\x8f\xad";
  56. const char* BIG5 = "\xbe\xc7\xa5\xcd\xc1\x60\xa4\x48\xbc\xc6\xb4\xb6\xb3\x71\xaf\x5a";
  57. TestIconv(UTF8, BIG5, CODES_BIG5);
  58. }
  59. void TestSurrogatePairs() {
  60. const char* utf8NonBMP = "\xf4\x80\x89\x84\xf4\x80\x89\x87\xf4\x80\x88\xba";
  61. wchar16 wNonBMPDummy[] = {0xDBC0, 0xDE44, 0xDBC0, 0xDE47, 0xDBC0, 0xDE3A};
  62. TestSurrogates(utf8NonBMP, wNonBMPDummy, Y_ARRAY_SIZE(wNonBMPDummy));
  63. const char* utf8NonBMP2 = "ab\xf4\x80\x89\x87n";
  64. wchar16 wNonBMPDummy2[] = {'a', 'b', 0xDBC0, 0xDE47, 'n'};
  65. TestSurrogates(utf8NonBMP2, wNonBMPDummy2, Y_ARRAY_SIZE(wNonBMPDummy2));
  66. }
  67. };
  68. UNIT_TEST_SUITE_REGISTRATION(TIconvTest);