| 1 | /* |
| 2 | * Copyright (C) 2012-2017 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 "config.h" |
| 27 | |
| 28 | #include "WTFStringUtilities.h" |
| 29 | #include <limits> |
| 30 | #include <wtf/MathExtras.h> |
| 31 | #include <wtf/text/CString.h> |
| 32 | #include <wtf/text/WTFString.h> |
| 33 | |
| 34 | namespace TestWebKitAPI { |
| 35 | |
| 36 | TEST(WTF, StringCreationFromLiteral) |
| 37 | { |
| 38 | String stringFromLiteralViaASCII("Explicit construction syntax"_s ); |
| 39 | EXPECT_EQ(strlen("Explicit construction syntax" ), stringFromLiteralViaASCII.length()); |
| 40 | EXPECT_EQ("Explicit construction syntax" , stringFromLiteralViaASCII); |
| 41 | EXPECT_TRUE(stringFromLiteralViaASCII.is8Bit()); |
| 42 | EXPECT_EQ(String("Explicit construction syntax" ), stringFromLiteralViaASCII); |
| 43 | |
| 44 | String stringFromLiteral = "String Literal"_str ; |
| 45 | EXPECT_EQ(strlen("String Literal" ), stringFromLiteral.length()); |
| 46 | EXPECT_EQ("String Literal" , stringFromLiteral); |
| 47 | EXPECT_TRUE(stringFromLiteral.is8Bit()); |
| 48 | EXPECT_EQ(String("String Literal" ), stringFromLiteral); |
| 49 | |
| 50 | String stringWithTemplate("Template Literal" , String::ConstructFromLiteral); |
| 51 | EXPECT_EQ(strlen("Template Literal" ), stringWithTemplate.length()); |
| 52 | EXPECT_EQ("Template Literal" , stringWithTemplate); |
| 53 | EXPECT_TRUE(stringWithTemplate.is8Bit()); |
| 54 | EXPECT_EQ(String("Template Literal" ), stringWithTemplate); |
| 55 | } |
| 56 | |
| 57 | TEST(WTF, StringASCII) |
| 58 | { |
| 59 | CString output; |
| 60 | |
| 61 | // Null String. |
| 62 | output = String().ascii(); |
| 63 | EXPECT_STREQ("" , output.data()); |
| 64 | |
| 65 | // Empty String. |
| 66 | output = emptyString().ascii(); |
| 67 | EXPECT_STREQ("" , output.data()); |
| 68 | |
| 69 | // Regular String. |
| 70 | output = String("foobar"_s ).ascii(); |
| 71 | EXPECT_STREQ("foobar" , output.data()); |
| 72 | } |
| 73 | |
| 74 | static inline const char* testStringNumberFixedPrecision(double number) |
| 75 | { |
| 76 | static char testBuffer[100] = { }; |
| 77 | std::strncpy(testBuffer, String::numberToStringFixedPrecision(number).utf8().data(), 99); |
| 78 | return testBuffer; |
| 79 | } |
| 80 | |
| 81 | TEST(WTF, StringNumberFixedPrecision) |
| 82 | { |
| 83 | using Limits = std::numeric_limits<double>; |
| 84 | |
| 85 | EXPECT_STREQ("Infinity" , testStringNumberFixedPrecision(Limits::infinity())); |
| 86 | EXPECT_STREQ("-Infinity" , testStringNumberFixedPrecision(-Limits::infinity())); |
| 87 | |
| 88 | EXPECT_STREQ("NaN" , testStringNumberFixedPrecision(-Limits::quiet_NaN())); |
| 89 | |
| 90 | EXPECT_STREQ("0" , testStringNumberFixedPrecision(0)); |
| 91 | EXPECT_STREQ("0" , testStringNumberFixedPrecision(-0)); |
| 92 | |
| 93 | EXPECT_STREQ("2.22507e-308" , testStringNumberFixedPrecision(Limits::min())); |
| 94 | EXPECT_STREQ("-1.79769e+308" , testStringNumberFixedPrecision(Limits::lowest())); |
| 95 | EXPECT_STREQ("1.79769e+308" , testStringNumberFixedPrecision(Limits::max())); |
| 96 | |
| 97 | EXPECT_STREQ("3.14159" , testStringNumberFixedPrecision(piDouble)); |
| 98 | EXPECT_STREQ("3.14159" , testStringNumberFixedPrecision(piFloat)); |
| 99 | EXPECT_STREQ("1.5708" , testStringNumberFixedPrecision(piOverTwoDouble)); |
| 100 | EXPECT_STREQ("1.5708" , testStringNumberFixedPrecision(piOverTwoFloat)); |
| 101 | EXPECT_STREQ("0.785398" , testStringNumberFixedPrecision(piOverFourDouble)); |
| 102 | EXPECT_STREQ("0.785398" , testStringNumberFixedPrecision(piOverFourFloat)); |
| 103 | |
| 104 | EXPECT_STREQ("2.71828" , testStringNumberFixedPrecision(2.71828182845904523536028747135266249775724709369995)); |
| 105 | |
| 106 | EXPECT_STREQ("2.99792e+8" , testStringNumberFixedPrecision(299792458)); |
| 107 | |
| 108 | EXPECT_STREQ("1.61803" , testStringNumberFixedPrecision(1.6180339887498948482)); |
| 109 | |
| 110 | EXPECT_STREQ("1000" , testStringNumberFixedPrecision(1e3)); |
| 111 | EXPECT_STREQ("1e+10" , testStringNumberFixedPrecision(1e10)); |
| 112 | EXPECT_STREQ("1e+20" , testStringNumberFixedPrecision(1e20)); |
| 113 | EXPECT_STREQ("1e+21" , testStringNumberFixedPrecision(1e21)); |
| 114 | EXPECT_STREQ("1e+30" , testStringNumberFixedPrecision(1e30)); |
| 115 | |
| 116 | EXPECT_STREQ("1100" , testStringNumberFixedPrecision(1.1e3)); |
| 117 | EXPECT_STREQ("1.1e+10" , testStringNumberFixedPrecision(1.1e10)); |
| 118 | EXPECT_STREQ("1.1e+20" , testStringNumberFixedPrecision(1.1e20)); |
| 119 | EXPECT_STREQ("1.1e+21" , testStringNumberFixedPrecision(1.1e21)); |
| 120 | EXPECT_STREQ("1.1e+30" , testStringNumberFixedPrecision(1.1e30)); |
| 121 | } |
| 122 | |
| 123 | static inline const char* testStringNumberFixedWidth(double number) |
| 124 | { |
| 125 | static char testBuffer[100] = { }; |
| 126 | std::strncpy(testBuffer, String::numberToStringFixedWidth(number, 6).utf8().data(), 99); |
| 127 | return testBuffer; |
| 128 | } |
| 129 | |
| 130 | TEST(WTF, StringNumberFixedWidth) |
| 131 | { |
| 132 | using Limits = std::numeric_limits<double>; |
| 133 | |
| 134 | EXPECT_STREQ("Infinity" , testStringNumberFixedWidth(Limits::infinity())); |
| 135 | EXPECT_STREQ("-Infinity" , testStringNumberFixedWidth(-Limits::infinity())); |
| 136 | |
| 137 | EXPECT_STREQ("NaN" , testStringNumberFixedWidth(-Limits::quiet_NaN())); |
| 138 | |
| 139 | EXPECT_STREQ("0.000000" , testStringNumberFixedWidth(0)); |
| 140 | EXPECT_STREQ("0.000000" , testStringNumberFixedWidth(-0)); |
| 141 | |
| 142 | EXPECT_STREQ("0.000000" , testStringNumberFixedWidth(Limits::min())); |
| 143 | EXPECT_STREQ("" , testStringNumberFixedWidth(Limits::lowest())); |
| 144 | EXPECT_STREQ("" , testStringNumberFixedWidth(Limits::max())); |
| 145 | |
| 146 | EXPECT_STREQ("3.141593" , testStringNumberFixedWidth(piDouble)); |
| 147 | EXPECT_STREQ("3.141593" , testStringNumberFixedWidth(piFloat)); |
| 148 | EXPECT_STREQ("1.570796" , testStringNumberFixedWidth(piOverTwoDouble)); |
| 149 | EXPECT_STREQ("1.570796" , testStringNumberFixedWidth(piOverTwoFloat)); |
| 150 | EXPECT_STREQ("0.785398" , testStringNumberFixedWidth(piOverFourDouble)); |
| 151 | EXPECT_STREQ("0.785398" , testStringNumberFixedWidth(piOverFourFloat)); |
| 152 | |
| 153 | EXPECT_STREQ("2.718282" , testStringNumberFixedWidth(2.71828182845904523536028747135266249775724709369995)); |
| 154 | |
| 155 | EXPECT_STREQ("299792458.000000" , testStringNumberFixedWidth(299792458)); |
| 156 | |
| 157 | EXPECT_STREQ("1.618034" , testStringNumberFixedWidth(1.6180339887498948482)); |
| 158 | |
| 159 | EXPECT_STREQ("1000.000000" , testStringNumberFixedWidth(1e3)); |
| 160 | EXPECT_STREQ("10000000000.000000" , testStringNumberFixedWidth(1e10)); |
| 161 | EXPECT_STREQ("100000000000000000000.000000" , testStringNumberFixedWidth(1e20)); |
| 162 | EXPECT_STREQ("1000000000000000000000.000000" , testStringNumberFixedWidth(1e21)); |
| 163 | EXPECT_STREQ("1000000000000000019884624838656.000000" , testStringNumberFixedWidth(1e30)); |
| 164 | |
| 165 | EXPECT_STREQ("1100.000000" , testStringNumberFixedWidth(1.1e3)); |
| 166 | EXPECT_STREQ("11000000000.000000" , testStringNumberFixedWidth(1.1e10)); |
| 167 | EXPECT_STREQ("110000000000000000000.000000" , testStringNumberFixedWidth(1.1e20)); |
| 168 | EXPECT_STREQ("1100000000000000000000.000000" , testStringNumberFixedWidth(1.1e21)); |
| 169 | EXPECT_STREQ("1099999999999999993725589651456.000000" , testStringNumberFixedWidth(1.1e30)); |
| 170 | } |
| 171 | |
| 172 | static inline const char* testStringNumber(double number) |
| 173 | { |
| 174 | static char testBuffer[100] = { }; |
| 175 | std::strncpy(testBuffer, String::numberToStringECMAScript(number).utf8().data(), 99); |
| 176 | return testBuffer; |
| 177 | } |
| 178 | |
| 179 | TEST(WTF, StringNumber) |
| 180 | { |
| 181 | using Limits = std::numeric_limits<double>; |
| 182 | |
| 183 | EXPECT_STREQ("Infinity" , testStringNumber(Limits::infinity())); |
| 184 | EXPECT_STREQ("-Infinity" , testStringNumber(-Limits::infinity())); |
| 185 | |
| 186 | EXPECT_STREQ("NaN" , testStringNumber(-Limits::quiet_NaN())); |
| 187 | |
| 188 | EXPECT_STREQ("0" , testStringNumber(0)); |
| 189 | EXPECT_STREQ("0" , testStringNumber(-0)); |
| 190 | |
| 191 | EXPECT_STREQ("2.2250738585072014e-308" , testStringNumber(Limits::min())); |
| 192 | EXPECT_STREQ("-1.7976931348623157e+308" , testStringNumber(Limits::lowest())); |
| 193 | EXPECT_STREQ("1.7976931348623157e+308" , testStringNumber(Limits::max())); |
| 194 | |
| 195 | EXPECT_STREQ("3.141592653589793" , testStringNumber(piDouble)); |
| 196 | EXPECT_STREQ("3.1415927410125732" , testStringNumber(piFloat)); |
| 197 | EXPECT_STREQ("1.5707963267948966" , testStringNumber(piOverTwoDouble)); |
| 198 | EXPECT_STREQ("1.5707963705062866" , testStringNumber(piOverTwoFloat)); |
| 199 | EXPECT_STREQ("0.7853981633974483" , testStringNumber(piOverFourDouble)); |
| 200 | EXPECT_STREQ("0.7853981852531433" , testStringNumber(piOverFourFloat)); |
| 201 | |
| 202 | EXPECT_STREQ("2.718281828459045" , testStringNumber(2.71828182845904523536028747135266249775724709369995)); |
| 203 | |
| 204 | EXPECT_STREQ("299792458" , testStringNumber(299792458)); |
| 205 | |
| 206 | EXPECT_STREQ("1.618033988749895" , testStringNumber(1.6180339887498948482)); |
| 207 | |
| 208 | EXPECT_STREQ("1000" , testStringNumber(1e3)); |
| 209 | EXPECT_STREQ("10000000000" , testStringNumber(1e10)); |
| 210 | EXPECT_STREQ("100000000000000000000" , testStringNumber(1e20)); |
| 211 | EXPECT_STREQ("1e+21" , testStringNumber(1e21)); |
| 212 | EXPECT_STREQ("1e+30" , testStringNumber(1e30)); |
| 213 | |
| 214 | EXPECT_STREQ("1100" , testStringNumber(1.1e3)); |
| 215 | EXPECT_STREQ("11000000000" , testStringNumber(1.1e10)); |
| 216 | EXPECT_STREQ("110000000000000000000" , testStringNumber(1.1e20)); |
| 217 | EXPECT_STREQ("1.1e+21" , testStringNumber(1.1e21)); |
| 218 | EXPECT_STREQ("1.1e+30" , testStringNumber(1.1e30)); |
| 219 | } |
| 220 | |
| 221 | TEST(WTF, StringReplaceWithLiteral) |
| 222 | { |
| 223 | // Cases for 8Bit source. |
| 224 | String testString = "1224" ; |
| 225 | EXPECT_TRUE(testString.is8Bit()); |
| 226 | testString.replaceWithLiteral('2', "" ); |
| 227 | EXPECT_STREQ("14" , testString.utf8().data()); |
| 228 | |
| 229 | testString = "1224" ; |
| 230 | EXPECT_TRUE(testString.is8Bit()); |
| 231 | testString.replaceWithLiteral('2', "3" ); |
| 232 | EXPECT_STREQ("1334" , testString.utf8().data()); |
| 233 | |
| 234 | testString = "1224" ; |
| 235 | EXPECT_TRUE(testString.is8Bit()); |
| 236 | testString.replaceWithLiteral('2', "555" ); |
| 237 | EXPECT_STREQ("15555554" , testString.utf8().data()); |
| 238 | |
| 239 | testString = "1224" ; |
| 240 | EXPECT_TRUE(testString.is8Bit()); |
| 241 | testString.replaceWithLiteral('3', "NotFound" ); |
| 242 | EXPECT_STREQ("1224" , testString.utf8().data()); |
| 243 | |
| 244 | // Cases for 16Bit source. |
| 245 | testString = String::fromUTF8("résumé" ); |
| 246 | EXPECT_FALSE(testString.is8Bit()); |
| 247 | testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "e" ); |
| 248 | EXPECT_STREQ("resume" , testString.utf8().data()); |
| 249 | |
| 250 | testString = String::fromUTF8("résumé" ); |
| 251 | EXPECT_FALSE(testString.is8Bit()); |
| 252 | testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "" ); |
| 253 | EXPECT_STREQ("rsum" , testString.utf8().data()); |
| 254 | |
| 255 | testString = String::fromUTF8("résumé" ); |
| 256 | EXPECT_FALSE(testString.is8Bit()); |
| 257 | testString.replaceWithLiteral('3', "NotFound" ); |
| 258 | EXPECT_STREQ("résumé" , testString.utf8().data()); |
| 259 | } |
| 260 | |
| 261 | TEST(WTF, StringIsolatedCopy) |
| 262 | { |
| 263 | String original = "1234" ; |
| 264 | auto copy = WTFMove(original).isolatedCopy(); |
| 265 | EXPECT_FALSE(original.impl() == copy.impl()); |
| 266 | } |
| 267 | |
| 268 | TEST(WTF, StringToInt) |
| 269 | { |
| 270 | bool ok = false; |
| 271 | |
| 272 | EXPECT_EQ(0, String().toInt()); |
| 273 | EXPECT_EQ(0, String().toInt(&ok)); |
| 274 | EXPECT_FALSE(ok); |
| 275 | |
| 276 | EXPECT_EQ(0, emptyString().toInt()); |
| 277 | EXPECT_EQ(0, emptyString().toInt(&ok)); |
| 278 | EXPECT_FALSE(ok); |
| 279 | |
| 280 | EXPECT_EQ(0, String("0" ).toInt()); |
| 281 | EXPECT_EQ(0, String("0" ).toInt(&ok)); |
| 282 | EXPECT_TRUE(ok); |
| 283 | |
| 284 | EXPECT_EQ(1, String("1" ).toInt()); |
| 285 | EXPECT_EQ(1, String("1" ).toInt(&ok)); |
| 286 | EXPECT_TRUE(ok); |
| 287 | |
| 288 | EXPECT_EQ(2147483647, String("2147483647" ).toInt()); |
| 289 | EXPECT_EQ(2147483647, String("2147483647" ).toInt(&ok)); |
| 290 | EXPECT_TRUE(ok); |
| 291 | |
| 292 | EXPECT_EQ(0, String("2147483648" ).toInt()); |
| 293 | EXPECT_EQ(0, String("2147483648" ).toInt(&ok)); |
| 294 | EXPECT_FALSE(ok); |
| 295 | |
| 296 | EXPECT_EQ(-2147483648, String("-2147483648" ).toInt()); |
| 297 | EXPECT_EQ(-2147483648, String("-2147483648" ).toInt(&ok)); |
| 298 | EXPECT_TRUE(ok); |
| 299 | |
| 300 | EXPECT_EQ(0, String("-2147483649" ).toInt()); |
| 301 | EXPECT_EQ(0, String("-2147483649" ).toInt(&ok)); |
| 302 | EXPECT_FALSE(ok); |
| 303 | |
| 304 | // fail if we see leading junk |
| 305 | EXPECT_EQ(0, String("x1" ).toInt()); |
| 306 | EXPECT_EQ(0, String("x1" ).toInt(&ok)); |
| 307 | EXPECT_FALSE(ok); |
| 308 | |
| 309 | // succeed if we see leading spaces |
| 310 | EXPECT_EQ(1, String(" 1" ).toInt()); |
| 311 | EXPECT_EQ(1, String(" 1" ).toInt(&ok)); |
| 312 | EXPECT_TRUE(ok); |
| 313 | |
| 314 | // silently ignore trailing junk |
| 315 | EXPECT_EQ(1, String("1x" ).toInt()); |
| 316 | EXPECT_EQ(1, String("1x" ).toInt(&ok)); |
| 317 | EXPECT_TRUE(ok); |
| 318 | } |
| 319 | |
| 320 | TEST(WTF, StringToDouble) |
| 321 | { |
| 322 | bool ok = false; |
| 323 | |
| 324 | EXPECT_EQ(0.0, String().toDouble()); |
| 325 | EXPECT_EQ(0.0, String().toDouble(&ok)); |
| 326 | EXPECT_FALSE(ok); |
| 327 | |
| 328 | EXPECT_EQ(0.0, emptyString().toDouble()); |
| 329 | EXPECT_EQ(0.0, emptyString().toDouble(&ok)); |
| 330 | EXPECT_FALSE(ok); |
| 331 | |
| 332 | EXPECT_EQ(0.0, String("0" ).toDouble()); |
| 333 | EXPECT_EQ(0.0, String("0" ).toDouble(&ok)); |
| 334 | EXPECT_TRUE(ok); |
| 335 | |
| 336 | EXPECT_EQ(1.0, String("1" ).toDouble()); |
| 337 | EXPECT_EQ(1.0, String("1" ).toDouble(&ok)); |
| 338 | EXPECT_TRUE(ok); |
| 339 | |
| 340 | // fail if we see leading junk |
| 341 | EXPECT_EQ(0.0, String("x1" ).toDouble()); |
| 342 | EXPECT_EQ(0.0, String("x1" ).toDouble(&ok)); |
| 343 | EXPECT_FALSE(ok); |
| 344 | |
| 345 | // succeed if we see leading spaces |
| 346 | EXPECT_EQ(1.0, String(" 1" ).toDouble()); |
| 347 | EXPECT_EQ(1.0, String(" 1" ).toDouble(&ok)); |
| 348 | EXPECT_TRUE(ok); |
| 349 | |
| 350 | // ignore trailing junk, but return false for "ok" |
| 351 | // FIXME: This is an inconsistency with toInt, which always guarantees |
| 352 | // it will return 0 if it's also going to return false for ok. |
| 353 | EXPECT_EQ(1.0, String("1x" ).toDouble()); |
| 354 | EXPECT_EQ(1.0, String("1x" ).toDouble(&ok)); |
| 355 | EXPECT_FALSE(ok); |
| 356 | |
| 357 | // parse only numbers, not special values such as "infinity" |
| 358 | EXPECT_EQ(0.0, String("infinity" ).toDouble()); |
| 359 | EXPECT_EQ(0.0, String("infinity" ).toDouble(&ok)); |
| 360 | EXPECT_FALSE(ok); |
| 361 | |
| 362 | // parse only numbers, not special values such as "nan" |
| 363 | EXPECT_EQ(0.0, String("nan" ).toDouble()); |
| 364 | EXPECT_EQ(0.0, String("nan" ).toDouble(&ok)); |
| 365 | EXPECT_FALSE(ok); |
| 366 | } |
| 367 | |
| 368 | TEST(WTF, StringhasInfixStartingAt) |
| 369 | { |
| 370 | EXPECT_TRUE(String("Test" ).is8Bit()); |
| 371 | EXPECT_TRUE(String("Te" ).is8Bit()); |
| 372 | EXPECT_TRUE(String("st" ).is8Bit()); |
| 373 | EXPECT_TRUE(String("Test" ).hasInfixStartingAt(String("Te" ), 0)); |
| 374 | EXPECT_FALSE(String("Test" ).hasInfixStartingAt(String("Te" ), 2)); |
| 375 | EXPECT_TRUE(String("Test" ).hasInfixStartingAt(String("st" ), 2)); |
| 376 | EXPECT_FALSE(String("Test" ).hasInfixStartingAt(String("ST" ), 2)); |
| 377 | |
| 378 | EXPECT_FALSE(String::fromUTF8("ä¸å›½" ).is8Bit()); |
| 379 | EXPECT_FALSE(String::fromUTF8("ä¸" ).is8Bit()); |
| 380 | EXPECT_FALSE(String::fromUTF8("国" ).is8Bit()); |
| 381 | EXPECT_TRUE(String::fromUTF8("ä¸å›½" ).hasInfixStartingAt(String::fromUTF8("ä¸" ), 0)); |
| 382 | EXPECT_FALSE(String::fromUTF8("ä¸å›½" ).hasInfixStartingAt(String::fromUTF8("ä¸" ), 1)); |
| 383 | EXPECT_TRUE(String::fromUTF8("ä¸å›½" ).hasInfixStartingAt(String::fromUTF8("国" ), 1)); |
| 384 | |
| 385 | EXPECT_FALSE(String::fromUTF8("ä¸å›½" ).hasInfixStartingAt(String("Te" ), 0)); |
| 386 | EXPECT_FALSE(String("Test" ).hasInfixStartingAt(String::fromUTF8("ä¸" ), 2)); |
| 387 | } |
| 388 | |
| 389 | TEST(WTF, StringExistingHash) |
| 390 | { |
| 391 | String string1("Template Literal" ); |
| 392 | EXPECT_FALSE(string1.isNull()); |
| 393 | EXPECT_FALSE(string1.impl()->hasHash()); |
| 394 | string1.impl()->hash(); |
| 395 | EXPECT_EQ(string1.existingHash(), string1.impl()->existingHash()); |
| 396 | String string2; |
| 397 | EXPECT_EQ(string2.existingHash(), 0u); |
| 398 | } |
| 399 | |
| 400 | TEST(WTF, StringUnicodeEqualUCharArray) |
| 401 | { |
| 402 | String string1("abc" ); |
| 403 | EXPECT_FALSE(string1.isNull()); |
| 404 | EXPECT_TRUE(string1.is8Bit()); |
| 405 | UChar ab[] = { 'a', 'b' }; |
| 406 | UChar abc[] = { 'a', 'b', 'c' }; |
| 407 | UChar abcd[] = { 'a', 'b', 'c', 'd' }; |
| 408 | UChar aBc[] = { 'a', 'B', 'c' }; |
| 409 | EXPECT_FALSE(equal(string1, ab)); |
| 410 | EXPECT_TRUE(equal(string1, abc)); |
| 411 | EXPECT_FALSE(equal(string1, abcd)); |
| 412 | EXPECT_FALSE(equal(string1, aBc)); |
| 413 | |
| 414 | String string2(abc, 3); |
| 415 | EXPECT_FALSE(equal(string2, ab)); |
| 416 | EXPECT_TRUE(equal(string2, abc)); |
| 417 | EXPECT_FALSE(equal(string2, abcd)); |
| 418 | EXPECT_FALSE(equal(string2, aBc)); |
| 419 | } |
| 420 | |
| 421 | TEST(WTF, StringRightBasic) |
| 422 | { |
| 423 | auto reference = String::fromUTF8("Cappuccino" ); |
| 424 | EXPECT_EQ(String::fromUTF8("" ), reference.right(0)); |
| 425 | EXPECT_EQ(String::fromUTF8("o" ), reference.right(1)); |
| 426 | EXPECT_EQ(String::fromUTF8("no" ), reference.right(2)); |
| 427 | EXPECT_EQ(String::fromUTF8("ino" ), reference.right(3)); |
| 428 | EXPECT_EQ(String::fromUTF8("cino" ), reference.right(4)); |
| 429 | EXPECT_EQ(String::fromUTF8("ccino" ), reference.right(5)); |
| 430 | EXPECT_EQ(String::fromUTF8("uccino" ), reference.right(6)); |
| 431 | EXPECT_EQ(String::fromUTF8("puccino" ), reference.right(7)); |
| 432 | EXPECT_EQ(String::fromUTF8("ppuccino" ), reference.right(8)); |
| 433 | EXPECT_EQ(String::fromUTF8("appuccino" ), reference.right(9)); |
| 434 | EXPECT_EQ(String::fromUTF8("Cappuccino" ), reference.right(10)); |
| 435 | } |
| 436 | |
| 437 | TEST(WTF, StringLeftBasic) |
| 438 | { |
| 439 | auto reference = String::fromUTF8("Cappuccino" ); |
| 440 | EXPECT_EQ(String::fromUTF8("" ), reference.left(0)); |
| 441 | EXPECT_EQ(String::fromUTF8("C" ), reference.left(1)); |
| 442 | EXPECT_EQ(String::fromUTF8("Ca" ), reference.left(2)); |
| 443 | EXPECT_EQ(String::fromUTF8("Cap" ), reference.left(3)); |
| 444 | EXPECT_EQ(String::fromUTF8("Capp" ), reference.left(4)); |
| 445 | EXPECT_EQ(String::fromUTF8("Cappu" ), reference.left(5)); |
| 446 | EXPECT_EQ(String::fromUTF8("Cappuc" ), reference.left(6)); |
| 447 | EXPECT_EQ(String::fromUTF8("Cappucc" ), reference.left(7)); |
| 448 | EXPECT_EQ(String::fromUTF8("Cappucci" ), reference.left(8)); |
| 449 | EXPECT_EQ(String::fromUTF8("Cappuccin" ), reference.left(9)); |
| 450 | EXPECT_EQ(String::fromUTF8("Cappuccino" ), reference.left(10)); |
| 451 | } |
| 452 | |
| 453 | TEST(WTF, StringReverseFindBasic) |
| 454 | { |
| 455 | auto reference = String::fromUTF8("Cappuccino" ); |
| 456 | EXPECT_EQ(reference.reverseFind('o'), 9U); |
| 457 | EXPECT_EQ(reference.reverseFind('n'), 8U); |
| 458 | EXPECT_EQ(reference.reverseFind('c'), 6U); |
| 459 | EXPECT_EQ(reference.reverseFind('p'), 3U); |
| 460 | EXPECT_EQ(reference.reverseFind('k'), notFound); |
| 461 | |
| 462 | EXPECT_EQ(reference.reverseFind('o', 8), notFound); |
| 463 | EXPECT_EQ(reference.reverseFind('c', 8), 6U); |
| 464 | EXPECT_EQ(reference.reverseFind('c', 6), 6U); |
| 465 | EXPECT_EQ(reference.reverseFind('c', 5), 5U); |
| 466 | EXPECT_EQ(reference.reverseFind('c', 4), notFound); |
| 467 | } |
| 468 | |
| 469 | TEST(WTF, StringSplitWithConsecutiveSeparators) |
| 470 | { |
| 471 | String string { " This is a sentence. " }; |
| 472 | |
| 473 | Vector<String> actual = string.split(' '); |
| 474 | Vector<String> expected { "This" , "is" , "a" , "sentence." }; |
| 475 | ASSERT_EQ(expected.size(), actual.size()); |
| 476 | for (auto i = 0u; i < actual.size(); ++i) |
| 477 | EXPECT_STREQ(expected[i].utf8().data(), actual[i].utf8().data()) << "Vectors differ at index " << i; |
| 478 | |
| 479 | actual = string.splitAllowingEmptyEntries(' '); |
| 480 | expected = { "" , "This" , "" , "" , "" , "" , "is" , "" , "a" , "" , "" , "" , "" , "" , "" , "sentence." , "" }; |
| 481 | ASSERT_EQ(expected.size(), actual.size()); |
| 482 | for (auto i = 0u; i < actual.size(); ++i) |
| 483 | EXPECT_STREQ(expected[i].utf8().data(), actual[i].utf8().data()) << "Vectors differ at index " << i; |
| 484 | } |
| 485 | |
| 486 | } // namespace TestWebKitAPI |
| 487 | |