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

Side by Side Diff: third_party/WebKit/Source/wtf/text/WTFStringTest.cpp

Issue 2771783003: Move wtf_unittests to platform/wtf/. (Closed)
Patch Set: Rebase. Created 3 years, 8 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
(Empty)
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "wtf/text/WTFString.h"
27
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "wtf/MathExtras.h"
30 #include "wtf/text/CString.h"
31 #include <limits>
32
33 namespace WTF {
34
35 TEST(StringTest, CreationFromLiteral) {
36 String stringFromLiteral("Explicit construction syntax");
37 EXPECT_EQ(strlen("Explicit construction syntax"), stringFromLiteral.length());
38 EXPECT_TRUE(stringFromLiteral == "Explicit construction syntax");
39 EXPECT_TRUE(stringFromLiteral.is8Bit());
40 EXPECT_TRUE(String("Explicit construction syntax") == stringFromLiteral);
41 }
42
43 TEST(StringTest, ASCII) {
44 CString output;
45
46 // Null String.
47 output = String().ascii();
48 EXPECT_STREQ("", output.data());
49
50 // Empty String.
51 output = emptyString.ascii();
52 EXPECT_STREQ("", output.data());
53
54 // Regular String.
55 output = String("foobar").ascii();
56 EXPECT_STREQ("foobar", output.data());
57 }
58
59 namespace {
60
61 void testNumberToStringECMAScript(double number, const char* reference) {
62 CString numberString = String::numberToStringECMAScript(number).latin1();
63 EXPECT_STREQ(reference, numberString.data());
64 }
65
66 } // anonymous namespace
67
68 TEST(StringTest, NumberToStringECMAScriptBoundaries) {
69 typedef std::numeric_limits<double> Limits;
70
71 // Infinity.
72 testNumberToStringECMAScript(Limits::infinity(), "Infinity");
73 testNumberToStringECMAScript(-Limits::infinity(), "-Infinity");
74
75 // NaN.
76 testNumberToStringECMAScript(-Limits::quiet_NaN(), "NaN");
77
78 // Zeros.
79 testNumberToStringECMAScript(0, "0");
80 testNumberToStringECMAScript(-0, "0");
81
82 // Min-Max.
83 testNumberToStringECMAScript(Limits::min(), "2.2250738585072014e-308");
84 testNumberToStringECMAScript(Limits::max(), "1.7976931348623157e+308");
85 }
86
87 TEST(StringTest, NumberToStringECMAScriptRegularNumbers) {
88 // Pi.
89 testNumberToStringECMAScript(piDouble, "3.141592653589793");
90 testNumberToStringECMAScript(piFloat, "3.1415927410125732");
91 testNumberToStringECMAScript(piOverTwoDouble, "1.5707963267948966");
92 testNumberToStringECMAScript(piOverTwoFloat, "1.5707963705062866");
93 testNumberToStringECMAScript(piOverFourDouble, "0.7853981633974483");
94 testNumberToStringECMAScript(piOverFourFloat, "0.7853981852531433");
95
96 // e.
97 const double e = 2.71828182845904523536028747135266249775724709369995;
98 testNumberToStringECMAScript(e, "2.718281828459045");
99
100 // c, speed of light in m/s.
101 const double c = 299792458;
102 testNumberToStringECMAScript(c, "299792458");
103
104 // Golen ratio.
105 const double phi = 1.6180339887498948482;
106 testNumberToStringECMAScript(phi, "1.618033988749895");
107 }
108
109 TEST(StringTest, ReplaceWithLiteral) {
110 // Cases for 8Bit source.
111 String testString = "1224";
112 EXPECT_TRUE(testString.is8Bit());
113 testString.replace('2', "");
114 EXPECT_STREQ("14", testString.utf8().data());
115
116 testString = "1224";
117 EXPECT_TRUE(testString.is8Bit());
118 testString.replace('2', "3");
119 EXPECT_STREQ("1334", testString.utf8().data());
120
121 testString = "1224";
122 EXPECT_TRUE(testString.is8Bit());
123 testString.replace('2', "555");
124 EXPECT_STREQ("15555554", testString.utf8().data());
125
126 testString = "1224";
127 EXPECT_TRUE(testString.is8Bit());
128 testString.replace('3', "NotFound");
129 EXPECT_STREQ("1224", testString.utf8().data());
130
131 // Cases for 16Bit source.
132 // U+00E9 (=0xC3 0xA9 in UTF-8) is e with accent.
133 testString = String::fromUTF8("r\xC3\xA9sum\xC3\xA9");
134 EXPECT_FALSE(testString.is8Bit());
135 testString.replace(UChar(0x00E9), "e");
136 EXPECT_STREQ("resume", testString.utf8().data());
137
138 testString = String::fromUTF8("r\xC3\xA9sum\xC3\xA9");
139 EXPECT_FALSE(testString.is8Bit());
140 testString.replace(UChar(0x00E9), "");
141 EXPECT_STREQ("rsum", testString.utf8().data());
142
143 testString = String::fromUTF8("r\xC3\xA9sum\xC3\xA9");
144 EXPECT_FALSE(testString.is8Bit());
145 testString.replace('3', "NotFound");
146 EXPECT_STREQ("r\xC3\xA9sum\xC3\xA9", testString.utf8().data());
147 }
148
149 TEST(StringTest, ComparisonOfSameStringVectors) {
150 Vector<String> stringVector;
151 stringVector.push_back("one");
152 stringVector.push_back("two");
153
154 Vector<String> sameStringVector;
155 sameStringVector.push_back("one");
156 sameStringVector.push_back("two");
157
158 EXPECT_EQ(stringVector, sameStringVector);
159 }
160
161 TEST(WTF, SimplifyWhiteSpace) {
162 String extraSpaces(" Hello world ");
163 EXPECT_EQ(String("Hello world"), extraSpaces.simplifyWhiteSpace());
164 EXPECT_EQ(String(" Hello world "),
165 extraSpaces.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace));
166
167 String extraSpacesAndNewlines(" \nHello\n world\n ");
168 EXPECT_EQ(String("Hello world"), extraSpacesAndNewlines.simplifyWhiteSpace());
169 EXPECT_EQ(
170 String(" Hello world "),
171 extraSpacesAndNewlines.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace));
172
173 String extraSpacesAndTabs(" \nHello\t world\t ");
174 EXPECT_EQ(String("Hello world"), extraSpacesAndTabs.simplifyWhiteSpace());
175 EXPECT_EQ(String(" Hello world "),
176 extraSpacesAndTabs.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace));
177 }
178
179 struct CaseFoldingTestData {
180 const char* sourceDescription;
181 const char* source;
182 const char** localeList;
183 size_t localeListLength;
184 const char* expected;
185 };
186
187 // \xC4\xB0 = U+0130 (capital dotted I)
188 // \xC4\xB1 = U+0131 (lowercase dotless I)
189 const char* turkicInput = "Isi\xC4\xB0 \xC4\xB0s\xC4\xB1I";
190 const char* greekInput =
191 "\xCE\x9F\xCE\x94\xCE\x8C\xCE\xA3 \xCE\x9F\xCE\xB4\xCF\x8C\xCF\x82 "
192 "\xCE\xA3\xCE\xBF \xCE\xA3\xCE\x9F o\xCE\xA3 \xCE\x9F\xCE\xA3 \xCF\x83 "
193 "\xE1\xBC\x95\xCE\xBE";
194 const char* lithuanianInput =
195 "I \xC3\x8F J J\xCC\x88 \xC4\xAE \xC4\xAE\xCC\x88 \xC3\x8C \xC3\x8D "
196 "\xC4\xA8 xi\xCC\x87\xCC\x88 xj\xCC\x87\xCC\x88 x\xC4\xAF\xCC\x87\xCC\x88 "
197 "xi\xCC\x87\xCC\x80 xi\xCC\x87\xCC\x81 xi\xCC\x87\xCC\x83 XI X\xC3\x8F XJ "
198 "XJ\xCC\x88 X\xC4\xAE X\xC4\xAE\xCC\x88";
199
200 const char* turkicLocales[] = {
201 "tr", "tr-TR", "tr_TR", "tr@foo=bar", "tr-US", "TR", "tr-tr", "tR",
202 "az", "az-AZ", "az_AZ", "az@foo=bar", "az-US", "Az", "AZ-AZ",
203 };
204 const char* nonTurkicLocales[] = {
205 "en", "en-US", "en_US", "en@foo=bar", "EN", "En",
206 "ja", "el", "fil", "fi", "lt",
207 };
208 const char* greekLocales[] = {
209 "el", "el-GR", "el_GR", "el@foo=bar", "el-US", "EL", "el-gr", "eL",
210 };
211 const char* nonGreekLocales[] = {
212 "en", "en-US", "en_US", "en@foo=bar", "EN", "En",
213 "ja", "tr", "az", "fil", "fi", "lt",
214 };
215 const char* lithuanianLocales[] = {
216 "lt", "lt-LT", "lt_LT", "lt@foo=bar", "lt-US", "LT", "lt-lt", "lT",
217 };
218 // Should not have "tr" or "az" because "lt" and 'tr/az' rules conflict with
219 // each other.
220 const char* nonLithuanianLocales[] = {
221 "en", "en-US", "en_US", "en@foo=bar", "EN", "En", "ja", "fil", "fi", "el",
222 };
223
224 TEST(StringTest, ToUpperLocale) {
225 CaseFoldingTestData testDataList[] = {
226 {
227 "Turkic input", turkicInput, turkicLocales,
228 sizeof(turkicLocales) / sizeof(const char*),
229 "IS\xC4\xB0\xC4\xB0 \xC4\xB0SII",
230 },
231 {
232 "Turkic input", turkicInput, nonTurkicLocales,
233 sizeof(nonTurkicLocales) / sizeof(const char*),
234 "ISI\xC4\xB0 \xC4\xB0SII",
235 },
236 {
237 "Greek input", greekInput, greekLocales,
238 sizeof(greekLocales) / sizeof(const char*),
239 "\xCE\x9F\xCE\x94\xCE\x9F\xCE\xA3 \xCE\x9F\xCE\x94\xCE\x9F\xCE\xA3 "
240 "\xCE\xA3\xCE\x9F \xCE\xA3\xCE\x9F \x4F\xCE\xA3 \xCE\x9F\xCE\xA3 "
241 "\xCE\xA3 \xCE\x95\xCE\x9E",
242 },
243 {
244 "Greek input", greekInput, nonGreekLocales,
245 sizeof(nonGreekLocales) / sizeof(const char*),
246 "\xCE\x9F\xCE\x94\xCE\x8C\xCE\xA3 \xCE\x9F\xCE\x94\xCE\x8C\xCE\xA3 "
247 "\xCE\xA3\xCE\x9F \xCE\xA3\xCE\x9F \x4F\xCE\xA3 \xCE\x9F\xCE\xA3 "
248 "\xCE\xA3 \xE1\xBC\x9D\xCE\x9E",
249 },
250 {
251 "Lithuanian input", lithuanianInput, lithuanianLocales,
252 sizeof(lithuanianLocales) / sizeof(const char*),
253 "I \xC3\x8F J J\xCC\x88 \xC4\xAE \xC4\xAE\xCC\x88 \xC3\x8C \xC3\x8D "
254 "\xC4\xA8 XI\xCC\x88 XJ\xCC\x88 X\xC4\xAE\xCC\x88 XI\xCC\x80 "
255 "XI\xCC\x81 XI\xCC\x83 XI X\xC3\x8F XJ XJ\xCC\x88 X\xC4\xAE "
256 "X\xC4\xAE\xCC\x88",
257 },
258 {
259 "Lithuanian input", lithuanianInput, nonLithuanianLocales,
260 sizeof(nonLithuanianLocales) / sizeof(const char*),
261 "I \xC3\x8F J J\xCC\x88 \xC4\xAE \xC4\xAE\xCC\x88 \xC3\x8C \xC3\x8D "
262 "\xC4\xA8 XI\xCC\x87\xCC\x88 XJ\xCC\x87\xCC\x88 "
263 "X\xC4\xAE\xCC\x87\xCC\x88 XI\xCC\x87\xCC\x80 XI\xCC\x87\xCC\x81 "
264 "XI\xCC\x87\xCC\x83 XI X\xC3\x8F XJ XJ\xCC\x88 X\xC4\xAE "
265 "X\xC4\xAE\xCC\x88",
266 },
267 };
268
269 for (size_t i = 0; i < sizeof(testDataList) / sizeof(testDataList[0]); ++i) {
270 const char* expected = testDataList[i].expected;
271 String source = String::fromUTF8(testDataList[i].source);
272 for (size_t j = 0; j < testDataList[i].localeListLength; ++j) {
273 const char* locale = testDataList[i].localeList[j];
274 EXPECT_STREQ(expected, source.upper(locale).utf8().data())
275 << testDataList[i].sourceDescription << "; locale=" << locale;
276 }
277 }
278 }
279
280 TEST(StringTest, ToLowerLocale) {
281 CaseFoldingTestData testDataList[] = {
282 {
283 "Turkic input", turkicInput, turkicLocales,
284 sizeof(turkicLocales) / sizeof(const char*),
285 "\xC4\xB1sii is\xC4\xB1\xC4\xB1",
286 },
287 {
288 "Turkic input", turkicInput, nonTurkicLocales,
289 sizeof(nonTurkicLocales) / sizeof(const char*),
290 // U+0130 is lowercased to U+0069 followed by U+0307
291 "isii\xCC\x87 i\xCC\x87s\xC4\xB1i",
292 },
293 {
294 "Greek input", greekInput, greekLocales,
295 sizeof(greekLocales) / sizeof(const char*),
296 "\xCE\xBF\xCE\xB4\xCF\x8C\xCF\x82 \xCE\xBF\xCE\xB4\xCF\x8C\xCF\x82 "
297 "\xCF\x83\xCE\xBF \xCF\x83\xCE\xBF \x6F\xCF\x82 \xCE\xBF\xCF\x82 "
298 "\xCF\x83 \xE1\xBC\x95\xCE\xBE",
299 },
300 {
301 "Greek input", greekInput, nonGreekLocales,
302 sizeof(greekLocales) / sizeof(const char*),
303 "\xCE\xBF\xCE\xB4\xCF\x8C\xCF\x82 \xCE\xBF\xCE\xB4\xCF\x8C\xCF\x82 "
304 "\xCF\x83\xCE\xBF \xCF\x83\xCE\xBF \x6F\xCF\x82 \xCE\xBF\xCF\x82 "
305 "\xCF\x83 \xE1\xBC\x95\xCE\xBE",
306 },
307 {
308 "Lithuanian input", lithuanianInput, lithuanianLocales,
309 sizeof(lithuanianLocales) / sizeof(const char*),
310 "i \xC3\xAF j j\xCC\x87\xCC\x88 \xC4\xAF \xC4\xAF\xCC\x87\xCC\x88 "
311 "i\xCC\x87\xCC\x80 i\xCC\x87\xCC\x81 i\xCC\x87\xCC\x83 "
312 "xi\xCC\x87\xCC\x88 xj\xCC\x87\xCC\x88 x\xC4\xAF\xCC\x87\xCC\x88 "
313 "xi\xCC\x87\xCC\x80 xi\xCC\x87\xCC\x81 xi\xCC\x87\xCC\x83 xi "
314 "x\xC3\xAF xj xj\xCC\x87\xCC\x88 x\xC4\xAF x\xC4\xAF\xCC\x87\xCC\x88",
315 },
316 {
317 "Lithuanian input", lithuanianInput, nonLithuanianLocales,
318 sizeof(nonLithuanianLocales) / sizeof(const char*),
319 "\x69 \xC3\xAF \x6A \x6A\xCC\x88 \xC4\xAF \xC4\xAF\xCC\x88 \xC3\xAC "
320 "\xC3\xAD \xC4\xA9 \x78\x69\xCC\x87\xCC\x88 \x78\x6A\xCC\x87\xCC\x88 "
321 "\x78\xC4\xAF\xCC\x87\xCC\x88 \x78\x69\xCC\x87\xCC\x80 "
322 "\x78\x69\xCC\x87\xCC\x81 \x78\x69\xCC\x87\xCC\x83 \x78\x69 "
323 "\x78\xC3\xAF \x78\x6A \x78\x6A\xCC\x88 \x78\xC4\xAF "
324 "\x78\xC4\xAF\xCC\x88",
325 },
326 };
327
328 for (size_t i = 0; i < sizeof(testDataList) / sizeof(testDataList[0]); ++i) {
329 const char* expected = testDataList[i].expected;
330 String source = String::fromUTF8(testDataList[i].source);
331 for (size_t j = 0; j < testDataList[i].localeListLength; ++j) {
332 const char* locale = testDataList[i].localeList[j];
333 EXPECT_STREQ(expected, source.lower(locale).utf8().data())
334 << testDataList[i].sourceDescription << "; locale=" << locale;
335 }
336 }
337 }
338
339 TEST(StringTest, StartsWithIgnoringUnicodeCase) {
340 // [U+017F U+212A i a] starts with "sk".
341 EXPECT_TRUE(String::fromUTF8("\xC5\xBF\xE2\x84\xAAia")
342 .startsWith("sk", TextCaseUnicodeInsensitive));
343 }
344
345 TEST(StringTest, StartsWithIgnoringASCIICase) {
346 String allASCII("LINK");
347 String allASCIILowerCase("link");
348 EXPECT_TRUE(allASCII.startsWith(allASCIILowerCase, TextCaseASCIIInsensitive));
349 String allASCIIMixedCase("lInK");
350 EXPECT_TRUE(allASCII.startsWith(allASCIIMixedCase, TextCaseASCIIInsensitive));
351 String allASCIIDifferent("foo");
352 EXPECT_FALSE(
353 allASCII.startsWith(allASCIIDifferent, TextCaseASCIIInsensitive));
354 String nonASCII = String::fromUTF8("LIN\xE2\x84\xAA");
355 EXPECT_FALSE(allASCII.startsWith(nonASCII, TextCaseASCIIInsensitive));
356 EXPECT_TRUE(allASCII.startsWith(nonASCII.lower(), TextCaseASCIIInsensitive));
357
358 EXPECT_FALSE(nonASCII.startsWith(allASCII, TextCaseASCIIInsensitive));
359 EXPECT_FALSE(
360 nonASCII.startsWith(allASCIILowerCase, TextCaseASCIIInsensitive));
361 EXPECT_FALSE(
362 nonASCII.startsWith(allASCIIMixedCase, TextCaseASCIIInsensitive));
363 EXPECT_FALSE(
364 nonASCII.startsWith(allASCIIDifferent, TextCaseASCIIInsensitive));
365 }
366
367 TEST(StringTest, EndsWithIgnoringASCIICase) {
368 String allASCII("LINK");
369 String allASCIILowerCase("link");
370 EXPECT_TRUE(allASCII.endsWith(allASCIILowerCase, TextCaseASCIIInsensitive));
371 String allASCIIMixedCase("lInK");
372 EXPECT_TRUE(allASCII.endsWith(allASCIIMixedCase, TextCaseASCIIInsensitive));
373 String allASCIIDifferent("foo");
374 EXPECT_FALSE(allASCII.endsWith(allASCIIDifferent, TextCaseASCIIInsensitive));
375 String nonASCII = String::fromUTF8("LIN\xE2\x84\xAA");
376 EXPECT_FALSE(allASCII.endsWith(nonASCII, TextCaseASCIIInsensitive));
377 EXPECT_TRUE(allASCII.endsWith(nonASCII.lower(), TextCaseASCIIInsensitive));
378
379 EXPECT_FALSE(nonASCII.endsWith(allASCII, TextCaseASCIIInsensitive));
380 EXPECT_FALSE(nonASCII.endsWith(allASCIILowerCase, TextCaseASCIIInsensitive));
381 EXPECT_FALSE(nonASCII.endsWith(allASCIIMixedCase, TextCaseASCIIInsensitive));
382 EXPECT_FALSE(nonASCII.endsWith(allASCIIDifferent, TextCaseASCIIInsensitive));
383 }
384
385 TEST(StringTest, EqualIgnoringASCIICase) {
386 String allASCII("LINK");
387 String allASCIILowerCase("link");
388 EXPECT_TRUE(equalIgnoringASCIICase(allASCII, allASCIILowerCase));
389 String allASCIIMixedCase("lInK");
390 EXPECT_TRUE(equalIgnoringASCIICase(allASCII, allASCIIMixedCase));
391 String allASCIIDifferent("foo");
392 EXPECT_FALSE(equalIgnoringASCIICase(allASCII, allASCIIDifferent));
393 String nonASCII = String::fromUTF8("LIN\xE2\x84\xAA");
394 EXPECT_FALSE(equalIgnoringASCIICase(allASCII, nonASCII));
395 EXPECT_TRUE(equalIgnoringASCIICase(allASCII, nonASCII.lower()));
396
397 EXPECT_FALSE(equalIgnoringASCIICase(nonASCII, allASCII));
398 EXPECT_FALSE(equalIgnoringASCIICase(nonASCII, allASCIILowerCase));
399 EXPECT_FALSE(equalIgnoringASCIICase(nonASCII, allASCIIMixedCase));
400 EXPECT_FALSE(equalIgnoringASCIICase(nonASCII, allASCIIDifferent));
401 }
402
403 TEST(StringTest, FindIgnoringASCIICase) {
404 String needle = String::fromUTF8("a\xCC\x88qa\xCC\x88");
405
406 // Multiple matches, non-overlapping
407 String haystack1 = String::fromUTF8(
408 "aA\xCC\x88QA\xCC\x88sA\xCC\x88qa\xCC\x88rfi\xC3\xA4q\xC3\xA4");
409 EXPECT_EQ(1u, haystack1.findIgnoringASCIICase(needle));
410 EXPECT_EQ(7u, haystack1.findIgnoringASCIICase(needle, 2));
411 EXPECT_EQ(kNotFound, haystack1.findIgnoringASCIICase(needle, 8));
412
413 // Multiple matches, overlapping
414 String haystack2 = String::fromUTF8("aA\xCC\x88QA\xCC\x88qa\xCC\x88rfi");
415 EXPECT_EQ(1u, haystack2.findIgnoringASCIICase(needle));
416 EXPECT_EQ(4u, haystack2.findIgnoringASCIICase(needle, 2));
417 EXPECT_EQ(kNotFound, haystack2.findIgnoringASCIICase(needle, 5));
418 }
419
420 TEST(StringTest, Lower) {
421 EXPECT_STREQ("link", String("LINK").lower().ascii().data());
422 EXPECT_STREQ("link", String("lInk").lower().ascii().data());
423 EXPECT_STREQ("lin\xE1k", String("lIn\xC1k").lower().latin1().data());
424 // U+212A -> k
425 EXPECT_STREQ("link",
426 String::fromUTF8("LIN\xE2\x84\xAA").lower().utf8().data());
427 }
428
429 TEST(StringTest, Upper) {
430 EXPECT_STREQ("CROSS", String("cross").upper().utf8().data());
431 // U+017F -> S
432 EXPECT_STREQ("CROSS", String::fromUTF8("cro\xC5\xBFs").upper().utf8().data());
433 }
434
435 TEST(StringTest, Ensure16Bit) {
436 String string8("8bit");
437 EXPECT_TRUE(string8.is8Bit());
438 string8.ensure16Bit();
439 EXPECT_FALSE(string8.is8Bit());
440 EXPECT_EQ("8bit", string8);
441
442 String string16(reinterpret_cast<const UChar*>(u"16bit"));
443 EXPECT_FALSE(string16.is8Bit());
444 string16.ensure16Bit();
445 EXPECT_FALSE(string16.is8Bit());
446 EXPECT_EQ("16bit", string16);
447
448 String empty8(StringImpl::empty);
449 EXPECT_TRUE(empty8.is8Bit());
450 empty8.ensure16Bit();
451 EXPECT_FALSE(empty8.is8Bit());
452 EXPECT_TRUE(empty8.isEmpty());
453 EXPECT_FALSE(empty8.isNull());
454
455 String empty16(StringImpl::empty16Bit);
456 EXPECT_FALSE(empty16.is8Bit());
457 empty16.ensure16Bit();
458 EXPECT_FALSE(empty16.is8Bit());
459 EXPECT_TRUE(empty16.isEmpty());
460 EXPECT_FALSE(empty16.isNull());
461
462 String nullString;
463 nullString.ensure16Bit();
464 EXPECT_TRUE(nullString.isNull());
465 }
466
467 CString toCStringThroughPrinter(const String& string) {
468 std::ostringstream output;
469 output << string;
470 const std::string& result = output.str();
471 return CString(result.data(), result.length());
472 }
473
474 TEST(StringTest, StringPrinter) {
475 EXPECT_EQ(CString("\"Hello!\""), toCStringThroughPrinter("Hello!"));
476 EXPECT_EQ(CString("\"\\\"\""), toCStringThroughPrinter("\""));
477 EXPECT_EQ(CString("\"\\\\\""), toCStringThroughPrinter("\\"));
478 EXPECT_EQ(
479 CString("\"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\""),
480 toCStringThroughPrinter(String("\x00\x01\x02\x03\x04\x05\x06\x07", 8)));
481 EXPECT_EQ(
482 CString("\"\\u0008\\t\\n\\u000B\\u000C\\r\\u000E\\u000F\""),
483 toCStringThroughPrinter(String("\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", 8)));
484 EXPECT_EQ(
485 CString("\"\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\""),
486 toCStringThroughPrinter(String("\x10\x11\x12\x13\x14\x15\x16\x17", 8)));
487 EXPECT_EQ(
488 CString("\"\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F\""),
489 toCStringThroughPrinter(String("\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 8)));
490 EXPECT_EQ(CString("\"\\u007F\\u0080\\u0081\""),
491 toCStringThroughPrinter("\x7F\x80\x81"));
492 EXPECT_EQ(CString("\"\""), toCStringThroughPrinter(emptyString));
493 EXPECT_EQ(CString("<null>"), toCStringThroughPrinter(String()));
494
495 static const UChar unicodeSample[] = {0x30C6, 0x30B9,
496 0x30C8}; // "Test" in Japanese.
497 EXPECT_EQ(CString("\"\\u30C6\\u30B9\\u30C8\""),
498 toCStringThroughPrinter(
499 String(unicodeSample, WTF_ARRAY_LENGTH(unicodeSample))));
500 }
501
502 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698