2 * Copyright 2012 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);
136 fbstring bmEscapedString;
137 fbstring escapedString;
138 fbstring unescapedString;
139 const size_t kBmStringLength = 64 << 10;
140 const uint32_t kPrintablePercentage = 90;
142 void initBenchmark() {
143 bmString.reserve(kBmStringLength);
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);
150 for (size_t i = 0; i < kBmStringLength; ++i) {
152 if (percentage(rnd) < kPrintablePercentage) {
155 c = nonPrintable(rnd);
156 // Generate characters in both non-printable ranges:
157 // 0..31 and 127..255
162 bmString.push_back(c);
165 bmEscapedString = cEscape<fbstring>(bmString);
168 BENCHMARK(BM_cEscape, iters) {
170 escapedString = cEscape<fbstring>(bmString);
171 doNotOptimizeAway(escapedString.size());
175 BENCHMARK(BM_cUnescape, iters) {
177 unescapedString = cUnescape<fbstring>(bmEscapedString);
178 doNotOptimizeAway(unescapedString.size());
186 double pow2(int exponent) {
187 return double(int64_t(1) << exponent);
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
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));
272 TEST(PrettyPrint, HexDump) {
273 std::string a("abc\x00\x02\xa0", 6); // embedded NUL
275 "00000000 61 62 63 00 02 a0 "
277 hexDump(a.data(), a.size()));
279 a = "abcdefghijklmnopqrstuvwxyz";
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 "
285 hexDump(a.data(), a.size()));
288 TEST(System, errnoStr) {
290 EXPECT_EQ(EACCES, errno);
291 EXPECT_EQ(EACCES, errno); // twice to make sure EXPECT_EQ doesn't change it
293 fbstring expected = strerror(ENOENT);
296 EXPECT_EQ(expected, errnoStr(ENOENT));
297 // Ensure that errno isn't changed
298 EXPECT_EQ(EACCES, errno);
300 // Per POSIX, all errno values are positive, so -1 is invalid
303 // Ensure that errno isn't changed
304 EXPECT_EQ(EACCES, errno);
307 namespace folly_test {
308 struct ThisIsAVeryLongStructureName {
310 } // namespace folly_test
312 TEST(System, demangle) {
313 EXPECT_EQ("folly_test::ThisIsAVeryLongStructureName",
314 demangle(typeid(folly_test::ThisIsAVeryLongStructureName)));
319 template<template<class,class> class VectorType>
321 VectorType<string,std::allocator<string> > parts;
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");
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");
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");
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");
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");
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");
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");
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");
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");
388 string orig = "ab2342asdfv~~!";
389 folly::split("", orig, parts, true);
390 EXPECT_EQ(parts.size(), 1);
391 EXPECT_EQ(parts[0], orig);
394 folly::split("452x;o38asfsajsdlfdf.j", "asfds", parts, true);
395 EXPECT_EQ(parts.size(), 1);
396 EXPECT_EQ(parts[0], "asfds");
399 folly::split("a", "", parts, true);
400 EXPECT_EQ(parts.size(), 0);
403 folly::split("a", "", parts);
404 EXPECT_EQ(parts.size(), 1);
405 EXPECT_EQ(parts[0], "");
408 folly::split("a", "abcdefg", parts, true);
409 EXPECT_EQ(parts.size(), 1);
410 EXPECT_EQ(parts[0], "bcdefg");
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");
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");
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!");
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], "");
445 template<template<class,class> class VectorType>
447 VectorType<StringPiece,std::allocator<StringPiece> > pieces;
448 VectorType<StringPiece,std::allocator<StringPiece> > pieces2;
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");
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");
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");
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");
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");
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");
498 folly::split("_", pieces[1], pieces2);
499 EXPECT_EQ(pieces2.size(), 2);
500 EXPECT_EQ(pieces2[0], "c");
501 EXPECT_EQ(pieces2[1], "d");
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");
513 string orig = "ab2342asdfv~~!";
514 folly::split("", orig.c_str(), pieces, true);
515 EXPECT_EQ(pieces.size(), 1);
516 EXPECT_EQ(pieces[0], orig);
519 folly::split("452x;o38asfsajsdlfdf.j", "asfds", pieces, true);
520 EXPECT_EQ(pieces.size(), 1);
521 EXPECT_EQ(pieces[0], "asfds");
524 folly::split("a", "", pieces, true);
525 EXPECT_EQ(pieces.size(), 0);
528 folly::split("a", "", pieces);
529 EXPECT_EQ(pieces.size(), 1);
530 EXPECT_EQ(pieces[0], "");
533 folly::split("a", "abcdefg", pieces, true);
534 EXPECT_EQ(pieces.size(), 1);
535 EXPECT_EQ(pieces[0], "bcdefg");
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");
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");
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!");
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], "");
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);
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");
587 VectorType<fbstring,std::allocator<fbstring> > blah;
588 folly::split('-', "a-b-c-d-f-e", blah);
589 EXPECT_EQ(blah.size(), 6);
594 TEST(Split, split_vector) {
595 splitTest<std::vector>();
597 TEST(Split, split_fbvector) {
598 splitTest<folly::fbvector>();
600 TEST(Split, pieces_vector) {
601 piecesTest<std::vector>();
603 TEST(Split, pieces_fbvector) {
604 piecesTest<folly::fbvector>();
607 //////////////////////////////////////////////////////////////////////
609 BENCHMARK(splitOnSingleChar, iters) {
610 const std::string line = "one:two:three:four";
611 for (int i = 0; i < iters << 4; ++i) {
612 std::vector<StringPiece> pieces;
613 folly::split(':', line, pieces);
617 BENCHMARK(splitStr, iters) {
618 const std::string line = "one-*-two-*-three-*-four";
619 for (int i = 0; i < iters << 4; ++i) {
620 std::vector<StringPiece> pieces;
621 folly::split("-*-", line, pieces);
625 BENCHMARK(boost_splitOnSingleChar, iters) {
626 std::string line = "one:two:three:four";
627 for (int i = 0; i < iters << 4; ++i) {
628 std::vector<boost::iterator_range<std::string::iterator>> pieces;
629 boost::split(pieces, line, [] (char c) { return c == ':'; });
633 int main(int argc, char *argv[]) {
634 testing::InitGoogleTest(&argc, argv);
635 google::ParseCommandLineFlags(&argc, &argv, true);
636 auto ret = RUN_ALL_TESTS();
639 if (FLAGS_benchmark) {
640 folly::runBenchmarks();