using namespace std;
using namespace folly;
-static int8_t s8;
-static uint8_t u8;
-static int16_t s16;
-static uint16_t u16;
-static int32_t s32;
-static uint32_t u32;
-static int64_t s64;
-static uint64_t u64;
+
TEST(Conv, digits10Minimal) {
// Not much of a test (and it's included in the test below anyway).
TEST(Conv, Integral2Integral) {
// Same size, different signs
- s64 = numeric_limits<uint8_t>::max();
+ int64_t s64 = numeric_limits<uint8_t>::max();
EXPECT_EQ(to<uint8_t>(s64), s64);
s64 = numeric_limits<int8_t>::max();
EXPECT_EQ(i, 42);
try {
auto i = to<int>(42.1);
+ LOG(ERROR) << "to<int> returned " << i << " instead of throwing";
EXPECT_TRUE(false);
} catch (std::range_error& e) {
//LOG(INFO) << e.what();
EXPECT_EQ(j, 42);
try {
auto i = to<char>(y);
- LOG(ERROR) << static_cast<unsigned int>(i);
+ LOG(ERROR) << "to<char> returned "
+ << static_cast<unsigned int>(i)
+ << " instead of throwing";
EXPECT_TRUE(false);
} catch (std::range_error& e) {
//LOG(INFO) << e.what();
EXPECT_EQ(j, 100);
try {
auto i = to<A>(5000000000L);
+ LOG(ERROR) << "to<A> returned "
+ << static_cast<unsigned int>(i)
+ << " instead of throwing";
EXPECT_TRUE(false);
} catch (std::range_error& e) {
//LOG(INFO) << e.what();
EXPECT_EQ(e, x);
try {
auto i = to<int32_t>(x);
- LOG(ERROR) << to<uint32_t>(x);
+ LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
EXPECT_TRUE(false);
} catch (std::range_error& e) {
}
EXPECT_EQ(e, E::x);
try {
auto i = to<int32_t>(E::x);
- LOG(ERROR) << to<uint32_t>(E::x);
+ LOG(ERROR) << "to<int32_t> returned " << i << " instead of throwing";
EXPECT_TRUE(false);
} catch (std::range_error& e) {
}
}
}
+// Benchmark unsigned to string conversion
+
+void u64ToStringClibMeasure(unsigned int n, uint64_t value) {
+ // FOLLY_RANGE_CHECK_TO_STRING expands to std::to_string, except on Android
+ // where std::to_string is not supported
+ FOR_EACH_RANGE (i, 0, n) {
+ FOLLY_RANGE_CHECK_TO_STRING(value + n);
+ }
+}
+
+void u64ToStringFollyMeasure(unsigned int n, uint64_t value) {
+ FOR_EACH_RANGE (i, 0, n) {
+ to<std::string>(value + n);
+ }
+}
+
// Benchmark uitoa with string append
void u2aAppendClassicBM(unsigned int n, uint64_t value) {
#undef DEFINE_BENCHMARK_GROUP
+#define DEFINE_BENCHMARK_GROUP(n) \
+ BENCHMARK_PARAM(u64ToStringClibMeasure, n); \
+ BENCHMARK_RELATIVE_PARAM(u64ToStringFollyMeasure, n); \
+ BENCHMARK_DRAW_LINE();
+
+DEFINE_BENCHMARK_GROUP(1);
+DEFINE_BENCHMARK_GROUP(12);
+DEFINE_BENCHMARK_GROUP(123);
+DEFINE_BENCHMARK_GROUP(1234);
+DEFINE_BENCHMARK_GROUP(12345);
+DEFINE_BENCHMARK_GROUP(123456);
+DEFINE_BENCHMARK_GROUP(1234567);
+DEFINE_BENCHMARK_GROUP(12345678);
+DEFINE_BENCHMARK_GROUP(123456789);
+DEFINE_BENCHMARK_GROUP(1234567890);
+DEFINE_BENCHMARK_GROUP(12345678901);
+DEFINE_BENCHMARK_GROUP(123456789012);
+DEFINE_BENCHMARK_GROUP(1234567890123);
+DEFINE_BENCHMARK_GROUP(12345678901234);
+DEFINE_BENCHMARK_GROUP(123456789012345);
+DEFINE_BENCHMARK_GROUP(1234567890123456);
+DEFINE_BENCHMARK_GROUP(12345678901234567);
+DEFINE_BENCHMARK_GROUP(123456789012345678);
+DEFINE_BENCHMARK_GROUP(1234567890123456789);
+DEFINE_BENCHMARK_GROUP(12345678901234567890U);
+
+#undef DEFINE_BENCHMARK_GROUP
+
#define DEFINE_BENCHMARK_GROUP(n) \
BENCHMARK_PARAM(clibAtoiMeasure, n); \
BENCHMARK_RELATIVE_PARAM(lexicalCastMeasure, n); \