summaryrefslogtreecommitdiffstats
path: root/base/i18n/icu_string_conversions_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'base/i18n/icu_string_conversions_unittest.cc')
-rw-r--r--base/i18n/icu_string_conversions_unittest.cc55
1 files changed, 48 insertions, 7 deletions
diff --git a/base/i18n/icu_string_conversions_unittest.cc b/base/i18n/icu_string_conversions_unittest.cc
index 969ddb7..0088a03 100644
--- a/base/i18n/icu_string_conversions_unittest.cc
+++ b/base/i18n/icu_string_conversions_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -9,9 +9,9 @@
#include <sstream>
#include "base/basictypes.h"
+#include "base/i18n/icu_string_conversions.h"
#include "base/logging.h"
#include "base/utf_string_conversions.h"
-#include "base/i18n/icu_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
@@ -39,7 +39,7 @@ string16 BuildString16(const wchar_t* s) {
#endif
}
-static const wchar_t* const kConvertRoundtripCases[] = {
+const wchar_t* const kConvertRoundtripCases[] = {
L"Google Video",
// "网页 图片 资讯更多 »"
L"\x7f51\x9875\x0020\x56fe\x7247\x0020\x8d44\x8baf\x66f4\x591a\x0020\x00bb",
@@ -68,7 +68,7 @@ static const wchar_t* const kConvertRoundtripCases[] = {
} // namespace
-TEST(StringUtilTest, ConvertCodepageUTF8) {
+TEST(ICUStringConversionsTest, ConvertCodepageUTF8) {
// Make sure WideToCodepage works like WideToUTF8.
for (size_t i = 0; i < arraysize(kConvertRoundtripCases); ++i) {
std::string expected(WideToUTF8(kConvertRoundtripCases[i]));
@@ -156,7 +156,7 @@ static const struct {
true,
#if defined(WCHAR_T_IS_UTF16)
L"\xD840\xDC00\x4E00",
-#else
+#elif defined(WCHAR_T_IS_UTF32)
L"\x20000\x4E00",
#endif
L"\xD840\xDC00\x4E00"},
@@ -234,7 +234,7 @@ static const struct {
NULL},
};
-TEST(StringUtilTest, ConvertBetweenCodepageAndWide) {
+TEST(ICUStringConversionsTest, ConvertBetweenCodepageAndWide) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kConvertCodepageCases); ++i) {
std::wstring wide;
bool success = CodepageToWide(kConvertCodepageCases[i].encoded,
@@ -296,7 +296,7 @@ TEST(StringUtilTest, ConvertBetweenCodepageAndWide) {
OnStringConversionError::SKIP, &encoded));
}
-TEST(StringUtilTest, ConvertBetweenCodepageAndUTF16) {
+TEST(ICUStringConversionsTest, ConvertBetweenCodepageAndUTF16) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kConvertCodepageCases); ++i) {
string16 utf16;
bool success = CodepageToUTF16(kConvertCodepageCases[i].encoded,
@@ -325,4 +325,45 @@ TEST(StringUtilTest, ConvertBetweenCodepageAndUTF16) {
}
}
+static const struct {
+ const char* codepage_name;
+ const char* encoded;
+ size_t input_offset;
+ size_t u16_output_offset;
+ size_t wide_output_offset;
+} kAdjustOffsetCases[] = {
+ {"gb2312", "", 0, string16::npos, std::wstring::npos},
+ {"gb2312", "\xC4\xE3\xBA\xC3", 0, 0, 0},
+ {"gb2312", "\xC4\xE3\xBA\xC3", 2, 1, 1},
+ {"gb2312", "\xC4\xE3\xBA\xC3", 4, string16::npos, std::wstring::npos},
+ {"gb2312", "\xC4\xE3\xBA\xC3", 1, string16::npos, std::wstring::npos},
+ {"gb2312", "\xC4\xE3\xBA\xC3", std::string::npos, string16::npos,
+ std::wstring::npos},
+ {"gb18030", "\x95\x32\x82\x36\xD2\xBB", 2, string16::npos,
+ std::wstring::npos},
+ {"gb18030", "\x95\x32\x82\x36\xD2\xBB", 4, 2, 1},
+};
+
+TEST(ICUStringConversionsTest, AdjustOffset) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kAdjustOffsetCases); ++i) {
+ string16 utf16;
+ size_t offset = kAdjustOffsetCases[i].input_offset;
+ EXPECT_TRUE(CodepageToUTF16AndAdjustOffset(kAdjustOffsetCases[i].encoded,
+ kAdjustOffsetCases[i].codepage_name,
+ OnStringConversionError::FAIL, &utf16, &offset));
+ EXPECT_EQ(kAdjustOffsetCases[i].u16_output_offset, offset);
+
+ std::wstring wide;
+ offset = kAdjustOffsetCases[i].input_offset;
+ CodepageToWideAndAdjustOffset(kAdjustOffsetCases[i].encoded,
+ kAdjustOffsetCases[i].codepage_name,
+ OnStringConversionError::FAIL, &wide, &offset);
+#if defined(WCHAR_T_IS_UTF16)
+ EXPECT_EQ(kAdjustOffsetCases[i].u16_output_offset, offset);
+#elif defined(WCHAR_T_IS_UTF32)
+ EXPECT_EQ(kAdjustOffsetCases[i].wide_output_offset, offset);
+#endif
+ }
+}
+
} // namespace base