| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true}, | 132 {L"\xd800\xdf00", "\xF0\x90\x8C\x80", true}, |
| 133 // Non-characters are passed through. | 133 // Non-characters are passed through. |
| 134 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, | 134 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, |
| 135 {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true}, | 135 {L"\xdbff\xdffeHello", "\xF4\x8F\xBF\xBEHello", true}, |
| 136 // The first character is a truncated UTF-16 character. | 136 // The first character is a truncated UTF-16 character. |
| 137 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, | 137 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, |
| 138 // Truncated at the end. | 138 // Truncated at the end. |
| 139 {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false}, | 139 {L"\x597d\xd800", "\xe5\xa5\xbd\xef\xbf\xbd", false}, |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 for (int i = 0; i < arraysize(convert_cases); i++) { | 142 for (const auto& test : convert_cases) { |
| 143 std::string converted; | 143 std::string converted; |
| 144 EXPECT_EQ(convert_cases[i].success, | 144 EXPECT_EQ(test.success, |
| 145 WideToUTF8(convert_cases[i].utf16, | 145 WideToUTF8(test.utf16, wcslen(test.utf16), &converted)); |
| 146 wcslen(convert_cases[i].utf16), | 146 std::string expected(test.utf8); |
| 147 &converted)); | |
| 148 std::string expected(convert_cases[i].utf8); | |
| 149 EXPECT_EQ(expected, converted); | 147 EXPECT_EQ(expected, converted); |
| 150 } | 148 } |
| 151 } | 149 } |
| 152 | 150 |
| 153 #elif defined(WCHAR_T_IS_UTF32) | 151 #elif defined(WCHAR_T_IS_UTF32) |
| 154 // This test is only valid when wchar_t == UTF-32. | 152 // This test is only valid when wchar_t == UTF-32. |
| 155 TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) { | 153 TEST(UTFStringConversionsTest, ConvertUTF32ToUTF8) { |
| 156 struct WideToUTF8Case { | 154 struct WideToUTF8Case { |
| 157 const wchar_t* utf32; | 155 const wchar_t* utf32; |
| 158 const char* utf8; | 156 const char* utf8; |
| 159 bool success; | 157 bool success; |
| 160 } convert_cases[] = { | 158 } convert_cases[] = { |
| 161 // Regular 16-bit input. | 159 // Regular 16-bit input. |
| 162 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, | 160 {L"\x4f60\x597d", "\xe4\xbd\xa0\xe5\xa5\xbd", true}, |
| 163 // Test a non-BMP character. | 161 // Test a non-BMP character. |
| 164 {L"A\x10300z", "A\xF0\x90\x8C\x80z", true}, | 162 {L"A\x10300z", "A\xF0\x90\x8C\x80z", true}, |
| 165 // Non-characters are passed through. | 163 // Non-characters are passed through. |
| 166 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, | 164 {L"\xffffHello", "\xEF\xBF\xBFHello", true}, |
| 167 {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true}, | 165 {L"\x10fffeHello", "\xF4\x8F\xBF\xBEHello", true}, |
| 168 // Invalid Unicode code points. | 166 // Invalid Unicode code points. |
| 169 {L"\xfffffffHello", "\xEF\xBF\xBDHello", false}, | 167 {L"\xfffffffHello", "\xEF\xBF\xBDHello", false}, |
| 170 // The first character is a truncated UTF-16 character. | 168 // The first character is a truncated UTF-16 character. |
| 171 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, | 169 {L"\xd800\x597d", "\xef\xbf\xbd\xe5\xa5\xbd", false}, |
| 172 {L"\xdc01Hello", "\xef\xbf\xbdHello", false}, | 170 {L"\xdc01Hello", "\xef\xbf\xbdHello", false}, |
| 173 }; | 171 }; |
| 174 | 172 |
| 175 for (size_t i = 0; i < arraysize(convert_cases); i++) { | 173 for (const auto& test : convert_cases) { |
| 176 std::string converted; | 174 std::string converted; |
| 177 EXPECT_EQ(convert_cases[i].success, | 175 EXPECT_EQ(test.success, |
| 178 WideToUTF8(convert_cases[i].utf32, | 176 WideToUTF8(test.utf32, wcslen(test.utf32), &converted)); |
| 179 wcslen(convert_cases[i].utf32), | 177 std::string expected(test.utf8); |
| 180 &converted)); | |
| 181 std::string expected(convert_cases[i].utf8); | |
| 182 EXPECT_EQ(expected, converted); | 178 EXPECT_EQ(expected, converted); |
| 183 } | 179 } |
| 184 } | 180 } |
| 185 #endif // defined(WCHAR_T_IS_UTF32) | 181 #endif // defined(WCHAR_T_IS_UTF32) |
| 186 | 182 |
| 187 TEST(UTFStringConversionsTest, ConvertMultiString) { | 183 TEST(UTFStringConversionsTest, ConvertMultiString) { |
| 188 static char16 multi16[] = { | 184 static char16 multi16[] = { |
| 189 'f', 'o', 'o', '\0', | 185 'f', 'o', 'o', '\0', |
| 190 'b', 'a', 'r', '\0', | 186 'b', 'a', 'r', '\0', |
| 191 'b', 'a', 'z', '\0', | 187 'b', 'a', 'z', '\0', |
| (...skipping 11 matching lines...) Expand all Loading... |
| 203 EXPECT_EQ(arraysize(multi16) - 1, multistring16.length()); | 199 EXPECT_EQ(arraysize(multi16) - 1, multistring16.length()); |
| 204 std::string expected; | 200 std::string expected; |
| 205 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); | 201 memcpy(WriteInto(&expected, arraysize(multi)), multi, sizeof(multi)); |
| 206 EXPECT_EQ(arraysize(multi) - 1, expected.length()); | 202 EXPECT_EQ(arraysize(multi) - 1, expected.length()); |
| 207 const std::string& converted = UTF16ToUTF8(multistring16); | 203 const std::string& converted = UTF16ToUTF8(multistring16); |
| 208 EXPECT_EQ(arraysize(multi) - 1, converted.length()); | 204 EXPECT_EQ(arraysize(multi) - 1, converted.length()); |
| 209 EXPECT_EQ(expected, converted); | 205 EXPECT_EQ(expected, converted); |
| 210 } | 206 } |
| 211 | 207 |
| 212 } // namespace base | 208 } // namespace base |
| OLD | NEW |