| 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 memcpy(WriteInto(&wmultistring, arraysize(wmulti)), wmulti, sizeof(wmulti)); | 219 memcpy(WriteInto(&wmultistring, arraysize(wmulti)), wmulti, sizeof(wmulti)); |
| 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) { | |
| 230 struct UTF8ToWideCase { | |
| 231 const char* utf8; | |
| 232 size_t input_offset; | |
| 233 size_t output_offset; | |
| 234 } utf8_to_wide_cases[] = { | |
| 235 {"", 0, std::wstring::npos}, | |
| 236 {"\xe4\xbd\xa0\xe5\xa5\xbd", 1, std::wstring::npos}, | |
| 237 {"\xe4\xbd\xa0\xe5\xa5\xbd", 3, 1}, | |
| 238 {"\xed\xb0\x80z", 3, 0}, | |
| 239 {"A\xF0\x90\x8C\x80z", 1, 1}, | |
| 240 {"A\xF0\x90\x8C\x80z", 2, std::wstring::npos}, | |
| 241 #if defined(WCHAR_T_IS_UTF16) | |
| 242 {"A\xF0\x90\x8C\x80z", 5, 3}, | |
| 243 #elif defined(WCHAR_T_IS_UTF32) | |
| 244 {"A\xF0\x90\x8C\x80z", 5, 2}, | |
| 245 #endif | |
| 246 }; | |
| 247 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf8_to_wide_cases); ++i) { | |
| 248 size_t offset = utf8_to_wide_cases[i].input_offset; | |
| 249 UTF8ToWideAndAdjustOffset(utf8_to_wide_cases[i].utf8, &offset); | |
| 250 EXPECT_EQ(utf8_to_wide_cases[i].output_offset, offset); | |
| 251 } | |
| 252 | |
| 253 #if defined(WCHAR_T_IS_UTF32) | |
| 254 struct UTF16ToWideCase { | |
| 255 const wchar_t* wide; | |
| 256 size_t input_offset; | |
| 257 size_t output_offset; | |
| 258 } utf16_to_wide_cases[] = { | |
| 259 {L"\xD840\xDC00\x4E00", 0, 0}, | |
| 260 {L"\xD840\xDC00\x4E00", 1, std::wstring::npos}, | |
| 261 {L"\xD840\xDC00\x4E00", 2, 1}, | |
| 262 }; | |
| 263 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(utf16_to_wide_cases); ++i) { | |
| 264 size_t offset = utf16_to_wide_cases[i].input_offset; | |
| 265 UTF16ToWideAndAdjustOffset(BuildString16(utf16_to_wide_cases[i].wide), | |
| 266 &offset); | |
| 267 EXPECT_EQ(utf16_to_wide_cases[i].output_offset, offset); | |
| 268 } | |
| 269 #endif | |
| 270 } | |
| 271 | |
| 272 } // namaspace base | 229 } // namaspace base |
| OLD | NEW |