sys_string_conversions_unittest.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include "butil/basictypes.h"
  6. #include "butil/strings/string_piece.h"
  7. #include "butil/strings/sys_string_conversions.h"
  8. #include "butil/strings/utf_string_conversions.h"
  9. #include "scoped_locale.h"
  10. #include <gtest/gtest.h>
  11. #ifdef WCHAR_T_IS_UTF32
  12. static const std::wstring kSysWideOldItalicLetterA = L"\x10300";
  13. #else
  14. static const std::wstring kSysWideOldItalicLetterA = L"\xd800\xdf00";
  15. #endif
  16. namespace butil {
  17. TEST(SysStrings, SysWideToUTF8) {
  18. EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world"));
  19. EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d"));
  20. // >16 bits
  21. EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(kSysWideOldItalicLetterA));
  22. // Error case. When Windows finds a UTF-16 character going off the end of
  23. // a string, it just converts that literal value to UTF-8, even though this
  24. // is invalid.
  25. //
  26. // This is what XP does, but Vista has different behavior, so we don't bother
  27. // verifying it:
  28. // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw",
  29. // SysWideToUTF8(L"\x4f60\xd800zyxw"));
  30. // Test embedded NULLs.
  31. std::wstring wide_null(L"a");
  32. wide_null.push_back(0);
  33. wide_null.push_back('b');
  34. std::string expected_null("a");
  35. expected_null.push_back(0);
  36. expected_null.push_back('b');
  37. EXPECT_EQ(expected_null, SysWideToUTF8(wide_null));
  38. }
  39. TEST(SysStrings, SysUTF8ToWide) {
  40. EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world"));
  41. EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd"));
  42. // >16 bits
  43. EXPECT_EQ(kSysWideOldItalicLetterA, SysUTF8ToWide("\xF0\x90\x8C\x80"));
  44. // Error case. When Windows finds an invalid UTF-8 character, it just skips
  45. // it. This seems weird because it's inconsistent with the reverse conversion.
  46. //
  47. // This is what XP does, but Vista has different behavior, so we don't bother
  48. // verifying it:
  49. // EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw"));
  50. // Test embedded NULLs.
  51. std::string utf8_null("a");
  52. utf8_null.push_back(0);
  53. utf8_null.push_back('b');
  54. std::wstring expected_null(L"a");
  55. expected_null.push_back(0);
  56. expected_null.push_back('b');
  57. EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null));
  58. }
  59. // FIXME(gejun): Following cases fail due to failure of setlocale
  60. #if 0 //defined(OS_LINUX) // Tests depend on setting a specific Linux locale.
  61. TEST(SysStrings, SysWideToNativeMB) {
  62. ScopedLocale locale("en_US.utf-8");
  63. EXPECT_EQ("Hello, world", SysWideToNativeMB(L"Hello, world"));
  64. EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToNativeMB(L"\x4f60\x597d"));
  65. // >16 bits
  66. EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToNativeMB(kSysWideOldItalicLetterA));
  67. // Error case. When Windows finds a UTF-16 character going off the end of
  68. // a string, it just converts that literal value to UTF-8, even though this
  69. // is invalid.
  70. //
  71. // This is what XP does, but Vista has different behavior, so we don't bother
  72. // verifying it:
  73. // EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw",
  74. // SysWideToNativeMB(L"\x4f60\xd800zyxw"));
  75. // Test embedded NULLs.
  76. std::wstring wide_null(L"a");
  77. wide_null.push_back(0);
  78. wide_null.push_back('b');
  79. std::string expected_null("a");
  80. expected_null.push_back(0);
  81. expected_null.push_back('b');
  82. EXPECT_EQ(expected_null, SysWideToNativeMB(wide_null));
  83. }
  84. // We assume the test is running in a UTF8 locale.
  85. TEST(SysStrings, SysNativeMBToWide) {
  86. ScopedLocale locale("en_US.utf-8");
  87. EXPECT_EQ(L"Hello, world", SysNativeMBToWide("Hello, world"));
  88. EXPECT_EQ(L"\x4f60\x597d", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5\xbd"));
  89. // >16 bits
  90. EXPECT_EQ(kSysWideOldItalicLetterA, SysNativeMBToWide("\xF0\x90\x8C\x80"));
  91. // Error case. When Windows finds an invalid UTF-8 character, it just skips
  92. // it. This seems weird because it's inconsistent with the reverse conversion.
  93. //
  94. // This is what XP does, but Vista has different behavior, so we don't bother
  95. // verifying it:
  96. // EXPECT_EQ(L"\x4f60zyxw", SysNativeMBToWide("\xe4\xbd\xa0\xe5\xa5zyxw"));
  97. // Test embedded NULLs.
  98. std::string utf8_null("a");
  99. utf8_null.push_back(0);
  100. utf8_null.push_back('b');
  101. std::wstring expected_null(L"a");
  102. expected_null.push_back(0);
  103. expected_null.push_back('b');
  104. EXPECT_EQ(expected_null, SysNativeMBToWide(utf8_null));
  105. }
  106. static const wchar_t* const kConvertRoundtripCases[] = {
  107. L"Google Video",
  108. // "网页 图片 资讯更多 »"
  109. L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb",
  110. // "Παγκόσμιος Ιστός"
  111. L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
  112. L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2",
  113. // "Поиск страниц на русском"
  114. L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442"
  115. L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430"
  116. L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c",
  117. // "전체서비스"
  118. L"\xc804\xccb4\xc11c\xbe44\xc2a4",
  119. // Test characters that take more than 16 bits. This will depend on whether
  120. // wchar_t is 16 or 32 bits.
  121. #if defined(WCHAR_T_IS_UTF16)
  122. L"\xd800\xdf00",
  123. // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E)
  124. L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44",
  125. #elif defined(WCHAR_T_IS_UTF32)
  126. L"\x10300",
  127. // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E)
  128. L"\x11d40\x11d41\x11d42\x11d43\x11d44",
  129. #endif
  130. };
  131. TEST(SysStrings, SysNativeMBAndWide) {
  132. ScopedLocale locale("en_US.utf-8");
  133. for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) {
  134. std::wstring wide = kConvertRoundtripCases[i];
  135. std::wstring trip = SysNativeMBToWide(SysWideToNativeMB(wide));
  136. EXPECT_EQ(wide.size(), trip.size());
  137. EXPECT_EQ(wide, trip);
  138. }
  139. // We assume our test is running in UTF-8, so double check through ICU.
  140. for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) {
  141. std::wstring wide = kConvertRoundtripCases[i];
  142. std::wstring trip = SysNativeMBToWide(WideToUTF8(wide));
  143. EXPECT_EQ(wide.size(), trip.size());
  144. EXPECT_EQ(wide, trip);
  145. }
  146. for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) {
  147. std::wstring wide = kConvertRoundtripCases[i];
  148. std::wstring trip = UTF8ToWide(SysWideToNativeMB(wide));
  149. EXPECT_EQ(wide.size(), trip.size());
  150. EXPECT_EQ(wide, trip);
  151. }
  152. }
  153. #endif // OS_LINUX
  154. } // namespace butil