| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Apple Inc. All rights reserved. | 3 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 // append() has special code paths for String backed StringView instead of | 110 // append() has special code paths for String backed StringView instead of |
| 111 // just char* backed ones. | 111 // just char* backed ones. |
| 112 StringBuilder builder6; | 112 StringBuilder builder6; |
| 113 builder6.append(String("ghi"), 1, 2); | 113 builder6.append(String("ghi"), 1, 2); |
| 114 expectBuilderContent("hi", builder6); | 114 expectBuilderContent("hi", builder6); |
| 115 | 115 |
| 116 // Test appending UChar32 characters to StringBuilder. | 116 // Test appending UChar32 characters to StringBuilder. |
| 117 StringBuilder builderForUChar32Append; | 117 StringBuilder builderForUChar32Append; |
| 118 UChar32 frakturAChar = 0x1D504; | 118 UChar32 frakturAChar = 0x1D504; |
| 119 builderForUChar32Append.append(frakturAChar); // The fraktur A is not in the
BMP, so it's two UTF-16 code units long. | 119 builderForUChar32Append.append(frakturAChar); // The fraktur A is not in the
BMP, so it's two UTF-16 code units long. |
| 120 EXPECT_FALSE(builderForUChar32Append.is8Bit()); |
| 120 EXPECT_EQ(2U, builderForUChar32Append.length()); | 121 EXPECT_EQ(2U, builderForUChar32Append.length()); |
| 121 builderForUChar32Append.append(static_cast<UChar32>('A')); | 122 builderForUChar32Append.append(static_cast<UChar32>('A')); |
| 122 EXPECT_EQ(3U, builderForUChar32Append.length()); | 123 EXPECT_EQ(3U, builderForUChar32Append.length()); |
| 123 const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar
), 'A' }; | 124 const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar
), 'A' }; |
| 124 expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), bui
lderForUChar32Append); | 125 expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), bui
lderForUChar32Append); |
| 125 } | 126 } |
| 126 | 127 |
| 127 TEST(StringBuilderTest, AppendSharingImpl) | 128 TEST(StringBuilderTest, AppendSharingImpl) |
| 128 { | 129 { |
| 129 String string("abc"); | 130 String string("abc"); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 builder2.toString(); // Test after reifyString(). | 236 builder2.toString(); // Test after reifyString(). |
| 236 EXPECT_TRUE(builder1 != builder2); | 237 EXPECT_TRUE(builder1 != builder2); |
| 237 | 238 |
| 238 builder2.resize(3); | 239 builder2.resize(3); |
| 239 EXPECT_TRUE(builder1 == builder2); | 240 EXPECT_TRUE(builder1 == builder2); |
| 240 | 241 |
| 241 builder1.toString(); // Test after reifyString(). | 242 builder1.toString(); // Test after reifyString(). |
| 242 EXPECT_TRUE(builder1 == builder2); | 243 EXPECT_TRUE(builder1 == builder2); |
| 243 } | 244 } |
| 244 | 245 |
| 245 TEST(StringBuilderTest, CanShrink) | |
| 246 { | |
| 247 StringBuilder builder; | |
| 248 builder.reserveCapacity(256); | |
| 249 EXPECT_TRUE(builder.canShrink()); | |
| 250 for (int i = 0; i < 256; i++) | |
| 251 builder.append('x'); | |
| 252 EXPECT_EQ(builder.length(), builder.capacity()); | |
| 253 EXPECT_FALSE(builder.canShrink()); | |
| 254 } | |
| 255 | |
| 256 TEST(StringBuilderTest, ToAtomicString) | 246 TEST(StringBuilderTest, ToAtomicString) |
| 257 { | 247 { |
| 258 StringBuilder builder; | 248 StringBuilder builder; |
| 259 builder.append("123"); | 249 builder.append("123"); |
| 260 AtomicString atomicString = builder.toAtomicString(); | 250 AtomicString atomicString = builder.toAtomicString(); |
| 261 EXPECT_EQ(String("123"), atomicString); | 251 EXPECT_EQ(String("123"), atomicString); |
| 262 | 252 |
| 263 builder.reserveCapacity(256); | 253 builder.reserveCapacity(256); |
| 264 EXPECT_TRUE(builder.canShrink()); | |
| 265 for (int i = builder.length(); i < 128; i++) | 254 for (int i = builder.length(); i < 128; i++) |
| 266 builder.append('x'); | 255 builder.append('x'); |
| 267 AtomicString atomicString1 = builder.toAtomicString(); | 256 AtomicString atomicString1 = builder.toAtomicString(); |
| 268 EXPECT_EQ(128u, atomicString1.length()); | 257 EXPECT_EQ(128u, atomicString1.length()); |
| 269 EXPECT_EQ('x', atomicString1[127]); | 258 EXPECT_EQ('x', atomicString1[127]); |
| 270 | 259 |
| 271 // Later change of builder should not affect the atomic string. | 260 // Later change of builder should not affect the atomic string. |
| 272 for (int i = builder.length(); i < 256; i++) | 261 for (int i = builder.length(); i < 256; i++) |
| 273 builder.append('x'); | 262 builder.append('x'); |
| 274 EXPECT_EQ(128u, atomicString1.length()); | 263 EXPECT_EQ(128u, atomicString1.length()); |
| 275 | 264 |
| 276 EXPECT_FALSE(builder.canShrink()); | |
| 277 String string = builder.toString(); | 265 String string = builder.toString(); |
| 278 AtomicString atomicString2 = builder.toAtomicString(); | 266 AtomicString atomicString2 = builder.toAtomicString(); |
| 279 // They should share the same StringImpl. | 267 // They should share the same StringImpl. |
| 280 EXPECT_EQ(atomicString2.impl(), string.impl()); | 268 EXPECT_EQ(atomicString2.impl(), string.impl()); |
| 281 } | 269 } |
| 282 | 270 |
| 283 TEST(StringBuilderTest, ToAtomicStringOnEmpty) | 271 TEST(StringBuilderTest, ToAtomicStringOnEmpty) |
| 284 { | 272 { |
| 285 { // Default constructed. | 273 { // Default constructed. |
| 286 StringBuilder builder; | 274 StringBuilder builder; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 StringBuilder reference; | 337 StringBuilder reference; |
| 350 reference.append(replacementCharacter); // Make it UTF-16. | 338 reference.append(replacementCharacter); // Make it UTF-16. |
| 351 reference.append(String::number(someNumber)); | 339 reference.append(String::number(someNumber)); |
| 352 StringBuilder test; | 340 StringBuilder test; |
| 353 test.append(replacementCharacter); | 341 test.append(replacementCharacter); |
| 354 test.appendNumber(someNumber); | 342 test.appendNumber(someNumber); |
| 355 EXPECT_EQ(reference, test); | 343 EXPECT_EQ(reference, test); |
| 356 } | 344 } |
| 357 | 345 |
| 358 } // namespace WTF | 346 } // namespace WTF |
| OLD | NEW |