Move and refactor code
[folly.git] / folly / test / StringTest.cpp
1 /*
2  * Copyright 2012 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/String.h"
18
19 #include <random>
20 #include <boost/algorithm/string.hpp>
21 #include <gtest/gtest.h>
22
23 #include "folly/Benchmark.h"
24
25 using namespace folly;
26 using namespace std;
27
28 TEST(StringPrintf, BasicTest) {
29   EXPECT_EQ("abc", stringPrintf("%s", "abc"));
30   EXPECT_EQ("abc", stringPrintf("%sbc", "a"));
31   EXPECT_EQ("abc", stringPrintf("a%sc", "b"));
32   EXPECT_EQ("abc", stringPrintf("ab%s", "c"));
33
34   EXPECT_EQ("abc", stringPrintf("abc"));
35 }
36
37 TEST(StringPrintf, NumericFormats) {
38   EXPECT_EQ("12", stringPrintf("%d", 12));
39   EXPECT_EQ("5000000000", stringPrintf("%ld", 5000000000UL));
40   EXPECT_EQ("5000000000", stringPrintf("%ld", 5000000000L));
41   EXPECT_EQ("-5000000000", stringPrintf("%ld", -5000000000L));
42   EXPECT_EQ("-1", stringPrintf("%d", 0xffffffff));
43   EXPECT_EQ("-1", stringPrintf("%ld", 0xffffffffffffffff));
44   EXPECT_EQ("-1", stringPrintf("%ld", 0xffffffffffffffffUL));
45
46   EXPECT_EQ("7.7", stringPrintf("%1.1f", 7.7));
47   EXPECT_EQ("7.7", stringPrintf("%1.1lf", 7.7));
48   EXPECT_EQ("7.70000000000000018",
49             stringPrintf("%.17f", 7.7));
50   EXPECT_EQ("7.70000000000000018",
51             stringPrintf("%.17lf", 7.7));
52 }
53
54 TEST(StringPrintf, Appending) {
55   string s;
56   stringAppendf(&s, "a%s", "b");
57   stringAppendf(&s, "%c", 'c');
58   EXPECT_EQ(s, "abc");
59   stringAppendf(&s, " %d", 123);
60   EXPECT_EQ(s, "abc 123");
61 }
62
63 TEST(StringPrintf, VariousSizes) {
64   // Test a wide variety of output sizes
65   for (int i = 0; i < 100; ++i) {
66     string expected(i + 1, 'a');
67     EXPECT_EQ("X" + expected + "X", stringPrintf("X%sX", expected.c_str()));
68   }
69
70   EXPECT_EQ("abc12345678910111213141516171819202122232425xyz",
71             stringPrintf("abc%d%d%d%d%d%d%d%d%d%d%d%d%d%d"
72                          "%d%d%d%d%d%d%d%d%d%d%dxyz",
73                          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
74                          17, 18, 19, 20, 21, 22, 23, 24, 25));
75 }
76
77 TEST(StringPrintf, oldStringPrintfTests) {
78   EXPECT_EQ(string("a/b/c/d"),
79             stringPrintf("%s/%s/%s/%s", "a", "b", "c", "d"));
80
81   EXPECT_EQ(string("    5    10"),
82             stringPrintf("%5d %5d", 5, 10));
83
84   // check printing w/ a big buffer
85   for (int size = (1 << 8); size <= (1 << 15); size <<= 1) {
86     string a(size, 'z');
87     string b = stringPrintf("%s", a.c_str());
88     EXPECT_EQ(a.size(), b.size());
89   }
90 }
91
92 TEST(StringPrintf, oldStringAppendf) {
93   string s = "hello";
94   stringAppendf(&s, "%s/%s/%s/%s", "a", "b", "c", "d");
95   EXPECT_EQ(string("helloa/b/c/d"), s);
96 }
97
98 BENCHMARK(new_stringPrintfSmall, iters) {
99   for (int64_t i = 0; i < iters; ++i) {
100     int32_t x = int32_t(i);
101     int32_t y = int32_t(i + 1);
102     string s =
103       stringPrintf("msg msg msg msg msg msg msg msg:  %d, %d, %s",
104                    x, y, "hello");
105   }
106 }
107
108 TEST(Escape, cEscape) {
109   EXPECT_EQ("hello world", cEscape<std::string>("hello world"));
110   EXPECT_EQ("hello \\\\world\\\" goodbye",
111             cEscape<std::string>("hello \\world\" goodbye"));
112   EXPECT_EQ("hello\\nworld", cEscape<std::string>("hello\nworld"));
113   EXPECT_EQ("hello\\377\\376", cEscape<std::string>("hello\xff\xfe"));
114 }
115
116 TEST(Escape, cUnescape) {
117   EXPECT_EQ("hello world", cUnescape<std::string>("hello world"));
118   EXPECT_EQ("hello \\world\" goodbye",
119             cUnescape<std::string>("hello \\\\world\\\" goodbye"));
120   EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\nworld"));
121   EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\012world"));
122   EXPECT_EQ("hello\nworld", cUnescape<std::string>("hello\\x0aworld"));
123   EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\377\\376"));
124   EXPECT_EQ("hello\xff\xfe", cUnescape<std::string>("hello\\xff\\xfe"));
125
126   EXPECT_THROW({cUnescape<std::string>("hello\\");},
127                std::invalid_argument);
128   EXPECT_THROW({cUnescape<std::string>("hello\\x");},
129                std::invalid_argument);
130   EXPECT_THROW({cUnescape<std::string>("hello\\q");},
131                std::invalid_argument);
132 }
133
134 namespace {
135 fbstring bmString;
136 fbstring bmEscapedString;
137 fbstring escapedString;
138 fbstring unescapedString;
139 const size_t kBmStringLength = 64 << 10;
140 const uint32_t kPrintablePercentage = 90;
141
142 void initBenchmark() {
143   bmString.reserve(kBmStringLength);
144
145   std::mt19937 rnd;
146   std::uniform_int_distribution<uint32_t> printable(32, 126);
147   std::uniform_int_distribution<uint32_t> nonPrintable(0, 160);
148   std::uniform_int_distribution<uint32_t> percentage(0, 99);
149
150   for (size_t i = 0; i < kBmStringLength; ++i) {
151     unsigned char c;
152     if (percentage(rnd) < kPrintablePercentage) {
153       c = printable(rnd);
154     } else {
155       c = nonPrintable(rnd);
156       // Generate characters in both non-printable ranges:
157       // 0..31 and 127..255
158       if (c >= 32) {
159         c += (126 - 32) + 1;
160       }
161     }
162     bmString.push_back(c);
163   }
164
165   bmEscapedString = cEscape<fbstring>(bmString);
166 }
167
168 BENCHMARK(BM_cEscape, iters) {
169   while (iters--) {
170     escapedString = cEscape<fbstring>(bmString);
171     doNotOptimizeAway(escapedString.size());
172   }
173 }
174
175 BENCHMARK(BM_cUnescape, iters) {
176   while (iters--) {
177     unescapedString = cUnescape<fbstring>(bmEscapedString);
178     doNotOptimizeAway(unescapedString.size());
179   }
180 }
181
182 }  // namespace
183
184 namespace {
185
186 double pow2(int exponent) {
187   return double(int64_t(1) << exponent);
188 }
189
190 }  // namespace
191
192 TEST(PrettyPrint, Basic) {
193   // check time printing
194   EXPECT_EQ(string("8.53e+07 s "), prettyPrint(85.3e6, PRETTY_TIME));
195   EXPECT_EQ(string("85.3 s "), prettyPrint(85.3, PRETTY_TIME));
196   EXPECT_EQ(string("85.3 ms"), prettyPrint(85.3e-3, PRETTY_TIME));
197   EXPECT_EQ(string("85.3 us"), prettyPrint(85.3e-6, PRETTY_TIME));
198   EXPECT_EQ(string("85.3 ns"), prettyPrint(85.3e-9, PRETTY_TIME));
199   EXPECT_EQ(string("85.3 ps"), prettyPrint(85.3e-12, PRETTY_TIME));
200   EXPECT_EQ(string("8.53e-14 s "), prettyPrint(85.3e-15, PRETTY_TIME));
201
202   EXPECT_EQ(string("0 s "), prettyPrint(0, PRETTY_TIME));
203   EXPECT_EQ(string("1 s "), prettyPrint(1.0, PRETTY_TIME));
204   EXPECT_EQ(string("1 ms"), prettyPrint(1.0e-3, PRETTY_TIME));
205   EXPECT_EQ(string("1 us"), prettyPrint(1.0e-6, PRETTY_TIME));
206   EXPECT_EQ(string("1 ns"), prettyPrint(1.0e-9, PRETTY_TIME));
207   EXPECT_EQ(string("1 ps"), prettyPrint(1.0e-12, PRETTY_TIME));
208
209   // check bytes printing
210   EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES));
211   EXPECT_EQ(string("833 kB"), prettyPrint(853.e3, PRETTY_BYTES));
212   EXPECT_EQ(string("813.5 MB"), prettyPrint(853.e6, PRETTY_BYTES));
213   EXPECT_EQ(string("7.944 GB"), prettyPrint(8.53e9, PRETTY_BYTES));
214   EXPECT_EQ(string("794.4 GB"), prettyPrint(853.e9, PRETTY_BYTES));
215   EXPECT_EQ(string("775.8 TB"), prettyPrint(853.e12, PRETTY_BYTES));
216
217   EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES));
218   EXPECT_EQ(string("1 B "), prettyPrint(pow2(0), PRETTY_BYTES));
219   EXPECT_EQ(string("1 kB"), prettyPrint(pow2(10), PRETTY_BYTES));
220   EXPECT_EQ(string("1 MB"), prettyPrint(pow2(20), PRETTY_BYTES));
221   EXPECT_EQ(string("1 GB"), prettyPrint(pow2(30), PRETTY_BYTES));
222   EXPECT_EQ(string("1 TB"), prettyPrint(pow2(40), PRETTY_BYTES));
223
224   // check bytes metric printing
225   EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_METRIC));
226   EXPECT_EQ(string("853 kB"), prettyPrint(853.e3, PRETTY_BYTES_METRIC));
227   EXPECT_EQ(string("853 MB"), prettyPrint(853.e6, PRETTY_BYTES_METRIC));
228   EXPECT_EQ(string("8.53 GB"), prettyPrint(8.53e9, PRETTY_BYTES_METRIC));
229   EXPECT_EQ(string("853 GB"), prettyPrint(853.e9, PRETTY_BYTES_METRIC));
230   EXPECT_EQ(string("853 TB"), prettyPrint(853.e12, PRETTY_BYTES_METRIC));
231
232   EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES_METRIC));
233   EXPECT_EQ(string("1 B "), prettyPrint(1.0, PRETTY_BYTES_METRIC));
234   EXPECT_EQ(string("1 kB"), prettyPrint(1.0e+3, PRETTY_BYTES_METRIC));
235   EXPECT_EQ(string("1 MB"), prettyPrint(1.0e+6, PRETTY_BYTES_METRIC));
236
237   EXPECT_EQ(string("1 GB"), prettyPrint(1.0e+9, PRETTY_BYTES_METRIC));
238   EXPECT_EQ(string("1 TB"), prettyPrint(1.0e+12, PRETTY_BYTES_METRIC));
239
240   // check metric-units (powers of 1000) printing
241   EXPECT_EQ(string("853  "), prettyPrint(853., PRETTY_UNITS_METRIC));
242   EXPECT_EQ(string("853 k"), prettyPrint(853.e3, PRETTY_UNITS_METRIC));
243   EXPECT_EQ(string("853 M"), prettyPrint(853.e6, PRETTY_UNITS_METRIC));
244   EXPECT_EQ(string("8.53 bil"), prettyPrint(8.53e9, PRETTY_UNITS_METRIC));
245   EXPECT_EQ(string("853 bil"), prettyPrint(853.e9, PRETTY_UNITS_METRIC));
246   EXPECT_EQ(string("853 tril"), prettyPrint(853.e12, PRETTY_UNITS_METRIC));
247
248   // check binary-units (powers of 1024) printing
249   EXPECT_EQ(string("0  "), prettyPrint(0, PRETTY_UNITS_BINARY));
250   EXPECT_EQ(string("1  "), prettyPrint(pow2(0), PRETTY_UNITS_BINARY));
251   EXPECT_EQ(string("1 k"), prettyPrint(pow2(10), PRETTY_UNITS_BINARY));
252   EXPECT_EQ(string("1 M"), prettyPrint(pow2(20), PRETTY_UNITS_BINARY));
253   EXPECT_EQ(string("1 G"), prettyPrint(pow2(30), PRETTY_UNITS_BINARY));
254   EXPECT_EQ(string("1 T"), prettyPrint(pow2(40), PRETTY_UNITS_BINARY));
255
256   EXPECT_EQ(string("1023  "),
257       prettyPrint(pow2(10) - 1, PRETTY_UNITS_BINARY));
258   EXPECT_EQ(string("1024 k"),
259       prettyPrint(pow2(20) - 1, PRETTY_UNITS_BINARY));
260   EXPECT_EQ(string("1024 M"),
261       prettyPrint(pow2(30) - 1, PRETTY_UNITS_BINARY));
262   EXPECT_EQ(string("1024 G"),
263       prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY));
264
265   // check that negative values work
266   EXPECT_EQ(string("-85.3 s "), prettyPrint(-85.3, PRETTY_TIME));
267   EXPECT_EQ(string("-85.3 ms"), prettyPrint(-85.3e-3, PRETTY_TIME));
268   EXPECT_EQ(string("-85.3 us"), prettyPrint(-85.3e-6, PRETTY_TIME));
269   EXPECT_EQ(string("-85.3 ns"), prettyPrint(-85.3e-9, PRETTY_TIME));
270 }
271
272 TEST(PrettyPrint, HexDump) {
273   std::string a("abc\x00\x02\xa0", 6);  // embedded NUL
274   EXPECT_EQ(
275     "00000000  61 62 63 00 02 a0                                 "
276     "|abc...          |\n",
277     hexDump(a.data(), a.size()));
278
279   a = "abcdefghijklmnopqrstuvwxyz";
280   EXPECT_EQ(
281     "00000000  61 62 63 64 65 66 67 68  69 6a 6b 6c 6d 6e 6f 70  "
282     "|abcdefghijklmnop|\n"
283     "00000010  71 72 73 74 75 76 77 78  79 7a                    "
284     "|qrstuvwxyz      |\n",
285     hexDump(a.data(), a.size()));
286 }
287
288 TEST(System, errnoStr) {
289   errno = EACCES;
290   EXPECT_EQ(EACCES, errno);
291   EXPECT_EQ(EACCES, errno);  // twice to make sure EXPECT_EQ doesn't change it
292
293   fbstring expected = strerror(ENOENT);
294
295   errno = EACCES;
296   EXPECT_EQ(expected, errnoStr(ENOENT));
297   // Ensure that errno isn't changed
298   EXPECT_EQ(EACCES, errno);
299
300   // Per POSIX, all errno values are positive, so -1 is invalid
301   errnoStr(-1);
302
303   // Ensure that errno isn't changed
304   EXPECT_EQ(EACCES, errno);
305 }
306
307 namespace folly_test {
308 struct ThisIsAVeryLongStructureName {
309 };
310 }  // namespace folly_test
311
312 TEST(System, demangle) {
313   EXPECT_EQ("folly_test::ThisIsAVeryLongStructureName",
314             demangle(typeid(folly_test::ThisIsAVeryLongStructureName)));
315 }
316
317 namespace {
318
319 template<template<class,class> class VectorType>
320 void splitTest() {
321   VectorType<string,std::allocator<string> > parts;
322
323   folly::split(',', "a,b,c", parts);
324   EXPECT_EQ(parts.size(), 3);
325   EXPECT_EQ(parts[0], "a");
326   EXPECT_EQ(parts[1], "b");
327   EXPECT_EQ(parts[2], "c");
328   parts.clear();
329
330   folly::split(',', string("a,b,c"), parts);
331   EXPECT_EQ(parts.size(), 3);
332   EXPECT_EQ(parts[0], "a");
333   EXPECT_EQ(parts[1], "b");
334   EXPECT_EQ(parts[2], "c");
335   parts.clear();
336
337   folly::split(',', "a,,c", parts);
338   EXPECT_EQ(parts.size(), 3);
339   EXPECT_EQ(parts[0], "a");
340   EXPECT_EQ(parts[1], "");
341   EXPECT_EQ(parts[2], "c");
342   parts.clear();
343
344   folly::split(',', string("a,,c"), parts);
345   EXPECT_EQ(parts.size(), 3);
346   EXPECT_EQ(parts[0], "a");
347   EXPECT_EQ(parts[1], "");
348   EXPECT_EQ(parts[2], "c");
349   parts.clear();
350
351   folly::split(',', "a,,c", parts, true);
352   EXPECT_EQ(parts.size(), 2);
353   EXPECT_EQ(parts[0], "a");
354   EXPECT_EQ(parts[1], "c");
355   parts.clear();
356
357   folly::split(',', string("a,,c"), parts, true);
358   EXPECT_EQ(parts.size(), 2);
359   EXPECT_EQ(parts[0], "a");
360   EXPECT_EQ(parts[1], "c");
361   parts.clear();
362
363   folly::split(',', string(",,a,,c,,,"), parts, true);
364   EXPECT_EQ(parts.size(), 2);
365   EXPECT_EQ(parts[0], "a");
366   EXPECT_EQ(parts[1], "c");
367   parts.clear();
368
369   // test multiple split w/o clear
370   folly::split(',', ",,a,,c,,,", parts, true);
371   EXPECT_EQ(parts.size(), 2);
372   EXPECT_EQ(parts[0], "a");
373   EXPECT_EQ(parts[1], "c");
374   folly::split(',', ",,a,,c,,,", parts, true);
375   EXPECT_EQ(parts.size(), 4);
376   EXPECT_EQ(parts[2], "a");
377   EXPECT_EQ(parts[3], "c");
378   parts.clear();
379
380   // test splits that with multi-line delimiter
381   folly::split("ab", "dabcabkdbkab", parts, true);
382   EXPECT_EQ(parts.size(), 3);
383   EXPECT_EQ(parts[0], "d");
384   EXPECT_EQ(parts[1], "c");
385   EXPECT_EQ(parts[2], "kdbk");
386   parts.clear();
387
388   string orig = "ab2342asdfv~~!";
389   folly::split("", orig, parts, true);
390   EXPECT_EQ(parts.size(), 1);
391   EXPECT_EQ(parts[0], orig);
392   parts.clear();
393
394   folly::split("452x;o38asfsajsdlfdf.j", "asfds", parts, true);
395   EXPECT_EQ(parts.size(), 1);
396   EXPECT_EQ(parts[0], "asfds");
397   parts.clear();
398
399   folly::split("a", "", parts, true);
400   EXPECT_EQ(parts.size(), 0);
401   parts.clear();
402
403   folly::split("a", "", parts);
404   EXPECT_EQ(parts.size(), 1);
405   EXPECT_EQ(parts[0], "");
406   parts.clear();
407
408   folly::split("a", "abcdefg", parts, true);
409   EXPECT_EQ(parts.size(), 1);
410   EXPECT_EQ(parts[0], "bcdefg");
411   parts.clear();
412
413   orig = "All, , your bases, are , , belong to us";
414   folly::split(", ", orig, parts, true);
415   EXPECT_EQ(parts.size(), 4);
416   EXPECT_EQ(parts[0], "All");
417   EXPECT_EQ(parts[1], "your bases");
418   EXPECT_EQ(parts[2], "are ");
419   EXPECT_EQ(parts[3], "belong to us");
420   parts.clear();
421   folly::split(", ", orig, parts);
422   EXPECT_EQ(parts.size(), 6);
423   EXPECT_EQ(parts[0], "All");
424   EXPECT_EQ(parts[1], "");
425   EXPECT_EQ(parts[2], "your bases");
426   EXPECT_EQ(parts[3], "are ");
427   EXPECT_EQ(parts[4], "");
428   EXPECT_EQ(parts[5], "belong to us");
429   parts.clear();
430
431   orig = ", Facebook, rul,es!, ";
432   folly::split(", ", orig, parts, true);
433   EXPECT_EQ(parts.size(), 2);
434   EXPECT_EQ(parts[0], "Facebook");
435   EXPECT_EQ(parts[1], "rul,es!");
436   parts.clear();
437   folly::split(", ", orig, parts);
438   EXPECT_EQ(parts.size(), 4);
439   EXPECT_EQ(parts[0], "");
440   EXPECT_EQ(parts[1], "Facebook");
441   EXPECT_EQ(parts[2], "rul,es!");
442   EXPECT_EQ(parts[3], "");
443 }
444
445 template<template<class,class> class VectorType>
446 void piecesTest() {
447   VectorType<StringPiece,std::allocator<StringPiece> > pieces;
448   VectorType<StringPiece,std::allocator<StringPiece> > pieces2;
449
450   folly::split(',', "a,b,c", pieces);
451   EXPECT_EQ(pieces.size(), 3);
452   EXPECT_EQ(pieces[0], "a");
453   EXPECT_EQ(pieces[1], "b");
454   EXPECT_EQ(pieces[2], "c");
455
456   pieces.clear();
457
458   folly::split(',', "a,,c", pieces);
459   EXPECT_EQ(pieces.size(), 3);
460   EXPECT_EQ(pieces[0], "a");
461   EXPECT_EQ(pieces[1], "");
462   EXPECT_EQ(pieces[2], "c");
463   pieces.clear();
464
465   folly::split(',', "a,,c", pieces, true);
466   EXPECT_EQ(pieces.size(), 2);
467   EXPECT_EQ(pieces[0], "a");
468   EXPECT_EQ(pieces[1], "c");
469   pieces.clear();
470
471   folly::split(',', ",,a,,c,,,", pieces, true);
472   EXPECT_EQ(pieces.size(), 2);
473   EXPECT_EQ(pieces[0], "a");
474   EXPECT_EQ(pieces[1], "c");
475   pieces.clear();
476
477   // test multiple split w/o clear
478   folly::split(',', ",,a,,c,,,", pieces, true);
479   EXPECT_EQ(pieces.size(), 2);
480   EXPECT_EQ(pieces[0], "a");
481   EXPECT_EQ(pieces[1], "c");
482   folly::split(',', ",,a,,c,,,", pieces, true);
483   EXPECT_EQ(pieces.size(), 4);
484   EXPECT_EQ(pieces[2], "a");
485   EXPECT_EQ(pieces[3], "c");
486   pieces.clear();
487
488   // test multiple split rounds
489   folly::split(",", "a_b,c_d", pieces);
490   EXPECT_EQ(pieces.size(), 2);
491   EXPECT_EQ(pieces[0], "a_b");
492   EXPECT_EQ(pieces[1], "c_d");
493   folly::split("_", pieces[0], pieces2);
494   EXPECT_EQ(pieces2.size(), 2);
495   EXPECT_EQ(pieces2[0], "a");
496   EXPECT_EQ(pieces2[1], "b");
497   pieces2.clear();
498   folly::split("_", pieces[1], pieces2);
499   EXPECT_EQ(pieces2.size(), 2);
500   EXPECT_EQ(pieces2[0], "c");
501   EXPECT_EQ(pieces2[1], "d");
502   pieces.clear();
503   pieces2.clear();
504
505   // test splits that with multi-line delimiter
506   folly::split("ab", "dabcabkdbkab", pieces, true);
507   EXPECT_EQ(pieces.size(), 3);
508   EXPECT_EQ(pieces[0], "d");
509   EXPECT_EQ(pieces[1], "c");
510   EXPECT_EQ(pieces[2], "kdbk");
511   pieces.clear();
512
513   string orig = "ab2342asdfv~~!";
514   folly::split("", orig.c_str(), pieces, true);
515   EXPECT_EQ(pieces.size(), 1);
516   EXPECT_EQ(pieces[0], orig);
517   pieces.clear();
518
519   folly::split("452x;o38asfsajsdlfdf.j", "asfds", pieces, true);
520   EXPECT_EQ(pieces.size(), 1);
521   EXPECT_EQ(pieces[0], "asfds");
522   pieces.clear();
523
524   folly::split("a", "", pieces, true);
525   EXPECT_EQ(pieces.size(), 0);
526   pieces.clear();
527
528   folly::split("a", "", pieces);
529   EXPECT_EQ(pieces.size(), 1);
530   EXPECT_EQ(pieces[0], "");
531   pieces.clear();
532
533   folly::split("a", "abcdefg", pieces, true);
534   EXPECT_EQ(pieces.size(), 1);
535   EXPECT_EQ(pieces[0], "bcdefg");
536   pieces.clear();
537
538   orig = "All, , your bases, are , , belong to us";
539   folly::split(", ", orig, pieces, true);
540   EXPECT_EQ(pieces.size(), 4);
541   EXPECT_EQ(pieces[0], "All");
542   EXPECT_EQ(pieces[1], "your bases");
543   EXPECT_EQ(pieces[2], "are ");
544   EXPECT_EQ(pieces[3], "belong to us");
545   pieces.clear();
546   folly::split(", ", orig, pieces);
547   EXPECT_EQ(pieces.size(), 6);
548   EXPECT_EQ(pieces[0], "All");
549   EXPECT_EQ(pieces[1], "");
550   EXPECT_EQ(pieces[2], "your bases");
551   EXPECT_EQ(pieces[3], "are ");
552   EXPECT_EQ(pieces[4], "");
553   EXPECT_EQ(pieces[5], "belong to us");
554   pieces.clear();
555
556   orig = ", Facebook, rul,es!, ";
557   folly::split(", ", orig, pieces, true);
558   EXPECT_EQ(pieces.size(), 2);
559   EXPECT_EQ(pieces[0], "Facebook");
560   EXPECT_EQ(pieces[1], "rul,es!");
561   pieces.clear();
562   folly::split(", ", orig, pieces);
563   EXPECT_EQ(pieces.size(), 4);
564   EXPECT_EQ(pieces[0], "");
565   EXPECT_EQ(pieces[1], "Facebook");
566   EXPECT_EQ(pieces[2], "rul,es!");
567   EXPECT_EQ(pieces[3], "");
568   pieces.clear();
569
570   const char* str = "a,b";
571   folly::split(',', StringPiece(str), pieces);
572   EXPECT_EQ(pieces.size(), 2);
573   EXPECT_EQ(pieces[0], "a");
574   EXPECT_EQ(pieces[1], "b");
575   EXPECT_EQ(pieces[0].start(), str);
576   EXPECT_EQ(pieces[1].start(), str + 2);
577
578   std::set<StringPiece> unique;
579   folly::splitTo<StringPiece>(":", "asd:bsd:asd:asd:bsd:csd::asd",
580     std::inserter(unique, unique.begin()), true);
581   EXPECT_EQ(unique.size(), 3);
582   if (unique.size() == 3) {
583     EXPECT_EQ(*unique.begin(), "asd");
584     EXPECT_EQ(*--unique.end(), "csd");
585   }
586
587   VectorType<fbstring,std::allocator<fbstring> > blah;
588   folly::split('-', "a-b-c-d-f-e", blah);
589   EXPECT_EQ(blah.size(), 6);
590 }
591
592 }
593
594 TEST(Split, split_vector) {
595   splitTest<std::vector>();
596 }
597 TEST(Split, split_fbvector) {
598   splitTest<folly::fbvector>();
599 }
600 TEST(Split, pieces_vector) {
601   piecesTest<std::vector>();
602 }
603 TEST(Split, pieces_fbvector) {
604   piecesTest<folly::fbvector>();
605 }
606
607 TEST(String, hexlify) {
608   string input1 = "0123";
609   string output1;
610   EXPECT_TRUE(hexlify(input1, output1));
611   EXPECT_EQ(output1, "30313233");
612
613   fbstring input2 = "abcdefg";
614   input2[1] = 0;
615   input2[3] = 0xff;
616   input2[5] = 0xb6;
617   fbstring output2;
618   EXPECT_TRUE(hexlify(input2, output2));
619   EXPECT_EQ(output2, "610063ff65b667");
620 }
621
622 TEST(String, unhexlify) {
623   string input1 = "30313233";
624   string output1;
625   EXPECT_TRUE(unhexlify(input1, output1));
626   EXPECT_EQ(output1, "0123");
627
628   fbstring input2 = "610063ff65b667";
629   fbstring output2;
630   EXPECT_TRUE(unhexlify(input2, output2));
631   EXPECT_EQ(output2.size(), 7);
632   EXPECT_EQ(output2[0], 'a');
633   EXPECT_EQ(output2[1], 0);
634   EXPECT_EQ(output2[2], 'c');
635   EXPECT_EQ(output2[3] & 0xff, 0xff);
636   EXPECT_EQ(output2[4], 'e');
637   EXPECT_EQ(output2[5] & 0xff, 0xb6);
638   EXPECT_EQ(output2[6], 'g');
639
640   string input3 = "x";
641   string output3;
642   EXPECT_FALSE(unhexlify(input3, output3));
643
644   string input4 = "xy";
645   string output4;
646   EXPECT_FALSE(unhexlify(input4, output4));
647 }
648
649 TEST(String, backslashify) {
650   EXPECT_EQ("abc", string("abc"));
651   EXPECT_EQ("abc", backslashify(string("abc")));
652   EXPECT_EQ("abc\\r", backslashify(string("abc\r")));
653   EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true));
654   EXPECT_EQ("\\0\\0", backslashify(string(2, '\0')));
655 }
656
657 TEST(String, humanify) {
658   // Simple cases; output is obvious.
659   EXPECT_EQ("abc", humanify(string("abc")));
660   EXPECT_EQ("abc\\\\r", humanify(string("abc\\r")));
661   EXPECT_EQ("0xff", humanify(string("\xff")));
662   EXPECT_EQ("abc\\xff", humanify(string("abc\xff")));
663   EXPECT_EQ("abc\\b", humanify(string("abc\b")));
664   EXPECT_EQ("0x00", humanify(string(1, '\0')));
665   EXPECT_EQ("0x0000", humanify(string(2, '\0')));
666
667
668   // Mostly printable, so backslash!  80, 60, and 40% printable, respectively
669   EXPECT_EQ("aaaa\\xff", humanify(string("aaaa\xff")));
670   EXPECT_EQ("aaa\\xff\\xff", humanify(string("aaa\xff\xff")));
671   EXPECT_EQ("aa\\xff\\xff\\xff", humanify(string("aa\xff\xff\xff")));
672
673   // 20% printable, and the printable portion isn't the prefix; hexify!
674   EXPECT_EQ("0xff61ffffff", humanify(string("\xff" "a\xff\xff\xff")));
675
676   // Same as previous, except swap first two chars; prefix is
677   // printable and within the threshold, so backslashify.
678   EXPECT_EQ("a\\xff\\xff\\xff\\xff", humanify(string("a\xff\xff\xff\xff")));
679
680   // Just too much unprintable; hex, despite prefix.
681   EXPECT_EQ("0x61ffffffffff", humanify(string("a\xff\xff\xff\xff\xff")));
682 }
683
684 //////////////////////////////////////////////////////////////////////
685
686 BENCHMARK(splitOnSingleChar, iters) {
687   const std::string line = "one:two:three:four";
688   for (int i = 0; i < iters << 4; ++i) {
689     std::vector<StringPiece> pieces;
690     folly::split(':', line, pieces);
691   }
692 }
693
694 BENCHMARK(splitStr, iters) {
695   const std::string line = "one-*-two-*-three-*-four";
696   for (int i = 0; i < iters << 4; ++i) {
697     std::vector<StringPiece> pieces;
698     folly::split("-*-", line, pieces);
699   }
700 }
701
702 BENCHMARK(boost_splitOnSingleChar, iters) {
703   std::string line = "one:two:three:four";
704   for (int i = 0; i < iters << 4; ++i) {
705     std::vector<boost::iterator_range<std::string::iterator>> pieces;
706     boost::split(pieces, line, [] (char c) { return c == ':'; });
707   }
708 }
709
710 int main(int argc, char *argv[]) {
711   testing::InitGoogleTest(&argc, argv);
712   google::ParseCommandLineFlags(&argc, &argv, true);
713   auto ret = RUN_ALL_TESTS();
714   if (!ret) {
715     initBenchmark();
716     if (FLAGS_benchmark) {
717       folly::runBenchmarks();
718     }
719   }
720   return ret;
721 }
722