2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "folly/String.h"
20 #include <boost/algorithm/string.hpp>
21 #include <gtest/gtest.h>
23 #include "folly/Benchmark.h"
25 using namespace folly;
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"));
34 EXPECT_EQ("abc", stringPrintf("abc"));
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));
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));
54 TEST(StringPrintf, Appending) {
56 stringAppendf(&s, "a%s", "b");
57 stringAppendf(&s, "%c", 'c');
59 stringAppendf(&s, " %d", 123);
60 EXPECT_EQ(s, "abc 123");
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()));
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));
77 TEST(StringPrintf, oldStringPrintfTests) {
78 EXPECT_EQ(string("a/b/c/d"),
79 stringPrintf("%s/%s/%s/%s", "a", "b", "c", "d"));
81 EXPECT_EQ(string(" 5 10"),
82 stringPrintf("%5d %5d", 5, 10));
84 // check printing w/ a big buffer
85 for (int size = (1 << 8); size <= (1 << 15); size <<= 1) {
87 string b = stringPrintf("%s", a.c_str());
88 EXPECT_EQ(a.size(), b.size());
92 TEST(StringPrintf, oldStringAppendf) {
94 stringAppendf(&s, "%s/%s/%s/%s", "a", "b", "c", "d");
95 EXPECT_EQ(string("helloa/b/c/d"), s);
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);
103 stringPrintf("msg msg msg msg msg msg msg msg: %d, %d, %s",
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"));
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"));
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);
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));
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);
159 void expectPrintable(StringPiece s) {
167 TEST(Escape, uriEscapeAllCombinations) {
170 StringPiece in(c, 2);
173 for (int i = 0; i < 256; ++i) {
175 for (int j = 0; j < 256; ++j) {
180 expectPrintable(tmp);
181 uriUnescape(tmp, out);
189 return ((v >= '0' && v <= '9') ||
190 (v >= 'A' && v <= 'F') ||
191 (v >= 'a' && v <= 'f'));
195 TEST(Escape, uriUnescapePercentDecoding) {
196 char c[4] = {'%', '\0', '\0', '\0'};
197 StringPiece in(c, 3);
199 unsigned int expected = 0;
200 for (int i = 0; i < 256; ++i) {
202 for (int j = 0; j < 256; ++j) {
204 if (isHex(i) && isHex(j)) {
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);
212 EXPECT_THROW({uriUnescape(in, out);}, std::invalid_argument);
220 fbstring cbmEscapedString;
221 fbstring cEscapedString;
222 fbstring cUnescapedString;
223 const size_t kCBmStringLength = 64 << 10;
224 const uint32_t kCPrintablePercentage = 90;
226 fbstring uribmString;
227 fbstring uribmEscapedString;
228 fbstring uriEscapedString;
229 fbstring uriUnescapedString;
230 const size_t kURIBmStringLength = 256;
231 const uint32_t kURIPassThroughPercentage = 50;
233 void initBenchmark() {
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);
241 cbmString.reserve(kCBmStringLength);
242 for (size_t i = 0; i < kCBmStringLength; ++i) {
244 if (percentage(rnd) < kCPrintablePercentage) {
247 c = nonPrintable(rnd);
248 // Generate characters in both non-printable ranges:
249 // 0..31 and 127..255
254 cbmString.push_back(c);
257 cbmEscapedString = cEscape<fbstring>(cbmString);
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);
264 uribmString.reserve(kURIBmStringLength);
265 for (size_t i = 0; i < kURIBmStringLength; ++i) {
267 if (percentage(rnd) < kURIPassThroughPercentage) {
268 c = passthrough(rnd);
270 c = encodeChars[encode(rnd)];
272 uribmString.push_back(c);
275 uribmEscapedString = uriEscape<fbstring>(uribmString);
278 BENCHMARK(BM_cEscape, iters) {
280 cEscapedString = cEscape<fbstring>(cbmString);
281 doNotOptimizeAway(cEscapedString.size());
285 BENCHMARK(BM_cUnescape, iters) {
287 cUnescapedString = cUnescape<fbstring>(cbmEscapedString);
288 doNotOptimizeAway(cUnescapedString.size());
292 BENCHMARK(BM_uriEscape, iters) {
294 uriEscapedString = uriEscape<fbstring>(uribmString);
295 doNotOptimizeAway(uriEscapedString.size());
299 BENCHMARK(BM_uriUnescape, iters) {
301 uriUnescapedString = uriUnescape<fbstring>(uribmEscapedString);
302 doNotOptimizeAway(uriUnescapedString.size());
310 double pow2(int exponent) {
311 return double(int64_t(1) << exponent);
316 TEST(PrettyPrint, Basic) {
317 // check time printing
318 EXPECT_EQ(string("8.53e+07 s "), prettyPrint(85.3e6, PRETTY_TIME));
319 EXPECT_EQ(string("85.3 s "), prettyPrint(85.3, PRETTY_TIME));
320 EXPECT_EQ(string("85.3 ms"), prettyPrint(85.3e-3, PRETTY_TIME));
321 EXPECT_EQ(string("85.3 us"), prettyPrint(85.3e-6, PRETTY_TIME));
322 EXPECT_EQ(string("85.3 ns"), prettyPrint(85.3e-9, PRETTY_TIME));
323 EXPECT_EQ(string("85.3 ps"), prettyPrint(85.3e-12, PRETTY_TIME));
324 EXPECT_EQ(string("8.53e-14 s "), prettyPrint(85.3e-15, PRETTY_TIME));
326 EXPECT_EQ(string("0 s "), prettyPrint(0, PRETTY_TIME));
327 EXPECT_EQ(string("1 s "), prettyPrint(1.0, PRETTY_TIME));
328 EXPECT_EQ(string("1 ms"), prettyPrint(1.0e-3, PRETTY_TIME));
329 EXPECT_EQ(string("1 us"), prettyPrint(1.0e-6, PRETTY_TIME));
330 EXPECT_EQ(string("1 ns"), prettyPrint(1.0e-9, PRETTY_TIME));
331 EXPECT_EQ(string("1 ps"), prettyPrint(1.0e-12, PRETTY_TIME));
333 // check bytes printing
334 EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES));
335 EXPECT_EQ(string("833 kB"), prettyPrint(853.e3, PRETTY_BYTES));
336 EXPECT_EQ(string("813.5 MB"), prettyPrint(853.e6, PRETTY_BYTES));
337 EXPECT_EQ(string("7.944 GB"), prettyPrint(8.53e9, PRETTY_BYTES));
338 EXPECT_EQ(string("794.4 GB"), prettyPrint(853.e9, PRETTY_BYTES));
339 EXPECT_EQ(string("775.8 TB"), prettyPrint(853.e12, PRETTY_BYTES));
341 EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES));
342 EXPECT_EQ(string("1 B "), prettyPrint(pow2(0), PRETTY_BYTES));
343 EXPECT_EQ(string("1 kB"), prettyPrint(pow2(10), PRETTY_BYTES));
344 EXPECT_EQ(string("1 MB"), prettyPrint(pow2(20), PRETTY_BYTES));
345 EXPECT_EQ(string("1 GB"), prettyPrint(pow2(30), PRETTY_BYTES));
346 EXPECT_EQ(string("1 TB"), prettyPrint(pow2(40), PRETTY_BYTES));
348 EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_IEC));
349 EXPECT_EQ(string("833 KiB"), prettyPrint(853.e3, PRETTY_BYTES_IEC));
350 EXPECT_EQ(string("813.5 MiB"), prettyPrint(853.e6, PRETTY_BYTES_IEC));
351 EXPECT_EQ(string("7.944 GiB"), prettyPrint(8.53e9, PRETTY_BYTES_IEC));
352 EXPECT_EQ(string("794.4 GiB"), prettyPrint(853.e9, PRETTY_BYTES_IEC));
353 EXPECT_EQ(string("775.8 TiB"), prettyPrint(853.e12, PRETTY_BYTES_IEC));
355 EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES_IEC));
356 EXPECT_EQ(string("1 B "), prettyPrint(pow2(0), PRETTY_BYTES_IEC));
357 EXPECT_EQ(string("1 KiB"), prettyPrint(pow2(10), PRETTY_BYTES_IEC));
358 EXPECT_EQ(string("1 MiB"), prettyPrint(pow2(20), PRETTY_BYTES_IEC));
359 EXPECT_EQ(string("1 GiB"), prettyPrint(pow2(30), PRETTY_BYTES_IEC));
360 EXPECT_EQ(string("1 TiB"), prettyPrint(pow2(40), PRETTY_BYTES_IEC));
362 // check bytes metric printing
363 EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_METRIC));
364 EXPECT_EQ(string("853 kB"), prettyPrint(853.e3, PRETTY_BYTES_METRIC));
365 EXPECT_EQ(string("853 MB"), prettyPrint(853.e6, PRETTY_BYTES_METRIC));
366 EXPECT_EQ(string("8.53 GB"), prettyPrint(8.53e9, PRETTY_BYTES_METRIC));
367 EXPECT_EQ(string("853 GB"), prettyPrint(853.e9, PRETTY_BYTES_METRIC));
368 EXPECT_EQ(string("853 TB"), prettyPrint(853.e12, PRETTY_BYTES_METRIC));
370 EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES_METRIC));
371 EXPECT_EQ(string("1 B "), prettyPrint(1.0, PRETTY_BYTES_METRIC));
372 EXPECT_EQ(string("1 kB"), prettyPrint(1.0e+3, PRETTY_BYTES_METRIC));
373 EXPECT_EQ(string("1 MB"), prettyPrint(1.0e+6, PRETTY_BYTES_METRIC));
375 EXPECT_EQ(string("1 GB"), prettyPrint(1.0e+9, PRETTY_BYTES_METRIC));
376 EXPECT_EQ(string("1 TB"), prettyPrint(1.0e+12, PRETTY_BYTES_METRIC));
378 // check metric-units (powers of 1000) printing
379 EXPECT_EQ(string("853 "), prettyPrint(853., PRETTY_UNITS_METRIC));
380 EXPECT_EQ(string("853 k"), prettyPrint(853.e3, PRETTY_UNITS_METRIC));
381 EXPECT_EQ(string("853 M"), prettyPrint(853.e6, PRETTY_UNITS_METRIC));
382 EXPECT_EQ(string("8.53 bil"), prettyPrint(8.53e9, PRETTY_UNITS_METRIC));
383 EXPECT_EQ(string("853 bil"), prettyPrint(853.e9, PRETTY_UNITS_METRIC));
384 EXPECT_EQ(string("853 tril"), prettyPrint(853.e12, PRETTY_UNITS_METRIC));
386 // check binary-units (powers of 1024) printing
387 EXPECT_EQ(string("0 "), prettyPrint(0, PRETTY_UNITS_BINARY));
388 EXPECT_EQ(string("1 "), prettyPrint(pow2(0), PRETTY_UNITS_BINARY));
389 EXPECT_EQ(string("1 k"), prettyPrint(pow2(10), PRETTY_UNITS_BINARY));
390 EXPECT_EQ(string("1 M"), prettyPrint(pow2(20), PRETTY_UNITS_BINARY));
391 EXPECT_EQ(string("1 G"), prettyPrint(pow2(30), PRETTY_UNITS_BINARY));
392 EXPECT_EQ(string("1 T"), prettyPrint(pow2(40), PRETTY_UNITS_BINARY));
394 EXPECT_EQ(string("1023 "),
395 prettyPrint(pow2(10) - 1, PRETTY_UNITS_BINARY));
396 EXPECT_EQ(string("1024 k"),
397 prettyPrint(pow2(20) - 1, PRETTY_UNITS_BINARY));
398 EXPECT_EQ(string("1024 M"),
399 prettyPrint(pow2(30) - 1, PRETTY_UNITS_BINARY));
400 EXPECT_EQ(string("1024 G"),
401 prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY));
403 EXPECT_EQ(string("0 "), prettyPrint(0, PRETTY_UNITS_BINARY_IEC));
404 EXPECT_EQ(string("1 "), prettyPrint(pow2(0), PRETTY_UNITS_BINARY_IEC));
405 EXPECT_EQ(string("1 Ki"), prettyPrint(pow2(10), PRETTY_UNITS_BINARY_IEC));
406 EXPECT_EQ(string("1 Mi"), prettyPrint(pow2(20), PRETTY_UNITS_BINARY_IEC));
407 EXPECT_EQ(string("1 Gi"), prettyPrint(pow2(30), PRETTY_UNITS_BINARY_IEC));
408 EXPECT_EQ(string("1 Ti"), prettyPrint(pow2(40), PRETTY_UNITS_BINARY_IEC));
410 EXPECT_EQ(string("1023 "),
411 prettyPrint(pow2(10) - 1, PRETTY_UNITS_BINARY_IEC));
412 EXPECT_EQ(string("1024 Ki"),
413 prettyPrint(pow2(20) - 1, PRETTY_UNITS_BINARY_IEC));
414 EXPECT_EQ(string("1024 Mi"),
415 prettyPrint(pow2(30) - 1, PRETTY_UNITS_BINARY_IEC));
416 EXPECT_EQ(string("1024 Gi"),
417 prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY_IEC));
419 // check that negative values work
420 EXPECT_EQ(string("-85.3 s "), prettyPrint(-85.3, PRETTY_TIME));
421 EXPECT_EQ(string("-85.3 ms"), prettyPrint(-85.3e-3, PRETTY_TIME));
422 EXPECT_EQ(string("-85.3 us"), prettyPrint(-85.3e-6, PRETTY_TIME));
423 EXPECT_EQ(string("-85.3 ns"), prettyPrint(-85.3e-9, PRETTY_TIME));
426 TEST(PrettyPrint, HexDump) {
427 std::string a("abc\x00\x02\xa0", 6); // embedded NUL
429 "00000000 61 62 63 00 02 a0 "
431 hexDump(a.data(), a.size()));
433 a = "abcdefghijklmnopqrstuvwxyz";
435 "00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 "
436 "|abcdefghijklmnop|\n"
437 "00000010 71 72 73 74 75 76 77 78 79 7a "
439 hexDump(a.data(), a.size()));
442 TEST(System, errnoStr) {
444 EXPECT_EQ(EACCES, errno);
445 EXPECT_EQ(EACCES, errno); // twice to make sure EXPECT_EQ doesn't change it
447 fbstring expected = strerror(ENOENT);
450 EXPECT_EQ(expected, errnoStr(ENOENT));
451 // Ensure that errno isn't changed
452 EXPECT_EQ(EACCES, errno);
454 // Per POSIX, all errno values are positive, so -1 is invalid
457 // Ensure that errno isn't changed
458 EXPECT_EQ(EACCES, errno);
461 namespace folly_test {
462 struct ThisIsAVeryLongStructureName {
464 } // namespace folly_test
466 TEST(System, demangle) {
467 EXPECT_EQ("folly_test::ThisIsAVeryLongStructureName",
468 demangle(typeid(folly_test::ThisIsAVeryLongStructureName)));
473 template<template<class,class> class VectorType>
475 VectorType<string,std::allocator<string> > parts;
477 folly::split(',', "a,b,c", parts);
478 EXPECT_EQ(parts.size(), 3);
479 EXPECT_EQ(parts[0], "a");
480 EXPECT_EQ(parts[1], "b");
481 EXPECT_EQ(parts[2], "c");
484 folly::split(',', StringPiece("a,b,c"), parts);
485 EXPECT_EQ(parts.size(), 3);
486 EXPECT_EQ(parts[0], "a");
487 EXPECT_EQ(parts[1], "b");
488 EXPECT_EQ(parts[2], "c");
491 folly::split(',', string("a,b,c"), parts);
492 EXPECT_EQ(parts.size(), 3);
493 EXPECT_EQ(parts[0], "a");
494 EXPECT_EQ(parts[1], "b");
495 EXPECT_EQ(parts[2], "c");
498 folly::split(',', "a,,c", parts);
499 EXPECT_EQ(parts.size(), 3);
500 EXPECT_EQ(parts[0], "a");
501 EXPECT_EQ(parts[1], "");
502 EXPECT_EQ(parts[2], "c");
505 folly::split(',', string("a,,c"), parts);
506 EXPECT_EQ(parts.size(), 3);
507 EXPECT_EQ(parts[0], "a");
508 EXPECT_EQ(parts[1], "");
509 EXPECT_EQ(parts[2], "c");
512 folly::split(',', "a,,c", parts, true);
513 EXPECT_EQ(parts.size(), 2);
514 EXPECT_EQ(parts[0], "a");
515 EXPECT_EQ(parts[1], "c");
518 folly::split(',', string("a,,c"), parts, true);
519 EXPECT_EQ(parts.size(), 2);
520 EXPECT_EQ(parts[0], "a");
521 EXPECT_EQ(parts[1], "c");
524 folly::split(',', string(",,a,,c,,,"), parts, true);
525 EXPECT_EQ(parts.size(), 2);
526 EXPECT_EQ(parts[0], "a");
527 EXPECT_EQ(parts[1], "c");
530 // test multiple split w/o clear
531 folly::split(',', ",,a,,c,,,", parts, true);
532 EXPECT_EQ(parts.size(), 2);
533 EXPECT_EQ(parts[0], "a");
534 EXPECT_EQ(parts[1], "c");
535 folly::split(',', ",,a,,c,,,", parts, true);
536 EXPECT_EQ(parts.size(), 4);
537 EXPECT_EQ(parts[2], "a");
538 EXPECT_EQ(parts[3], "c");
541 // test splits that with multi-line delimiter
542 folly::split("ab", "dabcabkdbkab", parts, true);
543 EXPECT_EQ(parts.size(), 3);
544 EXPECT_EQ(parts[0], "d");
545 EXPECT_EQ(parts[1], "c");
546 EXPECT_EQ(parts[2], "kdbk");
549 string orig = "ab2342asdfv~~!";
550 folly::split("", orig, parts, true);
551 EXPECT_EQ(parts.size(), 1);
552 EXPECT_EQ(parts[0], orig);
555 folly::split("452x;o38asfsajsdlfdf.j", "asfds", parts, true);
556 EXPECT_EQ(parts.size(), 1);
557 EXPECT_EQ(parts[0], "asfds");
560 folly::split("a", "", parts, true);
561 EXPECT_EQ(parts.size(), 0);
564 folly::split("a", "", parts);
565 EXPECT_EQ(parts.size(), 1);
566 EXPECT_EQ(parts[0], "");
569 folly::split("a", StringPiece(), parts, true);
570 EXPECT_EQ(parts.size(), 0);
573 folly::split("a", StringPiece(), parts);
574 EXPECT_EQ(parts.size(), 1);
575 EXPECT_EQ(parts[0], "");
578 folly::split("a", "abcdefg", parts, true);
579 EXPECT_EQ(parts.size(), 1);
580 EXPECT_EQ(parts[0], "bcdefg");
583 orig = "All, , your base, are , , belong to us";
584 folly::split(", ", orig, parts, true);
585 EXPECT_EQ(parts.size(), 4);
586 EXPECT_EQ(parts[0], "All");
587 EXPECT_EQ(parts[1], "your base");
588 EXPECT_EQ(parts[2], "are ");
589 EXPECT_EQ(parts[3], "belong to us");
591 folly::split(", ", orig, parts);
592 EXPECT_EQ(parts.size(), 6);
593 EXPECT_EQ(parts[0], "All");
594 EXPECT_EQ(parts[1], "");
595 EXPECT_EQ(parts[2], "your base");
596 EXPECT_EQ(parts[3], "are ");
597 EXPECT_EQ(parts[4], "");
598 EXPECT_EQ(parts[5], "belong to us");
601 orig = ", Facebook, rul,es!, ";
602 folly::split(", ", orig, parts, true);
603 EXPECT_EQ(parts.size(), 2);
604 EXPECT_EQ(parts[0], "Facebook");
605 EXPECT_EQ(parts[1], "rul,es!");
607 folly::split(", ", orig, parts);
608 EXPECT_EQ(parts.size(), 4);
609 EXPECT_EQ(parts[0], "");
610 EXPECT_EQ(parts[1], "Facebook");
611 EXPECT_EQ(parts[2], "rul,es!");
612 EXPECT_EQ(parts[3], "");
615 template<template<class,class> class VectorType>
617 VectorType<StringPiece,std::allocator<StringPiece> > pieces;
618 VectorType<StringPiece,std::allocator<StringPiece> > pieces2;
620 folly::split(',', "a,b,c", pieces);
621 EXPECT_EQ(pieces.size(), 3);
622 EXPECT_EQ(pieces[0], "a");
623 EXPECT_EQ(pieces[1], "b");
624 EXPECT_EQ(pieces[2], "c");
628 folly::split(',', "a,,c", pieces);
629 EXPECT_EQ(pieces.size(), 3);
630 EXPECT_EQ(pieces[0], "a");
631 EXPECT_EQ(pieces[1], "");
632 EXPECT_EQ(pieces[2], "c");
635 folly::split(',', "a,,c", pieces, true);
636 EXPECT_EQ(pieces.size(), 2);
637 EXPECT_EQ(pieces[0], "a");
638 EXPECT_EQ(pieces[1], "c");
641 folly::split(',', ",,a,,c,,,", pieces, true);
642 EXPECT_EQ(pieces.size(), 2);
643 EXPECT_EQ(pieces[0], "a");
644 EXPECT_EQ(pieces[1], "c");
647 // test multiple split w/o clear
648 folly::split(',', ",,a,,c,,,", pieces, true);
649 EXPECT_EQ(pieces.size(), 2);
650 EXPECT_EQ(pieces[0], "a");
651 EXPECT_EQ(pieces[1], "c");
652 folly::split(',', ",,a,,c,,,", pieces, true);
653 EXPECT_EQ(pieces.size(), 4);
654 EXPECT_EQ(pieces[2], "a");
655 EXPECT_EQ(pieces[3], "c");
658 // test multiple split rounds
659 folly::split(",", "a_b,c_d", pieces);
660 EXPECT_EQ(pieces.size(), 2);
661 EXPECT_EQ(pieces[0], "a_b");
662 EXPECT_EQ(pieces[1], "c_d");
663 folly::split("_", pieces[0], pieces2);
664 EXPECT_EQ(pieces2.size(), 2);
665 EXPECT_EQ(pieces2[0], "a");
666 EXPECT_EQ(pieces2[1], "b");
668 folly::split("_", pieces[1], pieces2);
669 EXPECT_EQ(pieces2.size(), 2);
670 EXPECT_EQ(pieces2[0], "c");
671 EXPECT_EQ(pieces2[1], "d");
675 // test splits that with multi-line delimiter
676 folly::split("ab", "dabcabkdbkab", pieces, true);
677 EXPECT_EQ(pieces.size(), 3);
678 EXPECT_EQ(pieces[0], "d");
679 EXPECT_EQ(pieces[1], "c");
680 EXPECT_EQ(pieces[2], "kdbk");
683 string orig = "ab2342asdfv~~!";
684 folly::split("", orig.c_str(), pieces, true);
685 EXPECT_EQ(pieces.size(), 1);
686 EXPECT_EQ(pieces[0], orig);
689 folly::split("452x;o38asfsajsdlfdf.j", "asfds", pieces, true);
690 EXPECT_EQ(pieces.size(), 1);
691 EXPECT_EQ(pieces[0], "asfds");
694 folly::split("a", "", pieces, true);
695 EXPECT_EQ(pieces.size(), 0);
698 folly::split("a", "", pieces);
699 EXPECT_EQ(pieces.size(), 1);
700 EXPECT_EQ(pieces[0], "");
703 folly::split("a", "abcdefg", pieces, true);
704 EXPECT_EQ(pieces.size(), 1);
705 EXPECT_EQ(pieces[0], "bcdefg");
708 orig = "All, , your base, are , , belong to us";
709 folly::split(", ", orig, pieces, true);
710 EXPECT_EQ(pieces.size(), 4);
711 EXPECT_EQ(pieces[0], "All");
712 EXPECT_EQ(pieces[1], "your base");
713 EXPECT_EQ(pieces[2], "are ");
714 EXPECT_EQ(pieces[3], "belong to us");
716 folly::split(", ", orig, pieces);
717 EXPECT_EQ(pieces.size(), 6);
718 EXPECT_EQ(pieces[0], "All");
719 EXPECT_EQ(pieces[1], "");
720 EXPECT_EQ(pieces[2], "your base");
721 EXPECT_EQ(pieces[3], "are ");
722 EXPECT_EQ(pieces[4], "");
723 EXPECT_EQ(pieces[5], "belong to us");
726 orig = ", Facebook, rul,es!, ";
727 folly::split(", ", orig, pieces, true);
728 EXPECT_EQ(pieces.size(), 2);
729 EXPECT_EQ(pieces[0], "Facebook");
730 EXPECT_EQ(pieces[1], "rul,es!");
732 folly::split(", ", orig, pieces);
733 EXPECT_EQ(pieces.size(), 4);
734 EXPECT_EQ(pieces[0], "");
735 EXPECT_EQ(pieces[1], "Facebook");
736 EXPECT_EQ(pieces[2], "rul,es!");
737 EXPECT_EQ(pieces[3], "");
740 const char* str = "a,b";
741 folly::split(',', StringPiece(str), pieces);
742 EXPECT_EQ(pieces.size(), 2);
743 EXPECT_EQ(pieces[0], "a");
744 EXPECT_EQ(pieces[1], "b");
745 EXPECT_EQ(pieces[0].start(), str);
746 EXPECT_EQ(pieces[1].start(), str + 2);
748 std::set<StringPiece> unique;
749 folly::splitTo<StringPiece>(":", "asd:bsd:asd:asd:bsd:csd::asd",
750 std::inserter(unique, unique.begin()), true);
751 EXPECT_EQ(unique.size(), 3);
752 if (unique.size() == 3) {
753 EXPECT_EQ(*unique.begin(), "asd");
754 EXPECT_EQ(*--unique.end(), "csd");
757 VectorType<fbstring,std::allocator<fbstring> > blah;
758 folly::split('-', "a-b-c-d-f-e", blah);
759 EXPECT_EQ(blah.size(), 6);
764 TEST(Split, split_vector) {
765 splitTest<std::vector>();
767 TEST(Split, split_fbvector) {
768 splitTest<folly::fbvector>();
770 TEST(Split, pieces_vector) {
771 piecesTest<std::vector>();
773 TEST(Split, pieces_fbvector) {
774 piecesTest<folly::fbvector>();
778 StringPiece a, b, c, d;
780 EXPECT_TRUE(folly::split<false>('.', "a.b.c.d", a, b, c, d));
781 EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
782 EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
783 EXPECT_TRUE(folly::split<false>('.', "a", a));
785 EXPECT_TRUE(folly::split('.', "a.b.c.d", a, b, c, d));
786 EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
787 EXPECT_TRUE(folly::split('.', "a.b", a, b));
788 EXPECT_TRUE(folly::split('.', "a", a));
790 EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b, c));
794 EXPECT_FALSE(folly::split<false>('.', "a.b", a, b, c));
795 EXPECT_TRUE(folly::split<false>('.', "a.b.c", a, b));
799 EXPECT_TRUE(folly::split('.', "a.b.c", a, b, c));
803 EXPECT_FALSE(folly::split('.', "a.b.c", a, b));
804 EXPECT_FALSE(folly::split('.', "a.b", a, b, c));
806 EXPECT_TRUE(folly::split<false>('.', "a.b", a, b));
809 EXPECT_FALSE(folly::split<false>('.', "a", a, b));
810 EXPECT_TRUE(folly::split<false>('.', "a.b", a));
813 EXPECT_TRUE(folly::split('.', "a.b", a, b));
816 EXPECT_FALSE(folly::split('.', "a", a, b));
817 EXPECT_FALSE(folly::split('.', "a.b", a));
823 std::vector<int> empty = { };
824 join(":", empty, output);
825 EXPECT_TRUE(output.empty());
827 std::vector<std::string> input1 = { "1", "23", "456", "" };
828 join(':', input1, output);
829 EXPECT_EQ(output, "1:23:456:");
830 output = join(':', input1);
831 EXPECT_EQ(output, "1:23:456:");
833 auto input2 = { 1, 23, 456 };
834 join("-*-", input2, output);
835 EXPECT_EQ(output, "1-*-23-*-456");
836 output = join("-*-", input2);
837 EXPECT_EQ(output, "1-*-23-*-456");
839 auto input3 = { 'f', 'a', 'c', 'e', 'b', 'o', 'o', 'k' };
840 join("", input3, output);
841 EXPECT_EQ(output, "facebook");
843 join("_", { "", "f", "a", "c", "e", "b", "o", "o", "k", "" }, output);
844 EXPECT_EQ(output, "_f_a_c_e_b_o_o_k_");
847 TEST(String, hexlify) {
848 string input1 = "0123";
850 EXPECT_TRUE(hexlify(input1, output1));
851 EXPECT_EQ(output1, "30313233");
853 fbstring input2 = "abcdefg";
858 EXPECT_TRUE(hexlify(input2, output2));
859 EXPECT_EQ(output2, "610063ff65b667");
862 TEST(String, unhexlify) {
863 string input1 = "30313233";
865 EXPECT_TRUE(unhexlify(input1, output1));
866 EXPECT_EQ(output1, "0123");
868 fbstring input2 = "610063ff65b667";
870 EXPECT_TRUE(unhexlify(input2, output2));
871 EXPECT_EQ(output2.size(), 7);
872 EXPECT_EQ(output2[0], 'a');
873 EXPECT_EQ(output2[1], 0);
874 EXPECT_EQ(output2[2], 'c');
875 EXPECT_EQ(output2[3] & 0xff, 0xff);
876 EXPECT_EQ(output2[4], 'e');
877 EXPECT_EQ(output2[5] & 0xff, 0xb6);
878 EXPECT_EQ(output2[6], 'g');
882 EXPECT_FALSE(unhexlify(input3, output3));
884 string input4 = "xy";
886 EXPECT_FALSE(unhexlify(input4, output4));
889 TEST(String, backslashify) {
890 EXPECT_EQ("abc", string("abc"));
891 EXPECT_EQ("abc", backslashify(string("abc")));
892 EXPECT_EQ("abc\\r", backslashify(string("abc\r")));
893 EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true));
894 EXPECT_EQ("\\0\\0", backslashify(string(2, '\0')));
897 TEST(String, humanify) {
898 // Simple cases; output is obvious.
899 EXPECT_EQ("abc", humanify(string("abc")));
900 EXPECT_EQ("abc\\\\r", humanify(string("abc\\r")));
901 EXPECT_EQ("0xff", humanify(string("\xff")));
902 EXPECT_EQ("abc\\xff", humanify(string("abc\xff")));
903 EXPECT_EQ("abc\\b", humanify(string("abc\b")));
904 EXPECT_EQ("0x00", humanify(string(1, '\0')));
905 EXPECT_EQ("0x0000", humanify(string(2, '\0')));
908 // Mostly printable, so backslash! 80, 60, and 40% printable, respectively
909 EXPECT_EQ("aaaa\\xff", humanify(string("aaaa\xff")));
910 EXPECT_EQ("aaa\\xff\\xff", humanify(string("aaa\xff\xff")));
911 EXPECT_EQ("aa\\xff\\xff\\xff", humanify(string("aa\xff\xff\xff")));
913 // 20% printable, and the printable portion isn't the prefix; hexify!
914 EXPECT_EQ("0xff61ffffff", humanify(string("\xff" "a\xff\xff\xff")));
916 // Same as previous, except swap first two chars; prefix is
917 // printable and within the threshold, so backslashify.
918 EXPECT_EQ("a\\xff\\xff\\xff\\xff", humanify(string("a\xff\xff\xff\xff")));
920 // Just too much unprintable; hex, despite prefix.
921 EXPECT_EQ("0x61ffffffffff", humanify(string("a\xff\xff\xff\xff\xff")));
924 //////////////////////////////////////////////////////////////////////
926 BENCHMARK(splitOnSingleChar, iters) {
927 static const std::string line = "one:two:three:four";
928 for (int i = 0; i < iters << 4; ++i) {
929 std::vector<StringPiece> pieces;
930 folly::split(':', line, pieces);
934 BENCHMARK(splitOnSingleCharFixed, iters) {
935 static const std::string line = "one:two:three:four";
936 for (int i = 0; i < iters << 4; ++i) {
937 StringPiece a, b, c, d;
938 folly::split(':', line, a, b, c, d);
942 BENCHMARK(splitOnSingleCharFixedAllowExtra, iters) {
943 static const std::string line = "one:two:three:four";
944 for (int i = 0; i < iters << 4; ++i) {
945 StringPiece a, b, c, d;
946 folly::split<false>(':', line, a, b, c, d);
950 BENCHMARK(splitStr, iters) {
951 static const std::string line = "one-*-two-*-three-*-four";
952 for (int i = 0; i < iters << 4; ++i) {
953 std::vector<StringPiece> pieces;
954 folly::split("-*-", line, pieces);
958 BENCHMARK(splitStrFixed, iters) {
959 static const std::string line = "one-*-two-*-three-*-four";
960 for (int i = 0; i < iters << 4; ++i) {
961 StringPiece a, b, c, d;
962 folly::split("-*-", line, a, b, c, d);
966 BENCHMARK(boost_splitOnSingleChar, iters) {
967 static const std::string line = "one:two:three:four";
968 for (int i = 0; i < iters << 4; ++i) {
969 std::vector<boost::iterator_range<std::string::const_iterator> > pieces;
970 boost::split(pieces, line, [] (char c) { return c == ':'; });
974 BENCHMARK(joinCharStr, iters) {
975 static const std::vector<std::string> input = {
976 "one", "two", "three", "four", "five", "six", "seven" };
977 for (int i = 0; i < iters << 4; ++i) {
979 folly::join(':', input, output);
983 BENCHMARK(joinStrStr, iters) {
984 static const std::vector<std::string> input = {
985 "one", "two", "three", "four", "five", "six", "seven" };
986 for (int i = 0; i < iters << 4; ++i) {
988 folly::join(":", input, output);
992 BENCHMARK(joinInt, iters) {
993 static const auto input = {
994 123, 456, 78910, 1112, 1314, 151, 61718 };
995 for (int i = 0; i < iters << 4; ++i) {
997 folly::join(":", input, output);
1001 int main(int argc, char *argv[]) {
1002 testing::InitGoogleTest(&argc, argv);
1003 google::ParseCommandLineFlags(&argc, &argv, true);
1004 auto ret = RUN_ALL_TESTS();
1007 if (FLAGS_benchmark) {
1008 folly::runBenchmarks();