OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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/string_piece.h" | |
6 #include "base/sys_string_conversions.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 | |
9 // Apparently Windows doesn't have constants for these. | |
10 static const int kCpLatin1 = 850; | |
11 static const int kCpBig5 = 950; | |
12 | |
13 TEST(SysStringsWin, SysWideToUTF8) { | |
14 using base::SysWideToUTF8; | |
15 EXPECT_EQ("Hello, world", SysWideToUTF8(L"Hello, world")); | |
16 EXPECT_EQ("\xe4\xbd\xa0\xe5\xa5\xbd", SysWideToUTF8(L"\x4f60\x597d")); | |
17 EXPECT_EQ("\xF0\x90\x8C\x80", SysWideToUTF8(L"\xd800\xdf00")); // >16 bits | |
18 | |
19 // Error case. When Windows finds a UTF-16 character going off the end of | |
20 // a string, it just converts that literal value to UTF-8, even though this | |
21 // is invalid. | |
22 // | |
23 // This is what XP does, but Vista has different behavior, so we don't bother | |
24 // verifying it: | |
25 //EXPECT_EQ("\xE4\xBD\xA0\xED\xA0\x80zyxw", SysWideToUTF8(L"\x4f60\xd800zyxw")
); | |
26 | |
27 // Test embedded NULLs. | |
28 std::wstring wide_null(L"a"); | |
29 wide_null.push_back(0); | |
30 wide_null.push_back('b'); | |
31 | |
32 std::string expected_null("a"); | |
33 expected_null.push_back(0); | |
34 expected_null.push_back('b'); | |
35 | |
36 EXPECT_EQ(expected_null, SysWideToUTF8(wide_null)); | |
37 } | |
38 | |
39 TEST(SysStringsWin, SysUTF8ToWide) { | |
40 using base::SysUTF8ToWide; | |
41 EXPECT_EQ(L"Hello, world", SysUTF8ToWide("Hello, world")); | |
42 EXPECT_EQ(L"\x4f60\x597d", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5\xbd")); | |
43 EXPECT_EQ(L"\xd800\xdf00", SysUTF8ToWide("\xF0\x90\x8C\x80")); // >16 bits | |
44 | |
45 // Error case. When Windows finds an invalid UTF-8 character, it just skips | |
46 // it. This seems weird because it's inconsistent with the reverse conversion. | |
47 // | |
48 // This is what XP does, but Vista has different behavior, so we don't bother | |
49 // verifying it: | |
50 //EXPECT_EQ(L"\x4f60zyxw", SysUTF8ToWide("\xe4\xbd\xa0\xe5\xa5zyxw")); | |
51 | |
52 // Test embedded NULLs. | |
53 std::string utf8_null("a"); | |
54 utf8_null.push_back(0); | |
55 utf8_null.push_back('b'); | |
56 | |
57 std::wstring expected_null(L"a"); | |
58 expected_null.push_back(0); | |
59 expected_null.push_back('b'); | |
60 | |
61 EXPECT_EQ(expected_null, SysUTF8ToWide(utf8_null)); | |
62 } | |
63 | |
OLD | NEW |