1 // Copyright 2014 PDFium 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 "core/fxcrt/bytestring.h"
6
7 #include <algorithm>
8 #include <iterator>
9 #include <vector>
10
11 #include "core/fxcrt/fx_string.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/base/span.h"
14 #include "third_party/base/stl_util.h"
15
16 namespace fxcrt {
17
TEST(ByteString,ElementAccess)18 TEST(ByteString, ElementAccess) {
19 const ByteString abc("abc");
20 EXPECT_EQ('a', abc[0]);
21 EXPECT_EQ('b', abc[1]);
22 EXPECT_EQ('c', abc[2]);
23 #ifndef NDEBUG
24 EXPECT_DEATH({ abc[3]; }, ".*");
25 #endif
26
27 pdfium::span<const char> abc_span = abc.span();
28 EXPECT_EQ(3u, abc_span.size());
29 EXPECT_EQ(0, memcmp(abc_span.data(), "abc", 3));
30
31 pdfium::span<const uint8_t> abc_raw_span = abc.raw_span();
32 EXPECT_EQ(3u, abc_raw_span.size());
33 EXPECT_EQ(0, memcmp(abc_raw_span.data(), "abc", 3));
34
35 ByteString mutable_abc = abc;
36 EXPECT_EQ(abc.c_str(), mutable_abc.c_str());
37 EXPECT_EQ('a', mutable_abc[0]);
38 EXPECT_EQ('b', mutable_abc[1]);
39 EXPECT_EQ('c', mutable_abc[2]);
40 EXPECT_EQ(abc.c_str(), mutable_abc.c_str());
41 EXPECT_EQ("abc", abc);
42
43 const char* c_str = abc.c_str();
44 mutable_abc.SetAt(0, 'd');
45 EXPECT_EQ(c_str, abc.c_str());
46 EXPECT_NE(c_str, mutable_abc.c_str());
47 EXPECT_EQ("abc", abc);
48 EXPECT_EQ("dbc", mutable_abc);
49
50 mutable_abc.SetAt(1, 'e');
51 EXPECT_EQ("abc", abc);
52 EXPECT_EQ("dec", mutable_abc);
53
54 mutable_abc.SetAt(2, 'f');
55 EXPECT_EQ("abc", abc);
56 EXPECT_EQ("def", mutable_abc);
57 #ifndef NDEBUG
58 EXPECT_DEATH({ mutable_abc.SetAt(3, 'g'); }, ".*");
59 EXPECT_EQ("abc", abc);
60 #endif
61 }
62
TEST(ByteString,Construct)63 TEST(ByteString, Construct) {
64 {
65 // Copy-construct.
66 ByteString string1("abc");
67 ByteString string2(string1);
68 EXPECT_EQ("abc", string1);
69 EXPECT_EQ("abc", string2);
70 EXPECT_EQ(2, string1.ReferenceCountForTesting());
71 EXPECT_EQ(2, string2.ReferenceCountForTesting());
72 }
73 {
74 // Move-construct.
75 ByteString string1("abc");
76 ByteString string2(std::move(string1));
77 EXPECT_TRUE(string1.IsEmpty());
78 EXPECT_EQ("abc", string2);
79 EXPECT_EQ(0, string1.ReferenceCountForTesting());
80 EXPECT_EQ(1, string2.ReferenceCountForTesting());
81 }
82 }
83
TEST(ByteString,Assign)84 TEST(ByteString, Assign) {
85 {
86 // Copy-assign.
87 ByteString string1;
88 EXPECT_EQ(0, string1.ReferenceCountForTesting());
89 {
90 ByteString string2("abc");
91 EXPECT_EQ(1, string2.ReferenceCountForTesting());
92
93 string1 = string2;
94 EXPECT_EQ(2, string1.ReferenceCountForTesting());
95 EXPECT_EQ(2, string2.ReferenceCountForTesting());
96 }
97 EXPECT_EQ(1, string1.ReferenceCountForTesting());
98 }
99 {
100 // Move-assign.
101 ByteString string1;
102 EXPECT_EQ(0, string1.ReferenceCountForTesting());
103 {
104 ByteString string2("abc");
105 EXPECT_EQ(1, string2.ReferenceCountForTesting());
106
107 string1 = std::move(string2);
108 EXPECT_EQ("abc", string1);
109 EXPECT_TRUE(string2.IsEmpty());
110 EXPECT_EQ(1, string1.ReferenceCountForTesting());
111 EXPECT_EQ(0, string2.ReferenceCountForTesting());
112 }
113 EXPECT_EQ(1, string1.ReferenceCountForTesting());
114 }
115 }
116
TEST(ByteString,OperatorLT)117 TEST(ByteString, OperatorLT) {
118 ByteString empty;
119 ByteString a("a");
120 ByteString abc("abc");
121 ByteString def("def");
122 ByteStringView v_empty;
123 ByteStringView v_a("a");
124 ByteStringView v_abc("abc");
125 ByteStringView v_def("def");
126 const char* const c_null = nullptr;
127 const char* const c_empty = "";
128 const char* const c_a = "a";
129 const char* const c_abc = "abc";
130 const char* const c_def = "def";
131
132 EXPECT_FALSE(empty < empty);
133 EXPECT_FALSE(a < a);
134 EXPECT_FALSE(abc < abc);
135 EXPECT_FALSE(def < def);
136 EXPECT_FALSE(c_null < empty);
137 EXPECT_FALSE(c_empty < empty);
138 EXPECT_FALSE(c_a < a);
139 EXPECT_FALSE(c_abc < abc);
140 EXPECT_FALSE(c_def < def);
141 EXPECT_FALSE(empty < c_null);
142 EXPECT_FALSE(empty < c_empty);
143 EXPECT_FALSE(a < c_a);
144 EXPECT_FALSE(abc < c_abc);
145 EXPECT_FALSE(def < c_def);
146 EXPECT_FALSE(empty < v_empty);
147 EXPECT_FALSE(a < v_a);
148 EXPECT_FALSE(abc < v_abc);
149 EXPECT_FALSE(def < v_def);
150
151 EXPECT_TRUE(empty < a);
152 EXPECT_FALSE(a < empty);
153 EXPECT_TRUE(c_null < a);
154 EXPECT_TRUE(c_empty < a);
155 EXPECT_FALSE(c_a < empty);
156 EXPECT_TRUE(empty < c_a);
157 EXPECT_FALSE(a < c_null);
158 EXPECT_FALSE(a < c_empty);
159 EXPECT_TRUE(empty < v_a);
160 EXPECT_FALSE(a < v_empty);
161
162 EXPECT_TRUE(empty < abc);
163 EXPECT_FALSE(abc < empty);
164 EXPECT_TRUE(c_null < abc);
165 EXPECT_TRUE(c_empty < abc);
166 EXPECT_FALSE(c_abc < empty);
167 EXPECT_TRUE(empty < c_abc);
168 EXPECT_FALSE(abc < c_null);
169 EXPECT_FALSE(abc < c_empty);
170 EXPECT_TRUE(empty < v_abc);
171 EXPECT_FALSE(abc < v_empty);
172
173 EXPECT_TRUE(empty < def);
174 EXPECT_FALSE(def < empty);
175 EXPECT_TRUE(c_null < def);
176 EXPECT_TRUE(c_empty < def);
177 EXPECT_FALSE(c_def < empty);
178 EXPECT_TRUE(empty < c_def);
179 EXPECT_FALSE(def < c_null);
180 EXPECT_FALSE(def < c_empty);
181 EXPECT_TRUE(empty < v_def);
182 EXPECT_FALSE(def < v_empty);
183
184 EXPECT_TRUE(a < abc);
185 EXPECT_FALSE(abc < a);
186 EXPECT_TRUE(c_a < abc);
187 EXPECT_FALSE(c_abc < a);
188 EXPECT_TRUE(a < c_abc);
189 EXPECT_FALSE(abc < c_a);
190 EXPECT_TRUE(a < v_abc);
191 EXPECT_FALSE(abc < v_a);
192
193 EXPECT_TRUE(a < def);
194 EXPECT_FALSE(def < a);
195 EXPECT_TRUE(c_a < def);
196 EXPECT_FALSE(c_def < a);
197 EXPECT_TRUE(a < c_def);
198 EXPECT_FALSE(def < c_a);
199 EXPECT_TRUE(a < v_def);
200 EXPECT_FALSE(def < v_a);
201
202 EXPECT_TRUE(abc < def);
203 EXPECT_FALSE(def < abc);
204 EXPECT_TRUE(c_abc < def);
205 EXPECT_FALSE(c_def < abc);
206 EXPECT_TRUE(abc < c_def);
207 EXPECT_FALSE(def < c_abc);
208 EXPECT_TRUE(abc < v_def);
209 EXPECT_FALSE(def < v_abc);
210 }
211
TEST(ByteString,OperatorEQ)212 TEST(ByteString, OperatorEQ) {
213 ByteString null_string;
214 EXPECT_TRUE(null_string == null_string);
215
216 ByteString empty_string("");
217 EXPECT_TRUE(empty_string == empty_string);
218 EXPECT_TRUE(empty_string == null_string);
219 EXPECT_TRUE(null_string == empty_string);
220
221 ByteString deleted_string("hello");
222 deleted_string.Delete(0, 5);
223 EXPECT_TRUE(deleted_string == deleted_string);
224 EXPECT_TRUE(deleted_string == null_string);
225 EXPECT_TRUE(deleted_string == empty_string);
226 EXPECT_TRUE(null_string == deleted_string);
227 EXPECT_TRUE(empty_string == deleted_string);
228
229 ByteString byte_string("hello");
230 EXPECT_TRUE(byte_string == byte_string);
231 EXPECT_FALSE(byte_string == null_string);
232 EXPECT_FALSE(byte_string == empty_string);
233 EXPECT_FALSE(byte_string == deleted_string);
234 EXPECT_FALSE(null_string == byte_string);
235 EXPECT_FALSE(empty_string == byte_string);
236 EXPECT_FALSE(deleted_string == byte_string);
237
238 ByteString byte_string_same1("hello");
239 EXPECT_TRUE(byte_string == byte_string_same1);
240 EXPECT_TRUE(byte_string_same1 == byte_string);
241
242 ByteString byte_string_same2(byte_string);
243 EXPECT_TRUE(byte_string == byte_string_same2);
244 EXPECT_TRUE(byte_string_same2 == byte_string);
245
246 ByteString byte_string1("he");
247 ByteString byte_string2("hellp");
248 ByteString byte_string3("hellod");
249 EXPECT_FALSE(byte_string == byte_string1);
250 EXPECT_FALSE(byte_string == byte_string2);
251 EXPECT_FALSE(byte_string == byte_string3);
252 EXPECT_FALSE(byte_string1 == byte_string);
253 EXPECT_FALSE(byte_string2 == byte_string);
254 EXPECT_FALSE(byte_string3 == byte_string);
255
256 ByteStringView null_string_c;
257 ByteStringView empty_string_c("");
258 EXPECT_TRUE(null_string == null_string_c);
259 EXPECT_TRUE(null_string == empty_string_c);
260 EXPECT_TRUE(empty_string == null_string_c);
261 EXPECT_TRUE(empty_string == empty_string_c);
262 EXPECT_TRUE(deleted_string == null_string_c);
263 EXPECT_TRUE(deleted_string == empty_string_c);
264 EXPECT_TRUE(null_string_c == null_string);
265 EXPECT_TRUE(empty_string_c == null_string);
266 EXPECT_TRUE(null_string_c == empty_string);
267 EXPECT_TRUE(empty_string_c == empty_string);
268 EXPECT_TRUE(null_string_c == deleted_string);
269 EXPECT_TRUE(empty_string_c == deleted_string);
270
271 ByteStringView byte_string_c_same1("hello");
272 EXPECT_TRUE(byte_string == byte_string_c_same1);
273 EXPECT_TRUE(byte_string_c_same1 == byte_string);
274
275 ByteStringView byte_string_c1("he");
276 ByteStringView byte_string_c2("hellp");
277 ByteStringView byte_string_c3("hellod");
278 EXPECT_FALSE(byte_string == byte_string_c1);
279 EXPECT_FALSE(byte_string == byte_string_c2);
280 EXPECT_FALSE(byte_string == byte_string_c3);
281 EXPECT_FALSE(byte_string_c1 == byte_string);
282 EXPECT_FALSE(byte_string_c2 == byte_string);
283 EXPECT_FALSE(byte_string_c3 == byte_string);
284
285 const char* const c_null_string = nullptr;
286 const char* const c_empty_string = "";
287 EXPECT_TRUE(null_string == c_null_string);
288 EXPECT_TRUE(null_string == c_empty_string);
289 EXPECT_TRUE(empty_string == c_null_string);
290 EXPECT_TRUE(empty_string == c_empty_string);
291 EXPECT_TRUE(deleted_string == c_null_string);
292 EXPECT_TRUE(deleted_string == c_empty_string);
293 EXPECT_TRUE(c_null_string == null_string);
294 EXPECT_TRUE(c_empty_string == null_string);
295 EXPECT_TRUE(c_null_string == empty_string);
296 EXPECT_TRUE(c_empty_string == empty_string);
297 EXPECT_TRUE(c_null_string == deleted_string);
298 EXPECT_TRUE(c_empty_string == deleted_string);
299
300 const char* const c_string_same1 = "hello";
301 EXPECT_TRUE(byte_string == c_string_same1);
302 EXPECT_TRUE(c_string_same1 == byte_string);
303
304 const char* const c_string1 = "he";
305 const char* const c_string2 = "hellp";
306 const char* const c_string3 = "hellod";
307 EXPECT_FALSE(byte_string == c_string1);
308 EXPECT_FALSE(byte_string == c_string2);
309 EXPECT_FALSE(byte_string == c_string3);
310 EXPECT_FALSE(c_string1 == byte_string);
311 EXPECT_FALSE(c_string2 == byte_string);
312 EXPECT_FALSE(c_string3 == byte_string);
313 }
314
TEST(ByteString,OperatorNE)315 TEST(ByteString, OperatorNE) {
316 ByteString null_string;
317 EXPECT_FALSE(null_string != null_string);
318
319 ByteString empty_string("");
320 EXPECT_FALSE(empty_string != empty_string);
321 EXPECT_FALSE(empty_string != null_string);
322 EXPECT_FALSE(null_string != empty_string);
323
324 ByteString deleted_string("hello");
325 deleted_string.Delete(0, 5);
326 EXPECT_FALSE(deleted_string != deleted_string);
327 EXPECT_FALSE(deleted_string != null_string);
328 EXPECT_FALSE(deleted_string != empty_string);
329 EXPECT_FALSE(deleted_string != deleted_string);
330 EXPECT_FALSE(null_string != deleted_string);
331 EXPECT_FALSE(empty_string != deleted_string);
332 EXPECT_FALSE(deleted_string != deleted_string);
333
334 ByteString byte_string("hello");
335 EXPECT_FALSE(byte_string != byte_string);
336 EXPECT_TRUE(byte_string != null_string);
337 EXPECT_TRUE(byte_string != empty_string);
338 EXPECT_TRUE(byte_string != deleted_string);
339 EXPECT_TRUE(null_string != byte_string);
340 EXPECT_TRUE(empty_string != byte_string);
341 EXPECT_TRUE(deleted_string != byte_string);
342
343 ByteString byte_string_same1("hello");
344 EXPECT_FALSE(byte_string != byte_string_same1);
345 EXPECT_FALSE(byte_string_same1 != byte_string);
346
347 ByteString byte_string_same2(byte_string);
348 EXPECT_FALSE(byte_string != byte_string_same2);
349 EXPECT_FALSE(byte_string_same2 != byte_string);
350
351 ByteString byte_string1("he");
352 ByteString byte_string2("hellp");
353 ByteString byte_string3("hellod");
354 EXPECT_TRUE(byte_string != byte_string1);
355 EXPECT_TRUE(byte_string != byte_string2);
356 EXPECT_TRUE(byte_string != byte_string3);
357 EXPECT_TRUE(byte_string1 != byte_string);
358 EXPECT_TRUE(byte_string2 != byte_string);
359 EXPECT_TRUE(byte_string3 != byte_string);
360
361 ByteStringView null_string_c;
362 ByteStringView empty_string_c("");
363 EXPECT_FALSE(null_string != null_string_c);
364 EXPECT_FALSE(null_string != empty_string_c);
365 EXPECT_FALSE(empty_string != null_string_c);
366 EXPECT_FALSE(empty_string != empty_string_c);
367 EXPECT_FALSE(null_string_c != null_string);
368 EXPECT_FALSE(empty_string_c != null_string);
369 EXPECT_FALSE(null_string_c != empty_string);
370 EXPECT_FALSE(empty_string_c != empty_string);
371
372 ByteStringView byte_string_c_same1("hello");
373 EXPECT_FALSE(byte_string != byte_string_c_same1);
374 EXPECT_FALSE(byte_string_c_same1 != byte_string);
375
376 ByteStringView byte_string_c1("he");
377 ByteStringView byte_string_c2("hellp");
378 ByteStringView byte_string_c3("hellod");
379 EXPECT_TRUE(byte_string != byte_string_c1);
380 EXPECT_TRUE(byte_string != byte_string_c2);
381 EXPECT_TRUE(byte_string != byte_string_c3);
382 EXPECT_TRUE(byte_string_c1 != byte_string);
383 EXPECT_TRUE(byte_string_c2 != byte_string);
384 EXPECT_TRUE(byte_string_c3 != byte_string);
385
386 const char* const c_null_string = nullptr;
387 const char* const c_empty_string = "";
388 EXPECT_FALSE(null_string != c_null_string);
389 EXPECT_FALSE(null_string != c_empty_string);
390 EXPECT_FALSE(empty_string != c_null_string);
391 EXPECT_FALSE(empty_string != c_empty_string);
392 EXPECT_FALSE(deleted_string != c_null_string);
393 EXPECT_FALSE(deleted_string != c_empty_string);
394 EXPECT_FALSE(c_null_string != null_string);
395 EXPECT_FALSE(c_empty_string != null_string);
396 EXPECT_FALSE(c_null_string != empty_string);
397 EXPECT_FALSE(c_empty_string != empty_string);
398 EXPECT_FALSE(c_null_string != deleted_string);
399 EXPECT_FALSE(c_empty_string != deleted_string);
400
401 const char* const c_string_same1 = "hello";
402 EXPECT_FALSE(byte_string != c_string_same1);
403 EXPECT_FALSE(c_string_same1 != byte_string);
404
405 const char* const c_string1 = "he";
406 const char* const c_string2 = "hellp";
407 const char* const c_string3 = "hellod";
408 EXPECT_TRUE(byte_string != c_string1);
409 EXPECT_TRUE(byte_string != c_string2);
410 EXPECT_TRUE(byte_string != c_string3);
411 EXPECT_TRUE(c_string1 != byte_string);
412 EXPECT_TRUE(c_string2 != byte_string);
413 EXPECT_TRUE(c_string3 != byte_string);
414 }
415
TEST(ByteString,OperatorPlus)416 TEST(ByteString, OperatorPlus) {
417 EXPECT_EQ("I like dogs", "I like " + ByteString("dogs"));
418 EXPECT_EQ("Dogs like me", ByteString("Dogs") + " like me");
419 EXPECT_EQ("Oh no, error number 42",
420 "Oh no, error number " + ByteString::Format("%d", 42));
421
422 {
423 // Make sure operator+= and Concat() increases string memory allocation
424 // geometrically.
425 int allocations = 0;
426 ByteString str("ABCDEFGHIJKLMN");
427 const char* buffer = str.c_str();
428 for (size_t i = 0; i < 10000; ++i) {
429 str += "!";
430 const char* new_buffer = str.c_str();
431 if (new_buffer != buffer) {
432 buffer = new_buffer;
433 ++allocations;
434 }
435 }
436 EXPECT_LT(allocations, 25);
437 EXPECT_GT(allocations, 10);
438 }
439 }
440
TEST(ByteString,Concat)441 TEST(ByteString, Concat) {
442 ByteString fred;
443 fred.Concat("FRED", 4);
444 EXPECT_EQ("FRED", fred);
445
446 fred.Concat("DY", 2);
447 EXPECT_EQ("FREDDY", fred);
448
449 fred.Delete(3, 3);
450 EXPECT_EQ("FRE", fred);
451
452 fred.Concat("D", 1);
453 EXPECT_EQ("FRED", fred);
454
455 ByteString copy = fred;
456 fred.Concat("DY", 2);
457 EXPECT_EQ("FREDDY", fred);
458 EXPECT_EQ("FRED", copy);
459 }
460
TEST(ByteString,Remove)461 TEST(ByteString, Remove) {
462 ByteString freed("FREED");
463 freed.Remove('E');
464 EXPECT_EQ("FRD", freed);
465 freed.Remove('F');
466 EXPECT_EQ("RD", freed);
467 freed.Remove('D');
468 EXPECT_EQ("R", freed);
469 freed.Remove('X');
470 EXPECT_EQ("R", freed);
471 freed.Remove('R');
472 EXPECT_EQ("", freed);
473
474 ByteString empty;
475 empty.Remove('X');
476 EXPECT_EQ("", empty);
477 }
478
TEST(ByteString,RemoveCopies)479 TEST(ByteString, RemoveCopies) {
480 ByteString freed("FREED");
481 const char* old_buffer = freed.c_str();
482
483 // No change with single reference - no copy.
484 freed.Remove('Q');
485 EXPECT_EQ("FREED", freed);
486 EXPECT_EQ(old_buffer, freed.c_str());
487
488 // Change with single reference - no copy.
489 freed.Remove('E');
490 EXPECT_EQ("FRD", freed);
491 EXPECT_EQ(old_buffer, freed.c_str());
492
493 // No change with multiple references - no copy.
494 ByteString shared(freed);
495 freed.Remove('Q');
496 EXPECT_EQ("FRD", freed);
497 EXPECT_EQ(old_buffer, freed.c_str());
498 EXPECT_EQ(old_buffer, shared.c_str());
499
500 // Change with multiple references -- must copy.
501 freed.Remove('D');
502 EXPECT_EQ("FR", freed);
503 EXPECT_NE(old_buffer, freed.c_str());
504 EXPECT_EQ("FRD", shared);
505 EXPECT_EQ(old_buffer, shared.c_str());
506 }
507
TEST(ByteString,Replace)508 TEST(ByteString, Replace) {
509 ByteString fred("FRED");
510 fred.Replace("FR", "BL");
511 EXPECT_EQ("BLED", fred);
512 fred.Replace("D", "DDY");
513 EXPECT_EQ("BLEDDY", fred);
514 fred.Replace("LEDD", "");
515 EXPECT_EQ("BY", fred);
516 fred.Replace("X", "CLAMS");
517 EXPECT_EQ("BY", fred);
518 fred.Replace("BY", "HI");
519 EXPECT_EQ("HI", fred);
520 fred.Replace("", "CLAMS");
521 EXPECT_EQ("HI", fred);
522 fred.Replace("HI", "");
523 EXPECT_EQ("", fred);
524 }
525
TEST(ByteString,Insert)526 TEST(ByteString, Insert) {
527 ByteString fred("FRED");
528 EXPECT_EQ(5u, fred.Insert(0, 'S'));
529 EXPECT_EQ("SFRED", fred);
530 EXPECT_EQ(6u, fred.Insert(1, 'T'));
531 EXPECT_EQ("STFRED", fred);
532 EXPECT_EQ(7u, fred.Insert(4, 'U'));
533 EXPECT_EQ("STFRUED", fred);
534 EXPECT_EQ(8u, fred.Insert(7, 'V'));
535 EXPECT_EQ("STFRUEDV", fred);
536 EXPECT_EQ(8u, fred.Insert(12, 'P'));
537 EXPECT_EQ("STFRUEDV", fred);
538 {
539 ByteString empty;
540 EXPECT_EQ(1u, empty.Insert(0, 'X'));
541 EXPECT_EQ("X", empty);
542 }
543 {
544 ByteString empty;
545 EXPECT_EQ(0u, empty.Insert(5, 'X'));
546 EXPECT_NE("X", empty);
547 }
548 }
549
TEST(ByteString,InsertAtFrontAndInsertAtBack)550 TEST(ByteString, InsertAtFrontAndInsertAtBack) {
551 {
552 ByteString empty;
553 EXPECT_EQ(1u, empty.InsertAtFront('D'));
554 EXPECT_EQ("D", empty);
555 EXPECT_EQ(2u, empty.InsertAtFront('E'));
556 EXPECT_EQ("ED", empty);
557 EXPECT_EQ(3u, empty.InsertAtFront('R'));
558 EXPECT_EQ("RED", empty);
559 EXPECT_EQ(4u, empty.InsertAtFront('F'));
560 EXPECT_EQ("FRED", empty);
561 }
562 {
563 ByteString empty;
564 EXPECT_EQ(1u, empty.InsertAtBack('F'));
565 EXPECT_EQ("F", empty);
566 EXPECT_EQ(2u, empty.InsertAtBack('R'));
567 EXPECT_EQ("FR", empty);
568 EXPECT_EQ(3u, empty.InsertAtBack('E'));
569 EXPECT_EQ("FRE", empty);
570 EXPECT_EQ(4u, empty.InsertAtBack('D'));
571 EXPECT_EQ("FRED", empty);
572 }
573 {
574 ByteString empty;
575 EXPECT_EQ(1u, empty.InsertAtBack('E'));
576 EXPECT_EQ("E", empty);
577 EXPECT_EQ(2u, empty.InsertAtFront('R'));
578 EXPECT_EQ("RE", empty);
579 EXPECT_EQ(3u, empty.InsertAtBack('D'));
580 EXPECT_EQ("RED", empty);
581 EXPECT_EQ(4u, empty.InsertAtFront('F'));
582 EXPECT_EQ("FRED", empty);
583 }
584 }
585
TEST(ByteString,Delete)586 TEST(ByteString, Delete) {
587 ByteString fred("FRED");
588 EXPECT_EQ(4u, fred.Delete(0, 0));
589 EXPECT_EQ("FRED", fred);
590 EXPECT_EQ(2u, fred.Delete(0, 2));
591 EXPECT_EQ("ED", fred);
592 EXPECT_EQ(1u, fred.Delete(1));
593 EXPECT_EQ("E", fred);
594 EXPECT_EQ(0u, fred.Delete(0));
595 EXPECT_EQ("", fred);
596 EXPECT_EQ(0u, fred.Delete(0));
597 EXPECT_EQ("", fred);
598
599 ByteString empty;
600 EXPECT_EQ(0u, empty.Delete(0));
601 EXPECT_EQ("", empty);
602 EXPECT_EQ(0u, empty.Delete(1));
603 EXPECT_EQ("", empty);
604 }
605
TEST(ByteString,Substr)606 TEST(ByteString, Substr) {
607 ByteString fred("FRED");
608 EXPECT_EQ("", fred.Substr(0, 0));
609 EXPECT_EQ("", fred.Substr(3, 0));
610 EXPECT_EQ("FRED", fred.Substr(0, 4));
611 EXPECT_EQ("RED", fred.Substr(1, 3));
612 EXPECT_EQ("ED", fred.Substr(2, 2));
613 EXPECT_EQ("D", fred.Substr(3, 1));
614 EXPECT_EQ("F", fred.Substr(0, 1));
615 EXPECT_EQ("R", fred.Substr(1, 1));
616 EXPECT_EQ("E", fred.Substr(2, 1));
617 EXPECT_EQ("D", fred.Substr(3, 1));
618 EXPECT_EQ("FR", fred.Substr(0, 2));
619 EXPECT_EQ("FRED", fred.Substr(0, 4));
620 EXPECT_EQ("", fred.Substr(0, 10));
621
622 EXPECT_EQ("RED", fred.Substr(1, 3));
623 EXPECT_EQ("", fred.Substr(4, 1));
624
625 ByteString empty;
626 EXPECT_EQ("", empty.Substr(0, 0));
627 }
628
TEST(ByteString,First)629 TEST(ByteString, First) {
630 ByteString fred("FRED");
631 EXPECT_EQ("", fred.First(0));
632 EXPECT_EQ("F", fred.First(1));
633 EXPECT_EQ("FR", fred.First(2));
634 EXPECT_EQ("FRE", fred.First(3));
635 EXPECT_EQ("FRED", fred.First(4));
636
637 EXPECT_EQ("", fred.First(5));
638
639 ByteString empty;
640 EXPECT_EQ("", empty.First(0));
641 EXPECT_EQ("", empty.First(1));
642 }
643
TEST(ByteString,Last)644 TEST(ByteString, Last) {
645 ByteString fred("FRED");
646 EXPECT_EQ("", fred.Last(0));
647 EXPECT_EQ("D", fred.Last(1));
648 EXPECT_EQ("ED", fred.Last(2));
649 EXPECT_EQ("RED", fred.Last(3));
650 EXPECT_EQ("FRED", fred.Last(4));
651
652 EXPECT_EQ("", fred.Last(5));
653
654 ByteString empty;
655 EXPECT_EQ("", empty.Last(0));
656 EXPECT_EQ("", empty.Last(1));
657 }
658
TEST(ByteString,Find)659 TEST(ByteString, Find) {
660 ByteString null_string;
661 EXPECT_FALSE(null_string.Find('a').has_value());
662 EXPECT_FALSE(null_string.Find('\0').has_value());
663
664 ByteString empty_string("");
665 EXPECT_FALSE(empty_string.Find('a').has_value());
666 EXPECT_FALSE(empty_string.Find('\0').has_value());
667
668 Optional<size_t> result;
669 ByteString single_string("a");
670 result = single_string.Find('a');
671 ASSERT_TRUE(result.has_value());
672 EXPECT_EQ(0u, result.value());
673 EXPECT_FALSE(single_string.Find('b').has_value());
674 EXPECT_FALSE(single_string.Find('\0').has_value());
675
676 ByteString longer_string("abccc");
677 result = longer_string.Find('a');
678 ASSERT_TRUE(result.has_value());
679 EXPECT_EQ(0u, result.value());
680 result = longer_string.Find('c');
681 ASSERT_TRUE(result.has_value());
682 EXPECT_EQ(2u, result.value());
683 result = longer_string.Find('c', 3);
684 ASSERT_TRUE(result.has_value());
685 EXPECT_EQ(3u, result.value());
686 EXPECT_FALSE(longer_string.Find('d').has_value());
687 EXPECT_FALSE(longer_string.Find('\0').has_value());
688
689 result = longer_string.Find("ab");
690 ASSERT_TRUE(result.has_value());
691 EXPECT_EQ(0u, result.value());
692 result = longer_string.Find("ccc");
693 ASSERT_TRUE(result.has_value());
694 EXPECT_EQ(2u, result.value());
695 result = longer_string.Find("cc", 3);
696 ASSERT_TRUE(result.has_value());
697 EXPECT_EQ(3u, result.value());
698 EXPECT_FALSE(longer_string.Find("d").has_value());
699
700 ByteString hibyte_string(
701 "ab\x8c"
702 "def");
703 result = hibyte_string.Find('\x8c');
704 ASSERT_TRUE(result.has_value());
705 EXPECT_EQ(2u, result.value());
706 }
707
TEST(ByteString,ReverseFind)708 TEST(ByteString, ReverseFind) {
709 ByteString null_string;
710 EXPECT_FALSE(null_string.ReverseFind('a').has_value());
711 EXPECT_FALSE(null_string.ReverseFind('\0').has_value());
712
713 ByteString empty_string("");
714 EXPECT_FALSE(empty_string.ReverseFind('a').has_value());
715 EXPECT_FALSE(empty_string.ReverseFind('\0').has_value());
716
717 Optional<size_t> result;
718 ByteString single_string("a");
719 result = single_string.ReverseFind('a');
720 ASSERT_TRUE(result.has_value());
721 EXPECT_EQ(0u, result.value());
722 EXPECT_FALSE(single_string.ReverseFind('b').has_value());
723 EXPECT_FALSE(single_string.ReverseFind('\0').has_value());
724
725 ByteString longer_string("abccc");
726 result = longer_string.ReverseFind('a');
727 ASSERT_TRUE(result.has_value());
728 EXPECT_EQ(0u, result.value());
729 result = longer_string.ReverseFind('c');
730 ASSERT_TRUE(result.has_value());
731 EXPECT_EQ(4u, result.value());
732 EXPECT_FALSE(longer_string.ReverseFind('\0').has_value());
733
734 ByteString hibyte_string(
735 "ab\x8c"
736 "def");
737 result = hibyte_string.ReverseFind('\x8c');
738 ASSERT_TRUE(result.has_value());
739 EXPECT_EQ(2u, result.value());
740 }
741
TEST(ByteString,UpperLower)742 TEST(ByteString, UpperLower) {
743 ByteString fred("F-Re.42D");
744 fred.MakeLower();
745 EXPECT_EQ("f-re.42d", fred);
746 fred.MakeUpper();
747 EXPECT_EQ("F-RE.42D", fred);
748
749 ByteString empty;
750 empty.MakeLower();
751 EXPECT_EQ("", empty);
752 empty.MakeUpper();
753 EXPECT_EQ("", empty);
754 }
755
TEST(ByteString,Trim)756 TEST(ByteString, Trim) {
757 ByteString fred(" FRED ");
758 fred.Trim();
759 EXPECT_EQ("FRED", fred);
760 fred.Trim('E');
761 EXPECT_EQ("FRED", fred);
762 fred.Trim('F');
763 EXPECT_EQ("RED", fred);
764 fred.Trim("ERP");
765 EXPECT_EQ("D", fred);
766
767 ByteString blank(" ");
768 blank.Trim("ERP");
769 EXPECT_EQ(" ", blank);
770 blank.Trim('E');
771 EXPECT_EQ(" ", blank);
772 blank.Trim();
773 EXPECT_EQ("", blank);
774
775 ByteString empty;
776 empty.Trim("ERP");
777 EXPECT_EQ("", empty);
778 empty.Trim('E');
779 EXPECT_EQ("", empty);
780 empty.Trim();
781 EXPECT_EQ("", empty);
782
783 ByteString abc(" ABCCBA ");
784 abc.Trim("A");
785 EXPECT_EQ(" ABCCBA ", abc);
786 abc.Trim(" A");
787 EXPECT_EQ("BCCB", abc);
788 }
789
TEST(ByteString,TrimLeft)790 TEST(ByteString, TrimLeft) {
791 ByteString fred(" FRED ");
792 fred.TrimLeft();
793 EXPECT_EQ("FRED ", fred);
794 fred.TrimLeft('E');
795 EXPECT_EQ("FRED ", fred);
796 fred.TrimLeft('F');
797 EXPECT_EQ("RED ", fred);
798 fred.TrimLeft("ERP");
799 EXPECT_EQ("D ", fred);
800
801 ByteString blank(" ");
802 blank.TrimLeft("ERP");
803 EXPECT_EQ(" ", blank);
804 blank.TrimLeft('E');
805 EXPECT_EQ(" ", blank);
806 blank.TrimLeft();
807 EXPECT_EQ("", blank);
808
809 ByteString empty;
810 empty.TrimLeft("ERP");
811 EXPECT_EQ("", empty);
812 empty.TrimLeft('E');
813 EXPECT_EQ("", empty);
814 empty.TrimLeft();
815 EXPECT_EQ("", empty);
816 }
817
TEST(ByteString,TrimLeftCopies)818 TEST(ByteString, TrimLeftCopies) {
819 {
820 // With a single reference, no copy takes place.
821 ByteString fred(" FRED ");
822 const char* old_buffer = fred.c_str();
823 fred.TrimLeft();
824 EXPECT_EQ("FRED ", fred);
825 EXPECT_EQ(old_buffer, fred.c_str());
826 }
827 {
828 // With multiple references, we must copy.
829 ByteString fred(" FRED ");
830 ByteString other_fred = fred;
831 const char* old_buffer = fred.c_str();
832 fred.TrimLeft();
833 EXPECT_EQ("FRED ", fred);
834 EXPECT_EQ(" FRED ", other_fred);
835 EXPECT_NE(old_buffer, fred.c_str());
836 }
837 {
838 // With multiple references, but no modifications, no copy.
839 ByteString fred("FRED");
840 ByteString other_fred = fred;
841 const char* old_buffer = fred.c_str();
842 fred.TrimLeft();
843 EXPECT_EQ("FRED", fred);
844 EXPECT_EQ("FRED", other_fred);
845 EXPECT_EQ(old_buffer, fred.c_str());
846 }
847 }
848
TEST(ByteString,TrimRight)849 TEST(ByteString, TrimRight) {
850 ByteString fred(" FRED ");
851 fred.TrimRight();
852 EXPECT_EQ(" FRED", fred);
853 fred.TrimRight('E');
854 EXPECT_EQ(" FRED", fred);
855 fred.TrimRight('D');
856 EXPECT_EQ(" FRE", fred);
857 fred.TrimRight("ERP");
858 EXPECT_EQ(" F", fred);
859
860 ByteString blank(" ");
861 blank.TrimRight("ERP");
862 EXPECT_EQ(" ", blank);
863 blank.TrimRight('E');
864 EXPECT_EQ(" ", blank);
865 blank.TrimRight();
866 EXPECT_EQ("", blank);
867
868 ByteString empty;
869 empty.TrimRight("ERP");
870 EXPECT_EQ("", empty);
871 empty.TrimRight('E');
872 EXPECT_EQ("", empty);
873 empty.TrimRight();
874 EXPECT_EQ("", empty);
875 }
876
TEST(ByteString,TrimRightCopies)877 TEST(ByteString, TrimRightCopies) {
878 {
879 // With a single reference, no copy takes place.
880 ByteString fred(" FRED ");
881 const char* old_buffer = fred.c_str();
882 fred.TrimRight();
883 EXPECT_EQ(" FRED", fred);
884 EXPECT_EQ(old_buffer, fred.c_str());
885 }
886 {
887 // With multiple references, we must copy.
888 ByteString fred(" FRED ");
889 ByteString other_fred = fred;
890 const char* old_buffer = fred.c_str();
891 fred.TrimRight();
892 EXPECT_EQ(" FRED", fred);
893 EXPECT_EQ(" FRED ", other_fred);
894 EXPECT_NE(old_buffer, fred.c_str());
895 }
896 {
897 // With multiple references, but no modifications, no copy.
898 ByteString fred("FRED");
899 ByteString other_fred = fred;
900 const char* old_buffer = fred.c_str();
901 fred.TrimRight();
902 EXPECT_EQ("FRED", fred);
903 EXPECT_EQ("FRED", other_fred);
904 EXPECT_EQ(old_buffer, fred.c_str());
905 }
906 }
907
TEST(ByteString,Reserve)908 TEST(ByteString, Reserve) {
909 {
910 ByteString str;
911 str.Reserve(6);
912 const char* old_buffer = str.c_str();
913 str += "ABCDEF";
914 EXPECT_EQ(old_buffer, str.c_str());
915 str += "Blah Blah Blah Blah Blah Blah";
916 EXPECT_NE(old_buffer, str.c_str());
917 }
918 {
919 ByteString str("A");
920 str.Reserve(6);
921 const char* old_buffer = str.c_str();
922 str += "BCDEF";
923 EXPECT_EQ(old_buffer, str.c_str());
924 str += "Blah Blah Blah Blah Blah Blah";
925 EXPECT_NE(old_buffer, str.c_str());
926 }
927 }
928
TEST(ByteString,GetBuffer)929 TEST(ByteString, GetBuffer) {
930 ByteString str1;
931 {
932 pdfium::span<char> buffer = str1.GetBuffer(12);
933 // NOLINTNEXTLINE(runtime/printf)
934 strcpy(buffer.data(), "clams");
935 }
936 str1.ReleaseBuffer(str1.GetStringLength());
937 EXPECT_EQ("clams", str1);
938
939 ByteString str2("cl");
940 {
941 pdfium::span<char> buffer = str2.GetBuffer(12);
942 // NOLINTNEXTLINE(runtime/printf)
943 strcpy(&buffer[2], "ams");
944 }
945 str2.ReleaseBuffer(str2.GetStringLength());
946 EXPECT_EQ("clams", str2);
947 }
948
TEST(ByteString,ReleaseBuffer)949 TEST(ByteString, ReleaseBuffer) {
950 {
951 ByteString str;
952 str.Reserve(12);
953 str += "clams";
954 const char* old_buffer = str.c_str();
955 str.ReleaseBuffer(4);
956 EXPECT_EQ(old_buffer, str.c_str());
957 EXPECT_EQ("clam", str);
958 }
959 {
960 ByteString str("c");
961 str.Reserve(12);
962 str += "lams";
963 const char* old_buffer = str.c_str();
964 str.ReleaseBuffer(4);
965 EXPECT_EQ(old_buffer, str.c_str());
966 EXPECT_EQ("clam", str);
967 }
968 {
969 ByteString str;
970 str.Reserve(200);
971 str += "clams";
972 const char* old_buffer = str.c_str();
973 str.ReleaseBuffer(4);
974 EXPECT_NE(old_buffer, str.c_str());
975 EXPECT_EQ("clam", str);
976 }
977 {
978 ByteString str("c");
979 str.Reserve(200);
980 str += "lams";
981 const char* old_buffer = str.c_str();
982 str.ReleaseBuffer(4);
983 EXPECT_NE(old_buffer, str.c_str());
984 EXPECT_EQ("clam", str);
985 }
986 }
987
TEST(ByteString,EmptyReverseIterator)988 TEST(ByteString, EmptyReverseIterator) {
989 ByteString empty;
990 auto iter = empty.rbegin();
991 EXPECT_TRUE(iter == empty.rend());
992 EXPECT_FALSE(iter != empty.rend());
993 EXPECT_FALSE(iter < empty.rend());
994 }
995
TEST(ByteString,OneCharReverseIterator)996 TEST(ByteString, OneCharReverseIterator) {
997 ByteString one_str("a");
998 auto iter = one_str.rbegin();
999 EXPECT_FALSE(iter == one_str.rend());
1000 EXPECT_TRUE(iter != one_str.rend());
1001 EXPECT_TRUE(iter < one_str.rend());
1002
1003 char ch = *iter++;
1004 EXPECT_EQ('a', ch);
1005 EXPECT_TRUE(iter == one_str.rend());
1006 EXPECT_FALSE(iter != one_str.rend());
1007 EXPECT_FALSE(iter < one_str.rend());
1008 }
1009
TEST(ByteString,MultiCharReverseIterator)1010 TEST(ByteString, MultiCharReverseIterator) {
1011 ByteString multi_str("abcd");
1012 auto iter = multi_str.rbegin();
1013 EXPECT_NE(iter, multi_str.rend());
1014 EXPECT_EQ(4, multi_str.rend() - iter);
1015 EXPECT_EQ(0, iter - multi_str.rbegin());
1016
1017 char ch = *iter++;
1018 EXPECT_EQ('d', ch);
1019 EXPECT_EQ('c', *iter);
1020 EXPECT_NE(iter, multi_str.rend());
1021 EXPECT_EQ(3, multi_str.rend() - iter);
1022 EXPECT_EQ(1, iter - multi_str.rbegin());
1023
1024 ch = *(++iter);
1025 EXPECT_EQ('b', ch);
1026 EXPECT_EQ('b', *iter);
1027 EXPECT_NE(iter, multi_str.rend());
1028 EXPECT_EQ(2, multi_str.rend() - iter);
1029 EXPECT_EQ(2, iter - multi_str.rbegin());
1030
1031 ch = *iter++;
1032 EXPECT_EQ('b', ch);
1033 EXPECT_EQ('a', *iter);
1034 EXPECT_NE(iter, multi_str.rend());
1035 EXPECT_EQ(1, multi_str.rend() - iter);
1036 EXPECT_EQ(3, iter - multi_str.rbegin());
1037
1038 ch = *iter++;
1039 EXPECT_EQ('a', ch);
1040 EXPECT_EQ(iter, multi_str.rend());
1041 EXPECT_EQ(0, multi_str.rend() - iter);
1042 EXPECT_EQ(4, iter - multi_str.rbegin());
1043
1044 ch = *(--iter);
1045 EXPECT_EQ('a', ch);
1046 EXPECT_EQ('a', *iter);
1047 EXPECT_NE(iter, multi_str.rend());
1048 EXPECT_EQ(1, multi_str.rend() - iter);
1049 EXPECT_EQ(3, iter - multi_str.rbegin());
1050
1051 ch = *iter--;
1052 EXPECT_EQ('a', ch);
1053 EXPECT_EQ('b', *iter);
1054 EXPECT_NE(iter, multi_str.rend());
1055 EXPECT_EQ(2, multi_str.rend() - iter);
1056 EXPECT_EQ(2, iter - multi_str.rbegin());
1057
1058 ch = *iter--;
1059 EXPECT_EQ('b', ch);
1060 EXPECT_EQ('c', *iter);
1061 EXPECT_NE(iter, multi_str.rend());
1062 EXPECT_EQ(3, multi_str.rend() - iter);
1063 EXPECT_EQ(1, iter - multi_str.rbegin());
1064
1065 ch = *(--iter);
1066 EXPECT_EQ('d', ch);
1067 EXPECT_EQ('d', *iter);
1068 EXPECT_EQ(iter, multi_str.rbegin());
1069 EXPECT_EQ(4, multi_str.rend() - iter);
1070 EXPECT_EQ(0, iter - multi_str.rbegin());
1071 }
1072
TEST(ByteStringView,Null)1073 TEST(ByteStringView, Null) {
1074 ByteStringView null_string;
1075 EXPECT_FALSE(null_string.raw_str());
1076 EXPECT_EQ(0u, null_string.GetLength());
1077 EXPECT_TRUE(null_string.IsEmpty());
1078
1079 ByteStringView another_null_string;
1080 EXPECT_EQ(null_string, another_null_string);
1081
1082 ByteStringView copied_null_string(null_string);
1083 EXPECT_FALSE(copied_null_string.raw_str());
1084 EXPECT_EQ(0u, copied_null_string.GetLength());
1085 EXPECT_TRUE(copied_null_string.IsEmpty());
1086 EXPECT_EQ(null_string, copied_null_string);
1087
1088 ByteStringView span_null_string = pdfium::span<const uint8_t>();
1089 EXPECT_FALSE(span_null_string.raw_str());
1090 EXPECT_EQ(0u, span_null_string.GetLength());
1091 EXPECT_TRUE(span_null_string.IsEmpty());
1092 EXPECT_EQ(null_string, span_null_string);
1093
1094 ByteStringView empty_string(""); // Pointer to NUL, not NULL pointer.
1095 EXPECT_TRUE(empty_string.raw_str());
1096 EXPECT_EQ(0u, empty_string.GetLength());
1097 EXPECT_TRUE(empty_string.IsEmpty());
1098 EXPECT_EQ(null_string, empty_string);
1099
1100 ByteStringView assigned_null_string("initially not nullptr");
1101 assigned_null_string = null_string;
1102 EXPECT_FALSE(assigned_null_string.raw_str());
1103 EXPECT_EQ(0u, assigned_null_string.GetLength());
1104 EXPECT_TRUE(assigned_null_string.IsEmpty());
1105 EXPECT_EQ(null_string, assigned_null_string);
1106
1107 ByteStringView assigned_nullptr_string("initially not nullptr");
1108 assigned_nullptr_string = nullptr;
1109 EXPECT_FALSE(assigned_nullptr_string.raw_str());
1110 EXPECT_EQ(0u, assigned_nullptr_string.GetLength());
1111 EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
1112 EXPECT_EQ(null_string, assigned_nullptr_string);
1113
1114 ByteStringView assigned_span_null_string("initially not null span");
1115 assigned_span_null_string = pdfium::span<const uint8_t>();
1116 EXPECT_FALSE(assigned_span_null_string.raw_str());
1117 EXPECT_EQ(0u, assigned_span_null_string.GetLength());
1118 EXPECT_TRUE(assigned_span_null_string.IsEmpty());
1119 EXPECT_EQ(null_string, assigned_span_null_string);
1120
1121 ByteStringView non_null_string("a");
1122 EXPECT_NE(null_string, non_null_string);
1123 }
1124
TEST(ByteStringView,NotNull)1125 TEST(ByteStringView, NotNull) {
1126 ByteStringView string3("abc");
1127 ByteStringView string6("abcdef");
1128 ByteStringView alternate_string3("abcdef", 3);
1129 ByteStringView span_string4(pdfium::as_bytes(pdfium::make_span("abcd", 4)));
1130 ByteStringView embedded_nul_string7("abc\0def", 7);
1131 ByteStringView illegal_string7("abcdef", 7);
1132
1133 EXPECT_EQ(3u, string3.GetLength());
1134 EXPECT_EQ(6u, string6.GetLength());
1135 EXPECT_EQ(3u, alternate_string3.GetLength());
1136 EXPECT_EQ(4u, span_string4.GetLength());
1137 EXPECT_EQ(7u, embedded_nul_string7.GetLength());
1138 EXPECT_EQ(7u, illegal_string7.GetLength());
1139
1140 EXPECT_NE(string3, string6);
1141 EXPECT_EQ(string3, alternate_string3);
1142 EXPECT_NE(string3, embedded_nul_string7);
1143 EXPECT_NE(string3, illegal_string7);
1144 EXPECT_NE(string6, alternate_string3);
1145 EXPECT_NE(string6, embedded_nul_string7);
1146 EXPECT_NE(string6, illegal_string7);
1147 EXPECT_NE(alternate_string3, embedded_nul_string7);
1148 EXPECT_NE(alternate_string3, illegal_string7);
1149 EXPECT_NE(embedded_nul_string7, illegal_string7);
1150
1151 ByteStringView copied_string3(string3);
1152 ByteStringView copied_alternate_string3(alternate_string3);
1153 ByteStringView copied_embedded_nul_string7(embedded_nul_string7);
1154
1155 EXPECT_EQ(string3, copied_string3);
1156 EXPECT_EQ(alternate_string3, copied_alternate_string3);
1157 EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7);
1158
1159 ByteStringView assigned_string3("intially something else");
1160 ByteStringView assigned_alternate_string3("initally something else");
1161 ByteStringView assigned_ptr_string3("initially something else");
1162 ByteStringView assigned_embedded_nul_string7("initially something else");
1163
1164 assigned_string3 = string3;
1165 assigned_alternate_string3 = alternate_string3;
1166 assigned_ptr_string3 = "abc";
1167 assigned_embedded_nul_string7 = embedded_nul_string7;
1168 EXPECT_EQ(string3, assigned_string3);
1169 EXPECT_EQ(alternate_string3, assigned_alternate_string3);
1170 EXPECT_EQ(alternate_string3, assigned_ptr_string3);
1171 EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7);
1172 }
1173
TEST(ByteStringView,FromChar)1174 TEST(ByteStringView, FromChar) {
1175 ByteStringView null_string;
1176 ByteStringView lower_a_string("a");
1177
1178 // Must have lvalues that outlive the corresponding ByteStringC.
1179 char nul = '\0';
1180 char lower_a = 'a';
1181 ByteStringView nul_string_from_char(nul);
1182 ByteStringView lower_a_string_from_char(lower_a);
1183
1184 // Pointer to nul, not nullptr ptr, hence length 1 ...
1185 EXPECT_EQ(1u, nul_string_from_char.GetLength());
1186 EXPECT_NE(null_string, nul_string_from_char);
1187
1188 EXPECT_EQ(1u, lower_a_string_from_char.GetLength());
1189 EXPECT_EQ(lower_a_string, lower_a_string_from_char);
1190 EXPECT_NE(nul_string_from_char, lower_a_string_from_char);
1191
1192 ByteStringView longer_string("ab");
1193 EXPECT_NE(longer_string, lower_a_string_from_char);
1194 }
1195
TEST(ByteStringView,FromVector)1196 TEST(ByteStringView, FromVector) {
1197 std::vector<uint8_t> null_vec;
1198 ByteStringView null_string(null_vec);
1199 EXPECT_EQ(0u, null_string.GetLength());
1200
1201 std::vector<uint8_t> lower_a_vec(10, static_cast<uint8_t>('a'));
1202 ByteStringView lower_a_string(lower_a_vec);
1203 EXPECT_EQ(static_cast<size_t>(10), lower_a_string.GetLength());
1204 EXPECT_EQ("aaaaaaaaaa", lower_a_string);
1205
1206 std::vector<uint8_t> cleared_vec;
1207 cleared_vec.push_back(42);
1208 cleared_vec.pop_back();
1209 ByteStringView cleared_string(cleared_vec);
1210 EXPECT_EQ(0u, cleared_string.GetLength());
1211 EXPECT_EQ(nullptr, cleared_string.raw_str());
1212 }
1213
TEST(ByteStringView,GetID)1214 TEST(ByteStringView, GetID) {
1215 ByteStringView null_string;
1216 EXPECT_EQ(0u, null_string.GetID());
1217
1218 ByteStringView empty_string("");
1219 EXPECT_EQ(0u, empty_string.GetID());
1220
1221 ByteStringView short_string("ab");
1222 EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID());
1223
1224 ByteStringView longer_string("abcdef");
1225 EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID());
1226 }
1227
TEST(ByteStringView,Find)1228 TEST(ByteStringView, Find) {
1229 ByteStringView null_string;
1230 EXPECT_FALSE(null_string.Find('a').has_value());
1231 EXPECT_FALSE(null_string.Find('\0').has_value());
1232
1233 ByteStringView empty_string("");
1234 EXPECT_FALSE(empty_string.Find('a').has_value());
1235 EXPECT_FALSE(empty_string.Find('\0').has_value());
1236
1237 Optional<size_t> result;
1238 ByteStringView single_string("a");
1239 result = single_string.Find('a');
1240 ASSERT_TRUE(result.has_value());
1241 EXPECT_EQ(0u, result.value());
1242 EXPECT_FALSE(single_string.Find('b').has_value());
1243 EXPECT_FALSE(single_string.Find('\0').has_value());
1244
1245 ByteStringView longer_string("abccc");
1246 result = longer_string.Find('a');
1247 ASSERT_TRUE(result.has_value());
1248 EXPECT_EQ(0u, result.value());
1249 result = longer_string.Find('c');
1250 ASSERT_TRUE(result.has_value());
1251 EXPECT_EQ(2u, result.value());
1252 EXPECT_FALSE(longer_string.Find('d').has_value());
1253 EXPECT_FALSE(longer_string.Find('\0').has_value());
1254
1255 ByteStringView hibyte_string(
1256 "ab\x8c"
1257 "def");
1258 result = hibyte_string.Find('\x8c');
1259 ASSERT_TRUE(result.has_value());
1260 EXPECT_EQ(2u, result.value());
1261 }
1262
TEST(ByteStringView,Substr)1263 TEST(ByteStringView, Substr) {
1264 ByteStringView null_string;
1265 EXPECT_EQ(null_string, null_string.Substr(0, 1));
1266 EXPECT_EQ(null_string, null_string.Substr(1, 1));
1267
1268 ByteStringView empty_string("");
1269 EXPECT_EQ("", empty_string.Substr(0, 1));
1270 EXPECT_EQ("", empty_string.Substr(1, 1));
1271
1272 ByteStringView single_character("a");
1273 EXPECT_EQ("", single_character.Substr(0, 0));
1274 EXPECT_EQ(single_character, single_character.Substr(0, 1));
1275 EXPECT_EQ("", single_character.Substr(1, 0));
1276 EXPECT_EQ("", single_character.Substr(1, 1));
1277
1278 ByteStringView longer_string("abcdef");
1279 EXPECT_EQ(longer_string, longer_string.Substr(0, 6));
1280 EXPECT_EQ("", longer_string.Substr(0, 187));
1281
1282 ByteStringView leading_substring("ab");
1283 EXPECT_EQ(leading_substring, longer_string.Substr(0, 2));
1284
1285 ByteStringView middle_substring("bcde");
1286 EXPECT_EQ(middle_substring, longer_string.Substr(1, 4));
1287
1288 ByteStringView trailing_substring("ef");
1289 EXPECT_EQ(trailing_substring, longer_string.Substr(4, 2));
1290 EXPECT_EQ("", longer_string.Substr(4, 3));
1291 }
1292
TEST(ByteStringView,TrimmedRight)1293 TEST(ByteStringView, TrimmedRight) {
1294 ByteStringView fred("FRED");
1295 EXPECT_EQ("FRED", fred.TrimmedRight('E'));
1296 EXPECT_EQ("FRE", fred.TrimmedRight('D'));
1297 ByteStringView fredd("FREDD");
1298 EXPECT_EQ("FRE", fred.TrimmedRight('D'));
1299 }
1300
TEST(ByteStringView,ElementAccess)1301 TEST(ByteStringView, ElementAccess) {
1302 // ByteStringView includes the NUL terminator for non-empty strings.
1303 ByteStringView abc("abc");
1304 EXPECT_EQ('a', static_cast<char>(abc[0]));
1305 EXPECT_EQ('b', static_cast<char>(abc[1]));
1306 EXPECT_EQ('c', static_cast<char>(abc[2]));
1307 #ifndef NDEBUG
1308 EXPECT_DEATH({ abc[3]; }, ".*");
1309 #endif
1310 }
1311
TEST(ByteStringView,OperatorLT)1312 TEST(ByteStringView, OperatorLT) {
1313 ByteStringView empty;
1314 ByteStringView a("a");
1315 ByteStringView abc("abc");
1316 ByteStringView def("def");
1317 const char* const c_null = nullptr;
1318 const char* const c_empty = "";
1319 const char* const c_a = "a";
1320 const char* const c_abc = "abc";
1321 const char* const c_def = "def";
1322
1323 EXPECT_FALSE(empty < empty);
1324 EXPECT_FALSE(a < a);
1325 EXPECT_FALSE(abc < abc);
1326 EXPECT_FALSE(def < def);
1327 EXPECT_FALSE(c_null < empty);
1328 EXPECT_FALSE(c_empty < empty);
1329 EXPECT_FALSE(c_a < a);
1330 EXPECT_FALSE(c_abc < abc);
1331 EXPECT_FALSE(c_def < def);
1332 EXPECT_FALSE(empty < c_null);
1333 EXPECT_FALSE(empty < c_empty);
1334 EXPECT_FALSE(a < c_a);
1335 EXPECT_FALSE(abc < c_abc);
1336 EXPECT_FALSE(def < c_def);
1337
1338 EXPECT_TRUE(empty < a);
1339 EXPECT_FALSE(a < empty);
1340 EXPECT_TRUE(empty < c_a);
1341 EXPECT_FALSE(a < c_null);
1342 EXPECT_FALSE(a < c_empty);
1343
1344 EXPECT_TRUE(empty < abc);
1345 EXPECT_FALSE(abc < empty);
1346 EXPECT_TRUE(empty < c_abc);
1347 EXPECT_FALSE(abc < c_null);
1348 EXPECT_FALSE(abc < c_empty);
1349
1350 EXPECT_TRUE(empty < def);
1351 EXPECT_FALSE(def < empty);
1352 EXPECT_TRUE(empty < c_def);
1353 EXPECT_FALSE(def < c_null);
1354 EXPECT_FALSE(def < c_empty);
1355
1356 EXPECT_TRUE(a < abc);
1357 EXPECT_FALSE(abc < a);
1358 EXPECT_TRUE(a < c_abc);
1359 EXPECT_FALSE(abc < c_a);
1360
1361 EXPECT_TRUE(a < def);
1362 EXPECT_FALSE(def < a);
1363 EXPECT_TRUE(a < c_def);
1364 EXPECT_FALSE(def < c_a);
1365
1366 EXPECT_TRUE(abc < def);
1367 EXPECT_FALSE(def < abc);
1368 EXPECT_TRUE(abc < c_def);
1369 EXPECT_FALSE(def < c_abc);
1370 }
1371
TEST(ByteStringView,OperatorEQ)1372 TEST(ByteStringView, OperatorEQ) {
1373 ByteStringView byte_string_c("hello");
1374 EXPECT_TRUE(byte_string_c == byte_string_c);
1375
1376 ByteStringView byte_string_c_same1("hello");
1377 EXPECT_TRUE(byte_string_c == byte_string_c_same1);
1378 EXPECT_TRUE(byte_string_c_same1 == byte_string_c);
1379
1380 ByteStringView byte_string_c_same2(byte_string_c);
1381 EXPECT_TRUE(byte_string_c == byte_string_c_same2);
1382 EXPECT_TRUE(byte_string_c_same2 == byte_string_c);
1383
1384 ByteStringView byte_string_c1("he");
1385 ByteStringView byte_string_c2("hellp");
1386 ByteStringView byte_string_c3("hellod");
1387 EXPECT_FALSE(byte_string_c == byte_string_c1);
1388 EXPECT_FALSE(byte_string_c == byte_string_c2);
1389 EXPECT_FALSE(byte_string_c == byte_string_c3);
1390 EXPECT_FALSE(byte_string_c1 == byte_string_c);
1391 EXPECT_FALSE(byte_string_c2 == byte_string_c);
1392 EXPECT_FALSE(byte_string_c3 == byte_string_c);
1393
1394 ByteString byte_string_same1("hello");
1395 EXPECT_TRUE(byte_string_c == byte_string_same1);
1396 EXPECT_TRUE(byte_string_same1 == byte_string_c);
1397
1398 ByteString byte_string1("he");
1399 ByteString byte_string2("hellp");
1400 ByteString byte_string3("hellod");
1401 EXPECT_FALSE(byte_string_c == byte_string1);
1402 EXPECT_FALSE(byte_string_c == byte_string2);
1403 EXPECT_FALSE(byte_string_c == byte_string3);
1404 EXPECT_FALSE(byte_string1 == byte_string_c);
1405 EXPECT_FALSE(byte_string2 == byte_string_c);
1406 EXPECT_FALSE(byte_string3 == byte_string_c);
1407
1408 const char* const c_string_same1 = "hello";
1409 EXPECT_TRUE(byte_string_c == c_string_same1);
1410 EXPECT_TRUE(c_string_same1 == byte_string_c);
1411
1412 const char* const c_string1 = "he";
1413 const char* const c_string2 = "hellp";
1414 const char* const c_string3 = "hellod";
1415 EXPECT_FALSE(byte_string_c == c_string1);
1416 EXPECT_FALSE(byte_string_c == c_string2);
1417 EXPECT_FALSE(byte_string_c == c_string3);
1418
1419 EXPECT_FALSE(c_string1 == byte_string_c);
1420 EXPECT_FALSE(c_string2 == byte_string_c);
1421 EXPECT_FALSE(c_string3 == byte_string_c);
1422
1423 pdfium::span<const uint8_t> span5(
1424 pdfium::as_bytes(pdfium::make_span("hello", 5)));
1425 EXPECT_EQ(byte_string_c.raw_span(), span5);
1426 }
1427
TEST(ByteStringView,OperatorNE)1428 TEST(ByteStringView, OperatorNE) {
1429 ByteStringView byte_string_c("hello");
1430 EXPECT_FALSE(byte_string_c != byte_string_c);
1431
1432 ByteStringView byte_string_c_same1("hello");
1433 EXPECT_FALSE(byte_string_c != byte_string_c_same1);
1434 EXPECT_FALSE(byte_string_c_same1 != byte_string_c);
1435
1436 ByteStringView byte_string_c_same2(byte_string_c);
1437 EXPECT_FALSE(byte_string_c != byte_string_c_same2);
1438 EXPECT_FALSE(byte_string_c_same2 != byte_string_c);
1439
1440 ByteStringView byte_string_c1("he");
1441 ByteStringView byte_string_c2("hellp");
1442 ByteStringView byte_string_c3("hellod");
1443 EXPECT_TRUE(byte_string_c != byte_string_c1);
1444 EXPECT_TRUE(byte_string_c != byte_string_c2);
1445 EXPECT_TRUE(byte_string_c != byte_string_c3);
1446 EXPECT_TRUE(byte_string_c1 != byte_string_c);
1447 EXPECT_TRUE(byte_string_c2 != byte_string_c);
1448 EXPECT_TRUE(byte_string_c3 != byte_string_c);
1449
1450 ByteString byte_string_same1("hello");
1451 EXPECT_FALSE(byte_string_c != byte_string_same1);
1452 EXPECT_FALSE(byte_string_same1 != byte_string_c);
1453
1454 ByteString byte_string1("he");
1455 ByteString byte_string2("hellp");
1456 ByteString byte_string3("hellod");
1457 EXPECT_TRUE(byte_string_c != byte_string1);
1458 EXPECT_TRUE(byte_string_c != byte_string2);
1459 EXPECT_TRUE(byte_string_c != byte_string3);
1460 EXPECT_TRUE(byte_string1 != byte_string_c);
1461 EXPECT_TRUE(byte_string2 != byte_string_c);
1462 EXPECT_TRUE(byte_string3 != byte_string_c);
1463
1464 const char* const c_string_same1 = "hello";
1465 EXPECT_FALSE(byte_string_c != c_string_same1);
1466 EXPECT_FALSE(c_string_same1 != byte_string_c);
1467
1468 const char* const c_string1 = "he";
1469 const char* const c_string2 = "hellp";
1470 const char* const c_string3 = "hellod";
1471 EXPECT_TRUE(byte_string_c != c_string1);
1472 EXPECT_TRUE(byte_string_c != c_string2);
1473 EXPECT_TRUE(byte_string_c != c_string3);
1474
1475 EXPECT_TRUE(c_string1 != byte_string_c);
1476 EXPECT_TRUE(c_string2 != byte_string_c);
1477 EXPECT_TRUE(c_string3 != byte_string_c);
1478 }
1479
TEST(ByteStringView,NullIterator)1480 TEST(ByteStringView, NullIterator) {
1481 ByteStringView null_str;
1482 int32_t sum = 0;
1483 bool any_present = false;
1484 for (const auto& c : null_str) {
1485 sum += c; // Avoid unused arg warnings.
1486 any_present = true;
1487 }
1488 EXPECT_FALSE(any_present);
1489 EXPECT_EQ(0, sum);
1490 }
1491
TEST(ByteStringView,EmptyIterator)1492 TEST(ByteStringView, EmptyIterator) {
1493 ByteStringView empty_str("");
1494 int32_t sum = 0;
1495 bool any_present = false;
1496 for (const auto& c : empty_str) {
1497 any_present = true;
1498 sum += c; // Avoid unused arg warnings.
1499 }
1500 EXPECT_FALSE(any_present);
1501 EXPECT_EQ(0, sum);
1502 }
1503
TEST(ByteStringView,OneCharIterator)1504 TEST(ByteStringView, OneCharIterator) {
1505 ByteStringView one_str("a");
1506 int32_t sum = 0;
1507 bool any_present = false;
1508 for (const auto& c : one_str) {
1509 any_present = true;
1510 sum += c; // Avoid unused arg warnings.
1511 }
1512 EXPECT_TRUE(any_present);
1513 EXPECT_EQ('a', sum);
1514 }
1515
TEST(ByteStringView,MultiCharIterator)1516 TEST(ByteStringView, MultiCharIterator) {
1517 ByteStringView one_str("abc");
1518 int32_t sum = 0;
1519 bool any_present = false;
1520 for (const auto& c : one_str) {
1521 any_present = true;
1522 sum += c; // Avoid unused arg warnings.
1523 }
1524 EXPECT_TRUE(any_present);
1525 EXPECT_EQ('a' + 'b' + 'c', sum);
1526 }
1527
TEST(ByteStringView,EmptyReverseIterator)1528 TEST(ByteStringView, EmptyReverseIterator) {
1529 ByteStringView empty;
1530 auto iter = empty.rbegin();
1531 EXPECT_TRUE(iter == empty.rend());
1532 EXPECT_FALSE(iter != empty.rend());
1533 EXPECT_FALSE(iter < empty.rend());
1534 }
1535
TEST(ByteStringView,OneCharReverseIterator)1536 TEST(ByteStringView, OneCharReverseIterator) {
1537 ByteStringView one_str("a");
1538 auto iter = one_str.rbegin();
1539 EXPECT_FALSE(iter == one_str.rend());
1540 EXPECT_TRUE(iter != one_str.rend());
1541 EXPECT_TRUE(iter < one_str.rend());
1542
1543 char ch = *iter++;
1544 EXPECT_EQ('a', ch);
1545 EXPECT_TRUE(iter == one_str.rend());
1546 EXPECT_FALSE(iter != one_str.rend());
1547 EXPECT_FALSE(iter < one_str.rend());
1548 }
1549
TEST(ByteStringView,MultiCharReverseIterator)1550 TEST(ByteStringView, MultiCharReverseIterator) {
1551 ByteStringView multi_str("abcd");
1552 auto iter = multi_str.rbegin();
1553 EXPECT_FALSE(iter == multi_str.rend());
1554
1555 char ch = *iter++;
1556 EXPECT_EQ('d', ch);
1557 EXPECT_EQ('c', *iter);
1558 EXPECT_FALSE(iter == multi_str.rend());
1559
1560 ch = *(++iter);
1561 EXPECT_EQ('b', ch);
1562 EXPECT_EQ('b', *iter);
1563 EXPECT_FALSE(iter == multi_str.rend());
1564
1565 ch = *iter++;
1566 EXPECT_EQ('b', ch);
1567 EXPECT_EQ('a', *iter);
1568 EXPECT_FALSE(iter == multi_str.rend());
1569
1570 ch = *iter++;
1571 EXPECT_EQ('a', ch);
1572 EXPECT_TRUE(iter == multi_str.rend());
1573
1574 ch = *(--iter);
1575 EXPECT_EQ('a', ch);
1576 EXPECT_EQ('a', *iter);
1577 EXPECT_FALSE(iter == multi_str.rend());
1578
1579 ch = *iter--;
1580 EXPECT_EQ('a', ch);
1581 EXPECT_EQ('b', *iter);
1582 EXPECT_FALSE(iter == multi_str.rend());
1583
1584 ch = *iter--;
1585 EXPECT_EQ('b', ch);
1586 EXPECT_EQ('c', *iter);
1587 EXPECT_FALSE(iter == multi_str.rend());
1588
1589 ch = *(--iter);
1590 EXPECT_EQ('d', ch);
1591 EXPECT_EQ('d', *iter);
1592 EXPECT_TRUE(iter == multi_str.rbegin());
1593 }
1594
TEST(ByteStringView,AnyAllNoneOf)1595 TEST(ByteStringView, AnyAllNoneOf) {
1596 ByteStringView str("aaaaaaaaaaaaaaaaab");
1597 EXPECT_FALSE(std::all_of(str.begin(), str.end(),
1598 [](const char& c) { return c == 'a'; }));
1599
1600 EXPECT_FALSE(std::none_of(str.begin(), str.end(),
1601 [](const char& c) { return c == 'a'; }));
1602
1603 EXPECT_TRUE(std::any_of(str.begin(), str.end(),
1604 [](const char& c) { return c == 'a'; }));
1605
1606 EXPECT_TRUE(pdfium::ContainsValue(str, 'a'));
1607 EXPECT_TRUE(pdfium::ContainsValue(str, 'b'));
1608 EXPECT_FALSE(pdfium::ContainsValue(str, 'z'));
1609 }
1610
TEST(ByteString,FormatWidth)1611 TEST(ByteString, FormatWidth) {
1612 EXPECT_EQ(" 1", ByteString::Format("%5d", 1));
1613 EXPECT_EQ("1", ByteString::Format("%d", 1));
1614 EXPECT_EQ(" 1", ByteString::Format("%*d", 5, 1));
1615 EXPECT_EQ("1", ByteString::Format("%-1d", 1));
1616 EXPECT_EQ("1", ByteString::Format("%0d", 1));
1617 }
1618
TEST(ByteString,FormatPrecision)1619 TEST(ByteString, FormatPrecision) {
1620 EXPECT_EQ("1.12", ByteString::Format("%.2f", 1.12345));
1621 EXPECT_EQ("1.123", ByteString::Format("%.*f", 3, 1.12345));
1622 EXPECT_EQ("1.123450", ByteString::Format("%f", 1.12345));
1623 EXPECT_EQ("1.123450", ByteString::Format("%-1f", 1.12345));
1624 EXPECT_EQ("1.123450", ByteString::Format("%0f", 1.12345));
1625 }
1626
TEST(ByteString,Empty)1627 TEST(ByteString, Empty) {
1628 ByteString empty_str;
1629 EXPECT_TRUE(empty_str.IsEmpty());
1630 EXPECT_EQ(0u, empty_str.GetLength());
1631
1632 const char* cstr = empty_str.c_str();
1633 EXPECT_NE(nullptr, cstr);
1634 EXPECT_EQ(0u, strlen(cstr));
1635
1636 const uint8_t* rstr = empty_str.raw_str();
1637 EXPECT_EQ(nullptr, rstr);
1638
1639 pdfium::span<const char> cspan = empty_str.span();
1640 EXPECT_TRUE(cspan.empty());
1641 EXPECT_EQ(nullptr, cspan.data());
1642
1643 pdfium::span<const uint8_t> rspan = empty_str.raw_span();
1644 EXPECT_TRUE(rspan.empty());
1645 EXPECT_EQ(nullptr, rspan.data());
1646 }
1647
TEST(ByteString,InitializerList)1648 TEST(ByteString, InitializerList) {
1649 ByteString many_str({"clams", " and ", "oysters"});
1650 EXPECT_EQ("clams and oysters", many_str);
1651 many_str = {"fish", " and ", "chips", " and ", "soda"};
1652 EXPECT_EQ("fish and chips and soda", many_str);
1653 }
1654
TEST(ByteString,NullIterator)1655 TEST(ByteString, NullIterator) {
1656 ByteString null_str;
1657 int32_t sum = 0;
1658 bool any_present = false;
1659 for (const auto& c : null_str) {
1660 sum += c; // Avoid unused arg warnings.
1661 any_present = true;
1662 }
1663 EXPECT_FALSE(any_present);
1664 EXPECT_EQ(0, sum);
1665 }
1666
TEST(ByteString,EmptyIterator)1667 TEST(ByteString, EmptyIterator) {
1668 ByteString empty_str("");
1669 int32_t sum = 0;
1670 bool any_present = false;
1671 for (const auto& c : empty_str) {
1672 any_present = true;
1673 sum += c; // Avoid unused arg warnings.
1674 }
1675 EXPECT_FALSE(any_present);
1676 EXPECT_EQ(0, sum);
1677 }
1678
TEST(ByteString,OneCharIterator)1679 TEST(ByteString, OneCharIterator) {
1680 ByteString one_str("a");
1681 int32_t sum = 0;
1682 bool any_present = false;
1683 for (const auto& c : one_str) {
1684 any_present = true;
1685 sum += c; // Avoid unused arg warnings.
1686 }
1687 EXPECT_TRUE(any_present);
1688 EXPECT_EQ('a', sum);
1689 }
1690
TEST(ByteString,MultiCharIterator)1691 TEST(ByteString, MultiCharIterator) {
1692 ByteString one_str("abc");
1693 int32_t sum = 0;
1694 bool any_present = false;
1695 for (const auto& c : one_str) {
1696 any_present = true;
1697 sum += c; // Avoid unused arg warnings.
1698 }
1699 EXPECT_TRUE(any_present);
1700 EXPECT_EQ('a' + 'b' + 'c', sum);
1701 }
1702
TEST(ByteString,StdBegin)1703 TEST(ByteString, StdBegin) {
1704 ByteString one_str("abc");
1705 std::vector<uint8_t> vec(std::begin(one_str), std::end(one_str));
1706 ASSERT_EQ(3u, vec.size());
1707 EXPECT_EQ('a', vec[0]);
1708 EXPECT_EQ('b', vec[1]);
1709 EXPECT_EQ('c', vec[2]);
1710 }
1711
TEST(ByteString,AnyAllNoneOf)1712 TEST(ByteString, AnyAllNoneOf) {
1713 ByteString str("aaaaaaaaaaaaaaaaab");
1714 EXPECT_FALSE(std::all_of(str.begin(), str.end(),
1715 [](const char& c) { return c == 'a'; }));
1716
1717 EXPECT_FALSE(std::none_of(str.begin(), str.end(),
1718 [](const char& c) { return c == 'a'; }));
1719
1720 EXPECT_TRUE(std::any_of(str.begin(), str.end(),
1721 [](const char& c) { return c == 'a'; }));
1722
1723 EXPECT_TRUE(pdfium::ContainsValue(str, 'a'));
1724 EXPECT_TRUE(pdfium::ContainsValue(str, 'b'));
1725 EXPECT_FALSE(pdfium::ContainsValue(str, 'z'));
1726 }
1727
TEST(CFX_BytrString,EqualNoCase)1728 TEST(CFX_BytrString, EqualNoCase) {
1729 ByteString str("aaa");
1730 EXPECT_TRUE(str.EqualNoCase("aaa"));
1731 EXPECT_TRUE(str.EqualNoCase("AAA"));
1732 EXPECT_TRUE(str.EqualNoCase("aaA"));
1733 EXPECT_TRUE(str.EqualNoCase("Aaa"));
1734 EXPECT_FALSE(str.EqualNoCase("aab"));
1735 EXPECT_FALSE(str.EqualNoCase("aaaa"));
1736 EXPECT_FALSE(str.EqualNoCase("BBBB"));
1737 EXPECT_FALSE(str.EqualNoCase("a"));
1738 EXPECT_FALSE(str.EqualNoCase(""));
1739 }
1740
TEST(ByteString,OStreamOverload)1741 TEST(ByteString, OStreamOverload) {
1742 std::ostringstream stream;
1743
1744 // Basic case, empty string
1745 ByteString str;
1746 stream << str;
1747 EXPECT_EQ("", stream.str());
1748
1749 // Basic case, non-empty string
1750 str = "def";
1751 stream.str("");
1752 stream << "abc" << str << "ghi";
1753 EXPECT_EQ("abcdefghi", stream.str());
1754
1755 // Changing the ByteString does not change the stream it was written to.
1756 str = "123";
1757 EXPECT_EQ("abcdefghi", stream.str());
1758
1759 // Writing it again to the stream will use the latest value.
1760 stream.str("");
1761 stream << "abc" << str << "ghi";
1762 EXPECT_EQ("abc123ghi", stream.str());
1763
1764 char stringWithNulls[]{'x', 'y', '\0', 'z'};
1765
1766 // Writing a ByteString with nulls and no specified length treats it as
1767 // a C-style null-terminated string.
1768 str = ByteString(stringWithNulls);
1769 EXPECT_EQ(2u, str.GetLength());
1770 stream.str("");
1771 stream << str;
1772 EXPECT_EQ(2u, stream.tellp());
1773
1774 // Writing a ByteString with nulls but specifying its length treats it as
1775 // a C++-style string.
1776 str = ByteString(stringWithNulls, 4);
1777 EXPECT_EQ(4u, str.GetLength());
1778 stream.str("");
1779 stream << str;
1780 EXPECT_EQ(4u, stream.tellp());
1781
1782 // << operators can be chained.
1783 ByteString str1("abc");
1784 ByteString str2("def");
1785 stream.str("");
1786 stream << str1 << str2;
1787 EXPECT_EQ("abcdef", stream.str());
1788 }
1789
TEST(ByteStringView,OStreamOverload)1790 TEST(ByteStringView, OStreamOverload) {
1791 // Basic case, empty string
1792 {
1793 std::ostringstream stream;
1794 ByteStringView str;
1795 stream << str;
1796 EXPECT_EQ("", stream.str());
1797 }
1798
1799 // Basic case, non-empty string
1800 {
1801 std::ostringstream stream;
1802 ByteStringView str("def");
1803 stream << "abc" << str << "ghi";
1804 EXPECT_EQ("abcdefghi", stream.str());
1805 }
1806
1807 // Changing the ByteStringView does not change the stream it was written to.
1808 {
1809 std::ostringstream stream;
1810 ByteStringView str("abc");
1811 stream << str;
1812 str = "123";
1813 EXPECT_EQ("abc", stream.str());
1814 }
1815
1816 // Writing it again to the stream will use the latest value.
1817 {
1818 std::ostringstream stream;
1819 ByteStringView str("abc");
1820 stream << str;
1821 stream.str("");
1822 str = "123";
1823 stream << str;
1824 EXPECT_EQ("123", stream.str());
1825 }
1826
1827 // Writing a ByteStringView with nulls and no specified length treats it as
1828 // a C-style null-terminated string.
1829 {
1830 std::ostringstream stream;
1831 char stringWithNulls[]{'x', 'y', '\0', 'z'};
1832 ByteStringView str(stringWithNulls);
1833 EXPECT_EQ(2u, str.GetLength());
1834 stream << str;
1835 EXPECT_EQ(2u, stream.tellp());
1836 str = "";
1837 }
1838
1839 // Writing a ByteStringView with nulls but specifying its length treats it as
1840 // a C++-style string.
1841 {
1842 std::ostringstream stream;
1843 char stringWithNulls[]{'x', 'y', '\0', 'z'};
1844 ByteStringView str(stringWithNulls, 4);
1845 EXPECT_EQ(4u, str.GetLength());
1846 stream << str;
1847 EXPECT_EQ(4u, stream.tellp());
1848 str = "";
1849 }
1850
1851 // << operators can be chained.
1852 {
1853 std::ostringstream stream;
1854 ByteStringView str1("abc");
1855 ByteStringView str2("def");
1856 stream << str1 << str2;
1857 EXPECT_EQ("abcdef", stream.str());
1858 }
1859 }
1860
TEST(ByteString,FormatInteger)1861 TEST(ByteString, FormatInteger) {
1862 // Base case of 0.
1863 EXPECT_EQ("0", ByteString::FormatInteger(0));
1864
1865 // Positive ordinary number.
1866 EXPECT_EQ("123456", ByteString::FormatInteger(123456));
1867
1868 // Negative ordinary number.
1869 EXPECT_EQ("-123456", ByteString::FormatInteger(-123456));
1870
1871 // int limits.
1872 EXPECT_EQ("2147483647", ByteString::FormatInteger(INT_MAX));
1873 EXPECT_EQ("-2147483648", ByteString::FormatInteger(INT_MIN));
1874 }
1875
TEST(ByteString,FX_HashCode_Ascii)1876 TEST(ByteString, FX_HashCode_Ascii) {
1877 EXPECT_EQ(0u, FX_HashCode_GetA("", false));
1878 EXPECT_EQ(65u, FX_HashCode_GetA("A", false));
1879 EXPECT_EQ(97u, FX_HashCode_GetA("A", true));
1880 EXPECT_EQ(31 * 65u + 66u, FX_HashCode_GetA("AB", false));
1881 EXPECT_EQ(31u * 65u + 255u, FX_HashCode_GetA("A\xff", false));
1882 EXPECT_EQ(31u * 97u + 255u, FX_HashCode_GetA("A\xff", true));
1883 }
1884
TEST(ByteString,FX_HashCode_Wide)1885 TEST(ByteString, FX_HashCode_Wide) {
1886 EXPECT_EQ(0u, FX_HashCode_GetAsIfW("", false));
1887 EXPECT_EQ(65u, FX_HashCode_GetAsIfW("A", false));
1888 EXPECT_EQ(97u, FX_HashCode_GetAsIfW("A", true));
1889 EXPECT_EQ(1313u * 65u + 66u, FX_HashCode_GetAsIfW("AB", false));
1890 EXPECT_EQ(1313u * 65u + 255u, FX_HashCode_GetAsIfW("A\xff", false));
1891 EXPECT_EQ(1313u * 97u + 255u, FX_HashCode_GetAsIfW("A\xff", true));
1892 }
1893
1894 } // namespace fxcrt
1895