bfad26e9b1ba2f0af110bf9a81cb2a78edcab03c
[folly.git] / folly / test / StringTest.cpp
1 /*
2  * Copyright 2014 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 TEST(Escape, uriEscape) {
135   EXPECT_EQ("hello%2c%20%2fworld", uriEscape<std::string>("hello, /world"));
136   EXPECT_EQ("hello%2c%20/world", uriEscape<std::string>("hello, /world",
137                                                         UriEscapeMode::PATH));
138   EXPECT_EQ("hello%2c+%2fworld", uriEscape<std::string>("hello, /world",
139                                                         UriEscapeMode::QUERY));
140 }
141
142 TEST(Escape, uriUnescape) {
143   EXPECT_EQ("hello, /world", uriUnescape<std::string>("hello, /world"));
144   EXPECT_EQ("hello, /world", uriUnescape<std::string>("hello%2c%20%2fworld"));
145   EXPECT_EQ("hello,+/world", uriUnescape<std::string>("hello%2c+%2fworld"));
146   EXPECT_EQ("hello, /world", uriUnescape<std::string>("hello%2c+%2fworld",
147                                                       UriEscapeMode::QUERY));
148   EXPECT_EQ("hello/", uriUnescape<std::string>("hello%2f"));
149   EXPECT_EQ("hello/", uriUnescape<std::string>("hello%2F"));
150   EXPECT_THROW({uriUnescape<std::string>("hello%");},
151                std::invalid_argument);
152   EXPECT_THROW({uriUnescape<std::string>("hello%2");},
153                std::invalid_argument);
154   EXPECT_THROW({uriUnescape<std::string>("hello%2g");},
155                std::invalid_argument);
156 }
157
158 namespace {
159 void expectPrintable(StringPiece s) {
160   for (char c : s) {
161     EXPECT_LE(32, c);
162     EXPECT_GE(127, c);
163   }
164 }
165 }  // namespace
166
167 TEST(Escape, uriEscapeAllCombinations) {
168   char c[3];
169   c[2] = '\0';
170   StringPiece in(c, 2);
171   fbstring tmp;
172   fbstring out;
173   for (int i = 0; i < 256; ++i) {
174     c[0] = i;
175     for (int j = 0; j < 256; ++j) {
176       c[1] = j;
177       tmp.clear();
178       out.clear();
179       uriEscape(in, tmp);
180       expectPrintable(tmp);
181       uriUnescape(tmp, out);
182       EXPECT_EQ(in, out);
183     }
184   }
185 }
186
187 namespace {
188 bool isHex(int v) {
189   return ((v >= '0' && v <= '9') ||
190           (v >= 'A' && v <= 'F') ||
191           (v >= 'a' && v <= 'f'));
192 }
193 }  // namespace
194
195 TEST(Escape, uriUnescapePercentDecoding) {
196   char c[4] = {'%', '\0', '\0', '\0'};
197   StringPiece in(c, 3);
198   fbstring out;
199   unsigned int expected = 0;
200   for (int i = 0; i < 256; ++i) {
201     c[1] = i;
202     for (int j = 0; j < 256; ++j) {
203       c[2] = j;
204       if (isHex(i) && isHex(j)) {
205         out.clear();
206         uriUnescape(in, out);
207         EXPECT_EQ(1, out.size());
208         EXPECT_EQ(1, sscanf(c + 1, "%x", &expected));
209         unsigned char v = out[0];
210         EXPECT_EQ(expected, v);
211       } else {
212         EXPECT_THROW({uriUnescape(in, out);}, std::invalid_argument);
213       }
214     }
215   }
216 }
217
218 namespace {
219 fbstring cbmString;
220 fbstring cbmEscapedString;
221 fbstring cEscapedString;
222 fbstring cUnescapedString;
223 const size_t kCBmStringLength = 64 << 10;
224 const uint32_t kCPrintablePercentage = 90;
225
226 fbstring uribmString;
227 fbstring uribmEscapedString;
228 fbstring uriEscapedString;
229 fbstring uriUnescapedString;
230 const size_t kURIBmStringLength = 256;
231 const uint32_t kURIPassThroughPercentage = 50;
232
233 void initBenchmark() {
234   std::mt19937 rnd;
235
236   // C escape
237   std::uniform_int_distribution<uint32_t> printable(32, 126);
238   std::uniform_int_distribution<uint32_t> nonPrintable(0, 160);
239   std::uniform_int_distribution<uint32_t> percentage(0, 99);
240
241   cbmString.reserve(kCBmStringLength);
242   for (size_t i = 0; i < kCBmStringLength; ++i) {
243     unsigned char c;
244     if (percentage(rnd) < kCPrintablePercentage) {
245       c = printable(rnd);
246     } else {
247       c = nonPrintable(rnd);
248       // Generate characters in both non-printable ranges:
249       // 0..31 and 127..255
250       if (c >= 32) {
251         c += (126 - 32) + 1;
252       }
253     }
254     cbmString.push_back(c);
255   }
256
257   cbmEscapedString = cEscape<fbstring>(cbmString);
258
259   // URI escape
260   std::uniform_int_distribution<uint32_t> passthrough('a', 'z');
261   std::string encodeChars = " ?!\"',+[]";
262   std::uniform_int_distribution<uint32_t> encode(0, encodeChars.size() - 1);
263
264   uribmString.reserve(kURIBmStringLength);
265   for (size_t i = 0; i < kURIBmStringLength; ++i) {
266     unsigned char c;
267     if (percentage(rnd) < kURIPassThroughPercentage) {
268       c = passthrough(rnd);
269     } else {
270       c = encodeChars[encode(rnd)];
271     }
272     uribmString.push_back(c);
273   }
274
275   uribmEscapedString = uriEscape<fbstring>(uribmString);
276 }
277
278 BENCHMARK(BM_cEscape, iters) {
279   while (iters--) {
280     cEscapedString = cEscape<fbstring>(cbmString);
281     doNotOptimizeAway(cEscapedString.size());
282   }
283 }
284
285 BENCHMARK(BM_cUnescape, iters) {
286   while (iters--) {
287     cUnescapedString = cUnescape<fbstring>(cbmEscapedString);
288     doNotOptimizeAway(cUnescapedString.size());
289   }
290 }
291
292 BENCHMARK(BM_uriEscape, iters) {
293   while (iters--) {
294     uriEscapedString = uriEscape<fbstring>(uribmString);
295     doNotOptimizeAway(uriEscapedString.size());
296   }
297 }
298
299 BENCHMARK(BM_uriUnescape, iters) {
300   while (iters--) {
301     uriUnescapedString = uriUnescape<fbstring>(uribmEscapedString);
302     doNotOptimizeAway(uriUnescapedString.size());
303   }
304 }
305
306 }  // namespace
307
308 namespace {
309
310 double pow2(int exponent) {
311   return double(int64_t(1) << exponent);
312 }
313
314 }  // namespace
315 struct PrettyTestCase{
316   std::string prettyString;
317   double realValue;
318   PrettyType prettyType;
319 };
320
321 PrettyTestCase prettyTestCases[] =
322 {
323   {string("8.53e+07 s "), 85.3e6,  PRETTY_TIME},
324   {string("8.53e+07 s "), 85.3e6,  PRETTY_TIME},
325   {string("85.3 ms"), 85.3e-3,  PRETTY_TIME},
326   {string("85.3 us"), 85.3e-6,  PRETTY_TIME},
327   {string("85.3 ns"), 85.3e-9,  PRETTY_TIME},
328   {string("85.3 ps"), 85.3e-12,  PRETTY_TIME},
329   {string("8.53e-14 s "), 85.3e-15,  PRETTY_TIME},
330
331   {string("0 s "), 0,  PRETTY_TIME},
332   {string("1 s "), 1.0,  PRETTY_TIME},
333   {string("1 ms"), 1.0e-3,  PRETTY_TIME},
334   {string("1 us"), 1.0e-6,  PRETTY_TIME},
335   {string("1 ns"), 1.0e-9,  PRETTY_TIME},
336   {string("1 ps"), 1.0e-12,  PRETTY_TIME},
337
338   // check bytes printing
339   {string("853 B "), 853.,  PRETTY_BYTES},
340   {string("833 kB"), 853.e3,  PRETTY_BYTES},
341   {string("813.5 MB"), 853.e6,  PRETTY_BYTES},
342   {string("7.944 GB"), 8.53e9,  PRETTY_BYTES},
343   {string("794.4 GB"), 853.e9,  PRETTY_BYTES},
344   {string("775.8 TB"), 853.e12,  PRETTY_BYTES},
345
346   {string("0 B "), 0,  PRETTY_BYTES},
347   {string("1 B "), pow2(0),  PRETTY_BYTES},
348   {string("1 kB"), pow2(10),  PRETTY_BYTES},
349   {string("1 MB"), pow2(20),  PRETTY_BYTES},
350   {string("1 GB"), pow2(30),  PRETTY_BYTES},
351   {string("1 TB"), pow2(40),  PRETTY_BYTES},
352
353   {string("853 B  "), 853.,  PRETTY_BYTES_IEC},
354   {string("833 KiB"), 853.e3,  PRETTY_BYTES_IEC},
355   {string("813.5 MiB"), 853.e6,  PRETTY_BYTES_IEC},
356   {string("7.944 GiB"), 8.53e9,  PRETTY_BYTES_IEC},
357   {string("794.4 GiB"), 853.e9,  PRETTY_BYTES_IEC},
358   {string("775.8 TiB"), 853.e12,  PRETTY_BYTES_IEC},
359
360   {string("0 B  "), 0,  PRETTY_BYTES_IEC},
361   {string("1 B  "), pow2(0),  PRETTY_BYTES_IEC},
362   {string("1 KiB"), pow2(10),  PRETTY_BYTES_IEC},
363   {string("1 MiB"), pow2(20),  PRETTY_BYTES_IEC},
364   {string("1 GiB"), pow2(30),  PRETTY_BYTES_IEC},
365   {string("1 TiB"), pow2(40),  PRETTY_BYTES_IEC},
366
367   // check bytes metric printing
368   {string("853 B "), 853.,  PRETTY_BYTES_METRIC},
369   {string("853 kB"), 853.e3,  PRETTY_BYTES_METRIC},
370   {string("853 MB"), 853.e6,  PRETTY_BYTES_METRIC},
371   {string("8.53 GB"), 8.53e9,  PRETTY_BYTES_METRIC},
372   {string("853 GB"), 853.e9,  PRETTY_BYTES_METRIC},
373   {string("853 TB"), 853.e12,  PRETTY_BYTES_METRIC},
374
375   {string("0 B "), 0,  PRETTY_BYTES_METRIC},
376   {string("1 B "), 1.0,  PRETTY_BYTES_METRIC},
377   {string("1 kB"), 1.0e+3,  PRETTY_BYTES_METRIC},
378   {string("1 MB"), 1.0e+6,  PRETTY_BYTES_METRIC},
379
380   {string("1 GB"), 1.0e+9,  PRETTY_BYTES_METRIC},
381   {string("1 TB"), 1.0e+12,  PRETTY_BYTES_METRIC},
382
383   // check metric-units (powers of 1000) printing
384   {string("853  "), 853.,  PRETTY_UNITS_METRIC},
385   {string("853 k"), 853.e3,  PRETTY_UNITS_METRIC},
386   {string("853 M"), 853.e6,  PRETTY_UNITS_METRIC},
387   {string("8.53 bil"), 8.53e9,  PRETTY_UNITS_METRIC},
388   {string("853 bil"), 853.e9,  PRETTY_UNITS_METRIC},
389   {string("853 tril"), 853.e12,  PRETTY_UNITS_METRIC},
390
391   // check binary-units (powers of 1024) printing
392   {string("0  "), 0,  PRETTY_UNITS_BINARY},
393   {string("1  "), pow2(0),  PRETTY_UNITS_BINARY},
394   {string("1 k"), pow2(10),  PRETTY_UNITS_BINARY},
395   {string("1 M"), pow2(20),  PRETTY_UNITS_BINARY},
396   {string("1 G"), pow2(30),  PRETTY_UNITS_BINARY},
397   {string("1 T"), pow2(40),  PRETTY_UNITS_BINARY},
398
399   {string("1023  "), pow2(10) - 1,  PRETTY_UNITS_BINARY},
400   {string("1024 k"), pow2(20) - 1,  PRETTY_UNITS_BINARY},
401   {string("1024 M"), pow2(30) - 1,  PRETTY_UNITS_BINARY},
402   {string("1024 G"), pow2(40) - 1,  PRETTY_UNITS_BINARY},
403
404   {string("0   "), 0,  PRETTY_UNITS_BINARY_IEC},
405   {string("1   "), pow2(0),  PRETTY_UNITS_BINARY_IEC},
406   {string("1 Ki"), pow2(10),  PRETTY_UNITS_BINARY_IEC},
407   {string("1 Mi"), pow2(20),  PRETTY_UNITS_BINARY_IEC},
408   {string("1 Gi"), pow2(30),  PRETTY_UNITS_BINARY_IEC},
409   {string("1 Ti"), pow2(40),  PRETTY_UNITS_BINARY_IEC},
410
411   {string("1023   "), pow2(10) - 1,  PRETTY_UNITS_BINARY_IEC},
412   {string("1024 Ki"), pow2(20) - 1,  PRETTY_UNITS_BINARY_IEC},
413   {string("1024 Mi"), pow2(30) - 1,  PRETTY_UNITS_BINARY_IEC},
414   {string("1024 Gi"), pow2(40) - 1,  PRETTY_UNITS_BINARY_IEC},
415
416   //check border SI cases
417
418   {string("1 Y"), 1e24,  PRETTY_SI},
419   {string("10 Y"), 1e25,  PRETTY_SI},
420   {string("1 y"), 1e-24,  PRETTY_SI},
421   {string("10 y"), 1e-23,  PRETTY_SI},
422
423   // check that negative values work
424   {string("-85.3 s "), -85.3,  PRETTY_TIME},
425   {string("-85.3 ms"), -85.3e-3,  PRETTY_TIME},
426   {string("-85.3 us"), -85.3e-6,  PRETTY_TIME},
427   {string("-85.3 ns"), -85.3e-9,  PRETTY_TIME},
428   // end of test
429   {string("endoftest"), 0, PRETTY_NUM_TYPES}
430 };
431
432 TEST(PrettyPrint, Basic) {
433   for (int i = 0; prettyTestCases[i].prettyType != PRETTY_NUM_TYPES; ++i){
434     const PrettyTestCase& prettyTest = prettyTestCases[i];
435     EXPECT_EQ(prettyTest.prettyString,
436               prettyPrint(prettyTest.realValue, prettyTest.prettyType));
437   }
438 }
439
440 TEST(PrettyToDouble, Basic) {
441   // check manually created tests
442   for (int i = 0; prettyTestCases[i].prettyType != PRETTY_NUM_TYPES; ++i){
443     PrettyTestCase testCase = prettyTestCases[i];
444     PrettyType formatType = testCase.prettyType;
445     double x = testCase.realValue;
446     std::string testString = testCase.prettyString;
447     double recoveredX;
448     try{
449       recoveredX = prettyToDouble(testString, formatType);
450     } catch (std::range_error &ex){
451       EXPECT_TRUE(false);
452     }
453     double relativeError = fabs(x) < 1e-5 ? (x-recoveredX) :
454                                             (x - recoveredX) / x;
455     EXPECT_NEAR(0, relativeError, 1e-3);
456   }
457
458   // checks for compatibility with prettyPrint over the whole parameter space
459   for (int i = 0 ; i < PRETTY_NUM_TYPES; ++i){
460     PrettyType formatType = static_cast<PrettyType>(i);
461     for (double x = 1e-18; x < 1e40; x *= 1.9){
462       bool addSpace = static_cast<PrettyType> (i) == PRETTY_SI;
463       for (int it = 0; it < 2; ++it, addSpace = true){
464         double recoveredX;
465         try{
466           recoveredX = prettyToDouble(prettyPrint(x, formatType, addSpace),
467                                              formatType);
468         } catch (std::range_error &ex){
469           EXPECT_TRUE(false);
470         }
471         double relativeError = (x - recoveredX) / x;
472         EXPECT_NEAR(0, relativeError, 1e-3);
473       }
474     }
475   }
476
477   // check for incorrect values
478   EXPECT_THROW(prettyToDouble("10Mx", PRETTY_SI), std::range_error);
479   EXPECT_THROW(prettyToDouble("10 Mx", PRETTY_SI), std::range_error);
480   EXPECT_THROW(prettyToDouble("10 M x", PRETTY_SI), std::range_error);
481
482   StringPiece testString = "10Mx";
483   EXPECT_DOUBLE_EQ(prettyToDouble(&testString, PRETTY_UNITS_METRIC), 10e6);
484   EXPECT_EQ(testString, "x");
485 }
486
487 TEST(PrettyPrint, HexDump) {
488   std::string a("abc\x00\x02\xa0", 6);  // embedded NUL
489   EXPECT_EQ(
490     "00000000  61 62 63 00 02 a0                                 "
491     "|abc...          |\n",
492     hexDump(a.data(), a.size()));
493
494   a = "abcdefghijklmnopqrstuvwxyz";
495   EXPECT_EQ(
496     "00000000  61 62 63 64 65 66 67 68  69 6a 6b 6c 6d 6e 6f 70  "
497     "|abcdefghijklmnop|\n"
498     "00000010  71 72 73 74 75 76 77 78  79 7a                    "
499     "|qrstuvwxyz      |\n",
500     hexDump(a.data(), a.size()));
501 }
502
503 TEST(System, errnoStr) {
504   errno = EACCES;
505   EXPECT_EQ(EACCES, errno);
506   EXPECT_EQ(EACCES, errno);  // twice to make sure EXPECT_EQ doesn't change it
507
508   fbstring expected = strerror(ENOENT);
509
510   errno = EACCES;
511   EXPECT_EQ(expected, errnoStr(ENOENT));
512   // Ensure that errno isn't changed
513   EXPECT_EQ(EACCES, errno);
514
515   // Per POSIX, all errno values are positive, so -1 is invalid
516   errnoStr(-1);
517
518   // Ensure that errno isn't changed
519   EXPECT_EQ(EACCES, errno);
520 }
521
522 namespace folly_test {
523 struct ThisIsAVeryLongStructureName {
524 };
525 }  // namespace folly_test
526
527 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
528 TEST(System, demangle) {
529   char expected[] = "folly_test::ThisIsAVeryLongStructureName";
530   EXPECT_STREQ(
531       expected,
532       demangle(typeid(folly_test::ThisIsAVeryLongStructureName)).c_str());
533
534   {
535     char buf[sizeof(expected)];
536     EXPECT_EQ(sizeof(expected) - 1,
537               demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
538                        buf, sizeof(buf)));
539     EXPECT_STREQ(expected, buf);
540
541     EXPECT_EQ(sizeof(expected) - 1,
542               demangle(typeid(folly_test::ThisIsAVeryLongStructureName),
543                        buf, 11));
544     EXPECT_STREQ("folly_test", buf);
545   }
546 }
547 #endif
548
549 namespace {
550
551 template<template<class,class> class VectorType>
552 void splitTest() {
553   VectorType<string,std::allocator<string> > parts;
554
555   folly::split(',', "a,b,c", parts);
556   EXPECT_EQ(parts.size(), 3);
557   EXPECT_EQ(parts[0], "a");
558   EXPECT_EQ(parts[1], "b");
559   EXPECT_EQ(parts[2], "c");
560   parts.clear();
561
562   folly::split(',', StringPiece("a,b,c"), parts);
563   EXPECT_EQ(parts.size(), 3);
564   EXPECT_EQ(parts[0], "a");
565   EXPECT_EQ(parts[1], "b");
566   EXPECT_EQ(parts[2], "c");
567   parts.clear();
568
569   folly::split(',', string("a,b,c"), parts);
570   EXPECT_EQ(parts.size(), 3);
571   EXPECT_EQ(parts[0], "a");
572   EXPECT_EQ(parts[1], "b");
573   EXPECT_EQ(parts[2], "c");
574   parts.clear();
575
576   folly::split(',', "a,,c", parts);
577   EXPECT_EQ(parts.size(), 3);
578   EXPECT_EQ(parts[0], "a");
579   EXPECT_EQ(parts[1], "");
580   EXPECT_EQ(parts[2], "c");
581   parts.clear();
582
583   folly::split(',', string("a,,c"), parts);
584   EXPECT_EQ(parts.size(), 3);
585   EXPECT_EQ(parts[0], "a");
586   EXPECT_EQ(parts[1], "");
587   EXPECT_EQ(parts[2], "c");
588   parts.clear();
589
590   folly::split(',', "a,,c", parts, true);
591   EXPECT_EQ(parts.size(), 2);
592   EXPECT_EQ(parts[0], "a");
593   EXPECT_EQ(parts[1], "c");
594   parts.clear();
595
596   folly::split(',', string("a,,c"), parts, true);
597   EXPECT_EQ(parts.size(), 2);
598   EXPECT_EQ(parts[0], "a");
599   EXPECT_EQ(parts[1], "c");
600   parts.clear();
601
602   folly::split(',', string(",,a,,c,,,"), parts, true);
603   EXPECT_EQ(parts.size(), 2);
604   EXPECT_EQ(parts[0], "a");
605   EXPECT_EQ(parts[1], "c");
606   parts.clear();
607
608   // test multiple split w/o clear
609   folly::split(',', ",,a,,c,,,", parts, true);
610   EXPECT_EQ(parts.size(), 2);
611   EXPECT_EQ(parts[0], "a");
612   EXPECT_EQ(parts[1], "c");
613   folly::split(',', ",,a,,c,,,", parts, true);
614   EXPECT_EQ(parts.size(), 4);
615   EXPECT_EQ(parts[2], "a");
616   EXPECT_EQ(parts[3], "c");
617   parts.clear();
618
619   // test splits that with multi-line delimiter
620   folly::split("ab", "dabcabkdbkab", parts, true);
621   EXPECT_EQ(parts.size(), 3);
622   EXPECT_EQ(parts[0], "d");
623   EXPECT_EQ(parts[1], "c");
624   EXPECT_EQ(parts[2], "kdbk");
625   parts.clear();
626
627   string orig = "ab2342asdfv~~!";
628   folly::split("", orig, parts, true);
629   EXPECT_EQ(parts.size(), 1);
630   EXPECT_EQ(parts[0], orig);
631   parts.clear();
632
633   folly::split("452x;o38asfsajsdlfdf.j", "asfds", parts, true);
634   EXPECT_EQ(parts.size(), 1);
635   EXPECT_EQ(parts[0], "asfds");
636   parts.clear();
637
638   folly::split("a", "", parts, true);
639   EXPECT_EQ(parts.size(), 0);
640   parts.clear();
641
642   folly::split("a", "", parts);
643   EXPECT_EQ(parts.size(), 1);
644   EXPECT_EQ(parts[0], "");
645   parts.clear();
646
647   folly::split("a", StringPiece(), parts, true);
648   EXPECT_EQ(parts.size(), 0);
649   parts.clear();
650
651   folly::split("a", StringPiece(), parts);
652   EXPECT_EQ(parts.size(), 1);
653   EXPECT_EQ(parts[0], "");
654   parts.clear();
655
656   folly::split("a", "abcdefg", parts, true);
657   EXPECT_EQ(parts.size(), 1);
658   EXPECT_EQ(parts[0], "bcdefg");
659   parts.clear();
660
661   orig = "All, , your base, are , , belong to us";
662   folly::split(", ", orig, parts, true);
663   EXPECT_EQ(parts.size(), 4);
664   EXPECT_EQ(parts[0], "All");
665   EXPECT_EQ(parts[1], "your base");
666   EXPECT_EQ(parts[2], "are ");
667   EXPECT_EQ(parts[3], "belong to us");
668   parts.clear();
669   folly::split(", ", orig, parts);
670   EXPECT_EQ(parts.size(), 6);
671   EXPECT_EQ(parts[0], "All");
672   EXPECT_EQ(parts[1], "");
673   EXPECT_EQ(parts[2], "your base");
674   EXPECT_EQ(parts[3], "are ");
675   EXPECT_EQ(parts[4], "");
676   EXPECT_EQ(parts[5], "belong to us");
677   parts.clear();
678
679   orig = ", Facebook, rul,es!, ";
680   folly::split(", ", orig, parts, true);
681   EXPECT_EQ(parts.size(), 2);
682   EXPECT_EQ(parts[0], "Facebook");
683   EXPECT_EQ(parts[1], "rul,es!");
684   parts.clear();
685   folly::split(", ", orig, parts);
686   EXPECT_EQ(parts.size(), 4);
687   EXPECT_EQ(parts[0], "");
688   EXPECT_EQ(parts[1], "Facebook");
689   EXPECT_EQ(parts[2], "rul,es!");
690   EXPECT_EQ(parts[3], "");
691 }
692
693 template<template<class,class> class VectorType>
694 void piecesTest() {
695   VectorType<StringPiece,std::allocator<StringPiece> > pieces;
696   VectorType<StringPiece,std::allocator<StringPiece> > pieces2;
697
698   folly::split(',', "a,b,c", pieces);
699   EXPECT_EQ(pieces.size(), 3);
700   EXPECT_EQ(pieces[0], "a");
701   EXPECT_EQ(pieces[1], "b");
702   EXPECT_EQ(pieces[2], "c");
703
704   pieces.clear();
705
706   folly::split(',', "a,,c", pieces);
707   EXPECT_EQ(pieces.size(), 3);
708   EXPECT_EQ(pieces[0], "a");
709   EXPECT_EQ(pieces[1], "");
710   EXPECT_EQ(pieces[2], "c");
711   pieces.clear();
712
713   folly::split(',', "a,,c", pieces, true);
714   EXPECT_EQ(pieces.size(), 2);
715   EXPECT_EQ(pieces[0], "a");
716   EXPECT_EQ(pieces[1], "c");
717   pieces.clear();
718
719   folly::split(',', ",,a,,c,,,", pieces, true);
720   EXPECT_EQ(pieces.size(), 2);
721   EXPECT_EQ(pieces[0], "a");
722   EXPECT_EQ(pieces[1], "c");
723   pieces.clear();
724
725   // test multiple split w/o clear
726   folly::split(',', ",,a,,c,,,", pieces, true);
727   EXPECT_EQ(pieces.size(), 2);
728   EXPECT_EQ(pieces[0], "a");
729   EXPECT_EQ(pieces[1], "c");
730   folly::split(',', ",,a,,c,,,", pieces, true);
731   EXPECT_EQ(pieces.size(), 4);
732   EXPECT_EQ(pieces[2], "a");
733   EXPECT_EQ(pieces[3], "c");
734   pieces.clear();
735
736   // test multiple split rounds
737   folly::split(",", "a_b,c_d", pieces);
738   EXPECT_EQ(pieces.size(), 2);
739   EXPECT_EQ(pieces[0], "a_b");
740   EXPECT_EQ(pieces[1], "c_d");
741   folly::split("_", pieces[0], pieces2);
742   EXPECT_EQ(pieces2.size(), 2);
743   EXPECT_EQ(pieces2[0], "a");
744   EXPECT_EQ(pieces2[1], "b");
745   pieces2.clear();
746   folly::split("_", pieces[1], pieces2);
747   EXPECT_EQ(pieces2.size(), 2);
748   EXPECT_EQ(pieces2[0], "c");
749   EXPECT_EQ(pieces2[1], "d");
750   pieces.clear();
751   pieces2.clear();
752
753   // test splits that with multi-line delimiter
754   folly::split("ab", "dabcabkdbkab", pieces, true);
755   EXPECT_EQ(pieces.size(), 3);
756   EXPECT_EQ(pieces[0], "d");
757   EXPECT_EQ(pieces[1], "c");
758   EXPECT_EQ(pieces[2], "kdbk");
759   pieces.clear();
760
761   string orig = "ab2342asdfv~~!";
762   folly::split("", orig.c_str(), pieces, true);
763   EXPECT_EQ(pieces.size(), 1);
764   EXPECT_EQ(pieces[0], orig);
765   pieces.clear();
766
767   folly::split("452x;o38asfsajsdlfdf.j", "asfds", pieces, true);
768   EXPECT_EQ(pieces.size(), 1);
769   EXPECT_EQ(pieces[0], "asfds");
770   pieces.clear();
771
772   folly::split("a", "", pieces, true);
773   EXPECT_EQ(pieces.size(), 0);
774   pieces.clear();
775
776   folly::split("a", "", pieces);
777   EXPECT_EQ(pieces.size(), 1);
778   EXPECT_EQ(pieces[0], "");
779   pieces.clear();
780
781   folly::split("a", "abcdefg", pieces, true);
782   EXPECT_EQ(pieces.size(), 1);
783   EXPECT_EQ(pieces[0], "bcdefg");
784   pieces.clear();
785
786   orig = "All, , your base, are , , belong to us";
787   folly::split(", ", orig, pieces, true);
788   EXPECT_EQ(pieces.size(), 4);
789   EXPECT_EQ(pieces[0], "All");
790   EXPECT_EQ(pieces[1], "your base");
791   EXPECT_EQ(pieces[2], "are ");
792   EXPECT_EQ(pieces[3], "belong to us");
793   pieces.clear();
794   folly::split(", ", orig, pieces);
795   EXPECT_EQ(pieces.size(), 6);
796   EXPECT_EQ(pieces[0], "All");
797   EXPECT_EQ(pieces[1], "");
798   EXPECT_EQ(pieces[2], "your base");
799   EXPECT_EQ(pieces[3], "are ");
800   EXPECT_EQ(pieces[4], "");
801   EXPECT_EQ(pieces[5], "belong to us");
802   pieces.clear();
803
804   orig = ", Facebook, rul,es!, ";
805   folly::split(", ", orig, pieces, true);
806   EXPECT_EQ(pieces.size(), 2);
807   EXPECT_EQ(pieces[0], "Facebook");
808   EXPECT_EQ(pieces[1], "rul,es!");
809   pieces.clear();
810   folly::split(", ", orig, pieces);
811   EXPECT_EQ(pieces.size(), 4);
812   EXPECT_EQ(pieces[0], "");
813   EXPECT_EQ(pieces[1], "Facebook");
814   EXPECT_EQ(pieces[2], "rul,es!");
815   EXPECT_EQ(pieces[3], "");
816   pieces.clear();
817
818   const char* str = "a,b";
819   folly::split(',', StringPiece(str), pieces);
820   EXPECT_EQ(pieces.size(), 2);
821   EXPECT_EQ(pieces[0], "a");
822   EXPECT_EQ(pieces[1], "b");
823   EXPECT_EQ(pieces[0].start(), str);
824   EXPECT_EQ(pieces[1].start(), str + 2);
825
826   std::set<StringPiece> unique;
827   folly::splitTo<StringPiece>(":", "asd:bsd:asd:asd:bsd:csd::asd",
828     std::inserter(unique, unique.begin()), true);
829   EXPECT_EQ(unique.size(), 3);
830   if (unique.size() == 3) {
831     EXPECT_EQ(*unique.begin(), "asd");
832     EXPECT_EQ(*--unique.end(), "csd");
833   }
834
835   VectorType<fbstring,std::allocator<fbstring> > blah;
836   folly::split('-', "a-b-c-d-f-e", blah);
837   EXPECT_EQ(blah.size(), 6);
838 }
839
840 }
841
842 TEST(Split, split_vector) {
843   splitTest<std::vector>();
844 }
845 TEST(Split, split_fbvector) {
846   splitTest<folly::fbvector>();
847 }
848 TEST(Split, pieces_vector) {
849   piecesTest<std::vector>();
850 }
851 TEST(Split, pieces_fbvector) {
852   piecesTest<folly::fbvector>();
853 }
854
855 TEST(Split, fixed) {
856   StringPiece a, b, c, d;
857
858   EXPECT_TRUE(folly::split<false>('.', "a.b.c.d", a, b, c, d));
859   EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
860   EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
861   EXPECT_TRUE(folly::split<false>('.', "a", a));
862
863   EXPECT_TRUE(folly::split('.', "a.b.c.d", a, b, c, d));
864   EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
865   EXPECT_TRUE(folly::split('.', "a.b", a, b));
866   EXPECT_TRUE(folly::split('.', "a", a));
867
868   EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
869   EXPECT_EQ("a", a);
870   EXPECT_EQ("b", b);
871   EXPECT_EQ("c", c);
872   EXPECT_FALSE(folly::split<false>('.', "a.b", a, b, c));
873   EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b));
874   EXPECT_EQ("a", a);
875   EXPECT_EQ("b.c", b);
876
877   EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
878   EXPECT_EQ("a", a);
879   EXPECT_EQ("b", b);
880   EXPECT_EQ("c", c);
881   EXPECT_FALSE(folly::split('.', "a.b.c", a, b));
882   EXPECT_FALSE(folly::split('.', "a.b", a, b, c));
883
884   EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
885   EXPECT_EQ("a", a);
886   EXPECT_EQ("b", b);
887   EXPECT_FALSE(folly::split<false>('.', "a", a, b));
888   EXPECT_TRUE(folly::split<false>('.', "a.b", a));
889   EXPECT_EQ("a.b", a);
890
891   EXPECT_TRUE(folly::split('.', "a.b", a, b));
892   EXPECT_EQ("a", a);
893   EXPECT_EQ("b", b);
894   EXPECT_FALSE(folly::split('.', "a", a, b));
895   EXPECT_FALSE(folly::split('.', "a.b", a));
896 }
897
898 TEST(Split, fixed_convert) {
899   StringPiece a, d;
900   int b;
901   double c;
902
903   EXPECT_TRUE(folly::split(':', "a:13:14.7:b", a, b, c, d));
904   EXPECT_EQ("a", a);
905   EXPECT_EQ(13, b);
906   EXPECT_NEAR(14.7, c, 1e-10);
907   EXPECT_EQ("b", d);
908
909   EXPECT_TRUE(folly::split<false>(':', "b:14:15.3:c", a, b, c, d));
910   EXPECT_EQ("b", a);
911   EXPECT_EQ(14, b);
912   EXPECT_NEAR(15.3, c, 1e-10);
913   EXPECT_EQ("c", d);
914
915   EXPECT_FALSE(folly::split(':', "a:13:14.7:b", a, b, d));
916
917   EXPECT_TRUE(folly::split<false>(':', "a:13:14.7:b", a, b, d));
918   EXPECT_EQ("a", a);
919   EXPECT_EQ(13, b);
920   EXPECT_EQ("14.7:b", d);
921
922   EXPECT_THROW(folly::split<false>(':', "a:13:14.7:b", a, b, c),
923                std::range_error);
924 }
925
926 TEST(String, join) {
927   string output;
928
929   std::vector<int> empty = { };
930   join(":", empty, output);
931   EXPECT_TRUE(output.empty());
932
933   std::vector<std::string> input1 = { "1", "23", "456", "" };
934   join(':', input1, output);
935   EXPECT_EQ(output, "1:23:456:");
936   output = join(':', input1);
937   EXPECT_EQ(output, "1:23:456:");
938
939   auto input2 = { 1, 23, 456 };
940   join("-*-", input2, output);
941   EXPECT_EQ(output, "1-*-23-*-456");
942   output = join("-*-", input2);
943   EXPECT_EQ(output, "1-*-23-*-456");
944
945   auto input3 = { 'f', 'a', 'c', 'e', 'b', 'o', 'o', 'k' };
946   join("", input3, output);
947   EXPECT_EQ(output, "facebook");
948
949   join("_", { "", "f", "a", "c", "e", "b", "o", "o", "k", "" }, output);
950   EXPECT_EQ(output, "_f_a_c_e_b_o_o_k_");
951 }
952
953 TEST(String, hexlify) {
954   string input1 = "0123";
955   string output1;
956   EXPECT_TRUE(hexlify(input1, output1));
957   EXPECT_EQ(output1, "30313233");
958
959   fbstring input2 = "abcdefg";
960   input2[1] = 0;
961   input2[3] = 0xff;
962   input2[5] = 0xb6;
963   fbstring output2;
964   EXPECT_TRUE(hexlify(input2, output2));
965   EXPECT_EQ(output2, "610063ff65b667");
966 }
967
968 TEST(String, unhexlify) {
969   string input1 = "30313233";
970   string output1;
971   EXPECT_TRUE(unhexlify(input1, output1));
972   EXPECT_EQ(output1, "0123");
973
974   fbstring input2 = "610063ff65b667";
975   fbstring output2;
976   EXPECT_TRUE(unhexlify(input2, output2));
977   EXPECT_EQ(output2.size(), 7);
978   EXPECT_EQ(output2[0], 'a');
979   EXPECT_EQ(output2[1], 0);
980   EXPECT_EQ(output2[2], 'c');
981   EXPECT_EQ(output2[3] & 0xff, 0xff);
982   EXPECT_EQ(output2[4], 'e');
983   EXPECT_EQ(output2[5] & 0xff, 0xb6);
984   EXPECT_EQ(output2[6], 'g');
985
986   string input3 = "x";
987   string output3;
988   EXPECT_FALSE(unhexlify(input3, output3));
989
990   string input4 = "xy";
991   string output4;
992   EXPECT_FALSE(unhexlify(input4, output4));
993 }
994
995 TEST(String, backslashify) {
996   EXPECT_EQ("abc", string("abc"));
997   EXPECT_EQ("abc", backslashify(string("abc")));
998   EXPECT_EQ("abc\\r", backslashify(string("abc\r")));
999   EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true));
1000   EXPECT_EQ("\\0\\0", backslashify(string(2, '\0')));
1001 }
1002
1003 TEST(String, humanify) {
1004   // Simple cases; output is obvious.
1005   EXPECT_EQ("abc", humanify(string("abc")));
1006   EXPECT_EQ("abc\\\\r", humanify(string("abc\\r")));
1007   EXPECT_EQ("0xff", humanify(string("\xff")));
1008   EXPECT_EQ("abc\\xff", humanify(string("abc\xff")));
1009   EXPECT_EQ("abc\\b", humanify(string("abc\b")));
1010   EXPECT_EQ("0x00", humanify(string(1, '\0')));
1011   EXPECT_EQ("0x0000", humanify(string(2, '\0')));
1012
1013
1014   // Mostly printable, so backslash!  80, 60, and 40% printable, respectively
1015   EXPECT_EQ("aaaa\\xff", humanify(string("aaaa\xff")));
1016   EXPECT_EQ("aaa\\xff\\xff", humanify(string("aaa\xff\xff")));
1017   EXPECT_EQ("aa\\xff\\xff\\xff", humanify(string("aa\xff\xff\xff")));
1018
1019   // 20% printable, and the printable portion isn't the prefix; hexify!
1020   EXPECT_EQ("0xff61ffffff", humanify(string("\xff" "a\xff\xff\xff")));
1021
1022   // Same as previous, except swap first two chars; prefix is
1023   // printable and within the threshold, so backslashify.
1024   EXPECT_EQ("a\\xff\\xff\\xff\\xff", humanify(string("a\xff\xff\xff\xff")));
1025
1026   // Just too much unprintable; hex, despite prefix.
1027   EXPECT_EQ("0x61ffffffffff", humanify(string("a\xff\xff\xff\xff\xff")));
1028 }
1029
1030 //////////////////////////////////////////////////////////////////////
1031
1032 BENCHMARK(splitOnSingleChar, iters) {
1033   static const std::string line = "one:two:three:four";
1034   for (int i = 0; i < iters << 4; ++i) {
1035     std::vector<StringPiece> pieces;
1036     folly::split(':', line, pieces);
1037   }
1038 }
1039
1040 BENCHMARK(splitOnSingleCharFixed, iters) {
1041   static const std::string line = "one:two:three:four";
1042   for (int i = 0; i < iters << 4; ++i) {
1043     StringPiece a, b, c, d;
1044     folly::split(':', line, a, b, c, d);
1045   }
1046 }
1047
1048 BENCHMARK(splitOnSingleCharFixedAllowExtra, iters) {
1049   static const std::string line = "one:two:three:four";
1050   for (int i = 0; i < iters << 4; ++i) {
1051     StringPiece a, b, c, d;
1052     folly::split<false>(':', line, a, b, c, d);
1053   }
1054 }
1055
1056 BENCHMARK(splitStr, iters) {
1057   static const std::string line = "one-*-two-*-three-*-four";
1058   for (int i = 0; i < iters << 4; ++i) {
1059     std::vector<StringPiece> pieces;
1060     folly::split("-*-", line, pieces);
1061   }
1062 }
1063
1064 BENCHMARK(splitStrFixed, iters) {
1065   static const std::string line = "one-*-two-*-three-*-four";
1066   for (int i = 0; i < iters << 4; ++i) {
1067     StringPiece a, b, c, d;
1068     folly::split("-*-", line, a, b, c, d);
1069   }
1070 }
1071
1072 BENCHMARK(boost_splitOnSingleChar, iters) {
1073   static const std::string line = "one:two:three:four";
1074   bool(*pred)(char) = [] (char c) -> bool { return c == ':'; };
1075   for (int i = 0; i < iters << 4; ++i) {
1076     std::vector<boost::iterator_range<std::string::const_iterator> > pieces;
1077     boost::split(pieces, line, pred);
1078   }
1079 }
1080
1081 BENCHMARK(joinCharStr, iters) {
1082   static const std::vector<std::string> input = {
1083     "one", "two", "three", "four", "five", "six", "seven" };
1084   for (int i = 0; i < iters << 4; ++i) {
1085     std::string output;
1086     folly::join(':', input, output);
1087   }
1088 }
1089
1090 BENCHMARK(joinStrStr, iters) {
1091   static const std::vector<std::string> input = {
1092     "one", "two", "three", "four", "five", "six", "seven" };
1093   for (int i = 0; i < iters << 4; ++i) {
1094     std::string output;
1095     folly::join(":", input, output);
1096   }
1097 }
1098
1099 BENCHMARK(joinInt, iters) {
1100   static const auto input = {
1101     123, 456, 78910, 1112, 1314, 151, 61718 };
1102   for (int i = 0; i < iters << 4; ++i) {
1103     std::string output;
1104     folly::join(":", input, output);
1105   }
1106 }
1107
1108 int main(int argc, char *argv[]) {
1109   testing::InitGoogleTest(&argc, argv);
1110   google::ParseCommandLineFlags(&argc, &argv, true);
1111   auto ret = RUN_ALL_TESTS();
1112   if (!ret) {
1113     initBenchmark();
1114     if (FLAGS_benchmark) {
1115       folly::runBenchmarks();
1116     }
1117   }
1118   return ret;
1119 }