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"
67 const int kFloatNumCases = 5000000;
73 if (
d == 0)
return "0";
74 if (
d < 0)
return "-" + PerfectDtoa(-
d);
79 while (
d >= 1
ULL << 63) ++exp,
d *= 0.5;
85 constexpr
int maxlen = 1100;
93 for (
int i = 0;
i != exp;
i += (exp > 0) ? 1 : -1) {
97 dig = dig * (exp > 0 ? 2 : 5) + carry;
105 memmove(
end + 1 + exp,
end + exp, 1 - exp);
116 Eq(
"0.1000000000000000055511151231257827021181583404541015625"));
117 EXPECT_THAT(PerfectDtoa(1e24),
Eq(
"999999999999999983222784"));
119 for (
int i = 0;
i < 100; ++
i) {
120 for (
double multiplier :
121 {1
e-300, 1
e-200, 1
e-100, 0.1, 1.0, 10.0, 1e100, 1e300}) {
122 double d = multiplier *
i;
129 template <
typename integer>
132 explicit constexpr MyInteger(integer
i) :
i(
i) {}
133 constexpr
operator integer()
const {
return i; }
135 constexpr MyInteger
operator+(MyInteger other)
const {
return i + other.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; }
140 constexpr
bool 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; }
147 integer as_integer()
const {
return i; }
150 typedef MyInteger<int64_t> MyInt64;
151 typedef MyInteger<uint64_t> MyUInt64;
206 char expected[16 + 1];
208 snprintf(expected,
sizeof(expected),
"%016" PRIx64,
static_cast<uint64_t>(
v));
209 EXPECT_EQ(expected, actual) <<
" Input " <<
v;
211 snprintf(expected,
sizeof(expected),
"%16" PRIx64,
static_cast<uint64_t>(
v));
212 EXPECT_EQ(expected, actual) <<
" Input " <<
v;
215 TEST(Numbers, TestFastPrints) {
216 for (
int i = -100;
i <= 100;
i++) {
220 for (
int i = 0;
i <= 100;
i++) {
227 CheckInt64(LONG_MIN);
230 CheckInt64(
uint64_t{100000000000000});
231 CheckInt64(
uint64_t{999999999999999});
232 CheckInt64(
uint64_t{1000000000000000000});
233 CheckInt64(
uint64_t{1199999999999999999});
234 CheckInt64(
int64_t{-700000000000000000});
235 CheckInt64(LONG_MAX);
239 CheckUInt64(
uint64_t{100000000000000});
240 CheckUInt64(
uint64_t{999999999999999});
241 CheckUInt64(
uint64_t{1000000000000000000});
242 CheckUInt64(
uint64_t{1199999999999999999});
245 for (
int i = 0;
i < 10000;
i++) {
248 CheckHex64(
uint64_t{0x123456789abcdef0});
251 template <
typename int_type,
typename in_val_type>
252 void VerifySimpleAtoiGood(in_val_type in_value, int_type exp_value) {
256 int_type
x =
static_cast<int_type
>(~exp_value);
258 <<
"in_value=" << in_value <<
" s=" <<
s <<
" x=" <<
x;
260 x =
static_cast<int_type
>(~exp_value);
265 template <
typename int_type,
typename in_val_type>
266 void VerifySimpleAtoiBad(in_val_type in_value) {
275 TEST(NumbersTest, Atoi) {
277 VerifySimpleAtoiGood<int32_t>(0, 0);
278 VerifySimpleAtoiGood<int32_t>(42, 42);
279 VerifySimpleAtoiGood<int32_t>(-42, -42);
287 VerifySimpleAtoiGood<uint32_t>(0, 0);
288 VerifySimpleAtoiGood<uint32_t>(42, 42);
289 VerifySimpleAtoiBad<uint32_t>(-42);
301 VerifySimpleAtoiGood<int64_t>(0, 0);
302 VerifySimpleAtoiGood<int64_t>(42, 42);
303 VerifySimpleAtoiGood<int64_t>(-42, -42);
318 VerifySimpleAtoiGood<uint64_t>(0, 0);
319 VerifySimpleAtoiGood<uint64_t>(42, 42);
320 VerifySimpleAtoiBad<uint64_t>(-42);
334 VerifySimpleAtoiGood<absl::uint128>(0, 0);
335 VerifySimpleAtoiGood<absl::uint128>(42, 42);
336 VerifySimpleAtoiBad<absl::uint128>(-42);
348 VerifySimpleAtoiGood<absl::uint128>(
353 VerifySimpleAtoiGood<absl::int128>(0, 0);
354 VerifySimpleAtoiGood<absl::int128>(42, 42);
355 VerifySimpleAtoiGood<absl::int128>(-42, -42);
369 VerifySimpleAtoiGood<absl::int128>(
372 VerifySimpleAtoiGood<absl::int128>(
378 VerifySimpleAtoiGood<int>(-42, -42);
379 VerifySimpleAtoiGood<int32_t>(-42, -42);
380 VerifySimpleAtoiGood<uint32_t>(42, 42);
381 VerifySimpleAtoiGood<unsigned int>(42, 42);
382 VerifySimpleAtoiGood<int64_t>(-42, -42);
383 VerifySimpleAtoiGood<long>(-42, -42);
384 VerifySimpleAtoiGood<uint64_t>(42, 42);
385 VerifySimpleAtoiGood<size_t>(42, 42);
386 VerifySimpleAtoiGood<std::string::size_type>(42, 42);
389 TEST(NumbersTest, Atod) {
395 TEST(NumbersTest, Prefixes) {
417 TEST(NumbersTest, Atoenum) {
423 VerifySimpleAtoiGood<E01>(E01_zero, E01_zero);
424 VerifySimpleAtoiGood<E01>(E01_one, E01_one);
432 VerifySimpleAtoiGood<E_101>(E_101_minusone, E_101_minusone);
433 VerifySimpleAtoiGood<E_101>(E_101_zero, E_101_zero);
434 VerifySimpleAtoiGood<E_101>(E_101_one, E_101_one);
439 E_bigint_max31 =
static_cast<int32_t>(0x7FFFFFFF),
442 VerifySimpleAtoiGood<E_bigint>(E_bigint_zero, E_bigint_zero);
443 VerifySimpleAtoiGood<E_bigint>(E_bigint_one, E_bigint_one);
444 VerifySimpleAtoiGood<E_bigint>(E_bigint_max31, E_bigint_max31);
449 E_fullint_max31 =
static_cast<int32_t>(0x7FFFFFFF),
453 VerifySimpleAtoiGood<E_fullint>(E_fullint_zero, E_fullint_zero);
454 VerifySimpleAtoiGood<E_fullint>(E_fullint_one, E_fullint_one);
455 VerifySimpleAtoiGood<E_fullint>(E_fullint_max31, E_fullint_max31);
456 VerifySimpleAtoiGood<E_fullint>(E_fullint_min32, E_fullint_min32);
461 E_biguint_max31 =
static_cast<uint32_t>(0x7FFFFFFF),
462 E_biguint_max32 =
static_cast<uint32_t>(0xFFFFFFFF),
465 VerifySimpleAtoiGood<E_biguint>(E_biguint_zero, E_biguint_zero);
466 VerifySimpleAtoiGood<E_biguint>(E_biguint_one, E_biguint_one);
467 VerifySimpleAtoiGood<E_biguint>(E_biguint_max31, E_biguint_max31);
468 VerifySimpleAtoiGood<E_biguint>(E_biguint_max32, E_biguint_max32);
545 TEST(stringtest, safe_strto32_range) {
558 TEST(stringtest, safe_strto64_range) {
571 TEST(stringtest, safe_strto32_leading_substring) {
593 TEST(stringtest, safe_strto64_leading_substring) {
686 const size_t kNumRandomTests = 10000;
688 template <
typename IntType>
693 std::random_device rd;
695 std::uniform_int_distribution<IntType> random_int(
697 std::uniform_int_distribution<int> random_base(2, 35);
698 for (
size_t i = 0;
i < kNumRandomTests;
i++) {
699 IntType
value = random_int(rng);
700 int base = random_base(rng);
703 IntType parsed_value;
712 &parsed_value,
base));
718 &parsed_value,
base));
725 TEST(stringtest, safe_strto32_random) {
728 TEST(stringtest, safe_strto64_random) {
731 TEST(stringtest, safe_strtou32_random) {
734 TEST(stringtest, safe_strtou64_random) {
737 TEST(stringtest, safe_strtou128_random) {
747 std::random_device rd;
749 std::uniform_int_distribution<uint64_t> random_uint64(
751 std::uniform_int_distribution<int> random_base(2, 35);
753 for (
size_t i = 0;
i < kNumRandomTests;
i++) {
754 IntType
value = random_uint64(rng);
756 int base = random_base(rng);
759 IntType parsed_value;
777 TEST(stringtest, safe_strto128_random) {
787 std::random_device rd;
789 std::uniform_int_distribution<int64_t> random_int64(
791 std::uniform_int_distribution<uint64_t> random_uint64(
793 std::uniform_int_distribution<int> random_base(2, 35);
795 for (
size_t i = 0;
i < kNumRandomTests; ++
i) {
796 int64_t high = random_int64(rng);
800 int base = random_base(rng);
803 IntType parsed_value;
828 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
831 <<
"\" base=" <<
e.base;
836 TEST(stringtest, safe_strtou32_base_length_delimited) {
846 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
849 <<
" base=" <<
e.base;
859 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
866 TEST(stringtest, safe_strtou64_base_length_delimited) {
876 <<
"str=\"" <<
e.str <<
"\" base=" <<
e.base;
885 #if defined(__GLIBC__) || defined(__BIONIC__)
886 #define ABSL_HAVE_FEENABLEEXCEPT 1
887 #define ABSL_HAVE_FEDISABLEEXCEPT 1
892 void SetUp()
override {
894 feholdexcept(&fp_env_);
895 #ifdef ABSL_HAVE_FEENABLEEXCEPT
897 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
905 #ifdef ABSL_HAVE_FEDISABLEEXCEPT
906 fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
926 template <
typename R>
927 void ExhaustiveFloat(
uint32_t cases, R&& runnable) {
938 std::set<float> floats = {3.4028234e38f};
939 for (
float f : {1.0, 3.14159265, 2.718281828, 1 / 2.718281828}) {
940 for (
float testf =
f; testf != 0; testf *= 0.1f) floats.insert(testf);
941 for (
float testf =
f; testf != 0; testf *= 0.5f) floats.insert(testf);
942 for (
float testf =
f; testf < 3e38f / 2; testf *= 2.0f)
943 floats.insert(testf);
944 for (
float testf =
f; testf < 3e38f / 10; testf *= 10) floats.insert(testf);
947 float last = *floats.begin();
951 int iters_per_float = cases / floats.size();
952 if (iters_per_float == 0) iters_per_float = 1;
953 for (
float f : floats) {
954 if (
f == last)
continue;
959 if (
f == last)
continue;
960 double step = (
double{
f} - last) / iters_per_float;
969 testf = std::nextafter(
f, 0.0
f);
983 TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) {
985 std::vector<double> mismatches;
986 auto checker = [&](
double d) {
993 if (strcmp(sixdigitsbuf, snprintfbuf) != 0) {
994 mismatches.push_back(
d);
995 if (mismatches.size() < 10) {
998 "=",
d,
" sixdigits=", sixdigitsbuf,
999 " printf(%g)=", snprintfbuf)
1009 checker(1.7976931348623157e308);
1010 checker(0.00390625);
1014 checker(0.001953125);
1016 checker(0.005859375);
1018 checker(1.089095e-15);
1019 checker(3.274195e-55);
1020 checker(6.534355e-146);
1021 checker(2.920845e+234);
1023 if (mismatches.empty()) {
1025 ExhaustiveFloat(kFloatNumCases, checker);
1028 std::vector<int> digit_testcases{
1029 100000, 100001, 100002, 100005, 100010, 100020, 100050, 100100,
1031 200000, 500000, 800000,
1033 900000, 990000, 999000, 999900, 999990, 999996, 999997, 999998, 999999};
1034 if (kFloatNumCases >= 1e9) {
1037 constexpr
int min_mantissa = 100000, max_mantissa = 999999;
1038 digit_testcases.resize(max_mantissa - min_mantissa + 1);
1039 std::iota(digit_testcases.begin(), digit_testcases.end(), min_mantissa);
1044 if (powten == 0) powten = 5
e-324;
1045 if (kFloatNumCases >= 1e9) {
1055 for (
int digits : digit_testcases) {
1056 if (
exponent == 308 && digits >= 179769)
break;
1057 double digiform = (digits + 0.5) * 0.00001;
1058 double testval = digiform * powten;
1059 double pretestval = nextafter(testval, 0);
1060 double posttestval = nextafter(testval, 1.7976931348623157e308);
1062 checker(pretestval);
1063 checker(posttestval);
1068 for (
size_t i = 0;
i < mismatches.size(); ++
i) {
1069 if (
i > 100)
i = mismatches.size() - 1;
1070 double d = mismatches[
i];
1075 double before = nextafter(
d, 0.0);
1076 double after = nextafter(
d, 1.7976931348623157e308);
1081 "Mismatch #",
i,
" d=",
d,
" (", ToNineDigits(
d),
")",
1082 " sixdigits='", sixdigitsbuf,
"'",
" snprintf='", snprintfbuf,
1083 "'",
" Before.=", PerfectDtoa(
before),
" ",
1085 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
before),
b1),
1087 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
d),
b1),
1088 " After.=.", PerfectDtoa(
after),
" ",
1090 " vs snprintf=", (snprintf(
b1,
sizeof(
b1),
"%g",
after),
b1))
1096 TEST(StrToInt32, Partial) {
1097 struct Int32TestLine {
1104 Int32TestLine int32_test_line[] = {
1108 {
"123@@@",
false, 123},
1109 {
absl::StrCat(int32_min, int32_max),
false, int32_min},
1110 {
absl::StrCat(int32_max, int32_max),
false, int32_max},
1113 for (
const Int32TestLine& test_line : int32_test_line) {
1129 TEST(StrToUint32, Partial) {
1130 struct Uint32TestLine {
1136 Uint32TestLine uint32_test_line[] = {
1140 {
"123@@@",
false, 123},
1141 {
absl::StrCat(uint32_max, uint32_max),
false, uint32_max},
1144 for (
const Uint32TestLine& test_line : uint32_test_line) {
1160 TEST(StrToInt64, Partial) {
1161 struct Int64TestLine {
1168 Int64TestLine int64_test_line[] = {
1172 {
"123@@@",
false, 123},
1173 {
absl::StrCat(int64_min, int64_max),
false, int64_min},
1174 {
absl::StrCat(int64_max, int64_max),
false, int64_max},
1177 for (
const Int64TestLine& test_line : int64_test_line) {
1193 TEST(StrToUint64, Partial) {
1194 struct Uint64TestLine {
1200 Uint64TestLine uint64_test_line[] = {
1204 {
"123@@@",
false, 123},
1205 {
absl::StrCat(uint64_max, uint64_max),
false, uint64_max},
1208 for (
const Uint64TestLine& test_line : uint64_test_line) {
1224 TEST(StrToInt32Base, PrefixOnly) {
1225 struct Int32TestLine {
1230 Int32TestLine int32_test_line[] = {
1236 {
"-0x",
false, 0 },
1238 const int base_array[] = { 0, 2, 8, 10, 16 };
1240 for (
const Int32TestLine&
line : int32_test_line) {
1241 for (
const int base : base_array) {
1258 TEST(StrToUint32Base, PrefixOnly) {
1259 struct Uint32TestLine {
1264 Uint32TestLine uint32_test_line[] = {
1269 const int base_array[] = { 0, 2, 8, 10, 16 };
1271 for (
const Uint32TestLine&
line : uint32_test_line) {
1272 for (
const int base : base_array) {
1289 TEST(StrToInt64Base, PrefixOnly) {
1290 struct Int64TestLine {
1295 Int64TestLine int64_test_line[] = {
1301 {
"-0x",
false, 0 },
1303 const int base_array[] = { 0, 2, 8, 10, 16 };
1305 for (
const Int64TestLine&
line : int64_test_line) {
1306 for (
const int base : base_array) {
1323 TEST(StrToUint64Base, PrefixOnly) {
1324 struct Uint64TestLine {
1329 Uint64TestLine uint64_test_line[] = {
1334 const int base_array[] = { 0, 2, 8, 10, 16 };
1336 for (
const Uint64TestLine&
line : uint64_test_line) {
1337 for (
const int base : base_array) {
1354 void TestFastHexToBufferZeroPad16(
uint64_t v) {
1359 snprintf(
buf2,
sizeof(
buf2),
"%016" PRIx64,
v);
1361 size_t expected_digits = snprintf(
buf2,
sizeof(
buf2),
"%" PRIx64,
v);
1371 for (
int i = 0;
i < 100000; ++
i) {
1372 TestFastHexToBufferZeroPad16(