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/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;
37 TEST(Conv, Integral2Integral) {
38 // Same size, different signs
39 s64 = numeric_limits<uint8_t>::max();
40 EXPECT_EQ(to<uint8_t>(s64), s64);
42 s64 = numeric_limits<int8_t>::max();
43 EXPECT_EQ(to<int8_t>(s64), s64);
46 TEST(Conv, Floating2Floating) {
48 double d1 = to<double>(f1);
52 auto f2 = to<float>(d2);
53 EXPECT_EQ(double(f2), d2);
55 double invalidFloat = std::numeric_limits<double>::max();
56 EXPECT_ANY_THROW(to<float>(invalidFloat));
57 invalidFloat = -std::numeric_limits<double>::max();
58 EXPECT_ANY_THROW(to<float>(invalidFloat));
61 auto shouldWork = to<float>(std::numeric_limits<double>::min());
62 // The value of `shouldWork' is an implementation defined choice
63 // between the following two alternatives.
64 EXPECT_TRUE(shouldWork == std::numeric_limits<float>::min() ||
71 template <class String>
72 void testIntegral2String() {
75 template <class String, class Int, class... Ints>
76 void testIntegral2String() {
77 typedef typename make_unsigned<Int>::type Uint;
78 typedef typename make_signed<Int>::type Sint;
81 EXPECT_EQ(to<String>(value), "123");
83 EXPECT_EQ(to<String>(svalue), "123");
85 EXPECT_EQ(to<String>(svalue), "-123");
87 value = numeric_limits<Uint>::min();
88 EXPECT_EQ(to<Uint>(to<String>(value)), value);
89 value = numeric_limits<Uint>::max();
90 EXPECT_EQ(to<Uint>(to<String>(value)), value);
92 svalue = numeric_limits<Sint>::min();
93 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
94 value = numeric_limits<Sint>::max();
95 EXPECT_EQ(to<Sint>(to<String>(svalue)), svalue);
97 testIntegral2String<String, Ints...>();
100 TEST(Conv, Integral2String) {
101 testIntegral2String<std::string, char, short, int, long>();
102 testIntegral2String<fbstring, char, short, int, long>();
105 template <class String>
106 void testString2Integral() {
109 template <class String, class Int, class... Ints>
110 void testString2Integral() {
111 typedef typename make_unsigned<Int>::type Uint;
112 typedef typename make_signed<Int>::type Sint;
114 // Unsigned numbers small enough to fit in a signed type
115 static const String strings[] = {
122 "0000000000000000000000000042"
124 static const Uint values[] = {
133 FOR_EACH_RANGE (i, 0, sizeof(strings) / sizeof(*strings)) {
134 EXPECT_EQ(to<Uint>(strings[i]), values[i]);
135 EXPECT_EQ(to<Sint>(strings[i]), values[i]);
138 // Unsigned numbers that won't fit in the signed variation
139 static const String uStrings[] = {
144 static const Uint uValues[] = {
149 FOR_EACH_RANGE (i, 0, sizeof(uStrings)/sizeof(*uStrings)) {
150 EXPECT_EQ(to<Uint>(uStrings[i]), uValues[i]);
151 if (sizeof(Int) == 1) {
152 EXPECT_THROW(to<Sint>(uStrings[i]), std::range_error);
156 if (sizeof(Int) >= 4) {
157 static const String strings2[] = {
163 static const Uint values2[] = {
169 FOR_EACH_RANGE (i, 0, sizeof(strings2)/sizeof(*strings2)) {
170 EXPECT_EQ(to<Uint>(strings2[i]), values2[i]);
171 EXPECT_EQ(to<Sint>(strings2[i]), values2[i]);
174 static const String uStrings2[] = {
180 static const Uint uValues2[] = {
186 FOR_EACH_RANGE (i, 0, sizeof(uStrings2)/sizeof(uStrings2)) {
187 EXPECT_EQ(to<Uint>(uStrings2[i]), uValues2[i]);
188 if (sizeof(Int) == 4) {
189 EXPECT_THROW(to<Sint>(uStrings2[i]), std::range_error);
194 if (sizeof(Int) >= 8) {
195 static_assert(sizeof(Int) <= 8, "Now that would be interesting");
196 static const String strings3[] = {
200 "100000000000000000",
201 "9223372036854775807",
203 static const Uint values3[] = {
206 (Uint)25687346509278435ULL,
207 (Uint)100000000000000000ULL,
208 (Uint)9223372036854775807ULL,
210 FOR_EACH_RANGE (i, 0, sizeof(strings3)/sizeof(*strings3)) {
211 EXPECT_EQ(to<Uint>(strings3[i]), values3[i]);
212 EXPECT_EQ(to<Sint>(strings3[i]), values3[i]);
215 static const String uStrings3[] = {
216 "9223372036854775808",
217 "9987435987394857987",
218 "17873648761234698740",
219 "18446744073709551615",
221 static const Uint uValues3[] = {
222 (Uint)9223372036854775808ULL,
223 (Uint)9987435987394857987ULL,
224 (Uint)17873648761234698740ULL,
225 (Uint)18446744073709551615ULL,
227 FOR_EACH_RANGE (i, 0, sizeof(uStrings3)/sizeof(*uStrings3)) {
228 EXPECT_EQ(to<Uint>(uStrings3[i]), uValues3[i]);
229 if (sizeof(Int) == 8) {
230 EXPECT_THROW(to<Sint>(uStrings3[i]), std::range_error);
235 // Minimum possible negative values, and negative sign overflow
236 static const String strings4[] = {
240 "-9223372036854775808",
242 static const String strings5[] = {
246 "-9223372036854775809",
248 static const Sint values4[] = {
252 (Sint)(-9223372036854775807LL - 1),
254 FOR_EACH_RANGE (i, 0, sizeof(strings4)/sizeof(*strings4)) {
255 if (sizeof(Int) > std::pow(2, i)) {
256 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
257 EXPECT_EQ(values4[i] - 1, to<Sint>(strings5[i]));
258 } else if (sizeof(Int) == std::pow(2, i)) {
259 EXPECT_EQ(values4[i], to<Sint>(strings4[i]));
260 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
262 EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
263 EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
267 // Bogus string values
268 static const String bogusStrings[] = {
275 "- 42", // whitespace is not allowed between the sign and the value
277 "12345678901234567890123456789",
279 for (const auto& str : bogusStrings) {
280 EXPECT_THROW(to<Sint>(str), std::range_error);
281 EXPECT_THROW(to<Uint>(str), std::range_error);
284 // A leading '+' character is only allowed when converting to signed types.
285 String posSign("+42");
286 EXPECT_EQ(42, to<Sint>(posSign));
287 EXPECT_THROW(to<Uint>(posSign), std::range_error);
289 testString2Integral<String, Ints...>();
292 TEST(Conv, String2Integral) {
293 testString2Integral<const char*, signed char, short, int, long, long long>();
294 testString2Integral<std::string, signed char, short, int, long, long long>();
295 testString2Integral<fbstring, signed char, short, int, long, long long>();
297 // Testing the behavior of the StringPiece* API
298 // StringPiece* normally parses as much valid data as it can,
299 // and advances the StringPiece to the end of the valid data.
300 char buf1[] = "100foo";
301 StringPiece sp1(buf1);
302 EXPECT_EQ(100, to<uint8_t>(&sp1));
303 EXPECT_EQ(buf1 + 3, sp1.begin());
304 // However, if the next character would cause an overflow it throws a
305 // range_error rather than consuming only as much as it can without
307 char buf2[] = "1002";
308 StringPiece sp2(buf2);
309 EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
310 EXPECT_EQ(buf2, sp2.begin());
313 TEST(Conv, StringPiece2Integral) {
314 string s = " +123 hello world ";
316 EXPECT_EQ(to<int>(&sp), 123);
317 EXPECT_EQ(sp, " hello world ");
320 TEST(Conv, StringPieceAppend) {
323 StringPiece sp(s, 0, 3);
324 string result = to<string>(s, sp);
325 EXPECT_EQ(result, "foobarfoo");
328 StringPiece sp1(s, 0, 3);
329 StringPiece sp2(s, 3, 3);
330 string result = to<string>(sp1, sp2);
331 EXPECT_EQ(result, s);
335 TEST(Conv, BadStringToIntegral) {
336 // Note that leading spaces (e.g. " 1") are valid.
337 vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
339 EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
343 template <class String>
344 void testVariadicTo() {
347 toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
348 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
351 EXPECT_TRUE(s.empty());
353 s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
354 EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
357 TEST(Conv, NullString) {
358 string s1 = to<string>((char *) NULL);
359 EXPECT_TRUE(s1.empty());
360 fbstring s2 = to<fbstring>((char *) NULL);
361 EXPECT_TRUE(s2.empty());
364 TEST(Conv, VariadicTo) {
365 testVariadicTo<string>();
366 testVariadicTo<fbstring>();
369 template <class String>
370 void testDoubleToString() {
371 EXPECT_EQ(to<string>(0.0), "0");
372 EXPECT_EQ(to<string>(0.5), "0.5");
373 EXPECT_EQ(to<string>(10.25), "10.25");
374 EXPECT_EQ(to<string>(1.123e10), "11230000000");
377 TEST(Conv, DoubleToString) {
378 testDoubleToString<string>();
379 testDoubleToString<fbstring>();
382 TEST(Conv, FBStringToString) {
384 string ret = to<string>(foo);
385 EXPECT_EQ(ret, "foo");
386 string ret2 = to<string>(foo, 2);
387 EXPECT_EQ(ret2, "foo2");
390 TEST(Conv, StringPieceToDouble) {
391 string s = "2134123.125 zorro";
393 EXPECT_EQ(to<double>(&pc), 2134123.125);
394 EXPECT_EQ(pc, " zorro");
396 EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
397 EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
399 // Test NaN conversion
401 to<double>("not a number");
403 } catch (const std::range_error &) {
406 EXPECT_TRUE(std::isnan(to<double>("NaN")));
407 EXPECT_EQ(to<double>("inf"), numeric_limits<double>::infinity());
408 EXPECT_EQ(to<double>("infinity"), numeric_limits<double>::infinity());
409 EXPECT_THROW(to<double>("infinitX"), std::range_error);
410 EXPECT_EQ(to<double>("-inf"), -numeric_limits<double>::infinity());
411 EXPECT_EQ(to<double>("-infinity"), -numeric_limits<double>::infinity());
412 EXPECT_THROW(to<double>("-infinitX"), std::range_error);
415 TEST(Conv, EmptyStringToInt) {
422 } catch (const std::range_error &) {
426 TEST(Conv, CorruptedStringToInt) {
428 StringPiece pc(s.data(), s.data() + 1); // Only "-"
433 } catch (const std::range_error &) {
437 TEST(Conv, EmptyStringToDouble) {
444 } catch (const std::range_error &) {
448 TEST(Conv, IntToDouble) {
449 auto d = to<double>(42);
451 /* This seems not work in ubuntu11.10, gcc 4.6.1
453 auto f = to<float>(957837589847);
455 } catch (std::range_error& e) {
456 //LOG(INFO) << e.what();
461 TEST(Conv, DoubleToInt) {
462 auto i = to<int>(42.0);
465 auto i = to<int>(42.1);
467 } catch (std::range_error& e) {
468 //LOG(INFO) << e.what();
472 TEST(Conv, EnumToInt) {
473 enum A { x = 42, y = 420, z = 65 };
476 auto j = to<char>(x);
479 auto i = to<char>(y);
480 LOG(ERROR) << static_cast<unsigned int>(i);
482 } catch (std::range_error& e) {
483 //LOG(INFO) << e.what();
487 TEST(Conv, EnumToString) {
489 enum A { x = 4, y = 420, z = 65 };
490 EXPECT_EQ("foo.4", to<string>("foo.", x));
491 EXPECT_EQ("foo.420", to<string>("foo.", y));
492 EXPECT_EQ("foo.65", to<string>("foo.", z));
495 TEST(Conv, IntToEnum) {
496 enum A { x = 42, y = 420 };
502 auto i = to<A>(5000000000L);
504 } catch (std::range_error& e) {
505 //LOG(INFO) << e.what();
509 template<typename Src>
510 void testStr2Bool() {
511 EXPECT_FALSE(to<bool>(Src("0")));
512 EXPECT_FALSE(to<bool>(Src(" 000 ")));
514 EXPECT_FALSE(to<bool>(Src("n")));
515 EXPECT_FALSE(to<bool>(Src("no")));
516 EXPECT_FALSE(to<bool>(Src("false")));
517 EXPECT_FALSE(to<bool>(Src("False")));
518 EXPECT_FALSE(to<bool>(Src(" fAlSe" )));
519 EXPECT_FALSE(to<bool>(Src("F")));
520 EXPECT_FALSE(to<bool>(Src("off")));
522 EXPECT_TRUE(to<bool>(Src("1")));
523 EXPECT_TRUE(to<bool>(Src(" 001 ")));
524 EXPECT_TRUE(to<bool>(Src("y")));
525 EXPECT_TRUE(to<bool>(Src("yes")));
526 EXPECT_TRUE(to<bool>(Src("\nyEs\t")));
527 EXPECT_TRUE(to<bool>(Src("true")));
528 EXPECT_TRUE(to<bool>(Src("True")));
529 EXPECT_TRUE(to<bool>(Src("T")));
530 EXPECT_TRUE(to<bool>(Src("on")));
532 EXPECT_THROW(to<bool>(Src("")), std::range_error);
533 EXPECT_THROW(to<bool>(Src("2")), std::range_error);
534 EXPECT_THROW(to<bool>(Src("11")), std::range_error);
535 EXPECT_THROW(to<bool>(Src("19")), std::range_error);
536 EXPECT_THROW(to<bool>(Src("o")), std::range_error);
537 EXPECT_THROW(to<bool>(Src("fal")), std::range_error);
538 EXPECT_THROW(to<bool>(Src("tru")), std::range_error);
539 EXPECT_THROW(to<bool>(Src("ye")), std::range_error);
540 EXPECT_THROW(to<bool>(Src("yes foo")), std::range_error);
541 EXPECT_THROW(to<bool>(Src("bar no")), std::range_error);
542 EXPECT_THROW(to<bool>(Src("one")), std::range_error);
543 EXPECT_THROW(to<bool>(Src("true_")), std::range_error);
544 EXPECT_THROW(to<bool>(Src("bogus_token_that_is_too_long")),
548 TEST(Conv, StringToBool) {
549 // testStr2Bool<const char *>();
550 testStr2Bool<std::string>();
552 // Test with strings that are not NUL terminated.
553 const char buf[] = "01234";
554 EXPECT_FALSE(to<bool>(StringPiece(buf, buf + 1))); // "0"
555 EXPECT_TRUE(to<bool>(StringPiece(buf + 1, buf + 2))); // "1"
556 const char buf2[] = "one two three";
557 EXPECT_TRUE(to<bool>(StringPiece(buf2, buf2 + 2))); // "on"
558 const char buf3[] = "false";
559 EXPECT_THROW(to<bool>(StringPiece(buf3, buf3 + 3)), // "fal"
562 // Test the StringPiece* API
563 const char buf4[] = "001foo";
564 StringPiece sp4(buf4);
565 EXPECT_TRUE(to<bool>(&sp4));
566 EXPECT_EQ(buf4 + 3, sp4.begin());
567 const char buf5[] = "0012";
568 StringPiece sp5(buf5);
569 EXPECT_THROW(to<bool>(&sp5), std::range_error);
570 EXPECT_EQ(buf5, sp5.begin());
573 ////////////////////////////////////////////////////////////////////////////////
574 // Benchmarks for ASCII to int conversion
575 ////////////////////////////////////////////////////////////////////////////////
576 // @author: Rajat Goel (rajat)
578 static int64_t handwrittenAtoi(const char* start, const char* end) {
580 bool positive = true;
584 throw std::runtime_error("empty string");
587 while (start < end && isspace(*start)) {
599 while (start < end && *start >= '0' && *start <= '9') {
600 auto const newRetVal = retVal * 10 + (*start++ - '0');
601 if (newRetVal < retVal) {
602 throw std::runtime_error("overflow");
608 throw std::runtime_error("extra chars at the end");
611 return positive ? retVal : -retVal;
614 static StringPiece pc1 = "1234567890123456789";
616 void handwrittenAtoiMeasure(uint n, uint digits) {
617 auto p = pc1.subpiece(pc1.size() - digits, digits);
618 FOR_EACH_RANGE (i, 0, n) {
619 doNotOptimizeAway(handwrittenAtoi(p.begin(), p.end()));
623 void follyAtoiMeasure(uint n, uint digits) {
624 auto p = pc1.subpiece(pc1.size() - digits, digits);
625 FOR_EACH_RANGE (i, 0, n) {
626 doNotOptimizeAway(folly::to<int64_t>(p.begin(), p.end()));
630 void clibAtoiMeasure(uint n, uint digits) {
631 auto p = pc1.subpiece(pc1.size() - digits, digits);
632 assert(*p.end() == 0);
633 static_assert(sizeof(long) == 8, "64-bit long assumed");
634 FOR_EACH_RANGE (i, 0, n) {
635 doNotOptimizeAway(atol(p.begin()));
639 void clibStrtoulMeasure(uint n, uint digits) {
640 auto p = pc1.subpiece(pc1.size() - digits, digits);
641 assert(*p.end() == 0);
643 FOR_EACH_RANGE (i, 0, n) {
644 doNotOptimizeAway(strtoul(p.begin(), &endptr, 10));
648 void lexicalCastMeasure(uint n, uint digits) {
649 auto p = pc1.subpiece(pc1.size() - digits, digits);
650 assert(*p.end() == 0);
651 FOR_EACH_RANGE (i, 0, n) {
652 doNotOptimizeAway(boost::lexical_cast<uint64_t>(p.begin()));
656 #define DEFINE_BENCHMARK_GROUP(n) \
657 BENCHMARK_PARAM(clibAtoiMeasure, n); \
658 BENCHMARK_RELATIVE_PARAM(lexicalCastMeasure, n); \
659 BENCHMARK_RELATIVE_PARAM(handwrittenAtoiMeasure, n); \
660 BENCHMARK_RELATIVE_PARAM(follyAtoiMeasure, n);
662 DEFINE_BENCHMARK_GROUP(1);
663 DEFINE_BENCHMARK_GROUP(2);
664 DEFINE_BENCHMARK_GROUP(3);
665 DEFINE_BENCHMARK_GROUP(4);
666 DEFINE_BENCHMARK_GROUP(5);
667 DEFINE_BENCHMARK_GROUP(6);
668 DEFINE_BENCHMARK_GROUP(7);
669 DEFINE_BENCHMARK_GROUP(8);
670 DEFINE_BENCHMARK_GROUP(9);
671 DEFINE_BENCHMARK_GROUP(10);
672 DEFINE_BENCHMARK_GROUP(11);
673 DEFINE_BENCHMARK_GROUP(12);
674 DEFINE_BENCHMARK_GROUP(13);
675 DEFINE_BENCHMARK_GROUP(14);
676 DEFINE_BENCHMARK_GROUP(15);
677 DEFINE_BENCHMARK_GROUP(16);
678 DEFINE_BENCHMARK_GROUP(17);
679 DEFINE_BENCHMARK_GROUP(18);
680 DEFINE_BENCHMARK_GROUP(19);
682 #undef DEFINE_BENCHMARK_GROUP
684 int main(int argc, char** argv) {
685 testing::InitGoogleTest(&argc, argv);
686 google::ParseCommandLineFlags(&argc, &argv, true);
687 auto ret = RUN_ALL_TESTS();
688 if (!ret && FLAGS_benchmark) {
689 folly::runBenchmarks();