17 #include "absl/strings/numbers.h"
19 #include <sys/types.h>
37 #include "gmock/gmock.h"
38 #include "gtest/gtest.h"
39 #include "absl/base/internal/raw_logging.h"
40 #include "absl/random/distributions.h"
41 #include "absl/random/random.h"
42 #include "absl/strings/internal/numbers_test_common.h"
43 #include "absl/strings/internal/ostringstream.h"
44 #include "absl/strings/internal/pow10_helper.h"
45 #include "absl/strings/str_cat.h"
68 const int kFloatNumCases = 5000000;
74 if (
d == 0)
return "0";
75 if (
d < 0)
return "-" + PerfectDtoa(-
d);
80 while (
d >= 1
ULL << 63) ++exp,
d *= 0.5;
86 constexpr
int maxlen = 1100;
94 for (
int i = 0;
i != exp;
i += (exp > 0) ? 1 : -1) {
98 dig = dig * (exp > 0 ? 2 : 5) + carry;
106 memmove(
end + 1 + exp,
end + exp, 1 - exp);
117 Eq(
"0.1000000000000000055511151231257827021181583404541015625"));
118 EXPECT_THAT(PerfectDtoa(1e24),
Eq(
"999999999999999983222784"));
120 for (
int i = 0;
i < 100; ++
i) {
121 for (
double multiplier :
122 {1
e-300, 1
e-200, 1
e-100, 0.1, 1.0, 10.0, 1e100, 1e300}) {
123 double d = multiplier *
i;
130 template <
typename integer>
133 explicit constexpr MyInteger(integer
i) :
i(
i) {}
134 constexpr
operator integer()
const {
return i; }
136 constexpr MyInteger
operator+(MyInteger other)
const {
return i + other.i; }
137 constexpr MyInteger
operator-(MyInteger other)
const {
return i - other.i; }
138 constexpr MyInteger
operator*(MyInteger other)
const {
return i * other.i; }
139 constexpr MyInteger
operator/(MyInteger other)
const {
return i / other.i; }
141 constexpr
bool operator<(MyInteger other)
const {
return i < other.i; }
142 constexpr
bool operator<=(MyInteger other)
const {
return i <= other.i; }
143 constexpr
bool operator==(MyInteger other)
const {
return i == other.i; }
144 constexpr
bool operator>=(MyInteger other)
const {
return i >= other.i; }
145 constexpr
bool operator>(MyInteger other)
const {
return i > other.i; }
146 constexpr
bool operator!=(MyInteger other)
const {
return i != other.i; }
148 integer as_integer()
const {
return i; }
151 typedef MyInteger<int64_t> MyInt64;
152 typedef MyInteger<uint64_t> MyUInt64;
207 char expected[16 + 1];
209 snprintf(expected,
sizeof(expected),
"%016" PRIx64,
static_cast<uint64_t>(
v));
210 EXPECT_EQ(expected, actual) <<
" Input " <<
v;
212 snprintf(expected,
sizeof(expected),
"%16" PRIx64,
static_cast<uint64_t>(
v));
213 EXPECT_EQ(expected, actual) <<
" Input " <<
v;
216 TEST(Numbers, TestFastPrints) {
217 for (
int i = -100;
i <= 100;
i++) {
221 for (
int i = 0;
i <= 100;
i++) {
228 CheckInt64(LONG_MIN);
231 CheckInt64(
uint64_t{100000000000000});
232 CheckInt64(
uint64_t{999999999999999});
233 CheckInt64(
uint64_t{1000000000000000000});
234 CheckInt64(
uint64_t{1199999999999999999});
235 CheckInt64(
int64_t{-700000000000000000});
236 CheckInt64(LONG_MAX);
240 CheckUInt64(
uint64_t{100000000000000});
241 CheckUInt64(
uint64_t{999999999999999});
242 CheckUInt64(
uint64_t{1000000000000000000});
243 CheckUInt64(
uint64_t{1199999999999999999});
246 for (
int i = 0;
i < 10000;
i++) {
249 CheckHex64(
uint64_t{0x123456789abcdef0});
252 template <
typename int_type,
typename in_val_type>
253 void VerifySimpleAtoiGood(in_val_type in_value, int_type exp_value) {
257 int_type
x =
static_cast<int_type
>(~exp_value);
259 <<
"in_value=" << in_value <<
" s=" <<
s <<
" x=" <<
x;
261 x =
static_cast<int_type
>(~exp_value);
266 template <
typename int_type,
typename in_val_type>
267 void VerifySimpleAtoiBad(in_val_type in_value) {
276 TEST(NumbersTest, Atoi) {
278 VerifySimpleAtoiGood<int32_t>(0, 0);
279 VerifySimpleAtoiGood<int32_t>(42, 42);
280 VerifySimpleAtoiGood<int32_t>(-42, -42);
288 VerifySimpleAtoiGood<uint32_t>(0, 0);
289 VerifySimpleAtoiGood<uint32_t>(42, 42);
290 VerifySimpleAtoiBad<uint32_t>(-42);
302 VerifySimpleAtoiGood<int64_t>(0, 0);
303 VerifySimpleAtoiGood<int64_t>(42, 42);
304 VerifySimpleAtoiGood<int64_t>(-42, -42);
319 VerifySimpleAtoiGood<uint64_t>(0, 0);
320 VerifySimpleAtoiGood<uint64_t>(42, 42);
321 VerifySimpleAtoiBad<uint64_t>(-42);
335 VerifySimpleAtoiGood<absl::uint128>(0, 0);
336 VerifySimpleAtoiGood<absl::uint128>(42, 42);
337 VerifySimpleAtoiBad<absl::uint128>(-42);
349 VerifySimpleAtoiGood<absl::uint128>(
354 VerifySimpleAtoiGood<absl::int128>(0, 0);
355 VerifySimpleAtoiGood<absl::int128>(42, 42);
356 VerifySimpleAtoiGood<absl::int128>(-42, -42);
370 VerifySimpleAtoiGood<absl::int128>(
373 VerifySimpleAtoiGood<absl::int128>(
379 VerifySimpleAtoiGood<int>(-42, -42);
380 VerifySimpleAtoiGood<int32_t>(-42, -42);
381 VerifySimpleAtoiGood<uint32_t>(42, 42);
382 VerifySimpleAtoiGood<unsigned int>(42, 42);
383 VerifySimpleAtoiGood<int64_t>(-42, -42);
384 VerifySimpleAtoiGood<long>(-42, -42);
385 VerifySimpleAtoiGood<uint64_t>(42, 42);
386 VerifySimpleAtoiGood<size_t>(42, 42);
387 VerifySimpleAtoiGood<std::string::size_type>(42, 42);
390 TEST(NumbersTest, Atod) {
396 TEST(NumbersTest, Prefixes) {
418 TEST(NumbersTest, Atoenum) {
424 VerifySimpleAtoiGood<E01>(E01_zero, E01_zero);
425 VerifySimpleAtoiGood<E01>(E01_one, E01_one);
433 VerifySimpleAtoiGood<E_101>(E_101_minusone, E_101_minusone);
434 VerifySimpleAtoiGood<E_101>(E_101_zero, E_101_zero);
435 VerifySimpleAtoiGood<E_101>(E_101_one, E_101_one);
440 E_bigint_max31 =
static_cast<int32_t>(0x7FFFFFFF),
443 VerifySimpleAtoiGood<E_bigint>(E_bigint_zero, E_bigint_zero);
444 VerifySimpleAtoiGood<E_bigint>(E_bigint_one, E_bigint_one);
445 VerifySimpleAtoiGood<E_bigint>(E_bigint_max31, E_bigint_max31);
450 E_fullint_max31 =
static_cast<int32_t>(0x7FFFFFFF),
454 VerifySimpleAtoiGood<E_fullint>(E_fullint_zero, E_fullint_zero);
455 VerifySimpleAtoiGood<E_fullint>(E_fullint_one, E_fullint_one);
456 VerifySimpleAtoiGood<E_fullint>(E_fullint_max31, E_fullint_max31);
457 VerifySimpleAtoiGood<E_fullint>(E_fullint_min32, E_fullint_min32);
462 E_biguint_max31 =
static_cast<uint32_t>(0x7FFFFFFF),
463 E_biguint_max32 =
static_cast<uint32_t>(0xFFFFFFFF),
466 VerifySimpleAtoiGood<E_biguint>(E_biguint_zero, E_biguint_zero);
467 VerifySimpleAtoiGood<E_biguint>(E_biguint_one, E_biguint_one);
468 VerifySimpleAtoiGood<E_biguint>(E_biguint_max31, E_biguint_max31);
469 VerifySimpleAtoiGood<E_biguint>(E_biguint_max32, E_biguint_max32);
472 template <
typename int_type,
typename in_val_type>
473 void VerifySimpleHexAtoiGood(in_val_type in_value, int_type exp_value) {
478 strm << std::hex << in_value;
483 int_type
x =
static_cast<int_type
>(~exp_value);
485 <<
"in_value=" << std::hex << in_value <<
" s=" <<
s <<
" x=" <<
x;
487 x =
static_cast<int_type
>(~exp_value);
493 template <
typename int_type,
typename in_val_type>
494 void VerifySimpleHexAtoiBad(in_val_type in_value) {
499 strm << std::hex << in_value;
510 TEST(NumbersTest, HexAtoi) {
512 VerifySimpleHexAtoiGood<int32_t>(0, 0);
513 VerifySimpleHexAtoiGood<int32_t>(0x42, 0x42);
514 VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42);
522 VerifySimpleHexAtoiGood<uint32_t>(0, 0);
523 VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42);
524 VerifySimpleHexAtoiBad<uint32_t>(-0x42);
536 VerifySimpleHexAtoiGood<int64_t>(0, 0);
537 VerifySimpleHexAtoiGood<int64_t>(0x42, 0x42);
538 VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42);
553 VerifySimpleHexAtoiGood<uint64_t>(0, 0);
554 VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42);
555 VerifySimpleHexAtoiBad<uint64_t>(-0x42);
569 VerifySimpleHexAtoiGood<absl::uint128>(0, 0);
570 VerifySimpleHexAtoiGood<absl::uint128>(0x42, 0x42);
571 VerifySimpleHexAtoiBad<absl::uint128>(-0x42);
583 VerifySimpleHexAtoiGood<absl::uint128>(
588 VerifySimpleHexAtoiGood<int>(-0x42, -0x42);
589 VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42);
590 VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42);
591 VerifySimpleHexAtoiGood<unsigned int>(0x42, 0x42);
592 VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42);
593 VerifySimpleHexAtoiGood<long>(-0x42, -0x42);
594 VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42);
595 VerifySimpleHexAtoiGood<size_t>(0x42, 0x42);
596 VerifySimpleHexAtoiGood<std::string::size_type>(0x42, 0x42);
688 TEST(stringtest, safe_strto32_range) {
701 TEST(stringtest, safe_strto64_range) {
714 TEST(stringtest, safe_strto32_leading_substring) {
736 TEST(stringtest, safe_strto64_leading_substring) {
829 const size_t kNumRandomTests = 10000;
831 template <
typename IntType>
836 std::random_device rd;
838 std::uniform_int_distribution<IntType> random_int(
840 std::uniform_int_distribution<int> random_base(2, 35);
841 for (
size_t i = 0;
i < kNumRandomTests;
i++) {
842 IntType
value = random_int(rng);
843 int base = random_base(rng);
846 IntType parsed_value;
855 &parsed_value,
base));
861 &parsed_value,
base));
868 TEST(stringtest, safe_strto32_random) {
871 TEST(stringtest, safe_strto64_random) {
874 TEST(stringtest, safe_strtou32_random) {
877 TEST(stringtest, safe_strtou64_random) {
880 TEST(stringtest, safe_strtou128_random) {
890 std::random_device rd;
892 std::uniform_int_distribution<uint64_t> random_uint64(
894 std::uniform_int_distribution<int> random_base(2, 35);
896 for (
size_t i = 0;
i < kNumRandomTests;
i++) {
897 IntType
value = random_uint64(rng);
899 int base = random_base(rng);
902 IntType parsed_value;
920 TEST(stringtest, safe_strto128_random) {
930 std::random_device rd;
932 std::uniform_int_distribution<int64_t> random_int64(
934 std::uniform_int_distribution<uint64_t> random_uint64(
936 std::uniform_int_distribution<int> random_base(2, 35);
938 for (
size_t i = 0;
i < kNumRandomTests; ++
i) {
939 int64_t high = random_int64(rng);
943 int base = random_base(rng);
946 IntType parsed_value;
971 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
974 <<
"\" base=" <<
e.base;
979 TEST(stringtest, safe_strtou32_base_length_delimited) {
989 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
992 <<
" base=" <<
e.base;
1002 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
1009 TEST(stringtest, safe_strtou64_base_length_delimited) {
1019 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
1028 #if defined(__GLIBC__) || defined(__BIONIC__)
1029 #define ABSL_HAVE_FEENABLEEXCEPT 1
1030 #define ABSL_HAVE_FEDISABLEEXCEPT 1
1035 void SetUp()
override {
1037 feholdexcept(&fp_env_);
1038 #ifdef ABSL_HAVE_FEENABLEEXCEPT
1040 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
1048 #ifdef ABSL_HAVE_FEDISABLEEXCEPT
1049 fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
1069 template <
typename R>
1070 void ExhaustiveFloat(
uint32_t cases, R&& runnable) {
1081 std::set<float> floats = {3.4028234e38f};
1082 for (
float f : {1.0, 3.14159265, 2.718281828, 1 / 2.718281828}) {
1083 for (
float testf =
f; testf != 0; testf *= 0.1f) floats.insert(testf);
1084 for (
float testf =
f; testf != 0; testf *= 0.5f) floats.insert(testf);
1085 for (
float testf =
f; testf < 3e38f / 2; testf *= 2.0f)
1086 floats.insert(testf);
1087 for (
float testf =
f; testf < 3e38f / 10; testf *= 10) floats.insert(testf);
1090 float last = *floats.begin();
1094 int iters_per_float = cases / floats.size();
1095 if (iters_per_float == 0) iters_per_float = 1;
1096 for (
float f : floats) {
1097 if (
f == last)
continue;
1102 if (
f == last)
continue;
1103 double step = (
double{
f} - last) / iters_per_float;
1106 if (testf != last) {
1112 testf = std::nextafter(
f, 0.0
f);
1126 TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) {
1128 std::vector<double> mismatches;
1129 auto checker = [&](
double d) {
1136 if (strcmp(sixdigitsbuf, snprintfbuf) != 0) {
1137 mismatches.push_back(
d);
1138 if (mismatches.size() < 10) {
1141 "=",
d,
" sixdigits=", sixdigitsbuf,
1142 " printf(%g)=", snprintfbuf)
1152 checker(1.7976931348623157e308);
1153 checker(0.00390625);
1157 checker(0.001953125);
1159 checker(0.005859375);
1161 checker(1.089095e-15);
1162 checker(3.274195e-55);
1163 checker(6.534355e-146);
1164 checker(2.920845e+234);
1166 if (mismatches.empty()) {
1168 ExhaustiveFloat(kFloatNumCases, checker);
1171 std::vector<int> digit_testcases{
1172 100000, 100001, 100002, 100005, 100010, 100020, 100050, 100100,
1174 200000, 500000, 800000,
1176 900000, 990000, 999000, 999900, 999990, 999996, 999997, 999998, 999999};
1177 if (kFloatNumCases >= 1e9) {
1180 constexpr
int min_mantissa = 100000, max_mantissa = 999999;
1181 digit_testcases.resize(max_mantissa - min_mantissa + 1);
1182 std::iota(digit_testcases.begin(), digit_testcases.end(), min_mantissa);
1187 if (powten == 0) powten = 5
e-324;
1188 if (kFloatNumCases >= 1e9) {
1198 for (
int digits : digit_testcases) {
1199 if (
exponent == 308 && digits >= 179769)
break;
1200 double digiform = (digits + 0.5) * 0.00001;
1201 double testval = digiform * powten;
1202 double pretestval = nextafter(testval, 0);
1203 double posttestval = nextafter(testval, 1.7976931348623157e308);
1205 checker(pretestval);
1206 checker(posttestval);
1211 for (
size_t i = 0;
i < mismatches.size(); ++
i) {
1212 if (
i > 100)
i = mismatches.size() - 1;
1213 double d = mismatches[
i];
1218 double before = nextafter(
d, 0.0);
1219 double after = nextafter(
d, 1.7976931348623157e308);
1224 "Mismatch #",
i,
" d=",
d,
" (", ToNineDigits(
d),
")",
1225 " sixdigits='", sixdigitsbuf,
"'",
" snprintf='", snprintfbuf,
1226 "'",
" Before.=", PerfectDtoa(
before),
" ",
1228 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
before),
b1),
1230 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
d),
b1),
1231 " After.=.", PerfectDtoa(
after),
" ",
1233 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
after),
b1))
1239 TEST(StrToInt32, Partial) {
1240 struct Int32TestLine {
1247 Int32TestLine int32_test_line[] = {
1251 {
"123@@@",
false, 123},
1252 {
absl::StrCat(int32_min, int32_max),
false, int32_min},
1253 {
absl::StrCat(int32_max, int32_max),
false, int32_max},
1256 for (
const Int32TestLine& test_line : int32_test_line) {
1272 TEST(StrToUint32, Partial) {
1273 struct Uint32TestLine {
1279 Uint32TestLine uint32_test_line[] = {
1283 {
"123@@@",
false, 123},
1284 {
absl::StrCat(uint32_max, uint32_max),
false, uint32_max},
1287 for (
const Uint32TestLine& test_line : uint32_test_line) {
1303 TEST(StrToInt64, Partial) {
1304 struct Int64TestLine {
1311 Int64TestLine int64_test_line[] = {
1315 {
"123@@@",
false, 123},
1316 {
absl::StrCat(int64_min, int64_max),
false, int64_min},
1317 {
absl::StrCat(int64_max, int64_max),
false, int64_max},
1320 for (
const Int64TestLine& test_line : int64_test_line) {
1336 TEST(StrToUint64, Partial) {
1337 struct Uint64TestLine {
1343 Uint64TestLine uint64_test_line[] = {
1347 {
"123@@@",
false, 123},
1348 {
absl::StrCat(uint64_max, uint64_max),
false, uint64_max},
1351 for (
const Uint64TestLine& test_line : uint64_test_line) {
1367 TEST(StrToInt32Base, PrefixOnly) {
1368 struct Int32TestLine {
1373 Int32TestLine int32_test_line[] = {
1379 {
"-0x",
false, 0 },
1381 const int base_array[] = { 0, 2, 8, 10, 16 };
1383 for (
const Int32TestLine&
line : int32_test_line) {
1384 for (
const int base : base_array) {
1401 TEST(StrToUint32Base, PrefixOnly) {
1402 struct Uint32TestLine {
1407 Uint32TestLine uint32_test_line[] = {
1412 const int base_array[] = { 0, 2, 8, 10, 16 };
1414 for (
const Uint32TestLine&
line : uint32_test_line) {
1415 for (
const int base : base_array) {
1432 TEST(StrToInt64Base, PrefixOnly) {
1433 struct Int64TestLine {
1438 Int64TestLine int64_test_line[] = {
1444 {
"-0x",
false, 0 },
1446 const int base_array[] = { 0, 2, 8, 10, 16 };
1448 for (
const Int64TestLine&
line : int64_test_line) {
1449 for (
const int base : base_array) {
1466 TEST(StrToUint64Base, PrefixOnly) {
1467 struct Uint64TestLine {
1472 Uint64TestLine uint64_test_line[] = {
1477 const int base_array[] = { 0, 2, 8, 10, 16 };
1479 for (
const Uint64TestLine&
line : uint64_test_line) {
1480 for (
const int base : base_array) {
1497 void TestFastHexToBufferZeroPad16(
uint64_t v) {
1502 snprintf(
buf2,
sizeof(
buf2),
"%016" PRIx64,
v);
1504 size_t expected_digits = snprintf(
buf2,
sizeof(
buf2),
"%" PRIx64,
v);
1514 for (
int i = 0;
i < 100000; ++
i) {
1515 TestFastHexToBufferZeroPad16(