utf_string_conversions_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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 5#include "base/basictypes.h" 6#include "base/logging.h" 7#include "base/string_piece.h" 8#include "base/string_util.h" 9#include "base/utf_string_conversions.h" 10#include "testing/gtest/include/gtest/gtest.h" 11 12namespace base { 13 14namespace { 15 16// Given a null-terminated string of wchar_t with each wchar_t representing 17// a UTF-16 code unit, returns a string16 made up of wchar_t's in the input. 18// Each wchar_t should be <= 0xFFFF and a non-BMP character (> U+FFFF) 19// should be represented as a surrogate pair (two UTF-16 units) 20// *even* where wchar_t is 32-bit (Linux and Mac). 21// 22// This is to help write tests for functions with string16 params until 23// the C++ 0x UTF-16 literal is well-supported by compilers. 24string16 BuildString16(const wchar_t* s) { 25#if defined(WCHAR_T_IS_UTF16) 26 return string16(s); 27#elif defined(WCHAR_T_IS_UTF32) 28 string16 u16; 29 while (*s != 0) { 30 DCHECK_LE(static_cast<unsigned int>(*s), 0xFFFFu); 31 u16.push_back(*s++); 32 } 33 return u16; 34#endif 35} 36 37const wchar_t* const kConvertRoundtripCases[] = { 38 L"Google Video", 39 // "网页 图片 资讯更多 »" 40 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb", 41 // "Παγκόσμιος Ιστός" 42 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9" 43 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2", 44 // "Поиск страниц на русском" 45 L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442" 46 L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430" 47 L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c", 48 // "전체서비스" 49 L"\xc804\xccb4\xc11c\xbe44\xc2a4", 50 51 // Test characters that take more than 16 bits. This will depend on whether 52 // wchar_t is 16 or 32 bits. 53#if defined(WCHAR_T_IS_UTF16) 54 L"\xd800\xdf00", 55 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) 56 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44", 57#elif defined(WCHAR_T_IS_UTF32) 58 L"\x10300", 59 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E) 60 L"\x11d40\x11d41\x11d42\x11d43\x11d44", 61#endif 62}; 63 64} // namespace 65 66TEST(UTFStringConversionsTest, ConvertUTF8AndWide) { 67 // we round-trip all the wide strings through UTF-8 to make sure everything 68 // agrees on the conversion. This uses the stream operators to test them 69 // simultaneously. 70 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) { 71 std::ostringstream utf8; 72 utf8 << WideToUTF8(kConvertRoundtripCases[i]); 73 std::wostringstream wide; 74 wide << UTF8ToWide(utf8.str()); 75 76 EXPECT_EQ(kConvertRoundtripCases[i], wide.str()); 77 } 78} 79 80TEST(UTFStringConversionsTest, ConvertUTF8AndWideEmptyString) { 81 // An empty std::wstring should be converted to an empty std::string, 82 // and vice versa. 83 std::wstring wempty; 84 std::string empty; 85 EXPECT_EQ(empty, WideToUTF8(wempty)); 86 EXPECT_EQ(wempty, UTF8ToWide(empty)); 87} 88 89TEST(UTFStringConversionsTest, ConvertUTF8ToWide) { 90 struct UTF8ToWideCase { 91 const char* utf8; 92 const wchar_t* wide; 93 bool success; 94 } convert_cases[] = { 95 // Regular UTF-8 input. 96 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true}, 97 // Non-character is passed through. 98 {"\xef\xbf\xbfHello", L"\xffffHello", true}, 99 // Truncated UTF-8 sequence. 100 {"\xe4\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false}, 101 // Truncated off the end. 102 {"\xe5\xa5\xbd\xe4\xa0", L"\x597d\xfffd", false}, 103 // Non-shortest-form UTF-8. 104 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false}, 105 // This UTF-8 character decodes to a UTF-16 surrogate, which is illegal. 106 {"\xed\xb0\x80", L"\xfffd", false}, 107 // Non-BMP characters. The second is a non-character regarded as valid. 108 // The result will either be in UTF-16 or UTF-32. 109#if defined(WCHAR_T_IS_UTF16) 110 {"A\xF0\x90\x8C\x80z", L"A\xd800\xdf00z", true}, 111 {"A\xF4\x8F\xBF\xBEz", L"A\xdbff\xdffez", true}, 112#elif defined(WCHAR_T_IS_UTF32) 113 {"A\xF0\x90\x8C\x80z", L"A\x10300z", true}, 114 {"A\xF4\x8F\xBF\xBEz", L"A\x10fffez", true}, 115#endif 116 }; 117 118 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(convert_cases); i++) { 119 std::wstring converted; 120 EXPECT_EQ(convert_cases[i].success, 121 UTF8ToWide(convert_cases[i].utf8, 122 strlen(convert_cases[i].utf8), 123 &converted)); 124 std::wstring expected(convert_cases[i].wide); 125 EXPECT_EQ(expected, converted); 126 } 127 128 // Manually test an embedded NULL. 129 std::wstring converted; 130 EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted)); 131 ASSERT_EQ(3U, converted.length()); 132 EXPECT_EQ(static_cast<wchar_t>(0), converted[0]); 133 EXPECT_EQ('Z', converted[1]); 134 EXPECT_EQ('\t', converted[2]); 135 136 // Make sure that conversion replaces, not appends. 137 EXPECT_TRUE(UTF8ToWide("B", 1, &converted)); 138 ASSERT_EQ(1U, converted.length()); 139 EXPECT_EQ('B', converted[0]); 140} 141 142#if defined(WCHAR_T_IS_UTF16) 143// This test is only valid when wchar_t == UTF-16. 144TEST(UTFStringConversionsTest, ConvertUTF16ToUTF8) { 145 struct WideToUTF8Case { 146 const wchar_t* utf16; 147 const char* utf8; 148 bool success; 149 } convert_cases[] = { 150 // Regular UTF-16 input. 151 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, 152 // Test a non-BMP character. 153 {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true}, 154 // Non-characters are passed through. 155 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, 156 {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true}, 157 // The first character is a truncated UTF-16 character. 158 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, 159 // Truncated at the end. 160 {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false}, 161 }; 162 163 for (int i = 0; i < arraysize(convert_cases); i++) { 164 std::string converted; 165 EXPECT_EQ(convert_cases[i].success, 166 WideToUTF8(convert_cases[i].utf16, 167 wcslen(convert_cases[i].utf16), 168 &converted)); 169 std::string expected(convert_cases[i].utf8); 170 EXPECT_EQ(expected, converted); 171 } 172} 173 174#elif defined(WCHAR_T_IS_UTF32) 175// This test is only valid when wchar_t == UTF-32. 176TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) { 177 struct WideToUTF8Case { 178 const wchar_t* utf32; 179 const char* utf8; 180 bool success; 181 } convert_cases[] = { 182 // Regular 16-bit input. 183 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, 184 // Test a non-BMP character. 185 {L"A\x10300z", "A\xF0\x90\x8C\x80z", true}, 186 // Non-characters are passed through. 187 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, 188 {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true}, 189 // Invalid Unicode code points. 190 {L"\xfffffffHello", "\xEF\xBF\xBDHello", false}, 191 // The first character is a truncated UTF-16 character. 192 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, 193 {L"\xdc01Hello", "\xef\xbf\xbdHello", false}, 194 }; 195 196 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(convert_cases); i++) { 197 std::string converted; 198 EXPECT_EQ(convert_cases[i].success, 199 WideToUTF8(convert_cases[i].utf32, 200 wcslen(convert_cases[i].utf32), 201 &converted)); 202 std::string expected(convert_cases[i].utf8); 203 EXPECT_EQ(expected, converted); 204 } 205} 206#endif // defined(WCHAR_T_IS_UTF32) 207 208TEST(UTFStringConversionsTest, ConvertMultiString) { 209 static wchar_t wmulti[] = { 210 L'f', L'o', L'o', L'\0', 211 L'b', L'a', L'r', L'\0', 212 L'b', L'a', L'z', L'\0', 213 L'\0' 214 }; 215 static char multi[] = { 216 'f', 'o', 'o', '\0', 217 'b', 'a', 'r', '\0', 218 'b', 'a', 'z', '\0', 219 '\0' 220 }; 221 std::wstring wmultistring; 222 memcpy(WriteInto(&wmultistring, arraysize(wmulti)), wmulti, sizeof(wmulti)); 223 EXPECT_EQ(arraysize(wmulti) - 1, wmultistring.length()); 224 std::string expected; 225 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); 226 EXPECT_EQ(arraysize(multi) - 1, expected.length()); 227 const std::string& converted = WideToUTF8(wmultistring); 228 EXPECT_EQ(arraysize(multi) - 1, converted.length()); 229 EXPECT_EQ(expected, converted); 230} 231 232} // base 233