Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Unified Diff: third_party/WebKit/Source/wtf/text/WTFStringTest.cpp

Issue 1768063002: Introduce String::fromUTF8Lenient() and use it for cache_name in CacheStorage API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: incorporated jsbell's comment Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/text/WTFString.cpp ('k') | third_party/WebKit/public/platform/WebString.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
diff --git a/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp b/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
index 319320f0341fb6e9908cd03ea3596f5b4b007c87..12c2d4b7d4165fbe9da8db8aad5912830906e692 100644
--- a/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
+++ b/third_party/WebKit/Source/wtf/text/WTFStringTest.cpp
@@ -66,6 +66,35 @@ void testNumberToStringECMAScript(double number, const char* reference)
EXPECT_STREQ(reference, numberString.data());
}
+void testRoundTripConvertSuccess(const UChar* characters, unsigned length)
+{
+ String utf16String(characters, length);
+ EXPECT_FALSE(utf16String.isNull());
+ std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length());
+ String utf16NewString = String::fromUTF8(utf8String.data(), utf8String.length());
+ EXPECT_FALSE(utf16NewString.isNull());
+ EXPECT_TRUE(utf16String == utf16NewString);
+}
+
+void testRoundTripConvertFailure(const UChar* characters, unsigned length)
+{
+ String utf16String(characters, length);
+ EXPECT_FALSE(utf16String.isNull());
+ std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length());
+ String utf16NewString = String::fromUTF8(utf8String.data(), utf8String.length());
+ EXPECT_TRUE(utf16NewString.isNull());
+}
+
+void testRoundTripLenientConvert(const UChar* characters, unsigned length)
+{
+ String utf16String(characters, length);
+ EXPECT_FALSE(utf16String.isNull());
+ std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length());
+ String utf16NewString = String::fromUTF8Lenient(utf8String.data(), utf8String.length());
+ EXPECT_FALSE(utf16NewString.isNull());
+ EXPECT_TRUE(utf16String == utf16NewString);
+}
+
} // anonymous namespace
TEST(StringTest, NumberToStringECMAScriptBoundaries)
@@ -401,4 +430,48 @@ TEST(StringTest, Lower)
EXPECT_STREQ("link", String::fromUTF8("LIN\xE2\x84\xAA").lower().utf8().data());
}
+TEST(StringTest, RoundTripConvert)
+{
+ for (UChar uchar = 0; uchar <= 0xD7FF; ++uchar)
+ testRoundTripConvertSuccess(&uchar, 1);
+ // String::fromUTF8() can't convert unpaired surrogates.
+ for (UChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar)
+ testRoundTripConvertFailure(&uchar, 1);
+ for (UChar uchar = 0xE000; uchar != 0; ++uchar)
+ testRoundTripConvertSuccess(&uchar, 1);
+
+ // String::fromUTF8() can't convert word-swapped surrogates.
+ const UChar wordSwappedSurrogate[] = { 0xDC00, 0xD800 };
+ testRoundTripConvertFailure(wordSwappedSurrogate, 2);
+
+ // A correct surrogate pair.
+ const UChar correctSurrogatePair[] = { 0xD83D, 0xDCA9 };
+ testRoundTripConvertSuccess(correctSurrogatePair, 2);
+}
+
+TEST(StringTest, RoundTripLenientConvert)
+{
+ // String::fromUTF8Lenient() can convert all.
+ UChar uchar = 0;
+ do {
+ testRoundTripLenientConvert(&uchar, 1);
+ } while (++uchar);
+
+ // All characters in one string.
+ UChar buffer[0x10000];
+ uchar = 0;
+ do {
+ buffer[uchar] = uchar;
+ } while (++uchar);
+ testRoundTripLenientConvert(buffer, 0x10000);
+
+ // Word-swapped surrogates.
+ const UChar wordSwappedSurrogate[] = { 0xDC00, 0xD800 };
+ testRoundTripLenientConvert(wordSwappedSurrogate, 2);
+
+ // A correct surrogate pair.
+ const UChar correctSurrogatePair[] = { 0xD83D, 0xDCA9 };
+ testRoundTripLenientConvert(correctSurrogatePair, 2);
+}
+
} // namespace WTF
« no previous file with comments | « third_party/WebKit/Source/wtf/text/WTFString.cpp ('k') | third_party/WebKit/public/platform/WebString.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698