17 #ifndef __STDC_FORMAT_MACROS 18 #define __STDC_FORMAT_MACROS 1 21 #include <boost/lexical_cast.hpp> 35 using namespace folly;
56 snprintf(buffer,
sizeof(buffer),
"%" PRIu64,
i);
62 for (
int p = 0; p < 64; p++) {
63 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power);
65 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power - 1);
67 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power + 1);
74 for (
int p = 0; p < 20; p++) {
75 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power);
77 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power - 1);
79 snprintf(buffer,
sizeof(buffer),
"%" PRIu64, power + 1);
96 double doubleV = 0.42;
100 EXPECT_EQ(to<std::string>(stringV),
"StdString");
103 EXPECT_EQ(to<folly::fbstring>(fbStrV),
"FBString");
106 EXPECT_EQ(to<folly::StringPiece>(spV),
"StringPiece");
119 TEST(Conv, Integral2Integral) {
128 TEST(Conv, Floating2Floating) {
130 double d1 = to<double>(f1);
134 auto f2 = to<float>(d2);
153 template <
class String>
156 template <
class String,
class Int,
class... Ints>
169 EXPECT_EQ(to<Uint>(to<String>(value)), value);
171 EXPECT_EQ(to<Uint>(to<String>(value)), value);
174 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
176 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
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);
196 value = __int128(1) << 64;
197 EXPECT_EQ(to<String>(value),
"18446744073709551616");
199 svalue = -(__int128(1) << 64);
200 EXPECT_EQ(to<String>(svalue),
"-18446744073709551616");
208 value = ~__int128(0);
209 EXPECT_EQ(to<String>(value),
"340282366920938463463374607431768211455");
211 svalue = -(Uint(1) << 127);
212 EXPECT_EQ(to<String>(svalue),
"-170141183460469231731687303715884105728");
214 svalue = (Uint(1) << 127) - 1;
215 EXPECT_EQ(to<String>(svalue),
"170141183460469231731687303715884105727");
221 EXPECT_EQ(to<Uint>(to<String>(value)), value);
223 EXPECT_EQ(to<Uint>(to<String>(value)), value);
226 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
228 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
235 testIntegral2String<std::string, int8_t, int16_t, int32_t, int64_t>();
236 testIntegral2String<fbstring, int8_t, int16_t, int32_t, int64_t>();
238 #if FOLLY_HAVE_INT128_T 239 test128Bit2String<std::string>();
240 test128Bit2String<fbstring>();
244 template <
class String>
247 template <
class String,
class Int,
class... Ints>
253 static const String
strings[] = {
260 "0000000000000000000000000042",
262 static const Uint
values[] = {
273 EXPECT_EQ(to<Sint>(strings[i]), values[i]);
277 static const String uStrings[] = {
282 static const Uint uValues[] = {
289 if (
sizeof(Int) == 1) {
294 if (
sizeof(Int) >= 4) {
295 static const String strings2[] = {
301 static const Uint values2[] = {
309 EXPECT_EQ(to<Sint>(strings2[i]), values2[i]);
312 static const String uStrings2[] = {
318 static const Uint uValues2[] = {
325 EXPECT_EQ(to<Uint>(uStrings2[
i]), uValues2[i]);
326 if (
sizeof(Int) == 4) {
332 if (
sizeof(Int) >= 8) {
333 static_assert(
sizeof(Int) <= 8,
"Now that would be interesting");
334 static const String strings3[] = {
338 "100000000000000000",
339 "9223372036854775807",
341 static const Uint values3[] = {
344 (Uint)25687346509278435ULL,
345 (Uint)100000000000000000ULL,
346 (Uint)9223372036854775807ULL,
350 EXPECT_EQ(to<Sint>(strings3[i]), values3[i]);
353 static const String uStrings3[] = {
354 "9223372036854775808",
355 "9987435987394857987",
356 "17873648761234698740",
357 "18446744073709551615",
359 static const Uint uValues3[] = {
360 (Uint)9223372036854775808ULL,
361 (Uint)9987435987394857987ULL,
362 (Uint)17873648761234698740ULL,
363 (Uint)18446744073709551615ULL,
366 EXPECT_EQ(to<Uint>(uStrings3[
i]), uValues3[i]);
367 if (
sizeof(Int) == 8) {
374 static const String strings4[] = {
378 "-9223372036854775808",
380 static const String strings5[] = {
384 "-9223372036854775809",
386 static const Sint values4[] = {
390 (Sint)(-9223372036854775807LL - 1),
393 if (
sizeof(Int) > std::pow(2,
i)) {
395 EXPECT_EQ(values4[i] - 1, to<Sint>(strings5[i]));
396 }
else if (
sizeof(Int) == std::pow(2,
i)) {
406 static const String bogusStrings[] = {
415 "12345678901234567890123456789",
417 for (
const auto& str : bogusStrings) {
423 String posSign(
"+42");
431 testString2Integral<const char*, int8_t, int16_t, int32_t, int64_t>();
432 testString2Integral<std::string, int8_t, int16_t, int32_t, int64_t>();
433 testString2Integral<fbstring, int8_t, int16_t, int32_t, int64_t>();
438 char buf1[] =
"100foo";
445 char buf2[] =
"1002";
451 TEST(Conv, StringPiece2Integral) {
452 string s =
" +123 hello world ";
458 TEST(Conv, StringPieceAppend) {
462 string result = to<string>(
s, sp);
468 string result = to<string>(sp1, sp2);
473 TEST(Conv, BadStringToIntegral) {
475 vector<string>
v = {
"a",
"",
" ",
"\n",
" a0",
"abcdef",
"1Z",
"!#"};
481 template <
class String>
483 String
s(
"Yukkuri shiteitte ne!!!");
485 String result = to<String>(
s);
489 template <
class String>
493 toAppend(
"Lorem ipsum ", 1234, String(
" dolor amet "), 567.89,
'!', &s);
494 EXPECT_EQ(s,
"Lorem ipsum 1234 dolor amet 567.89!");
499 s = to<String>(
"Lorem ipsum ",
nullptr, 1234,
" dolor amet ", 567.89,
'.');
500 EXPECT_EQ(s,
"Lorem ipsum 1234 dolor amet 567.89.");
503 template <
class String>
505 String
s(
"Yukkuri shiteitte ne!!!");
507 String charDelim = toDelim<String>(
'$',
s);
510 String strDelim = toDelim<String>(String(
">_<"),
s);
514 template <
class String>
517 toAppendDelim(
":", &s);
519 ":",
"Lorem ipsum ", 1234, String(
" dolor amet "), 567.89,
'!', &s);
520 EXPECT_EQ(s,
"Lorem ipsum :1234: dolor amet :567.89:!");
522 s = toDelim<String>(
':');
526 ":",
"Lorem ipsum ",
nullptr, 1234,
" dolor amet ", 567.89,
'.');
527 EXPECT_EQ(s,
"Lorem ipsum ::1234: dolor amet :567.89:.");
531 string s1 = to<string>((
char*)
nullptr);
533 fbstring s2 = to<fbstring>((
char*)
nullptr);
538 testIdenticalTo<string>();
539 testIdenticalTo<fbstring>();
540 testVariadicTo<string>();
541 testVariadicTo<fbstring>();
545 testIdenticalToDelim<string>();
546 testIdenticalToDelim<fbstring>();
547 testVariadicToDelim<string>();
548 testVariadicToDelim<fbstring>();
551 template <
class String>
556 EXPECT_EQ(to<string>(1.123e10),
"11230000000");
560 testDoubleToString<string>();
561 testDoubleToString<fbstring>();
566 string ret = to<string>(
foo);
568 string ret2 = to<string>(
foo, 2);
572 TEST(Conv, StringPieceToDouble) {
573 vector<tuple<const char*, const char*, double>> strs{
574 make_tuple(
"2134123.125 zorro",
" zorro", 2134123.125),
575 make_tuple(
" 2134123.125 zorro",
" zorro", 2134123.125),
576 make_tuple(
" 2134123.125 zorro",
" zorro", 2134123.125),
577 make_tuple(
" 2134123.125 zorro ",
" zorro ", 2134123.125),
578 make_tuple(
"2134123.125zorro",
"zorro", 2134123.125),
600 for (
const auto&
s : strs) {
602 EXPECT_EQ(get<2>(
s), to<double>(&pc)) <<
"\"" << get<0>(
s) <<
"\"";
610 to<double>(
"not a number");
612 }
catch (
const std::range_error&) {
622 EXPECT_EQ(to<double>(
"inf"), numeric_limits<double>::infinity());
623 EXPECT_EQ(to<double>(
"Inf"), numeric_limits<double>::infinity());
624 EXPECT_EQ(to<double>(
"INF"), numeric_limits<double>::infinity());
625 EXPECT_EQ(to<double>(
"inF"), numeric_limits<double>::infinity());
626 EXPECT_EQ(to<double>(
"infinity"), numeric_limits<double>::infinity());
627 EXPECT_EQ(to<double>(
"Infinity"), numeric_limits<double>::infinity());
628 EXPECT_EQ(to<double>(
"INFINITY"), numeric_limits<double>::infinity());
629 EXPECT_EQ(to<double>(
"iNfInItY"), numeric_limits<double>::infinity());
631 EXPECT_EQ(to<double>(
"-inf"), -numeric_limits<double>::infinity());
632 EXPECT_EQ(to<double>(
"-Inf"), -numeric_limits<double>::infinity());
633 EXPECT_EQ(to<double>(
"-INF"), -numeric_limits<double>::infinity());
634 EXPECT_EQ(to<double>(
"-inF"), -numeric_limits<double>::infinity());
635 EXPECT_EQ(to<double>(
"-infinity"), -numeric_limits<double>::infinity());
636 EXPECT_EQ(to<double>(
"-Infinity"), -numeric_limits<double>::infinity());
637 EXPECT_EQ(to<double>(
"-INFINITY"), -numeric_limits<double>::infinity());
638 EXPECT_EQ(to<double>(
"-iNfInItY"), -numeric_limits<double>::infinity());
639 EXPECT_THROW(to<double>(
"-infinitX"), std::range_error);
649 }
catch (
const std::range_error&) {
653 TEST(Conv, CorruptedStringToInt) {
660 }
catch (
const std::range_error&) {
664 TEST(Conv, EmptyStringToDouble) {
671 }
catch (
const std::range_error&) {
676 auto d = to<double>(42);
689 auto i = to<int>(42.0);
692 auto i2 = to<int>(42.1);
693 LOG(ERROR) <<
"to<int> returned " << i2 <<
" instead of throwing";
695 }
catch (std::range_error&) {
701 enum A {
x = 42,
y = 420,
z = 65 };
704 auto j = to<char>(
x);
707 auto i2 = to<char>(
y);
708 LOG(ERROR) <<
"to<char> returned " <<
static_cast<unsigned int>(i2)
709 <<
" instead of throwing";
711 }
catch (std::range_error&) {
718 enum A {
x = 4,
y = 420,
z = 65 };
725 enum A {
x = 42,
y = 420 };
731 auto i2 = to<A>(5000000000L);
732 LOG(ERROR) <<
"to<A> returned " <<
static_cast<unsigned int>(i2)
733 <<
" instead of throwing";
735 }
catch (std::range_error&) {
742 auto u = to<uint32_t>(
x);
744 auto s = to<string>(
x);
746 auto e = to<E>(3000000000U);
749 auto i = to<int32_t>(
x);
750 LOG(ERROR) <<
"to<int32_t> returned " <<
i <<
" instead of throwing";
752 }
catch (std::range_error&) {
756 TEST(Conv, UnsignedEnumClass) {
758 auto u = to<uint32_t>(
E::x);
772 TEST(Conv, EnumClassToString) {
773 enum class A {
x = 4,
y = 420,
z = 65 };
775 EXPECT_EQ(
"foo.420", to<string>(
"foo.", A::y));
776 EXPECT_EQ(
"foo.65", to<string>(
"foo.", A::z));
790 template <
typename Src>
821 EXPECT_THROW(to<bool>(Src(
"yes foo")), std::range_error);
822 EXPECT_THROW(to<bool>(Src(
"bar no")), std::range_error);
825 EXPECT_THROW(to<bool>(Src(
"bogus_token_that_is_too_long")), std::range_error);
830 testStr2Bool<std::string>();
833 const char buf[] =
"01234";
836 const char buf2[] =
"one two three";
838 const char buf3[] =
"false";
844 const char buf4[] =
"001foo";
848 const char buf5[] =
"0012";
855 const std::vector<int64_t> in{1, 2, 3};
856 std::vector<std::string> out(in.size());
857 std::transform(in.begin(), in.end(), out.begin(), to<std::string, int64_t>);
858 const std::vector<std::string> ref{
"1",
"2",
"3"};
882 #if FOLLY_HAVE_INT128_T 904 EXPECT_EQ(to<bool>(std::numeric_limits<double>::lowest()),
true);
905 EXPECT_EQ(to<bool>(std::numeric_limits<double>::quiet_NaN()),
true);
906 EXPECT_EQ(to<bool>(std::numeric_limits<double>::infinity()),
true);
907 EXPECT_EQ(to<bool>(-std::numeric_limits<double>::infinity()),
true);
912 template <
typename F>
920 std::string where = to<std::string>(__FILE__,
"(", line,
"): ");
923 ADD_FAILURE() << where << exprStr <<
" -> " << res;
927 EXPECT_FALSE(str.empty()) << where << exprStr <<
" -> " << str;
928 auto pos = str.find(
':');
930 std::ostringstream exp;
931 exp << str.substr(0, pos) +
": ";
933 exp <<
"\"" << value <<
"\"";
937 EXPECT_EQ(exp.str(), str) << where << exprStr <<
" -> " << str;
939 EXPECT_EQ(pos, std::string::npos) << where << exprStr <<
" -> " << str;
945 #define EXPECT_CONV_ERROR_QUOTE(expr, code, value, quoted) \ 947 [&] { return expr; }, \ 949 ConversionCode::code, \ 954 #define EXPECT_CONV_ERROR(expr, code, value) \ 955 EXPECT_CONV_ERROR_QUOTE(expr, code, value, true) 957 #define EXPECT_CONV_ERROR_STR(type, str, code) \ 958 EXPECT_CONV_ERROR(to<type>(str), code, str) 960 #define EXPECT_CONV_ERROR_STR_NOVAL(type, str, code) \ 961 EXPECT_CONV_ERROR(to<type>(str), code, nullptr) 963 TEST(Conv, ConversionErrorStrToBool) {
973 TEST(Conv, ConversionErrorStrToFloat) {
981 TEST(Conv, ConversionErrorStrToInt) {
1003 #define EXPECT_CONV_ERROR_PP_VAL(type, str, code, val) \ 1005 StringPiece input(str); \ 1006 EXPECT_CONV_ERROR(to<type>(input.begin(), input.end()), code, val); \ 1009 #define EXPECT_CONV_ERROR_PP(type, str, code) \ 1010 EXPECT_CONV_ERROR_PP_VAL(type, str, code, str) 1012 TEST(Conv, ConversionErrorPtrPairToInt) {
1032 template <
typename T,
typename V>
1034 std::ostringstream oss;
1035 #ifdef FOLLY_HAS_RTTI 1036 oss <<
"(" <<
demangle(
typeid(
T)) <<
") ";
1038 oss << to<std::string>(
value);
1043 #define EXPECT_CONV_ERROR_ARITH(type, val, code) \ 1044 EXPECT_CONV_ERROR_QUOTE( \ 1045 to<type>(val), code, prefixWithType<type>(val).c_str(), false) 1047 TEST(Conv, ConversionErrorIntToInt) {
1052 TEST(Conv, ConversionErrorFloatToFloat) {
1059 TEST(Conv, ConversionErrorIntToFloat) {
1064 TEST(Conv, ConversionErrorFloatToInt) {
1069 auto rv1 = folly::tryTo<bool>(
"xxxx");
1071 auto rv2 = folly::tryTo<bool>(
"false");
1074 auto rv3 = folly::tryTo<bool>(
"yes");
1080 auto rv1 = folly::tryTo<int>(
"1000000000000000000000000000000");
1082 auto rv2 = folly::tryTo<int>(
"4711");
1088 enum class A {
x = 42,
y = 420,
z = 65 };
1089 auto rv1 = folly::tryTo<A>(
"1000000000000000000000000000000");
1091 auto rv2 = folly::tryTo<A>(
"42");
1094 auto rv3 = folly::tryTo<A>(
"50");
1096 EXPECT_EQ(static_cast<A>(50), rv3.value());
1100 auto rv1 = folly::tryTo<float>(
"");
1102 auto rv2 = folly::tryTo<float>(
"3.14");
1108 auto rv1 = folly::tryTo<double>(
"");
1110 auto rv2 = folly::tryTo<double>(
"3.14");
1116 auto rv1 = folly::tryTo<uint8_t>(256);
1118 auto rv2 = folly::tryTo<uint8_t>(255);
1124 auto rv1 = folly::tryTo<float>(1e100);
1126 auto rv2 = folly::tryTo<double>(25.5f);
1132 auto rv1 = folly::tryTo<int>(100.001);
1134 auto rv2 = folly::tryTo<int>(100.0);
1142 auto rv2 = folly::tryTo<float>(1000ULL);
1148 StringPiece sp1(
"1000000000000000000000000000000");
1149 auto rv1 = folly::tryTo<int>(sp1.
begin(), sp1.
end());
1152 auto rv2 = folly::tryTo<int>(sp2.begin(), sp2.end());
1156 auto rv3 = folly::tryTo<int>(sp3.begin(), sp3.end());
1160 auto rv4 = folly::tryTo<uint16_t>(sp4.begin(), sp4.end());
1168 #define THE_GREAT_EXPECTATIONS(n, len) \ 1170 EXPECT_EQ((len), uint64ToBufferUnsafe((n), buf)); \ 1172 auto s = string(#n); \ 1173 s = s.substr(0, s.size() - 2); \ 1174 EXPECT_EQ(s, buf); \ 1200 #undef THE_GREAT_EXPECTATIONS 1207 auto res1 = folly::to<std::string>(str1,
".", str2);
1211 toAppendFit(str1, str2, 1, &res2);
1215 toAppendDelimFit(
",", str1, str2, &res3);
1226 return this->tuple_view() == other.
tuple_view();
1238 template <
class String>
1252 if (in ==
"SmallEnum") {
1259 template <
class String>
1267 EXPECT_EQ(expected, folly::to<my::Dimensions>(
"7x8"));
1268 auto str = folly::to<std::string>(expected);
1287 auto x = tryTo<int>(
"42").then([](
int) {});
1293 TEST(conv, TryIntToUnscopedEnumAndBack) {
1298 EXPECT_EQ(UnscopedEnum::Second, folly::tryTo<UnscopedEnum>(1).
value());
1302 TEST(conv, TryIntToScopedEnumAndBack) {
1303 enum class ScopedEnum {
1307 EXPECT_EQ(ScopedEnum::Second, folly::tryTo<ScopedEnum>(1).
value());
#define EXPECT_ANY_THROW(statement)
std::vector< uint8_t > buffer(kBufferSize+16)
void testVariadicToDelim()
#define EXPECT_THROW(statement, expected_exception)
bool operator==(const Dimensions &other) const
#define EXPECT_EQ(val1, val2)
uint32_t digits10(uint64_t v)
void testIdenticalToDelim()
constexpr std::enable_if< std::is_same< T, char >::value, size_t >::type estimateSpaceNeeded(T)
size_t estimateSpaceNeeded(const Dimensions &in)
—— Concurrent Priority Queue Implementation ——
void toAppend(SmallEnum, String *result)
std::tuple< const int &, const int & > tuple_view() const
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
#define EXPECT_CONV_ERROR(expr, code, value)
#define EXPECT_CONV_ERROR_STR_NOVAL(type, str, code)
ConversionCode errorCode() const
#define FOR_EACH_RANGE(i, begin, end)
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
constexpr Iter data() const
void testString2Integral()
static const char *const value
bool removePrefix(const const_range_type &prefix)
void toAppend(char value, Tgt *result)
void testDoubleToString()
#define EXPECT_CONV_ERROR_PP(type, str, code)
constexpr Iter end() const
TEST(Conv, timespecToStdChrono)
#define EXPECT_NEAR(val1, val2, abs_error)
constexpr Iter begin() const
#define EXPECT_TRUE(condition)
#define EXPECT_CONV_ERROR_ARITH(type, val, code)
#define EXPECT_FALSE(condition)
#define THE_GREAT_EXPECTATIONS(n, len)
Range< const char * > StringPiece
#define EXPECT_LT(val1, val2)
void testIntegral2String()
Expected< StringPiece, ConversionCode > parseTo(StringPiece in, SmallEnum &out)
static vector< fbstring > strings
#define EXPECT_CONV_ERROR_STR(type, str, code)
fbstring demangle(const char *name)
std::vector< int > values(1'000)
#define EXPECT_GT(val1, val2)