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

Side by Side 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 } 59 }
60 60
61 namespace { 61 namespace {
62 62
63 void testNumberToStringECMAScript(double number, const char* reference) 63 void testNumberToStringECMAScript(double number, const char* reference)
64 { 64 {
65 CString numberString = String::numberToStringECMAScript(number).latin1(); 65 CString numberString = String::numberToStringECMAScript(number).latin1();
66 EXPECT_STREQ(reference, numberString.data()); 66 EXPECT_STREQ(reference, numberString.data());
67 } 67 }
68 68
69 void testRoundTripConvertSuccess(const UChar* characters, unsigned length)
70 {
71 String utf16String(characters, length);
72 EXPECT_FALSE(utf16String.isNull());
73 std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length( ));
74 String utf16NewString = String::fromUTF8(utf8String.data(), utf8String.lengt h());
75 EXPECT_FALSE(utf16NewString.isNull());
76 EXPECT_TRUE(utf16String == utf16NewString);
77 }
78
79 void testRoundTripConvertFailure(const UChar* characters, unsigned length)
80 {
81 String utf16String(characters, length);
82 EXPECT_FALSE(utf16String.isNull());
83 std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length( ));
84 String utf16NewString = String::fromUTF8(utf8String.data(), utf8String.lengt h());
85 EXPECT_TRUE(utf16NewString.isNull());
86 }
87
88 void testRoundTripLenientConvert(const UChar* characters, unsigned length)
89 {
90 String utf16String(characters, length);
91 EXPECT_FALSE(utf16String.isNull());
92 std::string utf8String(utf16String.utf8().data(), utf16String.utf8().length( ));
93 String utf16NewString = String::fromUTF8Lenient(utf8String.data(), utf8Strin g.length());
94 EXPECT_FALSE(utf16NewString.isNull());
95 EXPECT_TRUE(utf16String == utf16NewString);
96 }
97
69 } // anonymous namespace 98 } // anonymous namespace
70 99
71 TEST(StringTest, NumberToStringECMAScriptBoundaries) 100 TEST(StringTest, NumberToStringECMAScriptBoundaries)
72 { 101 {
73 typedef std::numeric_limits<double> Limits; 102 typedef std::numeric_limits<double> Limits;
74 103
75 // Infinity. 104 // Infinity.
76 testNumberToStringECMAScript(Limits::infinity(), "Infinity"); 105 testNumberToStringECMAScript(Limits::infinity(), "Infinity");
77 testNumberToStringECMAScript(-Limits::infinity(), "-Infinity"); 106 testNumberToStringECMAScript(-Limits::infinity(), "-Infinity");
78 107
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 } 423 }
395 424
396 TEST(StringTest, Lower) 425 TEST(StringTest, Lower)
397 { 426 {
398 EXPECT_STREQ("link", String("LINK").lower().ascii().data()); 427 EXPECT_STREQ("link", String("LINK").lower().ascii().data());
399 EXPECT_STREQ("link", String("lInk").lower().ascii().data()); 428 EXPECT_STREQ("link", String("lInk").lower().ascii().data());
400 EXPECT_STREQ("lin\xE1k", String("lIn\xC1k").lower().latin1().data()); 429 EXPECT_STREQ("lin\xE1k", String("lIn\xC1k").lower().latin1().data());
401 EXPECT_STREQ("link", String::fromUTF8("LIN\xE2\x84\xAA").lower().utf8().data ()); 430 EXPECT_STREQ("link", String::fromUTF8("LIN\xE2\x84\xAA").lower().utf8().data ());
402 } 431 }
403 432
433 TEST(StringTest, RoundTripConvert)
434 {
435 for (UChar uchar = 0; uchar <= 0xD7FF; ++uchar)
436 testRoundTripConvertSuccess(&uchar, 1);
437 // String::fromUTF8() can't convert unpaired surrogates.
438 for (UChar uchar = 0xD800; uchar <= 0xDFFF; ++uchar)
439 testRoundTripConvertFailure(&uchar, 1);
440 for (UChar uchar = 0xE000; uchar != 0; ++uchar)
441 testRoundTripConvertSuccess(&uchar, 1);
442
443 // String::fromUTF8() can't convert word-swapped surrogates.
444 const UChar wordSwappedSurrogate[] = { 0xDC00, 0xD800 };
445 testRoundTripConvertFailure(wordSwappedSurrogate, 2);
446
447 // A correct surrogate pair.
448 const UChar correctSurrogatePair[] = { 0xD83D, 0xDCA9 };
449 testRoundTripConvertSuccess(correctSurrogatePair, 2);
450 }
451
452 TEST(StringTest, RoundTripLenientConvert)
453 {
454 // String::fromUTF8Lenient() can convert all.
455 UChar uchar = 0;
456 do {
457 testRoundTripLenientConvert(&uchar, 1);
458 } while (++uchar);
459
460 // All characters in one string.
461 UChar buffer[0x10000];
462 uchar = 0;
463 do {
464 buffer[uchar] = uchar;
465 } while (++uchar);
466 testRoundTripLenientConvert(buffer, 0x10000);
467
468 // Word-swapped surrogates.
469 const UChar wordSwappedSurrogate[] = { 0xDC00, 0xD800 };
470 testRoundTripLenientConvert(wordSwappedSurrogate, 2);
471
472 // A correct surrogate pair.
473 const UChar correctSurrogatePair[] = { 0xD83D, 0xDCA9 };
474 testRoundTripLenientConvert(correctSurrogatePair, 2);
475 }
476
404 } // namespace WTF 477 } // namespace WTF
OLDNEW
« 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