blob: 810771357a26120832ab53bbf8ddfb3db8817055 [file] [log] [blame]
[email protected]f1633932010-08-17 23:05:281// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]ce85f602009-11-07 01:34:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avi84f37e12015-12-25 09:31:425#include <stddef.h>
6
[email protected]379e7a52010-03-09 00:38:417#include "base/logging.h"
avi84f37e12015-12-25 09:31:428#include "base/macros.h"
[email protected]eb62f7262013-03-30 14:29:009#include "base/strings/string_piece.h"
[email protected]c851cfd2013-06-10 20:11:1410#include "base/strings/string_util.h"
[email protected]7ec49002013-05-06 21:32:3111#include "base/strings/utf_string_conversions.h"
avi84f37e12015-12-25 09:31:4212#include "build/build_config.h"
[email protected]ce85f602009-11-07 01:34:5313#include "testing/gtest/include/gtest/gtest.h"
14
15namespace base {
16
17namespace {
18
[email protected]ce85f602009-11-07 01:34:5319const wchar_t* const kConvertRoundtripCases[] = {
20 L"Google Video",
21 // "网页 图片 资讯更多 »"
22 L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb",
23 // "Παγκόσμιος Ιστός"
24 L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
25 L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2",
26 // "Поиск страниц на русском"
27 L"\x041f\x043e\x0438\x0441\x043a\x0020\x0441\x0442"
28 L"\x0440\x0430\x043d\x0438\x0446\x0020\x043d\x0430"
29 L"\x0020\x0440\x0443\x0441\x0441\x043a\x043e\x043c",
30 // "전체서비스"
31 L"\xc804\xccb4\xc11c\xbe44\xc2a4",
32
33 // Test characters that take more than 16 bits. This will depend on whether
34 // wchar_t is 16 or 32 bits.
35#if defined(WCHAR_T_IS_UTF16)
36 L"\xd800\xdf00",
37 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E)
38 L"\xd807\xdd40\xd807\xdd41\xd807\xdd42\xd807\xdd43\xd807\xdd44",
39#elif defined(WCHAR_T_IS_UTF32)
40 L"\x10300",
41 // ????? (Mathematical Alphanumeric Symbols (U+011d40 - U+011d44 : A,B,C,D,E)
42 L"\x11d40\x11d41\x11d42\x11d43\x11d44",
43#endif
44};
45
46} // namespace
47
48TEST(UTFStringConversionsTest, ConvertUTF8AndWide) {
49 // we round-trip all the wide strings through UTF-8 to make sure everything
50 // agrees on the conversion. This uses the stream operators to test them
51 // simultaneously.
52 for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) {
53 std::ostringstream utf8;
54 utf8 << WideToUTF8(kConvertRoundtripCases[i]);
55 std::wostringstream wide;
56 wide << UTF8ToWide(utf8.str());
57
58 EXPECT_EQ(kConvertRoundtripCases[i], wide.str());
59 }
60}
61
62TEST(UTFStringConversionsTest, ConvertUTF8AndWideEmptyString) {
63 // An empty std::wstring should be converted to an empty std::string,
64 // and vice versa.
65 std::wstring wempty;
66 std::string empty;
67 EXPECT_EQ(empty, WideToUTF8(wempty));
68 EXPECT_EQ(wempty, UTF8ToWide(empty));
69}
70
71TEST(UTFStringConversionsTest, ConvertUTF8ToWide) {
72 struct UTF8ToWideCase {
73 const char* utf8;
74 const wchar_t* wide;
75 bool success;
76 } convert_cases[] = {
77 // Regular UTF-8 input.
78 {"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true},
[email protected]858d48872010-01-16 17:56:0879 // Non-character is passed through.
80 {"\xef\xbf\xbfHello", L"\xffffHello", true},
[email protected]ce85f602009-11-07 01:34:5381 // Truncated UTF-8 sequence.
[email protected]d7a3e8e2010-01-01 22:16:3882 {"\xe4\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false},
[email protected]ce85f602009-11-07 01:34:5383 // Truncated off the end.
[email protected]d7a3e8e2010-01-01 22:16:3884 {"\xe5\xa5\xbd\xe4\xa0", L"\x597d\xfffd", false},
[email protected]ce85f602009-11-07 01:34:5385 // Non-shortest-form UTF-8.
[email protected]d7a3e8e2010-01-01 22:16:3886 {"\xf0\x84\xbd\xa0\xe5\xa5\xbd", L"\xfffd\x597d", false},
[email protected]ce85f602009-11-07 01:34:5387 // This UTF-8 character decodes to a UTF-16 surrogate, which is illegal.
[email protected]d7a3e8e2010-01-01 22:16:3888 {"\xed\xb0\x80", L"\xfffd", false},
[email protected]ce85f602009-11-07 01:34:5389 // Non-BMP characters. The second is a non-character regarded as valid.
90 // The result will either be in UTF-16 or UTF-32.
91#if defined(WCHAR_T_IS_UTF16)
92 {"A\xF0\x90\x8C\x80z", L"A\xd800\xdf00z", true},
[email protected]858d48872010-01-16 17:56:0893 {"A\xF4\x8F\xBF\xBEz", L"A\xdbff\xdffez", true},
[email protected]ce85f602009-11-07 01:34:5394#elif defined(WCHAR_T_IS_UTF32)
95 {"A\xF0\x90\x8C\x80z", L"A\x10300z", true},
[email protected]858d48872010-01-16 17:56:0896 {"A\xF4\x8F\xBF\xBEz", L"A\x10fffez", true},
[email protected]ce85f602009-11-07 01:34:5397#endif
98 };
99
viettrungluu805eabb2014-10-16 04:02:49100 for (size_t i = 0; i < arraysize(convert_cases); i++) {
[email protected]ce85f602009-11-07 01:34:53101 std::wstring converted;
102 EXPECT_EQ(convert_cases[i].success,
103 UTF8ToWide(convert_cases[i].utf8,
104 strlen(convert_cases[i].utf8),
105 &converted));
106 std::wstring expected(convert_cases[i].wide);
107 EXPECT_EQ(expected, converted);
108 }
109
110 // Manually test an embedded NULL.
111 std::wstring converted;
112 EXPECT_TRUE(UTF8ToWide("\00Z\t", 3, &converted));
113 ASSERT_EQ(3U, converted.length());
114 EXPECT_EQ(static_cast<wchar_t>(0), converted[0]);
115 EXPECT_EQ('Z', converted[1]);
116 EXPECT_EQ('\t', converted[2]);
117
118 // Make sure that conversion replaces, not appends.
119 EXPECT_TRUE(UTF8ToWide("B", 1, &converted));
120 ASSERT_EQ(1U, converted.length());
121 EXPECT_EQ('B', converted[0]);
122}
123
124#if defined(WCHAR_T_IS_UTF16)
125// This test is only valid when wchar_t == UTF-16.
126TEST(UTFStringConversionsTest, ConvertUTF16ToUTF8) {
127 struct WideToUTF8Case {
128 const wchar_t* utf16;
129 const char* utf8;
130 bool success;
131 } convert_cases[] = {
132 // Regular UTF-16 input.
133 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true},
134 // Test a non-BMP character.
135 {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true},
[email protected]858d48872010-01-16 17:56:08136 // Non-characters are passed through.
137 {L"\xffffHello", "\xEF\xBF\xBFHello", true},
138 {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true},
[email protected]ce85f602009-11-07 01:34:53139 // The first character is a truncated UTF-16 character.
[email protected]d7a3e8e2010-01-01 22:16:38140 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
[email protected]ce85f602009-11-07 01:34:53141 // Truncated at the end.
[email protected]d7a3e8e2010-01-01 22:16:38142 {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false},
[email protected]ce85f602009-11-07 01:34:53143 };
144
thakis9230c1d2015-12-10 00:36:25145 for (const auto& test : convert_cases) {
[email protected]ce85f602009-11-07 01:34:53146 std::string converted;
thakis9230c1d2015-12-10 00:36:25147 EXPECT_EQ(test.success,
148 WideToUTF8(test.utf16, wcslen(test.utf16), &converted));
149 std::string expected(test.utf8);
[email protected]ce85f602009-11-07 01:34:53150 EXPECT_EQ(expected, converted);
151 }
152}
153
154#elif defined(WCHAR_T_IS_UTF32)
155// This test is only valid when wchar_t == UTF-32.
156TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) {
157 struct WideToUTF8Case {
158 const wchar_t* utf32;
159 const char* utf8;
160 bool success;
161 } convert_cases[] = {
162 // Regular 16-bit input.
163 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true},
164 // Test a non-BMP character.
165 {L"A\x10300z", "A\xF0\x90\x8C\x80z", true},
[email protected]858d48872010-01-16 17:56:08166 // Non-characters are passed through.
167 {L"\xffffHello", "\xEF\xBF\xBFHello", true},
168 {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true},
[email protected]ce85f602009-11-07 01:34:53169 // Invalid Unicode code points.
[email protected]d7a3e8e2010-01-01 22:16:38170 {L"\xfffffffHello", "\xEF\xBF\xBDHello", false},
[email protected]ce85f602009-11-07 01:34:53171 // The first character is a truncated UTF-16 character.
[email protected]d7a3e8e2010-01-01 22:16:38172 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false},
173 {L"\xdc01Hello", "\xef\xbf\xbdHello", false},
[email protected]ce85f602009-11-07 01:34:53174 };
175
thakis9230c1d2015-12-10 00:36:25176 for (const auto& test : convert_cases) {
[email protected]ce85f602009-11-07 01:34:53177 std::string converted;
thakis9230c1d2015-12-10 00:36:25178 EXPECT_EQ(test.success,
179 WideToUTF8(test.utf32, wcslen(test.utf32), &converted));
180 std::string expected(test.utf8);
[email protected]ce85f602009-11-07 01:34:53181 EXPECT_EQ(expected, converted);
182 }
183}
184#endif // defined(WCHAR_T_IS_UTF32)
185
186TEST(UTFStringConversionsTest, ConvertMultiString) {
Brett Wilsone3c4d1a2015-07-07 23:38:09187 static char16 multi16[] = {
188 'f', 'o', 'o', '\0',
189 'b', 'a', 'r', '\0',
190 'b', 'a', 'z', '\0',
191 '\0'
[email protected]ce85f602009-11-07 01:34:53192 };
193 static char multi[] = {
194 'f', 'o', 'o', '\0',
195 'b', 'a', 'r', '\0',
196 'b', 'a', 'z', '\0',
197 '\0'
198 };
Brett Wilsone3c4d1a2015-07-07 23:38:09199 string16 multistring16;
200 memcpy(WriteInto(&multistring16, arraysize(multi16)), multi16,
201 sizeof(multi16));
202 EXPECT_EQ(arraysize(multi16) - 1, multistring16.length());
[email protected]ce85f602009-11-07 01:34:53203 std::string expected;
204 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi));
205 EXPECT_EQ(arraysize(multi) - 1, expected.length());
Brett Wilsone3c4d1a2015-07-07 23:38:09206 const std::string& converted = UTF16ToUTF8(multistring16);
[email protected]ce85f602009-11-07 01:34:53207 EXPECT_EQ(arraysize(multi) - 1, converted.length());
208 EXPECT_EQ(expected, converted);
209}
210
danakjc3762b92015-03-07 01:51:42211} // namespace base