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 |