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

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

Issue 2033293002: Implement StringImpl sharing for StringView::toString(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix preload scanner bug. Created 4 years, 6 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) 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 builder.append(builder1.characters8(), builder1.length()); 88 builder.append(builder1.characters8(), builder1.length());
89 expectBuilderContent("0123456789abcdefg#0123456789abcdefg#XYZ", builder); 89 expectBuilderContent("0123456789abcdefg#0123456789abcdefg#XYZ", builder);
90 90
91 StringBuilder builder2; 91 StringBuilder builder2;
92 builder2.reserveCapacity(100); 92 builder2.reserveCapacity(100);
93 builder2.append("xyz"); 93 builder2.append("xyz");
94 const LChar* characters = builder2.characters8(); 94 const LChar* characters = builder2.characters8();
95 builder2.append("0123456789"); 95 builder2.append("0123456789");
96 EXPECT_EQ(characters, builder2.characters8()); 96 EXPECT_EQ(characters, builder2.characters8());
97 97
98 StringBuilder builder3;
99 builder3.append("xyz", 1, 2);
100 expectBuilderContent("yz", builder3);
101
102 StringBuilder builder4;
103 builder4.append("abc", 5, 3);
104 expectEmpty(builder4);
105
106 StringBuilder builder5;
107 builder5.append(StringView(StringView("def"), 1, 1));
108 expectBuilderContent("e", builder5);
109
110 // append() has special code paths for String backed StringView instead of
111 // just char* backed ones.
112 StringBuilder builder6;
113 builder6.append(String("ghi"), 1, 2);
114 expectBuilderContent("hi", builder6);
115
98 // Test appending UChar32 characters to StringBuilder. 116 // Test appending UChar32 characters to StringBuilder.
99 StringBuilder builderForUChar32Append; 117 StringBuilder builderForUChar32Append;
100 UChar32 frakturAChar = 0x1D504; 118 UChar32 frakturAChar = 0x1D504;
101 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.
102 EXPECT_EQ(2U, builderForUChar32Append.length()); 120 EXPECT_EQ(2U, builderForUChar32Append.length());
103 builderForUChar32Append.append(static_cast<UChar32>('A')); 121 builderForUChar32Append.append(static_cast<UChar32>('A'));
104 EXPECT_EQ(3U, builderForUChar32Append.length()); 122 EXPECT_EQ(3U, builderForUChar32Append.length());
105 const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar ), 'A' }; 123 const UChar resultArray[] = { U16_LEAD(frakturAChar), U16_TRAIL(frakturAChar ), 'A' };
106 expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), bui lderForUChar32Append); 124 expectBuilderContent(String(resultArray, WTF_ARRAY_LENGTH(resultArray)), bui lderForUChar32Append);
107 } 125 }
108 126
127 TEST(StringBuilderTest, AppendSharingImpl)
128 {
129 String string("abc");
130 StringBuilder builder1;
131 builder1.append(string);
132 EXPECT_EQ(string.impl(), builder1.toString().impl());
133 EXPECT_EQ(string.impl(), builder1.toAtomicString().impl());
134
135 StringBuilder builder2;
136 builder2.append(string, 0, string.length());
137 EXPECT_EQ(string.impl(), builder2.toString().impl());
138 EXPECT_EQ(string.impl(), builder2.toAtomicString().impl());
139 }
140
109 TEST(StringBuilderTest, ToString) 141 TEST(StringBuilderTest, ToString)
110 { 142 {
111 StringBuilder builder; 143 StringBuilder builder;
112 builder.append("0123456789"); 144 builder.append("0123456789");
113 String string = builder.toString(); 145 String string = builder.toString();
114 EXPECT_EQ(String("0123456789"), string); 146 EXPECT_EQ(String("0123456789"), string);
115 EXPECT_EQ(string.impl(), builder.toString().impl()); 147 EXPECT_EQ(string.impl(), builder.toString().impl());
116 148
117 // Changing the StringBuilder should not affect the original result of toStr ing(). 149 // Changing the StringBuilder should not affect the original result of toStr ing().
118 builder.append("abcdefghijklmnopqrstuvwxyz"); 150 builder.append("abcdefghijklmnopqrstuvwxyz");
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 StringBuilder reference; 349 StringBuilder reference;
318 reference.append(replacementCharacter); // Make it UTF-16. 350 reference.append(replacementCharacter); // Make it UTF-16.
319 reference.append(String::number(someNumber)); 351 reference.append(String::number(someNumber));
320 StringBuilder test; 352 StringBuilder test;
321 test.append(replacementCharacter); 353 test.append(replacementCharacter);
322 test.appendNumber(someNumber); 354 test.appendNumber(someNumber);
323 EXPECT_EQ(reference, test); 355 EXPECT_EQ(reference, test);
324 } 356 }
325 357
326 } // namespace WTF 358 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698