1 | /* |
2 | * Copyright (C) 2011 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 | #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() (++wtfStringCopyCount) |
29 | |
30 | static int wtfStringCopyCount; |
31 | |
32 | #include <wtf/text/StringView.h> |
33 | #include <wtf/text/WTFString.h> |
34 | |
35 | namespace TestWebKitAPI { |
36 | |
37 | #define EXPECT_N_WTF_STRING_COPIES(count, expr) \ |
38 | do { \ |
39 | wtfStringCopyCount = 0; \ |
40 | String __testString = expr; \ |
41 | (void)__testString; \ |
42 | EXPECT_EQ(count, wtfStringCopyCount) << #expr; \ |
43 | } while (false) |
44 | |
45 | TEST(WTF, StringOperators) |
46 | { |
47 | String string("String" ); |
48 | AtomString atomString("AtomString" ); |
49 | ASCIILiteral literal { "ASCIILiteral"_s }; |
50 | |
51 | String stringViewBacking { "StringView" }; |
52 | StringView stringView { stringViewBacking }; |
53 | |
54 | EXPECT_EQ(0, wtfStringCopyCount); |
55 | |
56 | EXPECT_N_WTF_STRING_COPIES(2, string + string); |
57 | EXPECT_N_WTF_STRING_COPIES(2, string + atomString); |
58 | EXPECT_N_WTF_STRING_COPIES(2, atomString + string); |
59 | EXPECT_N_WTF_STRING_COPIES(2, atomString + atomString); |
60 | EXPECT_N_WTF_STRING_COPIES(1, stringView + string); |
61 | EXPECT_N_WTF_STRING_COPIES(1, string + stringView); |
62 | EXPECT_N_WTF_STRING_COPIES(1, stringView + atomString); |
63 | EXPECT_N_WTF_STRING_COPIES(1, atomString + stringView); |
64 | |
65 | EXPECT_N_WTF_STRING_COPIES(1, "C string" + string); |
66 | EXPECT_N_WTF_STRING_COPIES(1, string + "C string" ); |
67 | EXPECT_N_WTF_STRING_COPIES(1, "C string" + atomString); |
68 | EXPECT_N_WTF_STRING_COPIES(1, atomString + "C string" ); |
69 | EXPECT_N_WTF_STRING_COPIES(0, "C string" + stringView); |
70 | EXPECT_N_WTF_STRING_COPIES(0, stringView + "C string" ); |
71 | |
72 | EXPECT_N_WTF_STRING_COPIES(1, literal + string); |
73 | EXPECT_N_WTF_STRING_COPIES(1, string + literal); |
74 | EXPECT_N_WTF_STRING_COPIES(1, literal + atomString); |
75 | EXPECT_N_WTF_STRING_COPIES(1, atomString + literal); |
76 | EXPECT_N_WTF_STRING_COPIES(0, literal + stringView); |
77 | EXPECT_N_WTF_STRING_COPIES(0, stringView + literal); |
78 | |
79 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + "C string" + string); |
80 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + "C string" + string)); |
81 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + ("C string" + string)); |
82 | EXPECT_N_WTF_STRING_COPIES(2, string + "C string" + string + "C string" ); |
83 | EXPECT_N_WTF_STRING_COPIES(2, string + ("C string" + string + "C string" )); |
84 | EXPECT_N_WTF_STRING_COPIES(2, (string + "C string" ) + (string + "C string" )); |
85 | |
86 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + literal + string); |
87 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + literal + string)); |
88 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + (literal + string)); |
89 | EXPECT_N_WTF_STRING_COPIES(2, string + literal + string + literal); |
90 | EXPECT_N_WTF_STRING_COPIES(2, string + (literal + string + literal)); |
91 | EXPECT_N_WTF_STRING_COPIES(2, (string + literal) + (string + literal)); |
92 | |
93 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + "C string" + string); |
94 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + "C string" + string)); |
95 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + ("C string" + string)); |
96 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + literal + string); |
97 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + literal + string)); |
98 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + (literal + string)); |
99 | |
100 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + "C string" + atomString); |
101 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + "C string" + atomString)); |
102 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + ("C string" + atomString)); |
103 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + literal + atomString); |
104 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + literal + atomString)); |
105 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + (literal + atomString)); |
106 | |
107 | EXPECT_N_WTF_STRING_COPIES(0, literal + stringView + "C string" + stringView); |
108 | EXPECT_N_WTF_STRING_COPIES(0, literal + (stringView + "C string" + stringView)); |
109 | EXPECT_N_WTF_STRING_COPIES(0, (literal + stringView) + ("C string" + stringView)); |
110 | EXPECT_N_WTF_STRING_COPIES(0, "C string" + stringView + literal + stringView); |
111 | EXPECT_N_WTF_STRING_COPIES(0, "C string" + (stringView + literal + stringView)); |
112 | EXPECT_N_WTF_STRING_COPIES(0, ("C string" + stringView) + (literal + stringView)); |
113 | |
114 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + "C string" + string + literal + stringView); |
115 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + "C string" + string + literal + stringView)); |
116 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + ("C string" + string) + (literal + stringView)); |
117 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + literal + string + literal + stringView); |
118 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + literal + string + literal + stringView)); |
119 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + (literal + string) + (literal + stringView)); |
120 | |
121 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + "C string" + stringView + literal + string); |
122 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + "C string" + stringView + literal + string)); |
123 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + ("C string" + stringView) + (literal + string)); |
124 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + literal + stringView + literal + string); |
125 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + literal + stringView + literal + string)); |
126 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + (literal + stringView) + (literal + string)); |
127 | |
128 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + "C string" + atomString + literal + stringView); |
129 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + "C string" + atomString + literal + stringView)); |
130 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + ("C string" + atomString) + (literal + stringView)); |
131 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + literal + atomString + literal + stringView); |
132 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + literal + atomString + literal + stringView)); |
133 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + (literal + atomString) + (literal + stringView)); |
134 | |
135 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + "C string" + stringView + literal + atomString); |
136 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + "C string" + stringView + literal + atomString)); |
137 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + ("C string" + stringView) + (literal + atomString)); |
138 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + literal + stringView + literal + atomString); |
139 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + literal + stringView + literal + atomString)); |
140 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + (literal + stringView) + (literal + atomString)); |
141 | |
142 | EXPECT_N_WTF_STRING_COPIES(2, literal + stringView + "C string" + atomString + literal + string); |
143 | EXPECT_N_WTF_STRING_COPIES(2, literal + (stringView + "C string" + atomString + literal + string)); |
144 | EXPECT_N_WTF_STRING_COPIES(2, (literal + stringView) + ("C string" + atomString) + (literal + string)); |
145 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + stringView + literal + atomString + literal + string); |
146 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (stringView + literal + atomString + literal + string)); |
147 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + stringView) + (literal + atomString) + (literal + string)); |
148 | |
149 | EXPECT_N_WTF_STRING_COPIES(2, literal + stringView + "C string" + string + literal + atomString); |
150 | EXPECT_N_WTF_STRING_COPIES(2, literal + (stringView + "C string" + string + literal + atomString)); |
151 | EXPECT_N_WTF_STRING_COPIES(2, (literal + stringView) + ("C string" + string) + (literal + atomString)); |
152 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + stringView + literal + string + literal + atomString); |
153 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (stringView + literal + string + literal + atomString)); |
154 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + stringView) + (literal + string) + (literal + atomString)); |
155 | |
156 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + "C string" + atomString); |
157 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + "C string" + atomString)); |
158 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + ("C string" + atomString)); |
159 | EXPECT_N_WTF_STRING_COPIES(2, atomString + "C string" + atomString + "C string" ); |
160 | EXPECT_N_WTF_STRING_COPIES(2, atomString + ("C string" + atomString + "C string" )); |
161 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + "C string" ) + (atomString + "C string" )); |
162 | |
163 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + literal + atomString); |
164 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + literal + atomString)); |
165 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + (literal + atomString)); |
166 | EXPECT_N_WTF_STRING_COPIES(2, atomString + literal + atomString + literal); |
167 | EXPECT_N_WTF_STRING_COPIES(2, atomString + (literal + atomString + literal)); |
168 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + literal) + (atomString + literal)); |
169 | |
170 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + "C string" + atomString + "C string" + stringView); |
171 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + "C string" + atomString + "C string" + stringView)); |
172 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + ("C string" + atomString) + ("C string" + stringView)); |
173 | EXPECT_N_WTF_STRING_COPIES(2, string + "C string" + atomString + "C string" + stringView + "C string" ); |
174 | EXPECT_N_WTF_STRING_COPIES(2, string + ("C string" + atomString + "C string" + stringView + "C string" )); |
175 | EXPECT_N_WTF_STRING_COPIES(2, (string + "C string" ) + (atomString + "C string" ) + (stringView + "C string" )); |
176 | |
177 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + literal + atomString + literal + stringView); |
178 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + literal + atomString + literal + stringView)); |
179 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + (literal + atomString) + (literal + stringView)); |
180 | EXPECT_N_WTF_STRING_COPIES(2, string + literal + atomString + literal + stringView + literal); |
181 | EXPECT_N_WTF_STRING_COPIES(2, string + (literal + atomString + literal + stringView + literal)); |
182 | EXPECT_N_WTF_STRING_COPIES(2, (string + literal) + (atomString + literal) + (stringView + literal)); |
183 | |
184 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + string + "C string" + stringView + "C string" + atomString); |
185 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (string + "C string" + stringView + "C string" + atomString)); |
186 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + string) + ("C string" + stringView) + ("C string" + atomString)); |
187 | EXPECT_N_WTF_STRING_COPIES(2, string + "C string" + stringView + "C string" + atomString + "C string" ); |
188 | EXPECT_N_WTF_STRING_COPIES(2, string + ("C string" + stringView + "C string" + atomString + "C string" )); |
189 | EXPECT_N_WTF_STRING_COPIES(2, (string + "C string" ) + (stringView + "C string" ) + (atomString + "C string" )); |
190 | |
191 | EXPECT_N_WTF_STRING_COPIES(2, literal + string + literal + stringView + literal + atomString); |
192 | EXPECT_N_WTF_STRING_COPIES(2, literal + (string + literal + stringView + literal + atomString)); |
193 | EXPECT_N_WTF_STRING_COPIES(2, (literal + string) + (literal + stringView) + (literal + atomString)); |
194 | EXPECT_N_WTF_STRING_COPIES(2, string + literal + stringView + literal + atomString + literal); |
195 | EXPECT_N_WTF_STRING_COPIES(2, string + (literal + stringView + literal + atomString + literal)); |
196 | EXPECT_N_WTF_STRING_COPIES(2, (string + literal) + (stringView + literal) + (atomString + literal)); |
197 | |
198 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + "C string" + string + "C string" + stringView); |
199 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + "C string" + string + "C string" + stringView)); |
200 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + ("C string" + string) + ("C string" + stringView)); |
201 | EXPECT_N_WTF_STRING_COPIES(2, atomString + "C string" + string + "C string" + stringView + "C string" ); |
202 | EXPECT_N_WTF_STRING_COPIES(2, atomString + ("C string" + string + "C string" + stringView + "C string" )); |
203 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + "C string" ) + (string + "C string" ) + (stringView + "C string" )); |
204 | |
205 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + literal + string + literal + stringView); |
206 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + literal + string + literal + stringView)); |
207 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + (literal + string) + (literal + stringView)); |
208 | EXPECT_N_WTF_STRING_COPIES(2, atomString + literal + string + literal + stringView + literal); |
209 | EXPECT_N_WTF_STRING_COPIES(2, atomString + (literal + string + literal + stringView + literal)); |
210 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + literal) + (string + literal) + (stringView + literal)); |
211 | |
212 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + atomString + "C string" + stringView + "C string" + string); |
213 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (atomString + "C string" + stringView + "C string" + string)); |
214 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + atomString) + ("C string" + stringView) + ("C string" + string)); |
215 | EXPECT_N_WTF_STRING_COPIES(2, atomString + "C string" + stringView + "C string" + string + "C string" ); |
216 | EXPECT_N_WTF_STRING_COPIES(2, atomString + ("C string" + stringView + "C string" + string + "C string" )); |
217 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + "C string" ) + (stringView + "C string" ) + (string + "C string" )); |
218 | |
219 | EXPECT_N_WTF_STRING_COPIES(2, literal + atomString + literal + stringView + literal + string); |
220 | EXPECT_N_WTF_STRING_COPIES(2, literal + (atomString + literal + stringView + literal + string)); |
221 | EXPECT_N_WTF_STRING_COPIES(2, (literal + atomString) + (literal + stringView) + (literal + string)); |
222 | EXPECT_N_WTF_STRING_COPIES(2, atomString + literal + stringView + literal + string + literal); |
223 | EXPECT_N_WTF_STRING_COPIES(2, atomString + (literal + stringView + literal + string + literal)); |
224 | EXPECT_N_WTF_STRING_COPIES(2, (atomString + literal) + (stringView + literal) + (string + literal)); |
225 | |
226 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + stringView + "C string" + atomString + "C string" + string); |
227 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (stringView + "C string" + atomString + "C string" + string)); |
228 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + stringView) + ("C string" + atomString) + ("C string" + string)); |
229 | EXPECT_N_WTF_STRING_COPIES(2, stringView + "C string" + atomString + "C string" + string + "C string" ); |
230 | EXPECT_N_WTF_STRING_COPIES(2, stringView + ("C string" + atomString + "C string" + string + "C string" )); |
231 | EXPECT_N_WTF_STRING_COPIES(2, (stringView + "C string" ) + (atomString + "C string" ) + (string + "C string" )); |
232 | |
233 | EXPECT_N_WTF_STRING_COPIES(2, literal + stringView + literal + atomString + literal + string); |
234 | EXPECT_N_WTF_STRING_COPIES(2, literal + (stringView + literal + atomString + literal + string)); |
235 | EXPECT_N_WTF_STRING_COPIES(2, (literal + stringView) + (literal + atomString) + (literal + string)); |
236 | EXPECT_N_WTF_STRING_COPIES(2, stringView + literal + atomString + literal + string + literal); |
237 | EXPECT_N_WTF_STRING_COPIES(2, stringView + (literal + atomString + literal + string + literal)); |
238 | EXPECT_N_WTF_STRING_COPIES(2, (stringView + literal) + (atomString + literal) + (string + literal)); |
239 | |
240 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + stringView + "C string" + string + "C string" + atomString); |
241 | EXPECT_N_WTF_STRING_COPIES(2, "C string" + (stringView + "C string" + string + "C string" + atomString)); |
242 | EXPECT_N_WTF_STRING_COPIES(2, ("C string" + stringView) + ("C string" + string) + ("C string" + atomString)); |
243 | EXPECT_N_WTF_STRING_COPIES(2, stringView + "C string" + string + "C string" + atomString + "C string" ); |
244 | EXPECT_N_WTF_STRING_COPIES(2, stringView + ("C string" + string + "C string" + atomString + "C string" )); |
245 | EXPECT_N_WTF_STRING_COPIES(2, (stringView + "C string" ) + (string + "C string" ) + (atomString + "C string" )); |
246 | |
247 | EXPECT_N_WTF_STRING_COPIES(2, literal + stringView + literal + string + literal + atomString); |
248 | EXPECT_N_WTF_STRING_COPIES(2, literal + (stringView + literal + string + literal + atomString)); |
249 | EXPECT_N_WTF_STRING_COPIES(2, (literal + stringView) + (literal + string) + (literal + atomString)); |
250 | EXPECT_N_WTF_STRING_COPIES(2, stringView + literal + string + literal + atomString + literal); |
251 | EXPECT_N_WTF_STRING_COPIES(2, stringView + (literal + string + literal + atomString + literal)); |
252 | EXPECT_N_WTF_STRING_COPIES(2, (stringView + literal) + (string + literal) + (atomString + literal)); |
253 | } |
254 | |
255 | TEST(WTF, ConcatenateCharacterArrayAndEmptyString) |
256 | { |
257 | String emptyString; |
258 | EXPECT_EQ(static_cast<unsigned>(0), emptyString.length()); |
259 | |
260 | UChar ucharArray[] = { 't', 'e', 's', 't', '\0' }; |
261 | String concatenation16 = ucharArray + emptyString; |
262 | ASSERT_EQ(static_cast<unsigned>(4), concatenation16.length()); |
263 | ASSERT_TRUE(concatenation16 == String(ucharArray)); |
264 | |
265 | LChar lcharArray[] = { 't', 'e', 's', 't', '\0' }; |
266 | String concatenation8 = lcharArray + emptyString; |
267 | ASSERT_EQ(static_cast<unsigned>(4), concatenation8.length()); |
268 | ASSERT_TRUE(concatenation8 == String(lcharArray)); |
269 | } |
270 | |
271 | } // namespace TestWebKitAPI |
272 | |