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