OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 EXPECT_EQ(arraysize(wmulti) - 1, wmultistring.length()); | 220 EXPECT_EQ(arraysize(wmulti) - 1, wmultistring.length()); |
221 std::string expected; | 221 std::string expected; |
222 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); | 222 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); |
223 EXPECT_EQ(arraysize(multi) - 1, expected.length()); | 223 EXPECT_EQ(arraysize(multi) - 1, expected.length()); |
224 const std::string& converted = WideToUTF8(wmultistring); | 224 const std::string& converted = WideToUTF8(wmultistring); |
225 EXPECT_EQ(arraysize(multi) - 1, converted.length()); | 225 EXPECT_EQ(arraysize(multi) - 1, converted.length()); |
226 EXPECT_EQ(expected, converted); | 226 EXPECT_EQ(expected, converted); |
227 } | 227 } |
228 | 228 |
229 TEST(UTFStringConversionsTest, AdjustOffset) { | 229 TEST(UTFStringConversionsTest, AdjustOffset) { |
230 // Under the hood, all the functions call the same converter function, so we | |
231 // don't need to exhaustively check every case. | |
232 struct WideToUTF8Case { | |
233 const wchar_t* wide; | |
234 size_t input_offset; | |
235 size_t output_offset; | |
236 } wide_to_utf8_cases[] = { | |
237 {L"", 0, std::string::npos}, | |
238 {L"\x4f60\x597d", 0, 0}, | |
239 {L"\x4f60\x597d", 1, 3}, | |
240 {L"\x4f60\x597d", 2, std::string::npos}, | |
241 {L"\x4f60\x597d", std::wstring::npos, std::string::npos}, | |
242 {L"\xd800\x597dz", 1, 0}, | |
243 {L"\xd800\x597dz", 2, 3}, | |
244 }; | |
245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(wide_to_utf8_cases); ++i) { | |
246 size_t offset = wide_to_utf8_cases[i].input_offset; | |
247 WideToUTF8AndAdjustOffset(wide_to_utf8_cases[i].wide, &offset); | |
248 EXPECT_EQ(wide_to_utf8_cases[i].output_offset, offset); | |
249 } | |
250 | |
251 struct UTF8ToWideCase { | 230 struct UTF8ToWideCase { |
252 const char* utf8; | 231 const char* utf8; |
253 size_t input_offset; | 232 size_t input_offset; |
254 size_t output_offset; | 233 size_t output_offset; |
255 } utf8_to_wide_cases[] = { | 234 } utf8_to_wide_cases[] = { |
| 235 {"", 0, std::wstring::npos}, |
256 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, std::wstring::npos}, | 236 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, std::wstring::npos}, |
257 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, | 237 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, |
258 {"\xed\xb0\x80z", 3, 0}, | 238 {"\xed\xb0\x80z", 3, 0}, |
259 {"A\xF0\x90\x8C\x80z", 1, 1}, | 239 {"A\xF0\x90\x8C\x80z", 1, 1}, |
260 {"A\xF0\x90\x8C\x80z", 2, std::wstring::npos}, | 240 {"A\xF0\x90\x8C\x80z", 2, std::wstring::npos}, |
261 #if defined(WCHAR_T_IS_UTF16) | 241 #if defined(WCHAR_T_IS_UTF16) |
262 {"A\xF0\x90\x8C\x80z", 5, 3}, | 242 {"A\xF0\x90\x8C\x80z", 5, 3}, |
263 #elif defined(WCHAR_T_IS_UTF32) | 243 #elif defined(WCHAR_T_IS_UTF32) |
264 {"A\xF0\x90\x8C\x80z", 5, 2}, | 244 {"A\xF0\x90\x8C\x80z", 5, 2}, |
265 #endif | 245 #endif |
266 }; | 246 }; |
267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_wide_cases); ++i) { | 247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_wide_cases); ++i) { |
268 size_t offset = utf8_to_wide_cases[i].input_offset; | 248 size_t offset = utf8_to_wide_cases[i].input_offset; |
269 UTF8ToWideAndAdjustOffset(utf8_to_wide_cases[i].utf8, &offset); | 249 UTF8ToWideAndAdjustOffset(utf8_to_wide_cases[i].utf8, &offset); |
270 EXPECT_EQ(utf8_to_wide_cases[i].output_offset, offset); | 250 EXPECT_EQ(utf8_to_wide_cases[i].output_offset, offset); |
271 } | 251 } |
272 | 252 |
273 #if defined(WCHAR_T_IS_UTF32) | 253 #if defined(WCHAR_T_IS_UTF32) |
274 struct WideToUTF16Case { | |
275 const wchar_t* wide; | |
276 size_t input_offset; | |
277 size_t output_offset; | |
278 } wide_to_utf16_cases[] = { | |
279 {L"\x4F60\x597D", 1, 1}, | |
280 {L"\x20000\x4E00", 1, 2}, | |
281 }; | |
282 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(wide_to_utf16_cases); ++i) { | |
283 size_t offset = wide_to_utf16_cases[i].input_offset; | |
284 WideToUTF16AndAdjustOffset(wide_to_utf16_cases[i].wide, &offset); | |
285 EXPECT_EQ(wide_to_utf16_cases[i].output_offset, offset); | |
286 } | |
287 | |
288 struct UTF16ToWideCase { | 254 struct UTF16ToWideCase { |
289 const wchar_t* wide; | 255 const wchar_t* wide; |
290 size_t input_offset; | 256 size_t input_offset; |
291 size_t output_offset; | 257 size_t output_offset; |
292 } utf16_to_wide_cases[] = { | 258 } utf16_to_wide_cases[] = { |
293 {L"\xD840\xDC00\x4E00", 0, 0}, | 259 {L"\xD840\xDC00\x4E00", 0, 0}, |
294 {L"\xD840\xDC00\x4E00", 1, std::wstring::npos}, | 260 {L"\xD840\xDC00\x4E00", 1, std::wstring::npos}, |
295 {L"\xD840\xDC00\x4E00", 2, 1}, | 261 {L"\xD840\xDC00\x4E00", 2, 1}, |
296 }; | 262 }; |
297 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_wide_cases); ++i) { | 263 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_wide_cases); ++i) { |
298 size_t offset = utf16_to_wide_cases[i].input_offset; | 264 size_t offset = utf16_to_wide_cases[i].input_offset; |
299 UTF16ToWideAndAdjustOffset(BuildString16(utf16_to_wide_cases[i].wide), | 265 UTF16ToWideAndAdjustOffset(BuildString16(utf16_to_wide_cases[i].wide), |
300 &offset); | 266 &offset); |
301 EXPECT_EQ(utf16_to_wide_cases[i].output_offset, offset); | 267 EXPECT_EQ(utf16_to_wide_cases[i].output_offset, offset); |
302 } | 268 } |
303 #endif | 269 #endif |
304 } | 270 } |
305 | 271 |
306 } // namaspace base | 272 } // namaspace base |
OLD | NEW |