2 * Copyright 2015 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/Benchmark.h>
18 #include <folly/Conv.h>
19 #include <folly/Foreach.h>
20 #include <boost/lexical_cast.hpp>
21 #include <gtest/gtest.h>
26 using namespace folly;
30 TEST(Conv, digits10Minimal) {
31 // Not much of a test (and it's included in the test below anyway).
32 // I just want to inspect the generated assembly for this function.
33 folly::doNotOptimizeAway(digits10(random() * random()));
36 TEST(Conv, digits10) {
40 // first, some basic sniff tests
41 EXPECT_EQ( 1, digits10(0));
42 EXPECT_EQ( 1, digits10(1));
43 EXPECT_EQ( 1, digits10(9));
44 EXPECT_EQ( 2, digits10(10));
45 EXPECT_EQ( 2, digits10(99));
46 EXPECT_EQ( 3, digits10(100));
47 EXPECT_EQ( 3, digits10(999));
48 EXPECT_EQ( 4, digits10(1000));
49 EXPECT_EQ( 4, digits10(9999));
50 EXPECT_EQ(20, digits10(18446744073709551615ULL));
52 // try the first X nonnegatives.
53 // Covers some more cases of 2^p, 10^p
54 for (uint64_t i = 0; i < 100000; i++) {
55 snprintf(buffer, sizeof(buffer), "%lu", i);
56 EXPECT_EQ(strlen(buffer), digits10(i));
61 for (int p = 0; p < 64; p++) {
62 snprintf(buffer, sizeof(buffer), "%lu", power);
63 EXPECT_EQ(strlen(buffer), digits10(power));
64 snprintf(buffer, sizeof(buffer), "%lu", power - 1);
65 EXPECT_EQ(strlen(buffer), digits10(power - 1));
66 snprintf(buffer, sizeof(buffer), "%lu", power + 1);
67 EXPECT_EQ(strlen(buffer), digits10(power + 1));
73 for (int p = 0; p < 20; p++) {
74 snprintf(buffer, sizeof(buffer), "%lu", power);
75 EXPECT_EQ(strlen(buffer), digits10(power));
76 snprintf(buffer, sizeof(buffer), "%lu", power - 1);
77 EXPECT_EQ(strlen(buffer), digits10(power - 1));
78 snprintf(buffer, sizeof(buffer), "%lu", power + 1);
79 EXPECT_EQ(strlen(buffer), digits10(power + 1));
85 TEST(Conv, Type2Type) {
87 EXPECT_EQ(to<bool>(boolV), true);
90 EXPECT_EQ(to<int>(intV), 42);
93 EXPECT_EQ(to<float>(floatV), 4.2f);
95 double doubleV = 0.42;
96 EXPECT_EQ(to<double>(doubleV), 0.42);
98 std::string stringV = "StdString";
99 EXPECT_EQ(to<std::string>(stringV), "StdString");
101 folly::fbstring fbStrV = "FBString";
102 EXPECT_EQ(to<folly::fbstring>(fbStrV), "FBString");
104 folly::StringPiece spV("StringPiece");
105 EXPECT_EQ(to<folly::StringPiece>(spV), "StringPiece");
108 EXPECT_EQ(to<bool>(true), true);
109 EXPECT_EQ(to<int>(42), 42);
110 EXPECT_EQ(to<float>(4.2f), 4.2f);
111 EXPECT_EQ(to<double>(.42), .42);
112 EXPECT_EQ(to<std::string>(std::string("Hello")), "Hello");
113 EXPECT_EQ(to<folly::fbstring>(folly::fbstring("hello")), "hello");
114 EXPECT_EQ(to<folly::StringPiece>(folly::StringPiece("Forty Two")),
118 TEST(Conv, Integral2Integral) {
119 // Same size, different signs
120 int64_t s64 = numeric_limits<uint8_t>::max();
121 EXPECT_EQ(to<uint8_t>(s64), s64);
123 s64 = numeric_limits<int8_t>::max();
124 EXPECT_EQ(to<int8_t>(s64), s64);
127 TEST(Conv, Floating2Floating) {
129 double d1 = to<double>(f1);
133 auto f2 = to<float>(d2);
134 EXPECT_EQ(double(f2), d2);
136 double invalidFloat = std::numeric_limits<double>::max();
137 EXPECT_ANY_THROW(to<float>(invalidFloat));
138 invalidFloat = -std::numeric_limits<double>::max();
139 EXPECT_ANY_THROW(to<float>(invalidFloat));
142 auto shouldWork = to<float>(std::numeric_limits<double>::min());
143 // The value of `shouldWork' is an implementation defined choice
144 // between the following two alternatives.
145 EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
152 template <class String>
153 void testIntegral2String() {
156 template <class String, class Int, class... Ints>
157 void testIntegral2String() {
158 typedef typename make_unsigned<Int>::type Uint;
159 typedef typename make_signed<Int>::type Sint;
162 EXPECT_EQ(to<String>(value), "123");
164 EXPECT_EQ(to<String>(svalue), "123");
166 EXPECT_EQ(to<String>(svalue), "-123");
168 value = numeric_limits<Uint>::min();
169 EXPECT_EQ(to<Uint>(to<String>(value)), value);
170 value = numeric_limits<Uint>::max();
171 EXPECT_EQ(to<Uint>(to<String>(value)), value);
173 svalue = numeric_limits<Sint>::min();
174 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
175 value = numeric_limits<Sint>::max();
176 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
178 testIntegral2String<String, Ints...>();
181 #if FOLLY_HAVE_INT128_T
182 template <class String>
183 void test128Bit2String() {
184 typedef unsigned __int128 Uint;
185 typedef __int128 Sint;
187 EXPECT_EQ(detail::digitsEnough<unsigned __int128>(), 39);
190 EXPECT_EQ(to<String>(value), "123");
192 EXPECT_EQ(to<String>(svalue), "123");
194 EXPECT_EQ(to<String>(svalue), "-123");
196 value = __int128(1) << 64;
197 EXPECT_EQ(to<String>(value), "18446744073709551616");
199 svalue = -(__int128(1) << 64);
200 EXPECT_EQ(to<String>(svalue), "-18446744073709551616");
203 EXPECT_EQ(to<String>(value), "0");
206 EXPECT_EQ(to<String>(svalue), "0");
208 // TODO: the following do not compile to<__int128> ...
211 value = numeric_limits<Uint>::min();
212 EXPECT_EQ(to<Uint>(to<String>(value)), value);
213 value = numeric_limits<Uint>::max();
214 EXPECT_EQ(to<Uint>(to<String>(value)), value);
216 svalue = numeric_limits<Sint>::min();
217 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
218 value = numeric_limits<Sint>::max();
219 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
225 TEST(Conv, Integral2String) {
226 testIntegral2String<std::string, char, short, int, long>();
227 testIntegral2String<fbstring, char, short, int, long>();
229 #if FOLLY_HAVE_INT128_T
230 test128Bit2String<std::string>();
231 test128Bit2String<fbstring>();
235 template <class String>
236 void testString2Integral() {
239 template <class String, class Int, class... Ints>
240 void testString2Integral() {
241 typedef typename make_unsigned<Int>::type Uint;
242 typedef typename make_signed<Int>::type Sint;
244 // Unsigned numbers small enough to fit in a signed type
245 static const String strings[] = {
252 "0000000000000000000000000042"
254 static const Uint values[] = {
263 FOR_EACH_RANGE (i, 0, sizeof(strings) / sizeof(*strings)) {
264 EXPECT_EQ(to<Uint>(strings[i]), values[i]);
265 EXPECT_EQ(to<Sint>(strings[i]), values[i]);
268 // Unsigned numbers that won't fit in the signed variation
269 static const String uStrings[] = {
274 static const Uint uValues[] = {
279 FOR_EACH_RANGE (i, 0, sizeof(uStrings)/sizeof(*uStrings)) {
280 EXPECT_EQ(to<Uint>(uStrings[i]), uValues[i]);
281 if (sizeof(Int) == 1) {
282 EXPECT_THROW(to<Sint>(uStrings[i]), std::range_error);
286 if (sizeof(Int) >= 4) {
287 static const String strings2[] = {
293 static const Uint values2[] = {
299 FOR_EACH_RANGE (i, 0, sizeof(strings2)/sizeof(*strings2)) {
300 EXPECT_EQ(to<Uint>(strings2[i]), values2[i]);
301 EXPECT_EQ(to<Sint>(strings2[i]), values2[i]);
304 static const String uStrings2[] = {
310 static const Uint uValues2[] = {
316 FOR_EACH_RANGE (i, 0, sizeof(uStrings2)/sizeof(uStrings2)) {
317 EXPECT_EQ(to<Uint>(uStrings2[i]), uValues2[i]);
318 if (sizeof(Int) == 4) {
319 EXPECT_THROW(to<Sint>(uStrings2[i]), std::range_error);
324 if (sizeof(Int) >= 8) {
325 static_assert(sizeof(Int) <= 8, "Now that would be interesting");
326 static const String strings3[] = {
330 "100000000000000000",
331 "9223372036854775807",
333 static const Uint values3[] = {
336 (Uint)25687346509278435ULL,
337 (Uint)100000000000000000ULL,
338 (Uint)9223372036854775807ULL,
340 FOR_EACH_RANGE (i, 0, sizeof(strings3)/sizeof(*strings3)) {
341 EXPECT_EQ(to<Uint>(strings3[i]), values3[i]);
342 EXPECT_EQ(to<Sint>(strings3[i]), values3[i]);
345 static const String uStrings3[] = {
346 "9223372036854775808",
347 "9987435987394857987",
348 "17873648761234698740",
349 "18446744073709551615",
351 static const Uint uValues3[] = {
352 (Uint)9223372036854775808ULL,
353 (Uint)9987435987394857987ULL,
354 (Uint)17873648761234698740ULL,
355 (Uint)18446744073709551615ULL,
357 FOR_EACH_RANGE (i, 0, sizeof(uStrings3)/sizeof(*uStrings3)) {
358 EXPECT_EQ(to<Uint>(uStrings3[i]), uValues3[i]);
359 if (sizeof(Int) == 8) {
360 EXPECT_THROW(to<Sint>(uStrings3[i]), std::range_error);
365 // Minimum possible negative values, and negative sign overflow
366 static const String strings4[] = {
370 "-9223372036854775808",
372 static const String strings5[] = {
376 "-9223372036854775809",
378 static const Sint values4[] = {
382 (Sint)(-9223372036854775807LL - 1),
384 FOR_EACH_RANGE (i, 0, sizeof(strings4)/sizeof(*strings4)) {
385 if (sizeof(Int) > std::pow(2, i)) {
386 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
387 EXPECT_EQ(values4[i] - 1, to<Sint>(strings5[i]));
388 } else if (sizeof(Int) == std::pow(2, i)) {
389 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
390 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
392 EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
393 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
397 // Bogus string values
398 static const String bogusStrings[] = {
405 "- 42", // whitespace is not allowed between the sign and the value
407 "12345678901234567890123456789",
409 for (const auto& str : bogusStrings) {
410 EXPECT_THROW(to<Sint>(str), std::range_error);
411 EXPECT_THROW(to<Uint>(str), std::range_error);
414 // A leading '+' character is only allowed when converting to signed types.
415 String posSign("+42");
416 EXPECT_EQ(42, to<Sint>(posSign));
417 EXPECT_THROW(to<Uint>(posSign), std::range_error);
419 testString2Integral<String, Ints...>();
422 TEST(Conv, String2Integral) {
423 testString2Integral<const char*, signed char, short, int, long, long long>();
424 testString2Integral<std::string, signed char, short, int, long, long long>();
425 testString2Integral<fbstring, signed char, short, int, long, long long>();
427 // Testing the behavior of the StringPiece* API
428 // StringPiece* normally parses as much valid data as it can,
429 // and advances the StringPiece to the end of the valid data.
430 char buf1[] = "100foo";
431 StringPiece sp1(buf1);
432 EXPECT_EQ(100, to<uint8_t>(&sp1));
433 EXPECT_EQ(buf1 + 3, sp1.begin());
434 // However, if the next character would cause an overflow it throws a
435 // range_error rather than consuming only as much as it can without
437 char buf2[] = "1002";
438 StringPiece sp2(buf2);
439 EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
440 EXPECT_EQ(buf2, sp2.begin());
443 TEST(Conv, StringPiece2Integral) {
444 string s = " +123 hello world ";
446 EXPECT_EQ(to<int>(&sp), 123);
447 EXPECT_EQ(sp, " hello world ");
450 TEST(Conv, StringPieceAppend) {
453 StringPiece sp(s, 0, 3);
454 string result = to<string>(s, sp);
455 EXPECT_EQ(result, "foobarfoo");
458 StringPiece sp1(s, 0, 3);
459 StringPiece sp2(s, 3, 3);
460 string result = to<string>(sp1, sp2);
461 EXPECT_EQ(result, s);
465 TEST(Conv, BadStringToIntegral) {
466 // Note that leading spaces (e.g. " 1") are valid.
467 vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
469 EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
473 template <class String>
474 void testIdenticalTo() {
475 String s("Yukkuri shiteitte ne!!!");
477 String result = to<String>(s);
478 EXPECT_EQ(result, s);
481 template <class String>
482 void testVariadicTo() {
485 toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
486 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
489 EXPECT_TRUE(s.empty());
491 s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
492 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
495 template <class String>
496 void testIdenticalToDelim() {
497 String s("Yukkuri shiteitte ne!!!");
499 String charDelim = toDelim<String>('$', s);
500 EXPECT_EQ(charDelim, s);
502 String strDelim = toDelim<String>(String(">_<"), s);
503 EXPECT_EQ(strDelim, s);
506 template <class String>
507 void testVariadicToDelim() {
509 toAppendDelim(":", &s);
511 ":", "Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
512 EXPECT_EQ(s, "Lorem ipsum :1234: dolor amet :567.89:!");
514 s = toDelim<String>(':');
515 EXPECT_TRUE(s.empty());
518 ":", "Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
519 EXPECT_EQ(s, "Lorem ipsum ::1234: dolor amet :567.89:.");
522 TEST(Conv, NullString) {
523 string s1 = to<string>((char *) nullptr);
524 EXPECT_TRUE(s1.empty());
525 fbstring s2 = to<fbstring>((char *) nullptr);
526 EXPECT_TRUE(s2.empty());
529 TEST(Conv, VariadicTo) {
530 testIdenticalTo<string>();
531 testIdenticalTo<fbstring>();
532 testVariadicTo<string>();
533 testVariadicTo<fbstring>();
536 TEST(Conv, VariadicToDelim) {
537 testIdenticalToDelim<string>();
538 testIdenticalToDelim<fbstring>();
539 testVariadicToDelim<string>();
540 testVariadicToDelim<fbstring>();
543 template <class String>
544 void testDoubleToString() {
545 EXPECT_EQ(to<string>(0.0), "0");
546 EXPECT_EQ(to<string>(0.5), "0.5");
547 EXPECT_EQ(to<string>(10.25), "10.25");
548 EXPECT_EQ(to<string>(1.123e10), "11230000000");
551 TEST(Conv, DoubleToString) {
552 testDoubleToString<string>();
553 testDoubleToString<fbstring>();
556 TEST(Conv, FBStringToString) {
558 string ret = to<string>(foo);
559 EXPECT_EQ(ret, "foo");
560 string ret2 = to<string>(foo, 2);
561 EXPECT_EQ(ret2, "foo2");
564 TEST(Conv, StringPieceToDouble) {
565 string s = "2134123.125 zorro";
567 EXPECT_EQ(to<double>(&pc), 2134123.125);
568 EXPECT_EQ(pc, " zorro");
570 EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
571 EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
573 // Test NaN conversion
575 to<double>("not a number");
577 } catch (const std::range_error &) {
580 EXPECT_TRUE(std::isnan(to<double>("NaN")));
581 EXPECT_EQ(to<double>("inf"), numeric_limits<double>::infinity());
582 EXPECT_EQ(to<double>("infinity"), numeric_limits<double>::infinity());
583 EXPECT_THROW(to<double>("infinitX"), std::range_error);
584 EXPECT_EQ(to<double>("-inf"), -numeric_limits<double>::infinity());
585 EXPECT_EQ(to<double>("-infinity"), -numeric_limits<double>::infinity());
586 EXPECT_THROW(to<double>("-infinitX"), std::range_error);
589 TEST(Conv, EmptyStringToInt) {
596 } catch (const std::range_error &) {
600 TEST(Conv, CorruptedStringToInt) {
602 StringPiece pc(s.data(), s.data() + 1); // Only "-"
607 } catch (const std::range_error &) {
611 TEST(Conv, EmptyStringToDouble) {
618 } catch (const std::range_error &) {
622 TEST(Conv, IntToDouble) {
623 auto d = to<double>(42);
625 /* This seems not work in ubuntu11.10, gcc 4.6.1
627 auto f = to<float>(957837589847);
629 } catch (std::range_error& e) {
630 //LOG(INFO) << e.what();
635 TEST(Conv, DoubleToInt) {
636 auto i = to<int>(42.0);
639 auto i = to<int>(42.1);
640 LOG(ERROR) << "to<int> returned " << i << " instead of throwing";
642 } catch (std::range_error& e) {
643 //LOG(INFO) << e.what();
647 TEST(Conv, EnumToInt) {
648 enum A { x = 42, y = 420, z = 65 };
651 auto j = to<char>(x);
654 auto i = to<char>(y);
655 LOG(ERROR) << "to<char> returned "
656 << static_cast<unsigned int>(i)
657 << " instead of throwing";
659 } catch (std::range_error& e) {
660 //LOG(INFO) << e.what();
664 TEST(Conv, EnumToString) {
666 enum A { x = 4, y = 420, z = 65 };
667 EXPECT_EQ("foo.4", to<string>("foo.", x));
668 EXPECT_EQ("foo.420", to<string>("foo.", y));
669 EXPECT_EQ("foo.65", to<string>("foo.", z));
672 TEST(Conv, IntToEnum) {
673 enum A { x = 42, y = 420 };
679 auto i = to<A>(5000000000L);
680 LOG(ERROR) << "to<A> returned "
681 << static_cast<unsigned int>(i)
682 << " instead of throwing";
684 } catch (std::range_error& e) {
685 //LOG(INFO) << e.what();
689 TEST(Conv, UnsignedEnum) {
690 enum E : uint32_t { x = 3000000000U };
691 auto u = to<uint32_t>(x);
692 EXPECT_EQ(u, 3000000000U);
693 auto s = to<string>(x);
694 EXPECT_EQ("3000000000", s);
695 auto e = to<E>(3000000000U);
698 auto i = to<int32_t>(x);
699 LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
701 } catch (std::range_error& e) {
705 TEST(Conv, UnsignedEnumClass) {
706 enum class E : uint32_t { x = 3000000000U };
707 auto u = to<uint32_t>(E::x);
709 EXPECT_EQ(u, 3000000000U);
710 auto s = to<string>(E::x);
711 EXPECT_EQ("3000000000", s);
712 auto e = to<E>(3000000000U);
715 auto i = to<int32_t>(E::x);
716 LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
718 } catch (std::range_error& e) {
722 // Multi-argument to<string> uses toAppend, a different code path than
724 TEST(Conv, EnumClassToString) {
725 enum class A { x = 4, y = 420, z = 65 };
726 EXPECT_EQ("foo.4", to<string>("foo.", A::x));
727 EXPECT_EQ("foo.420", to<string>("foo.", A::y));
728 EXPECT_EQ("foo.65", to<string>("foo.", A::z));
731 TEST(Conv, IntegralToBool) {
732 EXPECT_FALSE(to<bool>(0));
733 EXPECT_FALSE(to<bool>(0ul));
735 EXPECT_TRUE(to<bool>(1));
736 EXPECT_TRUE(to<bool>(1ul));
738 EXPECT_TRUE(to<bool>(-42));
739 EXPECT_TRUE(to<bool>(42ul));
742 template<typename Src>
743 void testStr2Bool() {
744 EXPECT_FALSE(to<bool>(Src("0")));
745 EXPECT_FALSE(to<bool>(Src(" 000 ")));
747 EXPECT_FALSE(to<bool>(Src("n")));
748 EXPECT_FALSE(to<bool>(Src("no")));
749 EXPECT_FALSE(to<bool>(Src("false")));
750 EXPECT_FALSE(to<bool>(Src("False")));
751 EXPECT_FALSE(to<bool>(Src(" fAlSe" )));
752 EXPECT_FALSE(to<bool>(Src("F")));
753 EXPECT_FALSE(to<bool>(Src("off")));
755 EXPECT_TRUE(to<bool>(Src("1")));
756 EXPECT_TRUE(to<bool>(Src(" 001 ")));
757 EXPECT_TRUE(to<bool>(Src("y")));
758 EXPECT_TRUE(to<bool>(Src("yes")));
759 EXPECT_TRUE(to<bool>(Src("\nyEs\t")));
760 EXPECT_TRUE(to<bool>(Src("true")));
761 EXPECT_TRUE(to<bool>(Src("True")));
762 EXPECT_TRUE(to<bool>(Src("T")));
763 EXPECT_TRUE(to<bool>(Src("on")));
765 EXPECT_THROW(to<bool>(Src("")), std::range_error);
766 EXPECT_THROW(to<bool>(Src("2")), std::range_error);
767 EXPECT_THROW(to<bool>(Src("11")), std::range_error);
768 EXPECT_THROW(to<bool>(Src("19")), std::range_error);
769 EXPECT_THROW(to<bool>(Src("o")), std::range_error);
770 EXPECT_THROW(to<bool>(Src("fal")), std::range_error);
771 EXPECT_THROW(to<bool>(Src("tru")), std::range_error);
772 EXPECT_THROW(to<bool>(Src("ye")), std::range_error);
773 EXPECT_THROW(to<bool>(Src("yes foo")), std::range_error);
774 EXPECT_THROW(to<bool>(Src("bar no")), std::range_error);
775 EXPECT_THROW(to<bool>(Src("one")), std::range_error);
776 EXPECT_THROW(to<bool>(Src("true_")), std::range_error);
777 EXPECT_THROW(to<bool>(Src("bogus_token_that_is_too_long")),
781 TEST(Conv, StringToBool) {
782 // testStr2Bool<const char *>();
783 testStr2Bool<std::string>();
785 // Test with strings that are not NUL terminated.
786 const char buf[] = "01234";
787 EXPECT_FALSE(to<bool>(StringPiece(buf, buf + 1))); // "0"
788 EXPECT_TRUE(to<bool>(StringPiece(buf + 1, buf + 2))); // "1"
789 const char buf2[] = "one two three";
790 EXPECT_TRUE(to<bool>(StringPiece(buf2, buf2 + 2))); // "on"
791 const char buf3[] = "false";
792 EXPECT_THROW(to<bool>(StringPiece(buf3, buf3 + 3)), // "fal"
795 // Test the StringPiece* API
796 const char buf4[] = "001foo";
797 StringPiece sp4(buf4);
798 EXPECT_TRUE(to<bool>(&sp4));
799 EXPECT_EQ(buf4 + 3, sp4.begin());
800 const char buf5[] = "0012";
801 StringPiece sp5(buf5);
802 EXPECT_THROW(to<bool>(&sp5), std::range_error);
803 EXPECT_EQ(buf5, sp5.begin());
806 TEST(Conv, NewUint64ToString) {
809 #define THE_GREAT_EXPECTATIONS(n, len) \
811 EXPECT_EQ((len), uint64ToBufferUnsafe((n), buf)); \
813 auto s = string(#n); \
814 s = s.substr(0, s.size() - 2); \
818 THE_GREAT_EXPECTATIONS(0UL, 1);
819 THE_GREAT_EXPECTATIONS(1UL, 1);
820 THE_GREAT_EXPECTATIONS(12UL, 2);
821 THE_GREAT_EXPECTATIONS(123UL, 3);
822 THE_GREAT_EXPECTATIONS(1234UL, 4);
823 THE_GREAT_EXPECTATIONS(12345UL, 5);
824 THE_GREAT_EXPECTATIONS(123456UL, 6);
825 THE_GREAT_EXPECTATIONS(1234567UL, 7);
826 THE_GREAT_EXPECTATIONS(12345678UL, 8);
827 THE_GREAT_EXPECTATIONS(123456789UL, 9);
828 THE_GREAT_EXPECTATIONS(1234567890UL, 10);
829 THE_GREAT_EXPECTATIONS(12345678901UL, 11);
830 THE_GREAT_EXPECTATIONS(123456789012UL, 12);
831 THE_GREAT_EXPECTATIONS(1234567890123UL, 13);
832 THE_GREAT_EXPECTATIONS(12345678901234UL, 14);
833 THE_GREAT_EXPECTATIONS(123456789012345UL, 15);
834 THE_GREAT_EXPECTATIONS(1234567890123456UL, 16);
835 THE_GREAT_EXPECTATIONS(12345678901234567UL, 17);
836 THE_GREAT_EXPECTATIONS(123456789012345678UL, 18);
837 THE_GREAT_EXPECTATIONS(1234567890123456789UL, 19);
838 THE_GREAT_EXPECTATIONS(18446744073709551614UL, 20);
839 THE_GREAT_EXPECTATIONS(18446744073709551615UL, 20);
841 #undef THE_GREAT_EXPECTATIONS
844 TEST(Conv, allocate_size) {
845 std::string str1 = "meh meh meh";
846 std::string str2 = "zdech zdech zdech";
848 auto res1 = folly::to<std::string>(str1, ".", str2);
849 EXPECT_EQ(res1, str1 + "." + str2);
851 std::string res2; //empty
852 toAppendFit(str1, str2, 1, &res2);
853 EXPECT_EQ(res2, str1 + str2 + "1");
856 toAppendDelimFit(",", str1, str2, &res3);
857 EXPECT_EQ(res3, str1 + "," + str2);
860 ////////////////////////////////////////////////////////////////////////////////
861 // Benchmarks for ASCII to int conversion
862 ////////////////////////////////////////////////////////////////////////////////
863 // @author: Rajat Goel (rajat)
865 static int64_t handwrittenAtoi(const char* start, const char* end) {
867 bool positive = true;
871 throw std::runtime_error("empty string");
874 while (start < end && isspace(*start)) {
886 while (start < end && *start >= '0' && *start <= '9') {
887 auto const newRetVal = retVal * 10 + (*start++ - '0');
888 if (newRetVal < retVal) {
889 throw std::runtime_error("overflow");
895 throw std::runtime_error("extra chars at the end");
898 return positive ? retVal : -retVal;
901 static StringPiece pc1 = "1234567890123456789";
903 void handwrittenAtoiMeasure(unsigned int n, unsigned int digits) {
904 auto p = pc1.subpiece(pc1.size() - digits, digits);
905 FOR_EACH_RANGE (i, 0, n) {
906 doNotOptimizeAway(handwrittenAtoi(p.begin(), p.end()));
910 void follyAtoiMeasure(unsigned int n, unsigned int digits) {
911 auto p = pc1.subpiece(pc1.size() - digits, digits);
912 FOR_EACH_RANGE (i, 0, n) {
913 doNotOptimizeAway(folly::to<int64_t>(p.begin(), p.end()));
917 void clibAtoiMeasure(unsigned int n, unsigned int digits) {
918 auto p = pc1.subpiece(pc1.size() - digits, digits);
919 assert(*p.end() == 0);
920 static_assert(sizeof(long) == 8, "64-bit long assumed");
921 FOR_EACH_RANGE (i, 0, n) {
922 doNotOptimizeAway(atol(p.begin()));
926 void clibStrtoulMeasure(unsigned int n, unsigned int digits) {
927 auto p = pc1.subpiece(pc1.size() - digits, digits);
928 assert(*p.end() == 0);
930 FOR_EACH_RANGE (i, 0, n) {
931 doNotOptimizeAway(strtoul(p.begin(), &endptr, 10));
935 void lexicalCastMeasure(unsigned int n, unsigned int digits) {
936 auto p = pc1.subpiece(pc1.size() - digits, digits);
937 assert(*p.end() == 0);
938 FOR_EACH_RANGE (i, 0, n) {
939 doNotOptimizeAway(boost::lexical_cast<uint64_t>(p.begin()));
943 // Benchmarks for unsigned to string conversion, raw
945 unsigned u64ToAsciiTable(uint64_t value, char* dst) {
946 static const char digits[201] =
947 "00010203040506070809"
948 "10111213141516171819"
949 "20212223242526272829"
950 "30313233343536373839"
951 "40414243444546474849"
952 "50515253545556575859"
953 "60616263646566676869"
954 "70717273747576777879"
955 "80818283848586878889"
956 "90919293949596979899";
958 uint32_t const length = digits10(value);
959 uint32_t next = length - 1;
960 while (value >= 100) {
961 auto const i = (value % 100) * 2;
963 dst[next] = digits[i + 1];
964 dst[next - 1] = digits[i];
967 // Handle last 1-2 digits
969 dst[next] = '0' + uint32_t(value);
971 auto i = uint32_t(value) * 2;
972 dst[next] = digits[i + 1];
973 dst[next - 1] = digits[i];
978 void u64ToAsciiTableBM(unsigned int n, uint64_t value) {
979 // This is too fast, need to do 10 times per iteration
981 FOR_EACH_RANGE (i, 0, n) {
982 doNotOptimizeAway(u64ToAsciiTable(value + n, buf));
986 unsigned u64ToAsciiClassic(uint64_t value, char* dst) {
988 char* next = (char*)dst;
991 *next++ = '0' + (value % 10);
993 } while (value != 0);
994 unsigned length = next - start;
998 while (next > start) {
1008 void u64ToAsciiClassicBM(unsigned int n, uint64_t value) {
1009 // This is too fast, need to do 10 times per iteration
1011 FOR_EACH_RANGE (i, 0, n) {
1012 doNotOptimizeAway(u64ToAsciiClassic(value + n, buf));
1016 void u64ToAsciiFollyBM(unsigned int n, uint64_t value) {
1017 // This is too fast, need to do 10 times per iteration
1019 FOR_EACH_RANGE (i, 0, n) {
1020 doNotOptimizeAway(uint64ToBufferUnsafe(value + n, buf));
1024 // Benchmark uitoa with string append
1026 void u2aAppendClassicBM(unsigned int n, uint64_t value) {
1028 FOR_EACH_RANGE (i, 0, n) {
1029 // auto buf = &s.back() + 1;
1031 s.append(buffer, u64ToAsciiClassic(value, buffer));
1032 doNotOptimizeAway(s.size());
1036 void u2aAppendFollyBM(unsigned int n, uint64_t value) {
1038 FOR_EACH_RANGE (i, 0, n) {
1039 // auto buf = &s.back() + 1;
1041 s.append(buffer, uint64ToBufferUnsafe(value, buffer));
1042 doNotOptimizeAway(s.size());
1046 template <class String>
1047 struct StringIdenticalToBM {
1048 StringIdenticalToBM() {}
1049 void operator()(unsigned int n, size_t len) const {
1051 BENCHMARK_SUSPEND { s.append(len, '0'); }
1052 FOR_EACH_RANGE (i, 0, n) {
1053 String result = to<String>(s);
1054 doNotOptimizeAway(result.size());
1059 template <class String>
1060 struct StringVariadicToBM {
1061 StringVariadicToBM() {}
1062 void operator()(unsigned int n, size_t len) const {
1064 BENCHMARK_SUSPEND { s.append(len, '0'); }
1065 FOR_EACH_RANGE (i, 0, n) {
1066 String result = to<String>(s, nullptr);
1067 doNotOptimizeAway(result.size());
1072 static size_t bigInt = 11424545345345;
1073 static size_t smallInt = 104;
1074 static char someString[] = "this is some nice string";
1075 static char otherString[] = "this is a long string, so it's not so nice";
1076 static char reallyShort[] = "meh";
1077 static std::string stdString = "std::strings are very nice";
1078 static float fValue = 1.2355;
1079 static double dValue = 345345345.435;
1081 BENCHMARK(preallocateTestNoFloat, n) {
1082 for (size_t i = 0; i < n; ++i) {
1083 auto val1 = to<std::string>(bigInt, someString, stdString, otherString);
1084 auto val3 = to<std::string>(reallyShort, smallInt);
1085 auto val2 = to<std::string>(bigInt, stdString);
1086 auto val4 = to<std::string>(bigInt, stdString, dValue, otherString);
1087 auto val5 = to<std::string>(bigInt, someString, reallyShort);
1091 BENCHMARK(preallocateTestFloat, n) {
1092 for (size_t i = 0; i < n; ++i) {
1093 auto val1 = to<std::string>(stdString, ',', fValue, dValue);
1094 auto val2 = to<std::string>(stdString, ',', dValue);
1097 BENCHMARK_DRAW_LINE();
1099 static const StringIdenticalToBM<std::string> stringIdenticalToBM;
1100 static const StringVariadicToBM<std::string> stringVariadicToBM;
1101 static const StringIdenticalToBM<fbstring> fbstringIdenticalToBM;
1102 static const StringVariadicToBM<fbstring> fbstringVariadicToBM;
1104 #define DEFINE_BENCHMARK_GROUP(n) \
1105 BENCHMARK_PARAM(u64ToAsciiClassicBM, n); \
1106 BENCHMARK_RELATIVE_PARAM(u64ToAsciiTableBM, n); \
1107 BENCHMARK_RELATIVE_PARAM(u64ToAsciiFollyBM, n); \
1108 BENCHMARK_DRAW_LINE();
1110 DEFINE_BENCHMARK_GROUP(1);
1111 DEFINE_BENCHMARK_GROUP(12);
1112 DEFINE_BENCHMARK_GROUP(123);
1113 DEFINE_BENCHMARK_GROUP(1234);
1114 DEFINE_BENCHMARK_GROUP(12345);
1115 DEFINE_BENCHMARK_GROUP(123456);
1116 DEFINE_BENCHMARK_GROUP(1234567);
1117 DEFINE_BENCHMARK_GROUP(12345678);
1118 DEFINE_BENCHMARK_GROUP(123456789);
1119 DEFINE_BENCHMARK_GROUP(1234567890);
1120 DEFINE_BENCHMARK_GROUP(12345678901);
1121 DEFINE_BENCHMARK_GROUP(123456789012);
1122 DEFINE_BENCHMARK_GROUP(1234567890123);
1123 DEFINE_BENCHMARK_GROUP(12345678901234);
1124 DEFINE_BENCHMARK_GROUP(123456789012345);
1125 DEFINE_BENCHMARK_GROUP(1234567890123456);
1126 DEFINE_BENCHMARK_GROUP(12345678901234567);
1127 DEFINE_BENCHMARK_GROUP(123456789012345678);
1128 DEFINE_BENCHMARK_GROUP(1234567890123456789);
1129 DEFINE_BENCHMARK_GROUP(12345678901234567890U);
1131 #undef DEFINE_BENCHMARK_GROUP
1133 #define DEFINE_BENCHMARK_GROUP(n) \
1134 BENCHMARK_PARAM(clibAtoiMeasure, n); \
1135 BENCHMARK_RELATIVE_PARAM(lexicalCastMeasure, n); \
1136 BENCHMARK_RELATIVE_PARAM(handwrittenAtoiMeasure, n); \
1137 BENCHMARK_RELATIVE_PARAM(follyAtoiMeasure, n); \
1138 BENCHMARK_DRAW_LINE();
1140 DEFINE_BENCHMARK_GROUP(1);
1141 DEFINE_BENCHMARK_GROUP(2);
1142 DEFINE_BENCHMARK_GROUP(3);
1143 DEFINE_BENCHMARK_GROUP(4);
1144 DEFINE_BENCHMARK_GROUP(5);
1145 DEFINE_BENCHMARK_GROUP(6);
1146 DEFINE_BENCHMARK_GROUP(7);
1147 DEFINE_BENCHMARK_GROUP(8);
1148 DEFINE_BENCHMARK_GROUP(9);
1149 DEFINE_BENCHMARK_GROUP(10);
1150 DEFINE_BENCHMARK_GROUP(11);
1151 DEFINE_BENCHMARK_GROUP(12);
1152 DEFINE_BENCHMARK_GROUP(13);
1153 DEFINE_BENCHMARK_GROUP(14);
1154 DEFINE_BENCHMARK_GROUP(15);
1155 DEFINE_BENCHMARK_GROUP(16);
1156 DEFINE_BENCHMARK_GROUP(17);
1157 DEFINE_BENCHMARK_GROUP(18);
1158 DEFINE_BENCHMARK_GROUP(19);
1160 #undef DEFINE_BENCHMARK_GROUP
1162 #define DEFINE_BENCHMARK_GROUP(T, n) \
1163 BENCHMARK_PARAM(T ## VariadicToBM, n); \
1164 BENCHMARK_RELATIVE_PARAM(T ## IdenticalToBM, n); \
1165 BENCHMARK_DRAW_LINE();
1167 DEFINE_BENCHMARK_GROUP(string, 32);
1168 DEFINE_BENCHMARK_GROUP(string, 1024);
1169 DEFINE_BENCHMARK_GROUP(string, 32768);
1170 DEFINE_BENCHMARK_GROUP(fbstring, 32);
1171 DEFINE_BENCHMARK_GROUP(fbstring, 1024);
1172 DEFINE_BENCHMARK_GROUP(fbstring, 32768);
1174 #undef DEFINE_BENCHMARK_GROUP
1176 int main(int argc, char** argv) {
1177 testing::InitGoogleTest(&argc, argv);
1178 gflags::ParseCommandLineFlags(&argc, &argv, true);
1179 auto ret = RUN_ALL_TESTS();
1180 if (!ret && FLAGS_benchmark) {
1181 folly::runBenchmarks();