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