Pull from FB rev 63ce89e2f2301e6bba44a111cc7d4218022156f6
[folly.git] / folly / test / ConvTest.cpp
1 /*
2  * Copyright 2012 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 static int8_t s8;
29 static uint8_t u8;
30 static int16_t s16;
31 static uint16_t u16;
32 static int32_t s32;
33 static uint32_t u32;
34 static int64_t s64;
35 static uint64_t u64;
36
37 TEST(Conv, Integral2Integral) {
38   // Same size, different signs
39   s64 = numeric_limits<uint8_t>::max();
40   EXPECT_EQ(to<uint8_t>(s64), s64);
41
42   s64 = numeric_limits<int8_t>::max();
43   EXPECT_EQ(to<int8_t>(s64), s64);
44 }
45
46 TEST(Conv, Floating2Floating) {
47   float f1 = 1e3;
48   double d1 = to<double>(f1);
49   EXPECT_EQ(f1, d1);
50
51   double d2 = 23.0;
52   auto f2 = to<float>(d2);
53   EXPECT_EQ(double(f2), d2);
54
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));
59
60   try {
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() ||
65                 shouldWork == 0.f);
66   } catch (...) {
67     EXPECT_TRUE(false);
68   }
69 }
70
71 template <class String>
72 void testIntegral2String() {
73 }
74
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;
79
80   Uint value = 123;
81   EXPECT_EQ(to<String>(value), "123");
82   Sint svalue = 123;
83   EXPECT_EQ(to<String>(svalue), "123");
84   svalue = -123;
85   EXPECT_EQ(to<String>(svalue), "-123");
86
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);
91
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);
96
97   testIntegral2String<String, Ints...>();
98 }
99
100 TEST(Conv, Integral2String) {
101   testIntegral2String<std::string, char, short, int, long>();
102   testIntegral2String<fbstring, char, short, int, long>();
103 }
104
105 template <class String>
106 void testString2Integral() {
107 }
108
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;
113
114   // Unsigned numbers small enough to fit in a signed type
115   static const String strings[] = {
116     "0",
117     "00",
118     "2 ",
119     " 84",
120     " \n 123    \t\n",
121     " 127",
122     "0000000000000000000000000042"
123   };
124   static const Uint values[] = {
125     0,
126     0,
127     2,
128     84,
129     123,
130     127,
131     42
132   };
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]);
136   }
137
138   // Unsigned numbers that won't fit in the signed variation
139   static const String uStrings[] = {
140     " 128",
141     "213",
142     "255"
143   };
144   static const Uint uValues[] = {
145     128,
146     213,
147     255
148   };
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);
153     }
154   }
155
156   if (sizeof(Int) >= 4) {
157     static const String strings2[] = {
158       "256",
159       "6324 ",
160       "63245675 ",
161       "2147483647"
162     };
163     static const Uint values2[] = {
164       (Uint)256,
165       (Uint)6324,
166       (Uint)63245675,
167       (Uint)2147483647
168     };
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]);
172     }
173
174     static const String uStrings2[] = {
175       "2147483648",
176       "3147483648",
177       "4147483648",
178       "4000000000",
179     };
180     static const Uint uValues2[] = {
181       (Uint)2147483648U,
182       (Uint)3147483648U,
183       (Uint)4147483648U,
184       (Uint)4000000000U,
185     };
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);
190       }
191     }
192   }
193
194   if (sizeof(Int) >= 8) {
195     static_assert(sizeof(Int) <= 8, "Now that would be interesting");
196     static const String strings3[] = {
197       "2147483648",
198       "5000000001",
199       "25687346509278435",
200       "100000000000000000",
201       "9223372036854775807",
202     };
203     static const Uint values3[] = {
204       (Uint)2147483648ULL,
205       (Uint)5000000001ULL,
206       (Uint)25687346509278435ULL,
207       (Uint)100000000000000000ULL,
208       (Uint)9223372036854775807ULL,
209     };
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]);
213     }
214
215     static const String uStrings3[] = {
216       "9223372036854775808",
217       "9987435987394857987",
218       "17873648761234698740",
219       "18446744073709551615",
220     };
221     static const Uint uValues3[] = {
222       (Uint)9223372036854775808ULL,
223       (Uint)9987435987394857987ULL,
224       (Uint)17873648761234698740ULL,
225       (Uint)18446744073709551615ULL,
226     };
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);
231       }
232     }
233   }
234
235   // Minimum possible negative values, and negative sign overflow
236   static const String strings4[] = {
237     "-128",
238     "-32768",
239     "-2147483648",
240     "-9223372036854775808",
241   };
242   static const String strings5[] = {
243     "-129",
244     "-32769",
245     "-2147483649",
246     "-9223372036854775809",
247   };
248   static const Sint values4[] = {
249     (Sint)-128LL,
250     (Sint)-32768LL,
251     (Sint)-2147483648LL,
252     (Sint)(-9223372036854775807LL - 1),
253   };
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);
261     } else {
262       EXPECT_THROW(to<Sint>(strings4[i]), std::range_error);
263       EXPECT_THROW(to<Sint>(strings5[i]), std::range_error);
264     }
265   }
266
267   // Bogus string values
268   static const String bogusStrings[] = {
269     "",
270     "0x1234",
271     "123L",
272     "123a",
273     "x 123 ",
274     "234 y",
275     "- 42",  // whitespace is not allowed between the sign and the value
276     " +   13 ",
277     "12345678901234567890123456789",
278   };
279   for (const auto& str : bogusStrings) {
280     EXPECT_THROW(to<Sint>(str), std::range_error);
281     EXPECT_THROW(to<Uint>(str), std::range_error);
282   }
283
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);
288
289   testString2Integral<String, Ints...>();
290 }
291
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>();
296
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
306   // overflowing.
307   char buf2[] = "1002";
308   StringPiece sp2(buf2);
309   EXPECT_THROW(to<uint8_t>(&sp2), std::range_error);
310   EXPECT_EQ(buf2, sp2.begin());
311 }
312
313 TEST(Conv, StringPiece2Integral) {
314   string s = "  +123  hello world  ";
315   StringPiece sp = s;
316   EXPECT_EQ(to<int>(&sp), 123);
317   EXPECT_EQ(sp, "  hello world  ");
318 }
319
320 TEST(Conv, StringPieceAppend) {
321   string s = "foobar";
322   {
323     StringPiece sp(s, 0, 3);
324     string result = to<string>(s, sp);
325     EXPECT_EQ(result, "foobarfoo");
326   }
327   {
328     StringPiece sp1(s, 0, 3);
329     StringPiece sp2(s, 3, 3);
330     string result = to<string>(sp1, sp2);
331     EXPECT_EQ(result, s);
332   }
333 }
334
335 TEST(Conv, BadStringToIntegral) {
336   // Note that leading spaces (e.g.  " 1") are valid.
337   vector<string> v = { "a", "", " ", "\n", " a0", "abcdef", "1Z", "!#" };
338   for (auto& s: v) {
339     EXPECT_THROW(to<int>(s), std::range_error) << "s=" << s;
340   }
341 }
342
343 template <class String>
344 void testVariadicTo() {
345   String s;
346   toAppend(&s);
347   toAppend("Lorem ipsum ", 1234, String(" dolor amet "), 567.89, '!', &s);
348   EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89!");
349
350   s = to<String>();
351   EXPECT_TRUE(s.empty());
352
353   s = to<String>("Lorem ipsum ", nullptr, 1234, " dolor amet ", 567.89, '.');
354   EXPECT_EQ(s, "Lorem ipsum 1234 dolor amet 567.89.");
355 }
356
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());
362 }
363
364 TEST(Conv, VariadicTo) {
365   testVariadicTo<string>();
366   testVariadicTo<fbstring>();
367 }
368
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");
375 }
376
377 TEST(Conv, DoubleToString) {
378   testDoubleToString<string>();
379   testDoubleToString<fbstring>();
380 }
381
382 TEST(Conv, FBStringToString) {
383   fbstring foo("foo");
384   string ret = to<string>(foo);
385   EXPECT_EQ(ret, "foo");
386   string ret2 = to<string>(foo, 2);
387   EXPECT_EQ(ret2, "foo2");
388 }
389
390 TEST(Conv, StringPieceToDouble) {
391   string s = "2134123.125 zorro";
392   StringPiece pc(s);
393   EXPECT_EQ(to<double>(&pc), 2134123.125);
394   EXPECT_EQ(pc, " zorro");
395
396   EXPECT_THROW(to<double>(StringPiece(s)), std::range_error);
397   EXPECT_EQ(to<double>(StringPiece(s.data(), pc.data())), 2134123.125);
398
399 // Test NaN conversion
400   try {
401     to<double>("not a number");
402     EXPECT_TRUE(false);
403   } catch (const std::range_error &) {
404   }
405
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);
413 }
414
415 TEST(Conv, EmptyStringToInt) {
416   string s = "";
417   StringPiece pc(s);
418
419   try {
420     to<int>(pc);
421     EXPECT_TRUE(false);
422   } catch (const std::range_error &) {
423   }
424 }
425
426 TEST(Conv, CorruptedStringToInt) {
427   string s = "-1";
428   StringPiece pc(s.data(), s.data() + 1); // Only  "-"
429
430   try {
431     to<int64_t>(&pc);
432     EXPECT_TRUE(false);
433   } catch (const std::range_error &) {
434   }
435 }
436
437 TEST(Conv, EmptyStringToDouble) {
438   string s = "";
439   StringPiece pc(s);
440
441   try {
442     to<double>(pc);
443     EXPECT_TRUE(false);
444   } catch (const std::range_error &) {
445   }
446 }
447
448 TEST(Conv, IntToDouble) {
449   auto d = to<double>(42);
450   EXPECT_EQ(d, 42);
451   /* This seems not work in ubuntu11.10, gcc 4.6.1
452   try {
453     auto f = to<float>(957837589847);
454     EXPECT_TRUE(false);
455   } catch (std::range_error& e) {
456     //LOG(INFO) << e.what();
457   }
458   */
459 }
460
461 TEST(Conv, DoubleToInt) {
462   auto i = to<int>(42.0);
463   EXPECT_EQ(i, 42);
464   try {
465     auto i = to<int>(42.1);
466     EXPECT_TRUE(false);
467   } catch (std::range_error& e) {
468     //LOG(INFO) << e.what();
469   }
470 }
471
472 TEST(Conv, EnumToInt) {
473   enum A { x = 42, y = 420, z = 65 };
474   auto i = to<int>(x);
475   EXPECT_EQ(i, 42);
476   auto j = to<char>(x);
477   EXPECT_EQ(j, 42);
478   try {
479     auto i = to<char>(y);
480     LOG(ERROR) << static_cast<unsigned int>(i);
481     EXPECT_TRUE(false);
482   } catch (std::range_error& e) {
483     //LOG(INFO) << e.what();
484   }
485 }
486
487 TEST(Conv, EnumToString) {
488   // task 813959
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));
493 }
494
495 TEST(Conv, IntToEnum) {
496   enum A { x = 42, y = 420 };
497   auto i = to<A>(42);
498   EXPECT_EQ(i, A::x);
499   auto j = to<A>(100);
500   EXPECT_EQ(j, 100);
501   try {
502     auto i = to<A>(5000000000L);
503     EXPECT_TRUE(false);
504   } catch (std::range_error& e) {
505     //LOG(INFO) << e.what();
506   }
507 }
508
509 template<typename Src>
510 void testStr2Bool() {
511   EXPECT_FALSE(to<bool>(Src("0")));
512   EXPECT_FALSE(to<bool>(Src("  000  ")));
513
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")));
521
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")));
531
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")),
545                std::range_error);
546 }
547
548 TEST(Conv, StringToBool) {
549   // testStr2Bool<const char *>();
550   testStr2Bool<std::string>();
551
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"
560                std::range_error);
561
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());
571 }
572
573 ////////////////////////////////////////////////////////////////////////////////
574 // Benchmarks for ASCII to int conversion
575 ////////////////////////////////////////////////////////////////////////////////
576 // @author: Rajat Goel (rajat)
577
578 static int64_t handwrittenAtoi(const char* start, const char* end) {
579
580   bool positive = true;
581   int64_t retVal = 0;
582
583   if (start == end) {
584     throw std::runtime_error("empty string");
585   }
586
587   while (start < end && isspace(*start)) {
588     ++start;
589   }
590
591   switch (*start) {
592     case '-':
593       positive = false;
594     case '+':
595       ++start;
596     default:;
597   }
598
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");
603     }
604     retVal = newRetVal;
605   }
606
607   if (start != end) {
608     throw std::runtime_error("extra chars at the end");
609   }
610
611   return positive ? retVal : -retVal;
612 }
613
614 static StringPiece pc1 = "1234567890123456789";
615
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()));
620   }
621 }
622
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()));
627   }
628 }
629
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()));
636   }
637 }
638
639 void clibStrtoulMeasure(uint n, uint digits) {
640   auto p = pc1.subpiece(pc1.size() - digits, digits);
641   assert(*p.end() == 0);
642   char * endptr;
643   FOR_EACH_RANGE (i, 0, n) {
644     doNotOptimizeAway(strtoul(p.begin(), &endptr, 10));
645   }
646 }
647
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()));
653   }
654 }
655
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);
661
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);
681
682 #undef DEFINE_BENCHMARK_GROUP
683
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();
690   }
691   return ret;
692 }