2 * Copyright 2016 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/Conv.h>
18 #include <folly/Foreach.h>
19 #include <boost/lexical_cast.hpp>
20 #include <gtest/gtest.h>
25 using namespace folly;
28 TEST(Conv, digits10) {
32 // first, some basic sniff tests
33 EXPECT_EQ( 1, digits10(0));
34 EXPECT_EQ( 1, digits10(1));
35 EXPECT_EQ( 1, digits10(9));
36 EXPECT_EQ( 2, digits10(10));
37 EXPECT_EQ( 2, digits10(99));
38 EXPECT_EQ( 3, digits10(100));
39 EXPECT_EQ( 3, digits10(999));
40 EXPECT_EQ( 4, digits10(1000));
41 EXPECT_EQ( 4, digits10(9999));
42 EXPECT_EQ(20, digits10(18446744073709551615ULL));
44 // try the first X nonnegatives.
45 // Covers some more cases of 2^p, 10^p
46 for (uint64_t i = 0; i < 100000; i++) {
47 snprintf(buffer, sizeof(buffer), "%" PRIu64, i);
48 EXPECT_EQ(strlen(buffer), digits10(i));
53 for (int p = 0; p < 64; p++) {
54 snprintf(buffer, sizeof(buffer), "%" PRIu64, power);
55 EXPECT_EQ(strlen(buffer), digits10(power));
56 snprintf(buffer, sizeof(buffer), "%" PRIu64, power - 1);
57 EXPECT_EQ(strlen(buffer), digits10(power - 1));
58 snprintf(buffer, sizeof(buffer), "%" PRIu64, power + 1);
59 EXPECT_EQ(strlen(buffer), digits10(power + 1));
65 for (int p = 0; p < 20; p++) {
66 snprintf(buffer, sizeof(buffer), "%" PRIu64, power);
67 EXPECT_EQ(strlen(buffer), digits10(power));
68 snprintf(buffer, sizeof(buffer), "%" PRIu64, power - 1);
69 EXPECT_EQ(strlen(buffer), digits10(power - 1));
70 snprintf(buffer, sizeof(buffer), "%" PRIu64, power + 1);
71 EXPECT_EQ(strlen(buffer), digits10(power + 1));
77 TEST(Conv, Type2Type) {
79 EXPECT_EQ(to<bool>(boolV), true);
82 EXPECT_EQ(to<int>(intV), 42);
85 EXPECT_EQ(to<float>(floatV), 4.2f);
87 double doubleV = 0.42;
88 EXPECT_EQ(to<double>(doubleV), 0.42);
90 std::string stringV = "StdString";
91 EXPECT_EQ(to<std::string>(stringV), "StdString");
93 folly::fbstring fbStrV = "FBString";
94 EXPECT_EQ(to<folly::fbstring>(fbStrV), "FBString");
96 folly::StringPiece spV("StringPiece");
97 EXPECT_EQ(to<folly::StringPiece>(spV), "StringPiece");
100 EXPECT_EQ(to<bool>(true), true);
101 EXPECT_EQ(to<int>(42), 42);
102 EXPECT_EQ(to<float>(4.2f), 4.2f);
103 EXPECT_EQ(to<double>(.42), .42);
104 EXPECT_EQ(to<std::string>(std::string("Hello")), "Hello");
105 EXPECT_EQ(to<folly::fbstring>(folly::fbstring("hello")), "hello");
106 EXPECT_EQ(to<folly::StringPiece>(folly::StringPiece("Forty Two")),
110 TEST(Conv, Integral2Integral) {
111 // Same size, different signs
112 int64_t s64 = numeric_limits<uint8_t>::max();
113 EXPECT_EQ(to<uint8_t>(s64), s64);
115 s64 = numeric_limits<int8_t>::max();
116 EXPECT_EQ(to<int8_t>(s64), s64);
119 TEST(Conv, Floating2Floating) {
121 double d1 = to<double>(f1);
125 auto f2 = to<float>(d2);
126 EXPECT_EQ(double(f2), d2);
128 double invalidFloat = std::numeric_limits<double>::max();
129 EXPECT_ANY_THROW(to<float>(invalidFloat));
130 invalidFloat = -std::numeric_limits<double>::max();
131 EXPECT_ANY_THROW(to<float>(invalidFloat));
134 auto shouldWork = to<float>(std::numeric_limits<double>::min());
135 // The value of `shouldWork' is an implementation defined choice
136 // between the following two alternatives.
137 EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
144 template <class String>
145 void testIntegral2String() {
148 template <class String, class Int, class... Ints>
149 void testIntegral2String() {
150 typedef typename make_unsigned<Int>::type Uint;
151 typedef typename make_signed<Int>::type Sint;
154 EXPECT_EQ(to<String>(value), "123");
156 EXPECT_EQ(to<String>(svalue), "123");
158 EXPECT_EQ(to<String>(svalue), "-123");
160 value = numeric_limits<Uint>::min();
161 EXPECT_EQ(to<Uint>(to<String>(value)), value);
162 value = numeric_limits<Uint>::max();
163 EXPECT_EQ(to<Uint>(to<String>(value)), value);
165 svalue = numeric_limits<Sint>::min();
166 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
167 value = numeric_limits<Sint>::max();
168 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
170 testIntegral2String<String, Ints...>();
173 #if FOLLY_HAVE_INT128_T
174 template <class String>
175 void test128Bit2String() {
176 typedef unsigned __int128 Uint;
177 typedef __int128 Sint;
179 EXPECT_EQ(detail::digitsEnough<unsigned __int128>(), 39);
182 EXPECT_EQ(to<String>(value), "123");
184 EXPECT_EQ(to<String>(svalue), "123");
186 EXPECT_EQ(to<String>(svalue), "-123");
188 value = __int128(1) << 64;
189 EXPECT_EQ(to<String>(value), "18446744073709551616");
191 svalue = -(__int128(1) << 64);
192 EXPECT_EQ(to<String>(svalue), "-18446744073709551616");
195 EXPECT_EQ(to<String>(value), "0");
198 EXPECT_EQ(to<String>(svalue), "0");
200 // TODO: the following do not compile to<__int128> ...
203 value = numeric_limits<Uint>::min();
204 EXPECT_EQ(to<Uint>(to<String>(value)), value);
205 value = numeric_limits<Uint>::max();
206 EXPECT_EQ(to<Uint>(to<String>(value)), value);
208 svalue = numeric_limits<Sint>::min();
209 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
210 value = numeric_limits<Sint>::max();
211 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
217 TEST(Conv, Integral2String) {
218 testIntegral2String<std::string, char, short, int, long>();
219 testIntegral2String<fbstring, char, short, int, long>();
221 #if FOLLY_HAVE_INT128_T
222 test128Bit2String<std::string>();
223 test128Bit2String<fbstring>();
227 template <class String>
228 void testString2Integral() {
231 template <class String, class Int, class... Ints>
232 void testString2Integral() {
233 typedef typename make_unsigned<Int>::type Uint;
234 typedef typename make_signed<Int>::type Sint;
236 // Unsigned numbers small enough to fit in a signed type
237 static const String strings[] = {
244 "0000000000000000000000000042"
246 static const Uint values[] = {
255 FOR_EACH_RANGE (i, 0, sizeof(strings) / sizeof(*strings)) {
256 EXPECT_EQ(to<Uint>(strings[i]), values[i]);
257 EXPECT_EQ(to<Sint>(strings[i]), values[i]);
260 // Unsigned numbers that won't fit in the signed variation
261 static const String uStrings[] = {
266 static const Uint uValues[] = {
271 FOR_EACH_RANGE (i, 0, sizeof(uStrings)/sizeof(*uStrings)) {
272 EXPECT_EQ(to<Uint>(uStrings[i]), uValues[i]);
273 if (sizeof(Int) == 1) {
274 EXPECT_THROW(to<Sint>(uStrings[i]), std::range_error);
278 if (sizeof(Int) >= 4) {
279 static const String strings2[] = {
285 static const Uint values2[] = {
291 FOR_EACH_RANGE (i, 0, sizeof(strings2)/sizeof(*strings2)) {
292 EXPECT_EQ(to<Uint>(strings2[i]), values2[i]);
293 EXPECT_EQ(to<Sint>(strings2[i]), values2[i]);
296 static const String uStrings2[] = {
302 static const Uint uValues2[] = {
308 FOR_EACH_RANGE (i, 0, sizeof(uStrings2)/sizeof(uStrings2)) {
309 EXPECT_EQ(to<Uint>(uStrings2[i]), uValues2[i]);
310 if (sizeof(Int) == 4) {
311 EXPECT_THROW(to<Sint>(uStrings2[i]), std::range_error);
316 if (sizeof(Int) >= 8) {
317 static_assert(sizeof(Int) <= 8, "Now that would be interesting");
318 static const String strings3[] = {
322 "100000000000000000",
323 "9223372036854775807",
325 static const Uint values3[] = {
328 (Uint)25687346509278435ULL,
329 (Uint)100000000000000000ULL,
330 (Uint)9223372036854775807ULL,
332 FOR_EACH_RANGE (i, 0, sizeof(strings3)/sizeof(*strings3)) {
333 EXPECT_EQ(to<Uint>(strings3[i]), values3[i]);
334 EXPECT_EQ(to<Sint>(strings3[i]), values3[i]);
337 static const String uStrings3[] = {
338 "9223372036854775808",
339 "9987435987394857987",
340 "17873648761234698740",
341 "18446744073709551615",
343 static const Uint uValues3[] = {
344 (Uint)9223372036854775808ULL,
345 (Uint)9987435987394857987ULL,
346 (Uint)17873648761234698740ULL,
347 (Uint)18446744073709551615ULL,
349 FOR_EACH_RANGE (i, 0, sizeof(uStrings3)/sizeof(*uStrings3)) {
350 EXPECT_EQ(to<Uint>(uStrings3[i]), uValues3[i]);
351 if (sizeof(Int) == 8) {
352 EXPECT_THROW(to<Sint>(uStrings3[i]), std::range_error);
357 // Minimum possible negative values, and negative sign overflow
358 static const String strings4[] = {
362 "-9223372036854775808",
364 static const String strings5[] = {
368 "-9223372036854775809",
370 static const Sint values4[] = {
374 (Sint)(-9223372036854775807LL - 1),
376 FOR_EACH_RANGE (i, 0, sizeof(strings4)/sizeof(*strings4)) {
377 if (sizeof(Int) > std::pow(2, i)) {
378 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
379 EXPECT_EQ(values4[i] - 1, to<Sint>(strings5[i]));
380 } else if (sizeof(Int) == std::pow(2, i)) {
381 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
382 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
384 EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
385 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
389 // Bogus string values
390 static const String bogusStrings[] = {
397 "- 42", // whitespace is not allowed between the sign and the value
399 "12345678901234567890123456789",
401 for (const auto& str : bogusStrings) {
402 EXPECT_THROW(to<Sint>(str), std::range_error);
403 EXPECT_THROW(to<Uint>(str), std::range_error);
406 // A leading '+' character is only allowed when converting to signed types.
407 String posSign("+42");
408 EXPECT_EQ(42, to<Sint>(posSign));
409 EXPECT_THROW(to<Uint>(posSign), std::range_error);
411 testString2Integral<String, Ints...>();
414 TEST(Conv, String2Integral) {
415 testString2Integral<const char*, signed char, short, int, long, long long>();
416 testString2Integral<std::string, signed char, short, int, long, long long>();
417 testString2Integral<fbstring, signed char, short, int, long, long long>();
419 // Testing the behavior of the StringPiece* API
420 // StringPiece* normally parses as much valid data as it can,
421 // and advances the StringPiece to the end of the valid data.
422 char buf1[] = "100foo";
423 StringPiece sp1(buf1);
424 EXPECT_EQ(100, to<uint8_t>(&sp1));
425 EXPECT_EQ(buf1 + 3, sp1.begin());
426 // However, if the next character would cause an overflow it throws a
427 // range_error rather than consuming only as much as it can without
429 char buf2[] = "1002";
430 StringPiece sp2(buf2);
431 EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
432 EXPECT_EQ(buf2, sp2.begin());
435 TEST(Conv, StringPiece2Integral) {
436 string s = " +123 hello world ";
438 EXPECT_EQ(to<int>(&sp), 123);
439 EXPECT_EQ(sp, " hello world ");
442 TEST(Conv, StringPieceAppend) {
445 StringPiece sp(s, 0, 3);
446 string result = to<string>(s, sp);
447 EXPECT_EQ(result, "foobarfoo");
450 StringPiece sp1(s, 0, 3);
451 StringPiece sp2(s, 3, 3);
452 string result = to<string>(sp1, sp2);
453 EXPECT_EQ(result, s);
457 TEST(Conv, BadStringToIntegral) {
458 // Note that leading spaces (e.g. " 1") are valid.
459 vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
461 EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
465 template <class String>
466 void testIdenticalTo() {
467 String s("Yukkuri shiteitte ne!!!");
469 String result = to<String>(s);
470 EXPECT_EQ(result, s);
473 template <class String>
474 void testVariadicTo() {
477 toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
478 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
481 EXPECT_TRUE(s.empty());
483 s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
484 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
487 template <class String>
488 void testIdenticalToDelim() {
489 String s("Yukkuri shiteitte ne!!!");
491 String charDelim = toDelim<String>('$', s);
492 EXPECT_EQ(charDelim, s);
494 String strDelim = toDelim<String>(String(">_<"), s);
495 EXPECT_EQ(strDelim, s);
498 template <class String>
499 void testVariadicToDelim() {
501 toAppendDelim(":", &s);
503 ":", "Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
504 EXPECT_EQ(s, "Lorem ipsum :1234: dolor amet :567.89:!");
506 s = toDelim<String>(':');
507 EXPECT_TRUE(s.empty());
510 ":", "Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
511 EXPECT_EQ(s, "Lorem ipsum ::1234: dolor amet :567.89:.");
514 TEST(Conv, NullString) {
515 string s1 = to<string>((char *) nullptr);
516 EXPECT_TRUE(s1.empty());
517 fbstring s2 = to<fbstring>((char *) nullptr);
518 EXPECT_TRUE(s2.empty());
521 TEST(Conv, VariadicTo) {
522 testIdenticalTo<string>();
523 testIdenticalTo<fbstring>();
524 testVariadicTo<string>();
525 testVariadicTo<fbstring>();
528 TEST(Conv, VariadicToDelim) {
529 testIdenticalToDelim<string>();
530 testIdenticalToDelim<fbstring>();
531 testVariadicToDelim<string>();
532 testVariadicToDelim<fbstring>();
535 template <class String>
536 void testDoubleToString() {
537 EXPECT_EQ(to<string>(0.0), "0");
538 EXPECT_EQ(to<string>(0.5), "0.5");
539 EXPECT_EQ(to<string>(10.25), "10.25");
540 EXPECT_EQ(to<string>(1.123e10), "11230000000");
543 TEST(Conv, DoubleToString) {
544 testDoubleToString<string>();
545 testDoubleToString<fbstring>();
548 TEST(Conv, FBStringToString) {
550 string ret = to<string>(foo);
551 EXPECT_EQ(ret, "foo");
552 string ret2 = to<string>(foo, 2);
553 EXPECT_EQ(ret2, "foo2");
556 TEST(Conv, StringPieceToDouble) {
557 string s = "2134123.125 zorro";
559 EXPECT_EQ(to<double>(&pc), 2134123.125);
560 EXPECT_EQ(pc, " zorro");
562 EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
563 EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
565 // Test NaN conversion
567 to<double>("not a number");
569 } catch (const std::range_error &) {
572 EXPECT_TRUE(std::isnan(to<double>("NaN")));
573 EXPECT_EQ(to<double>("inf"), numeric_limits<double>::infinity());
574 EXPECT_EQ(to<double>("infinity"), numeric_limits<double>::infinity());
575 EXPECT_THROW(to<double>("infinitX"), std::range_error);
576 EXPECT_EQ(to<double>("-inf"), -numeric_limits<double>::infinity());
577 EXPECT_EQ(to<double>("-infinity"), -numeric_limits<double>::infinity());
578 EXPECT_THROW(to<double>("-infinitX"), std::range_error);
581 TEST(Conv, EmptyStringToInt) {
588 } catch (const std::range_error &) {
592 TEST(Conv, CorruptedStringToInt) {
594 StringPiece pc(s.data(), s.data() + 1); // Only "-"
599 } catch (const std::range_error &) {
603 TEST(Conv, EmptyStringToDouble) {
610 } catch (const std::range_error &) {
614 TEST(Conv, IntToDouble) {
615 auto d = to<double>(42);
617 /* This seems not work in ubuntu11.10, gcc 4.6.1
619 auto f = to<float>(957837589847);
621 } catch (std::range_error& e) {
622 //LOG(INFO) << e.what();
627 TEST(Conv, DoubleToInt) {
628 auto i = to<int>(42.0);
631 auto i = to<int>(42.1);
632 LOG(ERROR) << "to<int> returned " << i << " instead of throwing";
634 } catch (std::range_error& e) {
635 //LOG(INFO) << e.what();
639 TEST(Conv, EnumToInt) {
640 enum A { x = 42, y = 420, z = 65 };
643 auto j = to<char>(x);
646 auto i = to<char>(y);
647 LOG(ERROR) << "to<char> returned "
648 << static_cast<unsigned int>(i)
649 << " instead of throwing";
651 } catch (std::range_error& e) {
652 //LOG(INFO) << e.what();
656 TEST(Conv, EnumToString) {
658 enum A { x = 4, y = 420, z = 65 };
659 EXPECT_EQ("foo.4", to<string>("foo.", x));
660 EXPECT_EQ("foo.420", to<string>("foo.", y));
661 EXPECT_EQ("foo.65", to<string>("foo.", z));
664 TEST(Conv, IntToEnum) {
665 enum A { x = 42, y = 420 };
671 auto i = to<A>(5000000000L);
672 LOG(ERROR) << "to<A> returned "
673 << static_cast<unsigned int>(i)
674 << " instead of throwing";
676 } catch (std::range_error& e) {
677 //LOG(INFO) << e.what();
681 TEST(Conv, UnsignedEnum) {
682 enum E : uint32_t { x = 3000000000U };
683 auto u = to<uint32_t>(x);
684 EXPECT_EQ(u, 3000000000U);
685 auto s = to<string>(x);
686 EXPECT_EQ("3000000000", s);
687 auto e = to<E>(3000000000U);
690 auto i = to<int32_t>(x);
691 LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
693 } catch (std::range_error& e) {
697 TEST(Conv, UnsignedEnumClass) {
698 enum class E : uint32_t { x = 3000000000U };
699 auto u = to<uint32_t>(E::x);
701 EXPECT_EQ(u, 3000000000U);
702 EXPECT_EQ("3000000000", to<string>(E::x));
703 EXPECT_EQ(E::x, to<E>(3000000000U));
704 EXPECT_EQ(E::x, to<E>("3000000000"));
706 parseTo("3000000000", e);
708 EXPECT_THROW(to<int32_t>(E::x), std::range_error);
711 // Multi-argument to<string> uses toAppend, a different code path than
713 TEST(Conv, EnumClassToString) {
714 enum class A { x = 4, y = 420, z = 65 };
715 EXPECT_EQ("foo.4", to<string>("foo.", A::x));
716 EXPECT_EQ("foo.420", to<string>("foo.", A::y));
717 EXPECT_EQ("foo.65", to<string>("foo.", A::z));
720 TEST(Conv, IntegralToBool) {
721 EXPECT_FALSE(to<bool>(0));
722 EXPECT_FALSE(to<bool>(0ul));
724 EXPECT_TRUE(to<bool>(1));
725 EXPECT_TRUE(to<bool>(1ul));
727 EXPECT_TRUE(to<bool>(-42));
728 EXPECT_TRUE(to<bool>(42ul));
731 template<typename Src>
732 void testStr2Bool() {
733 EXPECT_FALSE(to<bool>(Src("0")));
734 EXPECT_FALSE(to<bool>(Src(" 000 ")));
736 EXPECT_FALSE(to<bool>(Src("n")));
737 EXPECT_FALSE(to<bool>(Src("no")));
738 EXPECT_FALSE(to<bool>(Src("false")));
739 EXPECT_FALSE(to<bool>(Src("False")));
740 EXPECT_FALSE(to<bool>(Src(" fAlSe" )));
741 EXPECT_FALSE(to<bool>(Src("F")));
742 EXPECT_FALSE(to<bool>(Src("off")));
744 EXPECT_TRUE(to<bool>(Src("1")));
745 EXPECT_TRUE(to<bool>(Src(" 001 ")));
746 EXPECT_TRUE(to<bool>(Src("y")));
747 EXPECT_TRUE(to<bool>(Src("yes")));
748 EXPECT_TRUE(to<bool>(Src("\nyEs\t")));
749 EXPECT_TRUE(to<bool>(Src("true")));
750 EXPECT_TRUE(to<bool>(Src("True")));
751 EXPECT_TRUE(to<bool>(Src("T")));
752 EXPECT_TRUE(to<bool>(Src("on")));
754 EXPECT_THROW(to<bool>(Src("")), std::range_error);
755 EXPECT_THROW(to<bool>(Src("2")), std::range_error);
756 EXPECT_THROW(to<bool>(Src("11")), std::range_error);
757 EXPECT_THROW(to<bool>(Src("19")), std::range_error);
758 EXPECT_THROW(to<bool>(Src("o")), std::range_error);
759 EXPECT_THROW(to<bool>(Src("fal")), std::range_error);
760 EXPECT_THROW(to<bool>(Src("tru")), std::range_error);
761 EXPECT_THROW(to<bool>(Src("ye")), std::range_error);
762 EXPECT_THROW(to<bool>(Src("yes foo")), std::range_error);
763 EXPECT_THROW(to<bool>(Src("bar no")), std::range_error);
764 EXPECT_THROW(to<bool>(Src("one")), std::range_error);
765 EXPECT_THROW(to<bool>(Src("true_")), std::range_error);
766 EXPECT_THROW(to<bool>(Src("bogus_token_that_is_too_long")),
770 TEST(Conv, StringToBool) {
771 // testStr2Bool<const char *>();
772 testStr2Bool<std::string>();
774 // Test with strings that are not NUL terminated.
775 const char buf[] = "01234";
776 EXPECT_FALSE(to<bool>(StringPiece(buf, buf + 1))); // "0"
777 EXPECT_TRUE(to<bool>(StringPiece(buf + 1, buf + 2))); // "1"
778 const char buf2[] = "one two three";
779 EXPECT_TRUE(to<bool>(StringPiece(buf2, buf2 + 2))); // "on"
780 const char buf3[] = "false";
781 EXPECT_THROW(to<bool>(StringPiece(buf3, buf3 + 3)), // "fal"
784 // Test the StringPiece* API
785 const char buf4[] = "001foo";
786 StringPiece sp4(buf4);
787 EXPECT_TRUE(to<bool>(&sp4));
788 EXPECT_EQ(buf4 + 3, sp4.begin());
789 const char buf5[] = "0012";
790 StringPiece sp5(buf5);
791 EXPECT_THROW(to<bool>(&sp5), std::range_error);
792 EXPECT_EQ(buf5, sp5.begin());
795 TEST(Conv, FloatToInt) {
796 EXPECT_EQ(to<int>(42.0f), 42);
797 EXPECT_EQ(to<int8_t>(-128.0f), int8_t(-128));
798 EXPECT_THROW(to<int8_t>(-129.0), std::range_error);
799 EXPECT_THROW(to<int8_t>(127.001), std::range_error);
800 EXPECT_THROW(to<uint8_t>(-0.0001), std::range_error);
802 to<uint64_t>(static_cast<float>(std::numeric_limits<uint64_t>::max())),
806 TEST(Conv, IntToFloat) {
807 EXPECT_EQ(to<float>(42ULL), 42.0);
808 EXPECT_EQ(to<float>(int8_t(-128)), -128.0);
810 to<float>(std::numeric_limits<uint64_t>::max()), std::range_error);
812 to<float>(std::numeric_limits<int64_t>::max()), std::range_error);
814 to<float>(std::numeric_limits<int64_t>::min() + 1), std::range_error);
815 #if FOLLY_HAVE_INT128_T
817 to<double>(std::numeric_limits<unsigned __int128>::max()),
820 to<double>(std::numeric_limits<__int128>::max()), std::range_error);
822 to<double>(std::numeric_limits<__int128>::min() + 1), std::range_error);
826 TEST(Conv, NewUint64ToString) {
829 #define THE_GREAT_EXPECTATIONS(n, len) \
831 EXPECT_EQ((len), uint64ToBufferUnsafe((n), buf)); \
833 auto s = string(#n); \
834 s = s.substr(0, s.size() - 2); \
838 THE_GREAT_EXPECTATIONS(0UL, 1);
839 THE_GREAT_EXPECTATIONS(1UL, 1);
840 THE_GREAT_EXPECTATIONS(12UL, 2);
841 THE_GREAT_EXPECTATIONS(123UL, 3);
842 THE_GREAT_EXPECTATIONS(1234UL, 4);
843 THE_GREAT_EXPECTATIONS(12345UL, 5);
844 THE_GREAT_EXPECTATIONS(123456UL, 6);
845 THE_GREAT_EXPECTATIONS(1234567UL, 7);
846 THE_GREAT_EXPECTATIONS(12345678UL, 8);
847 THE_GREAT_EXPECTATIONS(123456789UL, 9);
848 THE_GREAT_EXPECTATIONS(1234567890UL, 10);
849 THE_GREAT_EXPECTATIONS(12345678901UL, 11);
850 THE_GREAT_EXPECTATIONS(123456789012UL, 12);
851 THE_GREAT_EXPECTATIONS(1234567890123UL, 13);
852 THE_GREAT_EXPECTATIONS(12345678901234UL, 14);
853 THE_GREAT_EXPECTATIONS(123456789012345UL, 15);
854 THE_GREAT_EXPECTATIONS(1234567890123456UL, 16);
855 THE_GREAT_EXPECTATIONS(12345678901234567UL, 17);
856 THE_GREAT_EXPECTATIONS(123456789012345678UL, 18);
857 THE_GREAT_EXPECTATIONS(1234567890123456789UL, 19);
858 THE_GREAT_EXPECTATIONS(18446744073709551614UL, 20);
859 THE_GREAT_EXPECTATIONS(18446744073709551615UL, 20);
861 #undef THE_GREAT_EXPECTATIONS
864 TEST(Conv, allocate_size) {
865 std::string str1 = "meh meh meh";
866 std::string str2 = "zdech zdech zdech";
868 auto res1 = folly::to<std::string>(str1, ".", str2);
869 EXPECT_EQ(res1, str1 + "." + str2);
871 std::string res2; //empty
872 toAppendFit(str1, str2, 1, &res2);
873 EXPECT_EQ(res2, str1 + str2 + "1");
876 toAppendDelimFit(",", str1, str2, &res3);
877 EXPECT_EQ(res3, str1 + "," + str2);
883 std::tuple<const int&, const int&> tuple_view() const {
886 bool operator==(const Dimensions& other) const {
887 return this->tuple_view() == other.tuple_view();
891 void parseTo(folly::StringPiece in, Dimensions& out) {
892 out.w = folly::to<int>(&in);
893 in.removePrefix("x");
894 out.h = folly::to<int>(&in);
897 template <class String>
898 void toAppend(const Dimensions& in, String* result) {
899 folly::toAppend(in.w, 'x', in.h, result);
902 size_t estimateSpaceNeeded(const Dimensions&in) {
903 return 2000 + folly::estimateSpaceNeeded(in.w) +
904 folly::estimateSpaceNeeded(in.h);
908 TEST(Conv, custom_kkproviders) {
909 my::Dimensions expected{7, 8};
910 EXPECT_EQ(expected, folly::to<my::Dimensions>("7x8"));
911 auto str = folly::to<std::string>(expected);
912 EXPECT_EQ("7x8", str);
913 // make sure above implementation of estimateSpaceNeeded() is used.
914 EXPECT_GT(str.capacity(), 2000);
915 EXPECT_LT(str.capacity(), 2500);