8967a7d0f0a454a66739a1de33b2467a4d4a5fd9
[folly.git] / folly / test / ConvTest.cpp
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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>
22 #include <limits>
23 #include <stdexcept>
24
25 using namespace std;
26 using namespace folly;
27
28
29
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()));
34 }
35
36 TEST(Conv, digits10) {
37   char buffer[100];
38   uint64_t power;
39
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));
51
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));
57   }
58
59   // try powers of 2
60   power = 1;
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));
68     power *= 2;
69   }
70
71   // try powers of 10
72   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));
80     power *= 10;
81   }
82 }
83
84 // Test to<T>(T)
85 TEST(Conv, Type2Type) {
86   bool boolV = true;
87   EXPECT_EQ(to<bool>(boolV), true);
88
89   int intV = 42;
90   EXPECT_EQ(to<int>(intV), 42);
91
92   float floatV = 4.2;
93   EXPECT_EQ(to<float>(floatV), 4.2f);
94
95   double doubleV = 0.42;
96   EXPECT_EQ(to<double>(doubleV), 0.42);
97
98   std::string stringV = "StdString";
99   EXPECT_EQ(to<std::string>(stringV), "StdString");
100
101   folly::fbstring fbStrV = "FBString";
102   EXPECT_EQ(to<folly::fbstring>(fbStrV), "FBString");
103
104   folly::StringPiece spV("StringPiece");
105   EXPECT_EQ(to<folly::StringPiece>(spV), "StringPiece");
106
107   // Rvalues
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")),
115             "Forty Two");
116 }
117
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);
122
123   s64 = numeric_limits<int8_t>::max();
124   EXPECT_EQ(to<int8_t>(s64), s64);
125 }
126
127 TEST(Conv, Floating2Floating) {
128   float f1 = 1e3;
129   double d1 = to<double>(f1);
130   EXPECT_EQ(f1, d1);
131
132   double d2 = 23.0;
133   auto f2 = to<float>(d2);
134   EXPECT_EQ(double(f2), d2);
135
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));
140
141   try {
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() ||
146                 shouldWork == 0.f);
147   } catch (...) {
148     EXPECT_TRUE(false);
149   }
150 }
151
152 template <class String>
153 void testIntegral2String() {
154 }
155
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;
160
161   Uint value = 123;
162   EXPECT_EQ(to<String>(value), "123");
163   Sint svalue = 123;
164   EXPECT_EQ(to<String>(svalue), "123");
165   svalue = -123;
166   EXPECT_EQ(to<String>(svalue), "-123");
167
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);
172
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);
177
178   testIntegral2String<String, Ints...>();
179 }
180
181 #if FOLLY_HAVE_INT128_T
182 template <class String>
183 void test128Bit2String() {
184   typedef unsigned __int128 Uint;
185   typedef __int128 Sint;
186
187   EXPECT_EQ(detail::digitsEnough<unsigned __int128>(), 39);
188
189   Uint value = 123;
190   EXPECT_EQ(to<String>(value), "123");
191   Sint svalue = 123;
192   EXPECT_EQ(to<String>(svalue), "123");
193   svalue = -123;
194   EXPECT_EQ(to<String>(svalue), "-123");
195
196   value = __int128(1) << 64;
197   EXPECT_EQ(to<String>(value), "18446744073709551616");
198
199   svalue =  -(__int128(1) << 64);
200   EXPECT_EQ(to<String>(svalue), "-18446744073709551616");
201
202   value = 0;
203   EXPECT_EQ(to<String>(value), "0");
204
205   svalue = 0;
206   EXPECT_EQ(to<String>(svalue), "0");
207
208   // TODO: the following do not compile to<__int128> ...
209
210 #if 0
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);
215
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);
220 #endif
221 }
222
223 #endif
224
225 TEST(Conv, Integral2String) {
226   testIntegral2String<std::string, char, short, int, long>();
227   testIntegral2String<fbstring, char, short, int, long>();
228
229 #if FOLLY_HAVE_INT128_T
230   test128Bit2String<std::string>();
231   test128Bit2String<fbstring>();
232 #endif
233 }
234
235 template <class String>
236 void testString2Integral() {
237 }
238
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;
243
244   // Unsigned numbers small enough to fit in a signed type
245   static const String strings[] = {
246     "0",
247     "00",
248     "2 ",
249     " 84",
250     " \n 123    \t\n",
251     " 127",
252     "0000000000000000000000000042"
253   };
254   static const Uint values[] = {
255     0,
256     0,
257     2,
258     84,
259     123,
260     127,
261     42
262   };
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]);
266   }
267
268   // Unsigned numbers that won't fit in the signed variation
269   static const String uStrings[] = {
270     " 128",
271     "213",
272     "255"
273   };
274   static const Uint uValues[] = {
275     128,
276     213,
277     255
278   };
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);
283     }
284   }
285
286   if (sizeof(Int) >= 4) {
287     static const String strings2[] = {
288       "256",
289       "6324 ",
290       "63245675 ",
291       "2147483647"
292     };
293     static const Uint values2[] = {
294       (Uint)256,
295       (Uint)6324,
296       (Uint)63245675,
297       (Uint)2147483647
298     };
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]);
302     }
303
304     static const String uStrings2[] = {
305       "2147483648",
306       "3147483648",
307       "4147483648",
308       "4000000000",
309     };
310     static const Uint uValues2[] = {
311       (Uint)2147483648U,
312       (Uint)3147483648U,
313       (Uint)4147483648U,
314       (Uint)4000000000U,
315     };
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);
320       }
321     }
322   }
323
324   if (sizeof(Int) >= 8) {
325     static_assert(sizeof(Int) <= 8, "Now that would be interesting");
326     static const String strings3[] = {
327       "2147483648",
328       "5000000001",
329       "25687346509278435",
330       "100000000000000000",
331       "9223372036854775807",
332     };
333     static const Uint values3[] = {
334       (Uint)2147483648ULL,
335       (Uint)5000000001ULL,
336       (Uint)25687346509278435ULL,
337       (Uint)100000000000000000ULL,
338       (Uint)9223372036854775807ULL,
339     };
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]);
343     }
344
345     static const String uStrings3[] = {
346       "9223372036854775808",
347       "9987435987394857987",
348       "17873648761234698740",
349       "18446744073709551615",
350     };
351     static const Uint uValues3[] = {
352       (Uint)9223372036854775808ULL,
353       (Uint)9987435987394857987ULL,
354       (Uint)17873648761234698740ULL,
355       (Uint)18446744073709551615ULL,
356     };
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);
361       }
362     }
363   }
364
365   // Minimum possible negative values, and negative sign overflow
366   static const String strings4[] = {
367     "-128",
368     "-32768",
369     "-2147483648",
370     "-9223372036854775808",
371   };
372   static const String strings5[] = {
373     "-129",
374     "-32769",
375     "-2147483649",
376     "-9223372036854775809",
377   };
378   static const Sint values4[] = {
379     (Sint)-128LL,
380     (Sint)-32768LL,
381     (Sint)-2147483648LL,
382     (Sint)(-9223372036854775807LL - 1),
383   };
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);
391     } else {
392       EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
393       EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
394     }
395   }
396
397   // Bogus string values
398   static const String bogusStrings[] = {
399     "",
400     "0x1234",
401     "123L",
402     "123a",
403     "x 123 ",
404     "234 y",
405     "- 42",  // whitespace is not allowed between the sign and the value
406     " +   13 ",
407     "12345678901234567890123456789",
408   };
409   for (const auto& str : bogusStrings) {
410     EXPECT_THROW(to<Sint>(str), std::range_error);
411     EXPECT_THROW(to<Uint>(str), std::range_error);
412   }
413
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);
418
419   testString2Integral<String, Ints...>();
420 }
421
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>();
426
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
436   // overflowing.
437   char buf2[] = "1002";
438   StringPiece sp2(buf2);
439   EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
440   EXPECT_EQ(buf2, sp2.begin());
441 }
442
443 TEST(Conv, StringPiece2Integral) {
444   string s = "  +123  hello world  ";
445   StringPiece sp = s;
446   EXPECT_EQ(to<int>(&sp), 123);
447   EXPECT_EQ(sp, "  hello world  ");
448 }
449
450 TEST(Conv, StringPieceAppend) {
451   string s = "foobar";
452   {
453     StringPiece sp(s, 0, 3);
454     string result = to<string>(s, sp);
455     EXPECT_EQ(result, "foobarfoo");
456   }
457   {
458     StringPiece sp1(s, 0, 3);
459     StringPiece sp2(s, 3, 3);
460     string result = to<string>(sp1, sp2);
461     EXPECT_EQ(result, s);
462   }
463 }
464
465 TEST(Conv, BadStringToIntegral) {
466   // Note that leading spaces (e.g.  " 1") are valid.
467   vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
468   for (auto& s: v) {
469     EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
470   }
471 }
472
473 template <class String>
474 void testIdenticalTo() {
475   String s("Yukkuri shiteitte ne!!!");
476
477   String result = to<String>(s);
478   EXPECT_EQ(result, s);
479 }
480
481 template <class String>
482 void testVariadicTo() {
483   String s;
484   toAppend(&s);
485   toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
486   EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
487
488   s = to<String>();
489   EXPECT_TRUE(s.empty());
490
491   s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
492   EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
493 }
494
495 template <class String>
496 void testIdenticalToDelim() {
497   String s("Yukkuri shiteitte ne!!!");
498
499   String charDelim = toDelim<String>('$', s);
500   EXPECT_EQ(charDelim, s);
501
502   String strDelim = toDelim<String>(String(">_<"), s);
503   EXPECT_EQ(strDelim, s);
504 }
505
506 template <class String>
507 void testVariadicToDelim() {
508   String s;
509   toAppendDelim(":", &s);
510   toAppendDelim(
511       ":", "Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
512   EXPECT_EQ(s, "Lorem ipsum :1234: dolor amet :567.89:!");
513
514   s = toDelim<String>(':');
515   EXPECT_TRUE(s.empty());
516
517   s = toDelim<String>(
518       ":", "Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
519   EXPECT_EQ(s, "Lorem ipsum ::1234: dolor amet :567.89:.");
520 }
521
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());
527 }
528
529 TEST(Conv, VariadicTo) {
530   testIdenticalTo<string>();
531   testIdenticalTo<fbstring>();
532   testVariadicTo<string>();
533   testVariadicTo<fbstring>();
534 }
535
536 TEST(Conv, VariadicToDelim) {
537   testIdenticalToDelim<string>();
538   testIdenticalToDelim<fbstring>();
539   testVariadicToDelim<string>();
540   testVariadicToDelim<fbstring>();
541 }
542
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");
549 }
550
551 TEST(Conv, DoubleToString) {
552   testDoubleToString<string>();
553   testDoubleToString<fbstring>();
554 }
555
556 TEST(Conv, FBStringToString) {
557   fbstring foo("foo");
558   string ret = to<string>(foo);
559   EXPECT_EQ(ret, "foo");
560   string ret2 = to<string>(foo, 2);
561   EXPECT_EQ(ret2, "foo2");
562 }
563
564 TEST(Conv, StringPieceToDouble) {
565   string s = "2134123.125 zorro";
566   StringPiece pc(s);
567   EXPECT_EQ(to<double>(&pc), 2134123.125);
568   EXPECT_EQ(pc, " zorro");
569
570   EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
571   EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
572
573 // Test NaN conversion
574   try {
575     to<double>("not a number");
576     EXPECT_TRUE(false);
577   } catch (const std::range_error &) {
578   }
579
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);
587 }
588
589 TEST(Conv, EmptyStringToInt) {
590   string s = "";
591   StringPiece pc(s);
592
593   try {
594     to<int>(pc);
595     EXPECT_TRUE(false);
596   } catch (const std::range_error &) {
597   }
598 }
599
600 TEST(Conv, CorruptedStringToInt) {
601   string s = "-1";
602   StringPiece pc(s.data(), s.data() + 1); // Only  "-"
603
604   try {
605     to<int64_t>(&pc);
606     EXPECT_TRUE(false);
607   } catch (const std::range_error &) {
608   }
609 }
610
611 TEST(Conv, EmptyStringToDouble) {
612   string s = "";
613   StringPiece pc(s);
614
615   try {
616     to<double>(pc);
617     EXPECT_TRUE(false);
618   } catch (const std::range_error &) {
619   }
620 }
621
622 TEST(Conv, IntToDouble) {
623   auto d = to<double>(42);
624   EXPECT_EQ(d, 42);
625   /* This seems not work in ubuntu11.10, gcc 4.6.1
626   try {
627     auto f = to<float>(957837589847);
628     EXPECT_TRUE(false);
629   } catch (std::range_error& e) {
630     //LOG(INFO) << e.what();
631   }
632   */
633 }
634
635 TEST(Conv, DoubleToInt) {
636   auto i = to<int>(42.0);
637   EXPECT_EQ(i, 42);
638   try {
639     auto i = to<int>(42.1);
640     LOG(ERROR) << "to<int> returned " << i << " instead of throwing";
641     EXPECT_TRUE(false);
642   } catch (std::range_error& e) {
643     //LOG(INFO) << e.what();
644   }
645 }
646
647 TEST(Conv, EnumToInt) {
648   enum A { x = 42, y = 420, z = 65 };
649   auto i = to<int>(x);
650   EXPECT_EQ(i, 42);
651   auto j = to<char>(x);
652   EXPECT_EQ(j, 42);
653   try {
654     auto i = to<char>(y);
655     LOG(ERROR) << "to<char> returned "
656                << static_cast<unsigned int>(i)
657                << " instead of throwing";
658     EXPECT_TRUE(false);
659   } catch (std::range_error& e) {
660     //LOG(INFO) << e.what();
661   }
662 }
663
664 TEST(Conv, EnumToString) {
665   // task 813959
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));
670 }
671
672 TEST(Conv, IntToEnum) {
673   enum A { x = 42, y = 420 };
674   auto i = to<A>(42);
675   EXPECT_EQ(i, x);
676   auto j = to<A>(100);
677   EXPECT_EQ(j, 100);
678   try {
679     auto i = to<A>(5000000000L);
680     LOG(ERROR) << "to<A> returned "
681                << static_cast<unsigned int>(i)
682                << " instead of throwing";
683     EXPECT_TRUE(false);
684   } catch (std::range_error& e) {
685     //LOG(INFO) << e.what();
686   }
687 }
688
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);
696   EXPECT_EQ(e, x);
697   try {
698     auto i = to<int32_t>(x);
699     LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
700     EXPECT_TRUE(false);
701   } catch (std::range_error& e) {
702   }
703 }
704
705 TEST(Conv, UnsignedEnumClass) {
706   enum class E : uint32_t { x = 3000000000U };
707   auto u = to<uint32_t>(E::x);
708   EXPECT_GT(u, 0);
709   EXPECT_EQ(u, 3000000000U);
710   auto s = to<string>(E::x);
711   EXPECT_EQ("3000000000", s);
712   auto e = to<E>(3000000000U);
713   EXPECT_EQ(e, E::x);
714   try {
715     auto i = to<int32_t>(E::x);
716     LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
717     EXPECT_TRUE(false);
718   } catch (std::range_error& e) {
719   }
720 }
721
722 // Multi-argument to<string> uses toAppend, a different code path than
723 // to<string>(enum).
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));
729 }
730
731 TEST(Conv, IntegralToBool) {
732   EXPECT_FALSE(to<bool>(0));
733   EXPECT_FALSE(to<bool>(0ul));
734
735   EXPECT_TRUE(to<bool>(1));
736   EXPECT_TRUE(to<bool>(1ul));
737
738   EXPECT_TRUE(to<bool>(-42));
739   EXPECT_TRUE(to<bool>(42ul));
740 }
741
742 template<typename Src>
743 void testStr2Bool() {
744   EXPECT_FALSE(to<bool>(Src("0")));
745   EXPECT_FALSE(to<bool>(Src("  000  ")));
746
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")));
754
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")));
764
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")),
778                std::range_error);
779 }
780
781 TEST(Conv, StringToBool) {
782   // testStr2Bool<const char *>();
783   testStr2Bool<std::string>();
784
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"
793                std::range_error);
794
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());
804 }
805
806 TEST(Conv, NewUint64ToString) {
807   char buf[21];
808
809 #define THE_GREAT_EXPECTATIONS(n, len)                  \
810   do {                                                  \
811     EXPECT_EQ((len), uint64ToBufferUnsafe((n), buf));   \
812     buf[(len)] = 0;                                     \
813     auto s = string(#n);                                \
814     s = s.substr(0, s.size() - 2);                      \
815     EXPECT_EQ(s, buf);                                  \
816   } while (0)
817
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);
840
841 #undef THE_GREAT_EXPECTATIONS
842 }
843
844 TEST(Conv, allocate_size) {
845   std::string str1 = "meh meh meh";
846   std::string str2 = "zdech zdech zdech";
847
848   auto res1 = folly::to<std::string>(str1, ".", str2);
849   EXPECT_EQ(res1, str1 + "." + str2);
850
851   std::string res2; //empty
852   toAppendFit(str1, str2, 1, &res2);
853   EXPECT_EQ(res2, str1 + str2 + "1");
854
855   std::string res3;
856   toAppendDelimFit(",", str1, str2, &res3);
857   EXPECT_EQ(res3, str1 + "," + str2);
858 }
859
860 ////////////////////////////////////////////////////////////////////////////////
861 // Benchmarks for ASCII to int conversion
862 ////////////////////////////////////////////////////////////////////////////////
863 // @author: Rajat Goel (rajat)
864
865 static int64_t handwrittenAtoi(const char* start, const char* end) {
866
867   bool positive = true;
868   int64_t retVal = 0;
869
870   if (start == end) {
871     throw std::runtime_error("empty string");
872   }
873
874   while (start < end && isspace(*start)) {
875     ++start;
876   }
877
878   switch (*start) {
879     case '-':
880       positive = false;
881     case '+':
882       ++start;
883     default:;
884   }
885
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");
890     }
891     retVal = newRetVal;
892   }
893
894   if (start != end) {
895     throw std::runtime_error("extra chars at the end");
896   }
897
898   return positive ? retVal : -retVal;
899 }
900
901 static StringPiece pc1 = "1234567890123456789";
902
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()));
907   }
908 }
909
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()));
914   }
915 }
916
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()));
923   }
924 }
925
926 void clibStrtoulMeasure(unsigned int n, unsigned int digits) {
927   auto p = pc1.subpiece(pc1.size() - digits, digits);
928   assert(*p.end() == 0);
929   char * endptr;
930   FOR_EACH_RANGE (i, 0, n) {
931     doNotOptimizeAway(strtoul(p.begin(), &endptr, 10));
932   }
933 }
934
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()));
940   }
941 }
942
943 // Benchmarks for unsigned to string conversion, raw
944
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";
957
958   uint32_t const length = digits10(value);
959   uint32_t next = length - 1;
960   while (value >= 100) {
961     auto const i = (value % 100) * 2;
962     value /= 100;
963     dst[next] = digits[i + 1];
964     dst[next - 1] = digits[i];
965     next -= 2;
966   }
967   // Handle last 1-2 digits
968   if (value < 10) {
969     dst[next] = '0' + uint32_t(value);
970   } else {
971     auto i = uint32_t(value) * 2;
972     dst[next] = digits[i + 1];
973     dst[next - 1] = digits[i];
974   }
975   return length;
976 }
977
978 void u64ToAsciiTableBM(unsigned int n, uint64_t value) {
979   // This is too fast, need to do 10 times per iteration
980   char buf[20];
981   FOR_EACH_RANGE (i, 0, n) {
982     doNotOptimizeAway(u64ToAsciiTable(value + n, buf));
983   }
984 }
985
986 unsigned u64ToAsciiClassic(uint64_t value, char* dst) {
987   // Write backwards.
988   char* next = (char*)dst;
989   char* start = next;
990   do {
991     *next++ = '0' + (value % 10);
992     value /= 10;
993   } while (value != 0);
994   unsigned length = next - start;
995
996   // Reverse in-place.
997   next--;
998   while (next > start) {
999     char swap = *next;
1000     *next = *start;
1001     *start = swap;
1002     next--;
1003     start++;
1004   }
1005   return length;
1006 }
1007
1008 void u64ToAsciiClassicBM(unsigned int n, uint64_t value) {
1009   // This is too fast, need to do 10 times per iteration
1010   char buf[20];
1011   FOR_EACH_RANGE (i, 0, n) {
1012     doNotOptimizeAway(u64ToAsciiClassic(value + n, buf));
1013   }
1014 }
1015
1016 void u64ToAsciiFollyBM(unsigned int n, uint64_t value) {
1017   // This is too fast, need to do 10 times per iteration
1018   char buf[20];
1019   FOR_EACH_RANGE (i, 0, n) {
1020     doNotOptimizeAway(uint64ToBufferUnsafe(value + n, buf));
1021   }
1022 }
1023
1024 // Benchmark uitoa with string append
1025
1026 void u2aAppendClassicBM(unsigned int n, uint64_t value) {
1027   string s;
1028   FOR_EACH_RANGE (i, 0, n) {
1029     // auto buf = &s.back() + 1;
1030     char buffer[20];
1031     s.append(buffer, u64ToAsciiClassic(value, buffer));
1032     doNotOptimizeAway(s.size());
1033   }
1034 }
1035
1036 void u2aAppendFollyBM(unsigned int n, uint64_t value) {
1037   string s;
1038   FOR_EACH_RANGE (i, 0, n) {
1039     // auto buf = &s.back() + 1;
1040     char buffer[20];
1041     s.append(buffer, uint64ToBufferUnsafe(value, buffer));
1042     doNotOptimizeAway(s.size());
1043   }
1044 }
1045
1046 template <class String>
1047 struct StringIdenticalToBM {
1048   StringIdenticalToBM() {}
1049   void operator()(unsigned int n, size_t len) const {
1050     String s;
1051     BENCHMARK_SUSPEND { s.append(len, '0'); }
1052     FOR_EACH_RANGE (i, 0, n) {
1053       String result = to<String>(s);
1054       doNotOptimizeAway(result.size());
1055     }
1056   }
1057 };
1058
1059 template <class String>
1060 struct StringVariadicToBM {
1061   StringVariadicToBM() {}
1062   void operator()(unsigned int n, size_t len) const {
1063     String s;
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());
1068     }
1069   }
1070 };
1071
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;
1080
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);
1088   }
1089 }
1090
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);
1095   }
1096 }
1097 BENCHMARK_DRAW_LINE();
1098
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;
1103
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();
1109
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);
1130
1131 #undef DEFINE_BENCHMARK_GROUP
1132
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();
1139
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);
1159
1160 #undef DEFINE_BENCHMARK_GROUP
1161
1162 #define DEFINE_BENCHMARK_GROUP(T, n)                    \
1163   BENCHMARK_PARAM(T ## VariadicToBM, n);                \
1164   BENCHMARK_RELATIVE_PARAM(T ## IdenticalToBM, n);      \
1165   BENCHMARK_DRAW_LINE();
1166
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);
1173
1174 #undef DEFINE_BENCHMARK_GROUP
1175
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();
1182   }
1183   return ret;
1184 }