OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "wtf/text/StringView.h" | |
6 | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "wtf/text/AtomicString.h" | |
9 #include "wtf/text/StringImpl.h" | |
10 #include "wtf/text/WTFString.h" | |
11 | |
12 namespace WTF { | |
13 | |
14 const char* kChars = "12345"; | |
15 const LChar* kChars8 = reinterpret_cast<const LChar*>(kChars); | |
16 const UChar* kChars16 = reinterpret_cast<const UChar*>(u"12345"); | |
17 | |
18 TEST(StringViewTest, ConstructionStringImpl8) { | |
19 RefPtr<StringImpl> impl8Bit = StringImpl::create(kChars8, 5); | |
20 | |
21 // StringView(StringImpl*); | |
22 ASSERT_TRUE(StringView(impl8Bit.get()).is8Bit()); | |
23 EXPECT_FALSE(StringView(impl8Bit.get()).isNull()); | |
24 EXPECT_EQ(impl8Bit->characters8(), StringView(impl8Bit.get()).characters8()); | |
25 EXPECT_EQ(impl8Bit->length(), StringView(impl8Bit.get()).length()); | |
26 EXPECT_STREQ(kChars, StringView(impl8Bit.get()).toString().utf8().data()); | |
27 | |
28 // StringView(StringImpl*, unsigned offset); | |
29 ASSERT_TRUE(StringView(impl8Bit.get(), 2).is8Bit()); | |
30 EXPECT_FALSE(StringView(impl8Bit.get(), 2).isNull()); | |
31 EXPECT_EQ(impl8Bit->characters8() + 2, | |
32 StringView(impl8Bit.get(), 2).characters8()); | |
33 EXPECT_EQ(3u, StringView(impl8Bit.get(), 2).length()); | |
34 EXPECT_EQ(StringView("345"), StringView(impl8Bit.get(), 2)); | |
35 EXPECT_STREQ("345", StringView(impl8Bit.get(), 2).toString().utf8().data()); | |
36 | |
37 // StringView(StringImpl*, unsigned offset, unsigned length); | |
38 ASSERT_TRUE(StringView(impl8Bit.get(), 2, 1).is8Bit()); | |
39 EXPECT_FALSE(StringView(impl8Bit.get(), 2, 1).isNull()); | |
40 EXPECT_EQ(impl8Bit->characters8() + 2, | |
41 StringView(impl8Bit.get(), 2, 1).characters8()); | |
42 EXPECT_EQ(1u, StringView(impl8Bit.get(), 2, 1).length()); | |
43 EXPECT_EQ(StringView("3"), StringView(impl8Bit.get(), 2, 1)); | |
44 EXPECT_STREQ("3", StringView(impl8Bit.get(), 2, 1).toString().utf8().data()); | |
45 } | |
46 | |
47 TEST(StringViewTest, ConstructionStringImpl16) { | |
48 RefPtr<StringImpl> impl16Bit = StringImpl::create(kChars16, 5); | |
49 | |
50 // StringView(StringImpl*); | |
51 ASSERT_FALSE(StringView(impl16Bit.get()).is8Bit()); | |
52 EXPECT_FALSE(StringView(impl16Bit.get()).isNull()); | |
53 EXPECT_EQ(impl16Bit->characters16(), | |
54 StringView(impl16Bit.get()).characters16()); | |
55 EXPECT_EQ(impl16Bit->length(), StringView(impl16Bit.get()).length()); | |
56 EXPECT_STREQ(kChars, StringView(impl16Bit.get()).toString().utf8().data()); | |
57 | |
58 // StringView(StringImpl*, unsigned offset); | |
59 ASSERT_FALSE(StringView(impl16Bit.get(), 2).is8Bit()); | |
60 EXPECT_FALSE(StringView(impl16Bit.get(), 2).isNull()); | |
61 EXPECT_EQ(impl16Bit->characters16() + 2, | |
62 StringView(impl16Bit.get(), 2).characters16()); | |
63 EXPECT_EQ(3u, StringView(impl16Bit.get(), 2).length()); | |
64 EXPECT_EQ(StringView("345"), StringView(impl16Bit.get(), 2)); | |
65 EXPECT_STREQ("345", StringView(impl16Bit.get(), 2).toString().utf8().data()); | |
66 | |
67 // StringView(StringImpl*, unsigned offset, unsigned length); | |
68 ASSERT_FALSE(StringView(impl16Bit.get(), 2, 1).is8Bit()); | |
69 EXPECT_FALSE(StringView(impl16Bit.get(), 2, 1).isNull()); | |
70 EXPECT_EQ(impl16Bit->characters16() + 2, | |
71 StringView(impl16Bit.get(), 2, 1).characters16()); | |
72 EXPECT_EQ(1u, StringView(impl16Bit.get(), 2, 1).length()); | |
73 EXPECT_EQ(StringView("3"), StringView(impl16Bit.get(), 2, 1)); | |
74 EXPECT_STREQ("3", StringView(impl16Bit.get(), 2, 1).toString().utf8().data()); | |
75 } | |
76 | |
77 TEST(StringViewTest, ConstructionStringImplRef8) { | |
78 RefPtr<StringImpl> impl8Bit = StringImpl::create(kChars8, 5); | |
79 | |
80 // StringView(StringImpl&); | |
81 ASSERT_TRUE(StringView(*impl8Bit).is8Bit()); | |
82 EXPECT_FALSE(StringView(*impl8Bit).isNull()); | |
83 EXPECT_EQ(impl8Bit->characters8(), StringView(*impl8Bit).characters8()); | |
84 EXPECT_EQ(impl8Bit->length(), StringView(*impl8Bit).length()); | |
85 EXPECT_STREQ(kChars, StringView(*impl8Bit).toString().utf8().data()); | |
86 | |
87 // StringView(StringImpl&, unsigned offset); | |
88 ASSERT_TRUE(StringView(*impl8Bit, 2).is8Bit()); | |
89 EXPECT_FALSE(StringView(*impl8Bit, 2).isNull()); | |
90 EXPECT_EQ(impl8Bit->characters8() + 2, | |
91 StringView(*impl8Bit, 2).characters8()); | |
92 EXPECT_EQ(3u, StringView(*impl8Bit, 2).length()); | |
93 EXPECT_EQ(StringView("345"), StringView(*impl8Bit, 2)); | |
94 EXPECT_STREQ("345", StringView(*impl8Bit, 2).toString().utf8().data()); | |
95 | |
96 // StringView(StringImpl&, unsigned offset, unsigned length); | |
97 ASSERT_TRUE(StringView(*impl8Bit, 2, 1).is8Bit()); | |
98 EXPECT_FALSE(StringView(*impl8Bit, 2, 1).isNull()); | |
99 EXPECT_EQ(impl8Bit->characters8() + 2, | |
100 StringView(*impl8Bit, 2, 1).characters8()); | |
101 EXPECT_EQ(1u, StringView(*impl8Bit, 2, 1).length()); | |
102 EXPECT_EQ(StringView("3"), StringView(*impl8Bit, 2, 1)); | |
103 EXPECT_STREQ("3", StringView(*impl8Bit, 2, 1).toString().utf8().data()); | |
104 } | |
105 | |
106 TEST(StringViewTest, ConstructionStringImplRef16) { | |
107 RefPtr<StringImpl> impl16Bit = StringImpl::create(kChars16, 5); | |
108 | |
109 // StringView(StringImpl&); | |
110 ASSERT_FALSE(StringView(*impl16Bit).is8Bit()); | |
111 EXPECT_FALSE(StringView(*impl16Bit).isNull()); | |
112 EXPECT_EQ(impl16Bit->characters16(), StringView(*impl16Bit).characters16()); | |
113 EXPECT_EQ(impl16Bit->length(), StringView(*impl16Bit).length()); | |
114 EXPECT_STREQ(kChars, StringView(*impl16Bit).toString().utf8().data()); | |
115 | |
116 // StringView(StringImpl&, unsigned offset); | |
117 ASSERT_FALSE(StringView(*impl16Bit, 2).is8Bit()); | |
118 EXPECT_FALSE(StringView(*impl16Bit, 2).isNull()); | |
119 EXPECT_EQ(impl16Bit->characters16() + 2, | |
120 StringView(*impl16Bit, 2).characters16()); | |
121 EXPECT_EQ(3u, StringView(*impl16Bit, 2).length()); | |
122 EXPECT_EQ(StringView("345"), StringView(*impl16Bit, 2)); | |
123 EXPECT_STREQ("345", StringView(*impl16Bit, 2).toString().utf8().data()); | |
124 | |
125 // StringView(StringImpl&, unsigned offset, unsigned length); | |
126 ASSERT_FALSE(StringView(*impl16Bit, 2, 1).is8Bit()); | |
127 EXPECT_FALSE(StringView(*impl16Bit, 2, 1).isNull()); | |
128 EXPECT_EQ(impl16Bit->characters16() + 2, | |
129 StringView(*impl16Bit, 2, 1).characters16()); | |
130 EXPECT_EQ(1u, StringView(*impl16Bit, 2, 1).length()); | |
131 EXPECT_EQ(StringView("3"), StringView(*impl16Bit, 2, 1)); | |
132 EXPECT_STREQ("3", StringView(*impl16Bit, 2, 1).toString().utf8().data()); | |
133 } | |
134 | |
135 TEST(StringViewTest, ConstructionString8) { | |
136 String string8Bit = String(StringImpl::create(kChars8, 5)); | |
137 | |
138 // StringView(const String&); | |
139 ASSERT_TRUE(StringView(string8Bit).is8Bit()); | |
140 EXPECT_FALSE(StringView(string8Bit).isNull()); | |
141 EXPECT_EQ(string8Bit.characters8(), StringView(string8Bit).characters8()); | |
142 EXPECT_EQ(string8Bit.length(), StringView(string8Bit).length()); | |
143 EXPECT_STREQ(kChars, StringView(string8Bit).toString().utf8().data()); | |
144 | |
145 // StringView(const String&, unsigned offset); | |
146 ASSERT_TRUE(StringView(string8Bit, 2).is8Bit()); | |
147 EXPECT_FALSE(StringView(string8Bit, 2).isNull()); | |
148 EXPECT_EQ(string8Bit.characters8() + 2, | |
149 StringView(string8Bit, 2).characters8()); | |
150 EXPECT_EQ(3u, StringView(string8Bit, 2).length()); | |
151 EXPECT_EQ(StringView("345"), StringView(string8Bit, 2)); | |
152 EXPECT_STREQ("345", StringView(string8Bit, 2).toString().utf8().data()); | |
153 | |
154 // StringView(const String&, unsigned offset, unsigned length); | |
155 ASSERT_TRUE(StringView(string8Bit, 2, 1).is8Bit()); | |
156 EXPECT_FALSE(StringView(string8Bit, 2, 1).isNull()); | |
157 EXPECT_EQ(string8Bit.characters8() + 2, | |
158 StringView(string8Bit, 2, 1).characters8()); | |
159 EXPECT_EQ(1u, StringView(string8Bit, 2, 1).length()); | |
160 EXPECT_EQ(StringView("3"), StringView(string8Bit, 2, 1)); | |
161 EXPECT_STREQ("3", StringView(string8Bit, 2, 1).toString().utf8().data()); | |
162 } | |
163 | |
164 TEST(StringViewTest, ConstructionString16) { | |
165 String string16Bit = String(StringImpl::create(kChars16, 5)); | |
166 | |
167 // StringView(const String&); | |
168 ASSERT_FALSE(StringView(string16Bit).is8Bit()); | |
169 EXPECT_FALSE(StringView(string16Bit).isNull()); | |
170 EXPECT_EQ(string16Bit.characters16(), StringView(string16Bit).characters16()); | |
171 EXPECT_EQ(string16Bit.length(), StringView(string16Bit).length()); | |
172 EXPECT_STREQ(kChars, StringView(string16Bit).toString().utf8().data()); | |
173 | |
174 // StringView(const String&, unsigned offset); | |
175 ASSERT_FALSE(StringView(string16Bit, 2).is8Bit()); | |
176 EXPECT_FALSE(StringView(string16Bit, 2).isNull()); | |
177 EXPECT_EQ(string16Bit.characters16() + 2, | |
178 StringView(string16Bit, 2).characters16()); | |
179 EXPECT_EQ(3u, StringView(string16Bit, 2).length()); | |
180 EXPECT_EQ(StringView("345"), StringView(string16Bit, 2)); | |
181 EXPECT_STREQ("345", StringView(string16Bit, 2).toString().utf8().data()); | |
182 | |
183 // StringView(const String&, unsigned offset, unsigned length); | |
184 ASSERT_FALSE(StringView(string16Bit, 2, 1).is8Bit()); | |
185 EXPECT_FALSE(StringView(string16Bit, 2, 1).isNull()); | |
186 EXPECT_EQ(string16Bit.characters16() + 2, | |
187 StringView(string16Bit, 2, 1).characters16()); | |
188 EXPECT_EQ(1u, StringView(string16Bit, 2, 1).length()); | |
189 EXPECT_EQ(StringView("3"), StringView(string16Bit, 2, 1)); | |
190 EXPECT_STREQ("3", StringView(string16Bit, 2, 1).toString().utf8().data()); | |
191 } | |
192 | |
193 TEST(StringViewTest, ConstructionAtomicString8) { | |
194 AtomicString atom8Bit = AtomicString(StringImpl::create(kChars8, 5)); | |
195 | |
196 // StringView(const AtomicString&); | |
197 ASSERT_TRUE(StringView(atom8Bit).is8Bit()); | |
198 EXPECT_FALSE(StringView(atom8Bit).isNull()); | |
199 EXPECT_EQ(atom8Bit.characters8(), StringView(atom8Bit).characters8()); | |
200 EXPECT_EQ(atom8Bit.length(), StringView(atom8Bit).length()); | |
201 EXPECT_STREQ(kChars, StringView(atom8Bit).toString().utf8().data()); | |
202 | |
203 // StringView(const AtomicString&, unsigned offset); | |
204 ASSERT_TRUE(StringView(atom8Bit, 2).is8Bit()); | |
205 EXPECT_FALSE(StringView(atom8Bit, 2).isNull()); | |
206 EXPECT_EQ(atom8Bit.characters8() + 2, StringView(atom8Bit, 2).characters8()); | |
207 EXPECT_EQ(3u, StringView(atom8Bit, 2).length()); | |
208 EXPECT_EQ(StringView("345"), StringView(atom8Bit, 2)); | |
209 EXPECT_STREQ("345", StringView(atom8Bit, 2).toString().utf8().data()); | |
210 | |
211 // StringView(const AtomicString&, unsigned offset, unsigned length); | |
212 ASSERT_TRUE(StringView(atom8Bit, 2, 1).is8Bit()); | |
213 EXPECT_FALSE(StringView(atom8Bit, 2, 1).isNull()); | |
214 EXPECT_EQ(atom8Bit.characters8() + 2, | |
215 StringView(atom8Bit, 2, 1).characters8()); | |
216 EXPECT_EQ(1u, StringView(atom8Bit, 2, 1).length()); | |
217 EXPECT_EQ(StringView("3"), StringView(atom8Bit, 2, 1)); | |
218 EXPECT_STREQ("3", StringView(atom8Bit, 2, 1).toString().utf8().data()); | |
219 } | |
220 | |
221 TEST(StringViewTest, ConstructionAtomicString16) { | |
222 AtomicString atom16Bit = AtomicString(StringImpl::create(kChars16, 5)); | |
223 | |
224 // StringView(const AtomicString&); | |
225 ASSERT_FALSE(StringView(atom16Bit).is8Bit()); | |
226 EXPECT_FALSE(StringView(atom16Bit).isNull()); | |
227 EXPECT_EQ(atom16Bit.characters16(), StringView(atom16Bit).characters16()); | |
228 EXPECT_EQ(atom16Bit.length(), StringView(atom16Bit).length()); | |
229 EXPECT_STREQ(kChars, StringView(atom16Bit).toString().utf8().data()); | |
230 | |
231 // StringView(const AtomicString&, unsigned offset); | |
232 ASSERT_FALSE(StringView(atom16Bit, 2).is8Bit()); | |
233 EXPECT_FALSE(StringView(atom16Bit, 2).isNull()); | |
234 EXPECT_EQ(atom16Bit.characters16() + 2, | |
235 StringView(atom16Bit, 2).characters16()); | |
236 EXPECT_EQ(3u, StringView(atom16Bit, 2).length()); | |
237 EXPECT_EQ(StringView("345"), StringView(atom16Bit, 2)); | |
238 EXPECT_STREQ("345", StringView(atom16Bit, 2).toString().utf8().data()); | |
239 | |
240 // StringView(const AtomicString&, unsigned offset, unsigned length); | |
241 ASSERT_FALSE(StringView(atom16Bit, 2, 1).is8Bit()); | |
242 EXPECT_FALSE(StringView(atom16Bit, 2, 1).isNull()); | |
243 EXPECT_EQ(atom16Bit.characters16() + 2, | |
244 StringView(atom16Bit, 2, 1).characters16()); | |
245 EXPECT_EQ(1u, StringView(atom16Bit, 2, 1).length()); | |
246 EXPECT_EQ(StringView("3"), StringView(atom16Bit, 2, 1)); | |
247 EXPECT_STREQ("3", StringView(atom16Bit, 2, 1).toString().utf8().data()); | |
248 } | |
249 | |
250 TEST(StringViewTest, ConstructionStringView8) { | |
251 StringView view8Bit = StringView(kChars8, 5); | |
252 | |
253 // StringView(StringView&); | |
254 ASSERT_TRUE(StringView(view8Bit).is8Bit()); | |
255 EXPECT_FALSE(StringView(view8Bit).isNull()); | |
256 EXPECT_EQ(view8Bit.characters8(), StringView(view8Bit).characters8()); | |
257 EXPECT_EQ(view8Bit.length(), StringView(view8Bit).length()); | |
258 EXPECT_STREQ(kChars, StringView(view8Bit).toString().utf8().data()); | |
259 | |
260 // StringView(const StringView&, unsigned offset); | |
261 ASSERT_TRUE(StringView(view8Bit, 2).is8Bit()); | |
262 EXPECT_FALSE(StringView(view8Bit, 2).isNull()); | |
263 EXPECT_EQ(view8Bit.characters8() + 2, StringView(view8Bit, 2).characters8()); | |
264 EXPECT_EQ(3u, StringView(view8Bit, 2).length()); | |
265 EXPECT_EQ(StringView("345"), StringView(view8Bit, 2)); | |
266 EXPECT_STREQ("345", StringView(view8Bit, 2).toString().utf8().data()); | |
267 | |
268 // StringView(const StringView&, unsigned offset, unsigned length); | |
269 ASSERT_TRUE(StringView(view8Bit, 2, 1).is8Bit()); | |
270 EXPECT_FALSE(StringView(view8Bit, 2, 1).isNull()); | |
271 EXPECT_EQ(view8Bit.characters8() + 2, | |
272 StringView(view8Bit, 2, 1).characters8()); | |
273 EXPECT_EQ(1u, StringView(view8Bit, 2, 1).length()); | |
274 EXPECT_EQ(StringView("3"), StringView(view8Bit, 2, 1)); | |
275 EXPECT_STREQ("3", StringView(view8Bit, 2, 1).toString().utf8().data()); | |
276 } | |
277 | |
278 TEST(StringViewTest, ConstructionStringView16) { | |
279 StringView view16Bit = StringView(kChars16, 5); | |
280 | |
281 // StringView(StringView&); | |
282 ASSERT_FALSE(StringView(view16Bit).is8Bit()); | |
283 EXPECT_FALSE(StringView(view16Bit).isNull()); | |
284 EXPECT_EQ(view16Bit.characters16(), StringView(view16Bit).characters16()); | |
285 EXPECT_EQ(view16Bit.length(), StringView(view16Bit).length()); | |
286 EXPECT_EQ(kChars, StringView(view16Bit).toString()); | |
287 | |
288 // StringView(const StringView&, unsigned offset); | |
289 ASSERT_FALSE(StringView(view16Bit, 2).is8Bit()); | |
290 EXPECT_FALSE(StringView(view16Bit, 2).isNull()); | |
291 EXPECT_EQ(view16Bit.characters16() + 2, | |
292 StringView(view16Bit, 2).characters16()); | |
293 EXPECT_EQ(3u, StringView(view16Bit, 2).length()); | |
294 EXPECT_EQ(StringView("345"), StringView(view16Bit, 2)); | |
295 EXPECT_STREQ("345", StringView(view16Bit, 2).toString().utf8().data()); | |
296 | |
297 // StringView(const StringView&, unsigned offset, unsigned length); | |
298 ASSERT_FALSE(StringView(view16Bit, 2, 1).is8Bit()); | |
299 EXPECT_FALSE(StringView(view16Bit, 2, 1).isNull()); | |
300 EXPECT_EQ(view16Bit.characters16() + 2, | |
301 StringView(view16Bit, 2, 1).characters16()); | |
302 EXPECT_EQ(1u, StringView(view16Bit, 2, 1).length()); | |
303 EXPECT_EQ(StringView("3"), StringView(view16Bit, 2, 1)); | |
304 EXPECT_STREQ("3", StringView(view16Bit, 2, 1).toString().utf8().data()); | |
305 } | |
306 | |
307 TEST(StringViewTest, ConstructionLiteral8) { | |
308 // StringView(const LChar* chars); | |
309 ASSERT_TRUE(StringView(kChars8).is8Bit()); | |
310 EXPECT_FALSE(StringView(kChars8).isNull()); | |
311 EXPECT_EQ(kChars8, StringView(kChars8).characters8()); | |
312 EXPECT_EQ(5u, StringView(kChars8).length()); | |
313 EXPECT_STREQ(kChars, StringView(kChars8).toString().utf8().data()); | |
314 | |
315 // StringView(const char* chars); | |
316 ASSERT_TRUE(StringView(kChars).is8Bit()); | |
317 EXPECT_FALSE(StringView(kChars).isNull()); | |
318 EXPECT_EQ(kChars8, StringView(kChars).characters8()); | |
319 EXPECT_EQ(5u, StringView(kChars).length()); | |
320 EXPECT_STREQ(kChars, StringView(kChars).toString().utf8().data()); | |
321 | |
322 // StringView(const LChar* chars, unsigned length); | |
323 ASSERT_TRUE(StringView(kChars8, 2).is8Bit()); | |
324 EXPECT_FALSE(StringView(kChars8, 2).isNull()); | |
325 EXPECT_EQ(2u, StringView(kChars8, 2).length()); | |
326 EXPECT_EQ(StringView("12"), StringView(kChars8, 2)); | |
327 EXPECT_STREQ("12", StringView(kChars8, 2).toString().utf8().data()); | |
328 | |
329 // StringView(const char* chars, unsigned length); | |
330 ASSERT_TRUE(StringView(kChars, 2).is8Bit()); | |
331 EXPECT_FALSE(StringView(kChars, 2).isNull()); | |
332 EXPECT_EQ(2u, StringView(kChars, 2).length()); | |
333 EXPECT_EQ(StringView("12"), StringView(kChars, 2)); | |
334 EXPECT_STREQ("12", StringView(kChars, 2).toString().utf8().data()); | |
335 } | |
336 | |
337 TEST(StringViewTest, ConstructionLiteral16) { | |
338 // StringView(const UChar* chars); | |
339 ASSERT_FALSE(StringView(kChars16).is8Bit()); | |
340 EXPECT_FALSE(StringView(kChars16).isNull()); | |
341 EXPECT_EQ(kChars16, StringView(kChars16).characters16()); | |
342 EXPECT_EQ(5u, StringView(kChars16).length()); | |
343 EXPECT_EQ(String(kChars16), StringView(kChars16).toString().utf8().data()); | |
344 | |
345 // StringView(const UChar* chars, unsigned length); | |
346 ASSERT_FALSE(StringView(kChars16, 2).is8Bit()); | |
347 EXPECT_FALSE(StringView(kChars16, 2).isNull()); | |
348 EXPECT_EQ(kChars16, StringView(kChars16, 2).characters16()); | |
349 EXPECT_EQ(StringView("12"), StringView(kChars16, 2)); | |
350 EXPECT_EQ(StringView(reinterpret_cast<const UChar*>(u"12")), | |
351 StringView(kChars16, 2)); | |
352 EXPECT_EQ(2u, StringView(kChars16, 2).length()); | |
353 EXPECT_EQ(String("12"), StringView(kChars16, 2).toString()); | |
354 } | |
355 | |
356 TEST(StringViewTest, IsEmpty) { | |
357 EXPECT_FALSE(StringView(kChars).isEmpty()); | |
358 EXPECT_TRUE(StringView(kChars, 0).isEmpty()); | |
359 EXPECT_FALSE(StringView(String(kChars)).isEmpty()); | |
360 EXPECT_TRUE(StringView(String(kChars), 5).isEmpty()); | |
361 EXPECT_TRUE(StringView(String(kChars), 4, 0).isEmpty()); | |
362 EXPECT_TRUE(StringView().isEmpty()); | |
363 EXPECT_TRUE(StringView("").isEmpty()); | |
364 EXPECT_TRUE(StringView(reinterpret_cast<const UChar*>(u"")).isEmpty()); | |
365 EXPECT_FALSE(StringView(kChars16).isEmpty()); | |
366 } | |
367 | |
368 TEST(StringViewTest, ToString) { | |
369 EXPECT_EQ(emptyString.impl(), StringView("").toString().impl()); | |
370 EXPECT_EQ(nullAtom.impl(), StringView().toString().impl()); | |
371 // NOTE: All the construction tests also check toString(). | |
372 } | |
373 | |
374 TEST(StringViewTest, ToAtomicString) { | |
375 EXPECT_EQ(nullAtom.impl(), StringView().toAtomicString()); | |
376 EXPECT_EQ(emptyAtom.impl(), StringView("").toAtomicString()); | |
377 EXPECT_EQ(AtomicString("12"), StringView(kChars8, 2).toAtomicString()); | |
378 // AtomicString will convert to 8bit if possible when creating the string. | |
379 EXPECT_EQ(AtomicString("12").impl(), | |
380 StringView(kChars16, 2).toAtomicString().impl()); | |
381 } | |
382 | |
383 TEST(StringViewTest, ToStringImplSharing) { | |
384 String string(kChars); | |
385 EXPECT_EQ(string.impl(), StringView(string).sharedImpl()); | |
386 EXPECT_EQ(string.impl(), StringView(string).toString().impl()); | |
387 EXPECT_EQ(string.impl(), StringView(string).toAtomicString().impl()); | |
388 } | |
389 | |
390 TEST(StringViewTest, NullString) { | |
391 EXPECT_TRUE(StringView().isNull()); | |
392 EXPECT_TRUE(StringView(String()).isNull()); | |
393 EXPECT_TRUE(StringView(AtomicString()).isNull()); | |
394 EXPECT_TRUE(StringView(static_cast<const char*>(nullptr)).isNull()); | |
395 StringView view(kChars); | |
396 EXPECT_FALSE(view.isNull()); | |
397 view.clear(); | |
398 EXPECT_TRUE(view.isNull()); | |
399 EXPECT_EQ(String(), StringView()); | |
400 EXPECT_TRUE(StringView().toString().isNull()); | |
401 EXPECT_FALSE(equalStringView(StringView(), "")); | |
402 EXPECT_TRUE(equalStringView(StringView(), StringView())); | |
403 EXPECT_FALSE(equalStringView(StringView(), "abc")); | |
404 EXPECT_FALSE(equalStringView("abc", StringView())); | |
405 EXPECT_FALSE(equalIgnoringASCIICase(StringView(), "")); | |
406 EXPECT_TRUE(equalIgnoringASCIICase(StringView(), StringView())); | |
407 EXPECT_FALSE(equalIgnoringASCIICase(StringView(), "abc")); | |
408 EXPECT_FALSE(equalIgnoringASCIICase("abc", StringView())); | |
409 } | |
410 | |
411 TEST(StringViewTest, IndexAccess) { | |
412 StringView view8(kChars8); | |
413 EXPECT_EQ('1', view8[0]); | |
414 EXPECT_EQ('3', view8[2]); | |
415 StringView view16(kChars16); | |
416 EXPECT_EQ('1', view16[0]); | |
417 EXPECT_EQ('3', view16[2]); | |
418 } | |
419 | |
420 TEST(StringViewTest, EqualIgnoringASCIICase) { | |
421 static const char* link8 = "link"; | |
422 static const char* linkCaps8 = "LINK"; | |
423 static const char* nonASCII8 = "a\xE1"; | |
424 static const char* nonASCIICaps8 = "A\xE1"; | |
425 static const char* nonASCIIInvalid8 = "a\xC1"; | |
426 | |
427 static const UChar link16[5] = {0x006c, 0x0069, 0x006e, 0x006b, 0}; // link | |
428 static const UChar linkCaps16[5] = {0x004c, 0x0049, 0x004e, 0x004b, | |
429 0}; // LINK | |
430 static const UChar nonASCII16[3] = {0x0061, 0x00e1, 0}; // a\xE1 | |
431 static const UChar nonASCIICaps16[3] = {0x0041, 0x00e1, 0}; // A\xE1 | |
432 static const UChar nonASCIIInvalid16[3] = {0x0061, 0x00c1, 0}; // a\xC1 | |
433 | |
434 EXPECT_TRUE(equalIgnoringASCIICase(StringView(link16), link8)); | |
435 EXPECT_TRUE(equalIgnoringASCIICase(StringView(link16), linkCaps16)); | |
436 EXPECT_TRUE(equalIgnoringASCIICase(StringView(link16), linkCaps8)); | |
437 EXPECT_TRUE(equalIgnoringASCIICase(StringView(link8), linkCaps8)); | |
438 EXPECT_TRUE(equalIgnoringASCIICase(StringView(link8), link16)); | |
439 | |
440 EXPECT_TRUE(equalIgnoringASCIICase(StringView(nonASCII8), nonASCIICaps8)); | |
441 EXPECT_TRUE(equalIgnoringASCIICase(StringView(nonASCII8), nonASCIICaps16)); | |
442 EXPECT_TRUE(equalIgnoringASCIICase(StringView(nonASCII16), nonASCIICaps16)); | |
443 EXPECT_TRUE(equalIgnoringASCIICase(StringView(nonASCII16), nonASCIICaps8)); | |
444 EXPECT_FALSE(equalIgnoringASCIICase(StringView(nonASCII8), nonASCIIInvalid8)); | |
445 EXPECT_FALSE( | |
446 equalIgnoringASCIICase(StringView(nonASCII8), nonASCIIInvalid16)); | |
447 | |
448 EXPECT_TRUE(equalIgnoringASCIICase(StringView("link"), "lInK")); | |
449 EXPECT_FALSE(equalIgnoringASCIICase(StringView("link"), "INKL")); | |
450 EXPECT_FALSE( | |
451 equalIgnoringASCIICase(StringView("link"), "link different length")); | |
452 EXPECT_FALSE( | |
453 equalIgnoringASCIICase(StringView("link different length"), "link")); | |
454 | |
455 EXPECT_TRUE(equalIgnoringASCIICase(StringView(""), "")); | |
456 } | |
457 | |
458 } // namespace WTF | |
OLD | NEW |