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