numbers_test.cc (84403B)
1 // Copyright 2017 The Abseil Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // This file tests string processing functions related to numeric values. 16 17 #include "absl/strings/numbers.h" 18 19 #include <sys/types.h> 20 21 #include <cfenv> // NOLINT(build/c++11) 22 #include <cfloat> 23 #include <cinttypes> 24 #include <climits> 25 #include <cmath> 26 #include <cstddef> 27 #include <cstdint> 28 #include <cstdio> 29 #include <cstdlib> 30 #include <cstring> 31 #include <ios> 32 #include <limits> 33 #include <numeric> 34 #include <random> 35 #include <set> 36 #include <string> 37 #include <type_traits> 38 #include <vector> 39 40 #include "gmock/gmock.h" 41 #include "gtest/gtest.h" 42 #include "absl/log/log.h" 43 #include "absl/numeric/int128.h" 44 #include "absl/random/distributions.h" 45 #include "absl/random/random.h" 46 #include "absl/strings/internal/numbers_test_common.h" 47 #include "absl/strings/internal/ostringstream.h" 48 #include "absl/strings/internal/pow10_helper.h" 49 #include "absl/strings/str_cat.h" 50 #include "absl/strings/string_view.h" 51 52 namespace { 53 54 using absl::SimpleAtoi; 55 using absl::SimpleHexAtoi; 56 using absl::numbers_internal::kSixDigitsToBufferSize; 57 using absl::numbers_internal::safe_strto16_base; 58 using absl::numbers_internal::safe_strto32_base; 59 using absl::numbers_internal::safe_strto64_base; 60 using absl::numbers_internal::safe_strto8_base; 61 using absl::numbers_internal::safe_strtou16_base; 62 using absl::numbers_internal::safe_strtou32_base; 63 using absl::numbers_internal::safe_strtou64_base; 64 using absl::numbers_internal::safe_strtou8_base; 65 using absl::numbers_internal::SixDigitsToBuffer; 66 using absl::strings_internal::Itoa; 67 using absl::strings_internal::strtouint32_test_cases; 68 using absl::strings_internal::strtouint64_test_cases; 69 using testing::Eq; 70 using testing::MatchesRegex; 71 using testing::Pointee; 72 73 // Number of floats to test with. 74 // 5,000,000 is a reasonable default for a test that only takes a few seconds. 75 // 1,000,000,000+ triggers checking for all possible mantissa values for 76 // double-precision tests. 2,000,000,000+ triggers checking for every possible 77 // single-precision float. 78 const int kFloatNumCases = 5000000; 79 80 // This is a slow, brute-force routine to compute the exact base-10 81 // representation of a double-precision floating-point number. It 82 // is useful for debugging only. 83 std::string PerfectDtoa(double d) { 84 if (d == 0) return "0"; 85 if (d < 0) return "-" + PerfectDtoa(-d); 86 87 // Basic theory: decompose d into mantissa and exp, where 88 // d = mantissa * 2^exp, and exp is as close to zero as possible. 89 int64_t mantissa, exp = 0; 90 while (d >= 1ULL << 63) ++exp, d *= 0.5; 91 while ((mantissa = d) != d) --exp, d *= 2.0; 92 93 // Then convert mantissa to ASCII, and either double it (if 94 // exp > 0) or halve it (if exp < 0) repeatedly. "halve it" 95 // in this case means multiplying it by five and dividing by 10. 96 constexpr int maxlen = 1100; // worst case is actually 1030 or so. 97 char buf[maxlen + 5]; 98 for (int64_t num = mantissa, pos = maxlen; --pos >= 0;) { 99 buf[pos] = '0' + (num % 10); 100 num /= 10; 101 } 102 char* begin = &buf[0]; 103 char* end = buf + maxlen; 104 for (int i = 0; i != exp; i += (exp > 0) ? 1 : -1) { 105 int carry = 0; 106 for (char* p = end; --p != begin;) { 107 int dig = *p - '0'; 108 dig = dig * (exp > 0 ? 2 : 5) + carry; 109 carry = dig / 10; 110 dig %= 10; 111 *p = '0' + dig; 112 } 113 } 114 if (exp < 0) { 115 // "dividing by 10" above means we have to add the decimal point. 116 memmove(end + 1 + exp, end + exp, 1 - exp); 117 end[exp] = '.'; 118 ++end; 119 } 120 while (*begin == '0' && begin[1] != '.') ++begin; 121 return {begin, end}; 122 } 123 124 TEST(ToString, PerfectDtoa) { 125 EXPECT_THAT(PerfectDtoa(1), Eq("1")); 126 EXPECT_THAT(PerfectDtoa(0.1), 127 Eq("0.1000000000000000055511151231257827021181583404541015625")); 128 EXPECT_THAT(PerfectDtoa(1e24), Eq("999999999999999983222784")); 129 EXPECT_THAT(PerfectDtoa(5e-324), MatchesRegex("0.0000.*625")); 130 for (int i = 0; i < 100; ++i) { 131 for (double multiplier : 132 {1e-300, 1e-200, 1e-100, 0.1, 1.0, 10.0, 1e100, 1e300}) { 133 double d = multiplier * i; 134 std::string s = PerfectDtoa(d); 135 EXPECT_DOUBLE_EQ(d, strtod(s.c_str(), nullptr)); 136 } 137 } 138 } 139 140 template <typename integer> 141 struct MyInteger { 142 integer i; 143 explicit constexpr MyInteger(integer i) : i(i) {} 144 constexpr operator integer() const { return i; } 145 146 constexpr MyInteger operator+(MyInteger other) const { return i + other.i; } 147 constexpr MyInteger operator-(MyInteger other) const { return i - other.i; } 148 constexpr MyInteger operator*(MyInteger other) const { return i * other.i; } 149 constexpr MyInteger operator/(MyInteger other) const { return i / other.i; } 150 151 constexpr bool operator<(MyInteger other) const { return i < other.i; } 152 constexpr bool operator<=(MyInteger other) const { return i <= other.i; } 153 constexpr bool operator==(MyInteger other) const { return i == other.i; } 154 constexpr bool operator>=(MyInteger other) const { return i >= other.i; } 155 constexpr bool operator>(MyInteger other) const { return i > other.i; } 156 constexpr bool operator!=(MyInteger other) const { return i != other.i; } 157 158 integer as_integer() const { return i; } 159 }; 160 161 typedef MyInteger<int64_t> MyInt64; 162 typedef MyInteger<uint64_t> MyUInt64; 163 164 void CheckInt32(int32_t x) { 165 char buffer[absl::numbers_internal::kFastToBufferSize]; 166 char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); 167 std::string expected = std::to_string(x); 168 EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; 169 170 char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); 171 EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; 172 } 173 174 void CheckInt64(int64_t x) { 175 char buffer[absl::numbers_internal::kFastToBufferSize + 3]; 176 buffer[0] = '*'; 177 buffer[23] = '*'; 178 buffer[24] = '*'; 179 char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); 180 std::string expected = std::to_string(x); 181 EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; 182 EXPECT_EQ(buffer[0], '*'); 183 EXPECT_EQ(buffer[23], '*'); 184 EXPECT_EQ(buffer[24], '*'); 185 186 char* my_actual = 187 absl::numbers_internal::FastIntToBuffer(MyInt64(x), &buffer[1]); 188 EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; 189 } 190 191 void CheckUInt32(uint32_t x) { 192 char buffer[absl::numbers_internal::kFastToBufferSize]; 193 char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); 194 std::string expected = std::to_string(x); 195 EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; 196 197 char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); 198 EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; 199 } 200 201 void CheckUInt64(uint64_t x) { 202 char buffer[absl::numbers_internal::kFastToBufferSize + 1]; 203 char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); 204 std::string expected = std::to_string(x); 205 EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; 206 207 char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); 208 EXPECT_EQ(expected, std::string(&buffer[1], generic_actual)) 209 << " Input " << x; 210 211 char* my_actual = 212 absl::numbers_internal::FastIntToBuffer(MyUInt64(x), &buffer[1]); 213 EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; 214 } 215 216 void CheckHex64(uint64_t v) { 217 char expected[16 + 1]; 218 std::string actual = absl::StrCat(absl::Hex(v, absl::kZeroPad16)); 219 snprintf(expected, sizeof(expected), "%016" PRIx64, static_cast<uint64_t>(v)); 220 EXPECT_EQ(expected, actual) << " Input " << v; 221 actual = absl::StrCat(absl::Hex(v, absl::kSpacePad16)); 222 snprintf(expected, sizeof(expected), "%16" PRIx64, static_cast<uint64_t>(v)); 223 EXPECT_EQ(expected, actual) << " Input " << v; 224 } 225 226 TEST(Numbers, TestFastPrints) { 227 for (int i = -100; i <= 100; i++) { 228 CheckInt32(i); 229 CheckInt64(i); 230 } 231 for (int i = 0; i <= 100; i++) { 232 CheckUInt32(i); 233 CheckUInt64(i); 234 } 235 // Test min int to make sure that works 236 CheckInt32(INT_MIN); 237 CheckInt32(INT_MAX); 238 CheckInt64(LONG_MIN); 239 CheckInt64(uint64_t{1000000000}); 240 CheckInt64(uint64_t{9999999999}); 241 CheckInt64(uint64_t{100000000000000}); 242 CheckInt64(uint64_t{999999999999999}); 243 CheckInt64(uint64_t{1000000000000000000}); 244 CheckInt64(uint64_t{1199999999999999999}); 245 CheckInt64(int64_t{-700000000000000000}); 246 CheckInt64(LONG_MAX); 247 CheckUInt32(std::numeric_limits<uint32_t>::max()); 248 CheckUInt64(uint64_t{1000000000}); 249 CheckUInt64(uint64_t{9999999999}); 250 CheckUInt64(uint64_t{100000000000000}); 251 CheckUInt64(uint64_t{999999999999999}); 252 CheckUInt64(uint64_t{1000000000000000000}); 253 CheckUInt64(uint64_t{1199999999999999999}); 254 CheckUInt64(std::numeric_limits<uint64_t>::max()); 255 256 for (int i = 0; i < 10000; i++) { 257 CheckHex64(i); 258 } 259 CheckHex64(uint64_t{0x123456789abcdef0}); 260 } 261 262 template <typename int_type, typename in_val_type> 263 void VerifySimpleAtoiGood(in_val_type in_value, int_type exp_value) { 264 std::string s = absl::StrCat(in_value); 265 int_type x = static_cast<int_type>(~exp_value); 266 EXPECT_TRUE(SimpleAtoi(s, &x)) 267 << "in_value=" << in_value << " s=" << s << " x=" << x; 268 EXPECT_EQ(exp_value, x); 269 x = static_cast<int_type>(~exp_value); 270 EXPECT_TRUE(SimpleAtoi(s.c_str(), &x)); 271 EXPECT_EQ(exp_value, x); 272 } 273 274 template <typename int_type, typename in_val_type> 275 void VerifySimpleAtoiBad(in_val_type in_value) { 276 std::string s = absl::StrCat(in_value); 277 int_type x; 278 EXPECT_FALSE(SimpleAtoi(s, &x)); 279 EXPECT_FALSE(SimpleAtoi(s.c_str(), &x)); 280 } 281 282 TEST(NumbersTest, Atoi) { 283 // SimpleAtoi(absl::string_view, int8_t) 284 VerifySimpleAtoiGood<int8_t>(0, 0); 285 VerifySimpleAtoiGood<int8_t>(42, 42); 286 VerifySimpleAtoiGood<int8_t>(-42, -42); 287 288 VerifySimpleAtoiGood<int8_t>(std::numeric_limits<int8_t>::min(), 289 std::numeric_limits<int8_t>::min()); 290 VerifySimpleAtoiGood<int8_t>(std::numeric_limits<int8_t>::max(), 291 std::numeric_limits<int8_t>::max()); 292 293 VerifySimpleAtoiBad<int8_t>(std::numeric_limits<uint8_t>::max()); 294 VerifySimpleAtoiBad<int8_t>(std::numeric_limits<int16_t>::min()); 295 VerifySimpleAtoiBad<int8_t>(std::numeric_limits<int16_t>::max()); 296 297 // SimpleAtoi(absl::string_view, uint8_t) 298 VerifySimpleAtoiGood<uint8_t>(0, 0); 299 VerifySimpleAtoiGood<uint8_t>(42, 42); 300 VerifySimpleAtoiBad<uint8_t>(-42); 301 302 VerifySimpleAtoiBad<uint8_t>(std::numeric_limits<int8_t>::min()); 303 VerifySimpleAtoiGood<uint8_t>(std::numeric_limits<int8_t>::max(), 304 std::numeric_limits<int8_t>::max()); 305 VerifySimpleAtoiGood<uint8_t>(std::numeric_limits<uint8_t>::max(), 306 std::numeric_limits<uint8_t>::max()); 307 308 VerifySimpleAtoiBad<uint8_t>(std::numeric_limits<int16_t>::min()); 309 VerifySimpleAtoiBad<uint8_t>(std::numeric_limits<int16_t>::max()); 310 VerifySimpleAtoiBad<uint8_t>(std::numeric_limits<uint16_t>::max()); 311 312 // SimpleAtoi(absl::string_view, uint16_t) 313 VerifySimpleAtoiGood<int16_t>(0, 0); 314 VerifySimpleAtoiGood<int16_t>(42, 42); 315 VerifySimpleAtoiGood<int16_t>(-42, -42); 316 317 VerifySimpleAtoiGood<int16_t>(std::numeric_limits<int16_t>::min(), 318 std::numeric_limits<int16_t>::min()); 319 VerifySimpleAtoiGood<int16_t>(std::numeric_limits<int16_t>::max(), 320 std::numeric_limits<int16_t>::max()); 321 322 VerifySimpleAtoiBad<int16_t>(std::numeric_limits<uint16_t>::max()); 323 VerifySimpleAtoiBad<int16_t>(std::numeric_limits<int32_t>::min()); 324 VerifySimpleAtoiBad<int16_t>(std::numeric_limits<int32_t>::max()); 325 326 // SimpleAtoi(absl::string_view, uint16_t) 327 VerifySimpleAtoiGood<uint16_t>(0, 0); 328 VerifySimpleAtoiGood<uint16_t>(42, 42); 329 VerifySimpleAtoiBad<uint16_t>(-42); 330 331 VerifySimpleAtoiBad<uint16_t>(std::numeric_limits<int16_t>::min()); 332 VerifySimpleAtoiGood<uint16_t>(std::numeric_limits<int16_t>::max(), 333 std::numeric_limits<int16_t>::max()); 334 VerifySimpleAtoiGood<uint16_t>(std::numeric_limits<uint16_t>::max(), 335 std::numeric_limits<uint16_t>::max()); 336 337 VerifySimpleAtoiBad<uint16_t>(std::numeric_limits<int16_t>::min()); 338 VerifySimpleAtoiBad<uint16_t>(std::numeric_limits<int32_t>::max()); 339 VerifySimpleAtoiBad<uint16_t>(std::numeric_limits<uint32_t>::max()); 340 341 // SimpleAtoi(absl::string_view, int32_t) 342 VerifySimpleAtoiGood<int32_t>(0, 0); 343 VerifySimpleAtoiGood<int32_t>(42, 42); 344 VerifySimpleAtoiGood<int32_t>(-42, -42); 345 346 VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(), 347 std::numeric_limits<int32_t>::min()); 348 VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(), 349 std::numeric_limits<int32_t>::max()); 350 351 // SimpleAtoi(absl::string_view, uint32_t) 352 VerifySimpleAtoiGood<uint32_t>(0, 0); 353 VerifySimpleAtoiGood<uint32_t>(42, 42); 354 VerifySimpleAtoiBad<uint32_t>(-42); 355 356 VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min()); 357 VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(), 358 std::numeric_limits<int32_t>::max()); 359 VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(), 360 std::numeric_limits<uint32_t>::max()); 361 VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min()); 362 VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max()); 363 VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max()); 364 365 // SimpleAtoi(absl::string_view, int64_t) 366 VerifySimpleAtoiGood<int64_t>(0, 0); 367 VerifySimpleAtoiGood<int64_t>(42, 42); 368 VerifySimpleAtoiGood<int64_t>(-42, -42); 369 370 VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(), 371 std::numeric_limits<int32_t>::min()); 372 VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(), 373 std::numeric_limits<int32_t>::max()); 374 VerifySimpleAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(), 375 std::numeric_limits<uint32_t>::max()); 376 VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(), 377 std::numeric_limits<int64_t>::min()); 378 VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(), 379 std::numeric_limits<int64_t>::max()); 380 VerifySimpleAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max()); 381 382 // SimpleAtoi(absl::string_view, uint64_t) 383 VerifySimpleAtoiGood<uint64_t>(0, 0); 384 VerifySimpleAtoiGood<uint64_t>(42, 42); 385 VerifySimpleAtoiBad<uint64_t>(-42); 386 387 VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min()); 388 VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(), 389 std::numeric_limits<int32_t>::max()); 390 VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(), 391 std::numeric_limits<uint32_t>::max()); 392 VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min()); 393 VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(), 394 std::numeric_limits<int64_t>::max()); 395 VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(), 396 std::numeric_limits<uint64_t>::max()); 397 398 // SimpleAtoi(absl::string_view, absl::uint128) 399 VerifySimpleAtoiGood<absl::uint128>(0, 0); 400 VerifySimpleAtoiGood<absl::uint128>(42, 42); 401 VerifySimpleAtoiBad<absl::uint128>(-42); 402 403 VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min()); 404 VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(), 405 std::numeric_limits<int32_t>::max()); 406 VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(), 407 std::numeric_limits<uint32_t>::max()); 408 VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min()); 409 VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(), 410 std::numeric_limits<int64_t>::max()); 411 VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(), 412 std::numeric_limits<uint64_t>::max()); 413 VerifySimpleAtoiGood<absl::uint128>( 414 std::numeric_limits<absl::uint128>::max(), 415 std::numeric_limits<absl::uint128>::max()); 416 417 // SimpleAtoi(absl::string_view, absl::int128) 418 VerifySimpleAtoiGood<absl::int128>(0, 0); 419 VerifySimpleAtoiGood<absl::int128>(42, 42); 420 VerifySimpleAtoiGood<absl::int128>(-42, -42); 421 422 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::min(), 423 std::numeric_limits<int32_t>::min()); 424 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::max(), 425 std::numeric_limits<int32_t>::max()); 426 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint32_t>::max(), 427 std::numeric_limits<uint32_t>::max()); 428 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::min(), 429 std::numeric_limits<int64_t>::min()); 430 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::max(), 431 std::numeric_limits<int64_t>::max()); 432 VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint64_t>::max(), 433 std::numeric_limits<uint64_t>::max()); 434 VerifySimpleAtoiGood<absl::int128>( 435 std::numeric_limits<absl::int128>::min(), 436 std::numeric_limits<absl::int128>::min()); 437 VerifySimpleAtoiGood<absl::int128>( 438 std::numeric_limits<absl::int128>::max(), 439 std::numeric_limits<absl::int128>::max()); 440 VerifySimpleAtoiBad<absl::int128>(std::numeric_limits<absl::uint128>::max()); 441 442 // Some other types 443 VerifySimpleAtoiGood<short>(-42, -42); // NOLINT: runtime-int 444 VerifySimpleAtoiGood<int>(-42, -42); 445 VerifySimpleAtoiGood<int32_t>(-42, -42); 446 VerifySimpleAtoiGood<uint32_t>(42, 42); 447 VerifySimpleAtoiGood<unsigned int>(42, 42); 448 VerifySimpleAtoiGood<int64_t>(-42, -42); 449 VerifySimpleAtoiGood<long>(-42, -42); // NOLINT: runtime-int 450 VerifySimpleAtoiGood<uint64_t>(42, 42); 451 VerifySimpleAtoiGood<size_t>(42, 42); 452 VerifySimpleAtoiGood<std::string::size_type>(42, 42); 453 } 454 455 TEST(NumbersTest, Atod) { 456 // DBL_TRUE_MIN and FLT_TRUE_MIN were not mandated in <cfloat> before C++17. 457 #if !defined(DBL_TRUE_MIN) 458 static constexpr double DBL_TRUE_MIN = 459 4.940656458412465441765687928682213723650598026143247644255856825e-324; 460 #endif 461 #if !defined(FLT_TRUE_MIN) 462 static constexpr float FLT_TRUE_MIN = 463 1.401298464324817070923729583289916131280261941876515771757068284e-45f; 464 #endif 465 466 double d; 467 float f; 468 469 // NaN can be spelled in multiple ways. 470 EXPECT_TRUE(absl::SimpleAtod("NaN", &d)); 471 EXPECT_TRUE(std::isnan(d)); 472 EXPECT_TRUE(absl::SimpleAtod("nAN", &d)); 473 EXPECT_TRUE(std::isnan(d)); 474 EXPECT_TRUE(absl::SimpleAtod("-nan", &d)); 475 EXPECT_TRUE(std::isnan(d)); 476 477 // Likewise for Infinity. 478 EXPECT_TRUE(absl::SimpleAtod("inf", &d)); 479 EXPECT_TRUE(std::isinf(d) && (d > 0)); 480 EXPECT_TRUE(absl::SimpleAtod("+Infinity", &d)); 481 EXPECT_TRUE(std::isinf(d) && (d > 0)); 482 EXPECT_TRUE(absl::SimpleAtod("-INF", &d)); 483 EXPECT_TRUE(std::isinf(d) && (d < 0)); 484 485 // Parse DBL_MAX. Parsing something more than twice as big should also 486 // produce infinity. 487 EXPECT_TRUE(absl::SimpleAtod("1.7976931348623157e+308", &d)); 488 EXPECT_EQ(d, 1.7976931348623157e+308); 489 EXPECT_TRUE(absl::SimpleAtod("5e308", &d)); 490 EXPECT_TRUE(std::isinf(d) && (d > 0)); 491 // Ditto, but for FLT_MAX. 492 EXPECT_TRUE(absl::SimpleAtof("3.4028234663852886e+38", &f)); 493 EXPECT_EQ(f, 3.4028234663852886e+38f); 494 EXPECT_TRUE(absl::SimpleAtof("7e38", &f)); 495 EXPECT_TRUE(std::isinf(f) && (f > 0)); 496 497 // Parse the largest N such that parsing 1eN produces a finite value and the 498 // smallest M = N + 1 such that parsing 1eM produces infinity. 499 // 500 // The 309 exponent (and 39) confirms the "definition of 501 // kEiselLemireMaxExclExp10" comment in charconv.cc. 502 EXPECT_TRUE(absl::SimpleAtod("1e308", &d)); 503 EXPECT_EQ(d, 1e308); 504 EXPECT_FALSE(std::isinf(d)); 505 EXPECT_TRUE(absl::SimpleAtod("1e309", &d)); 506 EXPECT_TRUE(std::isinf(d)); 507 // Ditto, but for Atof instead of Atod. 508 EXPECT_TRUE(absl::SimpleAtof("1e38", &f)); 509 EXPECT_EQ(f, 1e38f); 510 EXPECT_FALSE(std::isinf(f)); 511 EXPECT_TRUE(absl::SimpleAtof("1e39", &f)); 512 EXPECT_TRUE(std::isinf(f)); 513 514 // Parse the largest N such that parsing 9.999999999999999999eN, with 19 515 // nines, produces a finite value. 516 // 517 // 9999999999999999999, with 19 nines but no decimal point, is the largest 518 // "repeated nines" integer that fits in a uint64_t. 519 EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e307", &d)); 520 EXPECT_EQ(d, 9.999999999999999999e307); 521 EXPECT_FALSE(std::isinf(d)); 522 EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e308", &d)); 523 EXPECT_TRUE(std::isinf(d)); 524 // Ditto, but for Atof instead of Atod. 525 EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e37", &f)); 526 EXPECT_EQ(f, 9.999999999999999999e37f); 527 EXPECT_FALSE(std::isinf(f)); 528 EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e38", &f)); 529 EXPECT_TRUE(std::isinf(f)); 530 531 // Parse DBL_MIN (normal), DBL_TRUE_MIN (subnormal) and (DBL_TRUE_MIN / 10) 532 // (effectively zero). 533 EXPECT_TRUE(absl::SimpleAtod("2.2250738585072014e-308", &d)); 534 EXPECT_EQ(d, 2.2250738585072014e-308); 535 EXPECT_TRUE(absl::SimpleAtod("4.9406564584124654e-324", &d)); 536 EXPECT_EQ(d, 4.9406564584124654e-324); 537 EXPECT_TRUE(absl::SimpleAtod("4.9406564584124654e-325", &d)); 538 EXPECT_EQ(d, 0); 539 // Ditto, but for FLT_MIN, FLT_TRUE_MIN and (FLT_TRUE_MIN / 10). 540 EXPECT_TRUE(absl::SimpleAtof("1.1754943508222875e-38", &f)); 541 EXPECT_EQ(f, 1.1754943508222875e-38f); 542 EXPECT_TRUE(absl::SimpleAtof("1.4012984643248171e-45", &f)); 543 EXPECT_EQ(f, 1.4012984643248171e-45f); 544 EXPECT_TRUE(absl::SimpleAtof("1.4012984643248171e-46", &f)); 545 EXPECT_EQ(f, 0); 546 547 // Parse the largest N (the most negative -N) such that parsing 1e-N produces 548 // a normal or subnormal (but still positive) or zero value. 549 EXPECT_TRUE(absl::SimpleAtod("1e-307", &d)); 550 EXPECT_EQ(d, 1e-307); 551 EXPECT_GE(d, DBL_MIN); 552 EXPECT_LT(d, DBL_MIN * 10); 553 EXPECT_TRUE(absl::SimpleAtod("1e-323", &d)); 554 EXPECT_EQ(d, 1e-323); 555 EXPECT_GE(d, DBL_TRUE_MIN); 556 EXPECT_LT(d, DBL_TRUE_MIN * 10); 557 EXPECT_TRUE(absl::SimpleAtod("1e-324", &d)); 558 EXPECT_EQ(d, 0); 559 // Ditto, but for Atof instead of Atod. 560 EXPECT_TRUE(absl::SimpleAtof("1e-37", &f)); 561 EXPECT_EQ(f, 1e-37f); 562 EXPECT_GE(f, FLT_MIN); 563 EXPECT_LT(f, FLT_MIN * 10); 564 EXPECT_TRUE(absl::SimpleAtof("1e-45", &f)); 565 EXPECT_EQ(f, 1e-45f); 566 EXPECT_GE(f, FLT_TRUE_MIN); 567 EXPECT_LT(f, FLT_TRUE_MIN * 10); 568 EXPECT_TRUE(absl::SimpleAtof("1e-46", &f)); 569 EXPECT_EQ(f, 0); 570 571 // Parse the largest N (the most negative -N) such that parsing 572 // 9.999999999999999999e-N, with 19 nines, produces a normal or subnormal 573 // (but still positive) or zero value. 574 // 575 // 9999999999999999999, with 19 nines but no decimal point, is the largest 576 // "repeated nines" integer that fits in a uint64_t. 577 // 578 // The -324/-325 exponents (and -46/-47) confirms the "definition of 579 // kEiselLemireMinInclExp10" comment in charconv.cc. 580 EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-308", &d)); 581 EXPECT_EQ(d, 9.999999999999999999e-308); 582 EXPECT_GE(d, DBL_MIN); 583 EXPECT_LT(d, DBL_MIN * 10); 584 EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-324", &d)); 585 EXPECT_EQ(d, 9.999999999999999999e-324); 586 EXPECT_GE(d, DBL_TRUE_MIN); 587 EXPECT_LT(d, DBL_TRUE_MIN * 10); 588 EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-325", &d)); 589 EXPECT_EQ(d, 0); 590 // Ditto, but for Atof instead of Atod. 591 EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-38", &f)); 592 EXPECT_EQ(f, 9.999999999999999999e-38f); 593 EXPECT_GE(f, FLT_MIN); 594 EXPECT_LT(f, FLT_MIN * 10); 595 EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-46", &f)); 596 EXPECT_EQ(f, 9.999999999999999999e-46f); 597 EXPECT_GE(f, FLT_TRUE_MIN); 598 EXPECT_LT(f, FLT_TRUE_MIN * 10); 599 EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-47", &f)); 600 EXPECT_EQ(f, 0); 601 602 // Leading and/or trailing whitespace is OK. 603 EXPECT_TRUE(absl::SimpleAtod(" \t\r\n 2.718", &d)); 604 EXPECT_EQ(d, 2.718); 605 EXPECT_TRUE(absl::SimpleAtod(" 3.141 ", &d)); 606 EXPECT_EQ(d, 3.141); 607 608 // Leading or trailing not-whitespace is not OK. 609 EXPECT_FALSE(absl::SimpleAtod("n 0", &d)); 610 EXPECT_FALSE(absl::SimpleAtod("0n ", &d)); 611 612 // Multiple leading 0s are OK. 613 EXPECT_TRUE(absl::SimpleAtod("000123", &d)); 614 EXPECT_EQ(d, 123); 615 EXPECT_TRUE(absl::SimpleAtod("000.456", &d)); 616 EXPECT_EQ(d, 0.456); 617 618 // An absent leading 0 (for a fraction < 1) is OK. 619 EXPECT_TRUE(absl::SimpleAtod(".5", &d)); 620 EXPECT_EQ(d, 0.5); 621 EXPECT_TRUE(absl::SimpleAtod("-.707", &d)); 622 EXPECT_EQ(d, -0.707); 623 624 // Unary + is OK. 625 EXPECT_TRUE(absl::SimpleAtod("+6.0221408e+23", &d)); 626 EXPECT_EQ(d, 6.0221408e+23); 627 628 // Underscores are not OK. 629 EXPECT_FALSE(absl::SimpleAtod("123_456", &d)); 630 631 // The decimal separator must be '.' and is never ','. 632 EXPECT_TRUE(absl::SimpleAtod("8.9", &d)); 633 EXPECT_FALSE(absl::SimpleAtod("8,9", &d)); 634 635 // These examples are called out in the EiselLemire function's comments. 636 EXPECT_TRUE(absl::SimpleAtod("4503599627370497.5", &d)); 637 EXPECT_EQ(d, 4503599627370497.5); 638 EXPECT_TRUE(absl::SimpleAtod("1e+23", &d)); 639 EXPECT_EQ(d, 1e+23); 640 EXPECT_TRUE(absl::SimpleAtod("9223372036854775807", &d)); 641 EXPECT_EQ(d, 9223372036854775807); 642 // Ditto, but for Atof instead of Atod. 643 EXPECT_TRUE(absl::SimpleAtof("0.0625", &f)); 644 EXPECT_EQ(f, 0.0625f); 645 EXPECT_TRUE(absl::SimpleAtof("20040229.0", &f)); 646 EXPECT_EQ(f, 20040229.0f); 647 EXPECT_TRUE(absl::SimpleAtof("2147483647.0", &f)); 648 EXPECT_EQ(f, 2147483647.0f); 649 650 // Some parsing algorithms don't always round correctly (but absl::SimpleAtod 651 // should). This test case comes from 652 // https://github.com/serde-rs/json/issues/707 653 // 654 // See also atod_manual_test.cc for running many more test cases. 655 EXPECT_TRUE(absl::SimpleAtod("122.416294033786585", &d)); 656 EXPECT_EQ(d, 122.416294033786585); 657 EXPECT_TRUE(absl::SimpleAtof("122.416294033786585", &f)); 658 EXPECT_EQ(f, 122.416294033786585f); 659 } 660 661 TEST(NumbersTest, Prefixes) { 662 double d; 663 EXPECT_FALSE(absl::SimpleAtod("++1", &d)); 664 EXPECT_FALSE(absl::SimpleAtod("+-1", &d)); 665 EXPECT_FALSE(absl::SimpleAtod("-+1", &d)); 666 EXPECT_FALSE(absl::SimpleAtod("--1", &d)); 667 EXPECT_TRUE(absl::SimpleAtod("-1", &d)); 668 EXPECT_EQ(d, -1.); 669 EXPECT_TRUE(absl::SimpleAtod("+1", &d)); 670 EXPECT_EQ(d, +1.); 671 672 float f; 673 EXPECT_FALSE(absl::SimpleAtof("++1", &f)); 674 EXPECT_FALSE(absl::SimpleAtof("+-1", &f)); 675 EXPECT_FALSE(absl::SimpleAtof("-+1", &f)); 676 EXPECT_FALSE(absl::SimpleAtof("--1", &f)); 677 EXPECT_TRUE(absl::SimpleAtof("-1", &f)); 678 EXPECT_EQ(f, -1.f); 679 EXPECT_TRUE(absl::SimpleAtof("+1", &f)); 680 EXPECT_EQ(f, +1.f); 681 } 682 683 TEST(NumbersTest, Atoenum) { 684 enum E01 { 685 E01_zero = 0, 686 E01_one = 1, 687 }; 688 689 VerifySimpleAtoiGood<E01>(E01_zero, E01_zero); 690 VerifySimpleAtoiGood<E01>(E01_one, E01_one); 691 692 enum E_101 { 693 E_101_minusone = -1, 694 E_101_zero = 0, 695 E_101_one = 1, 696 }; 697 698 VerifySimpleAtoiGood<E_101>(E_101_minusone, E_101_minusone); 699 VerifySimpleAtoiGood<E_101>(E_101_zero, E_101_zero); 700 VerifySimpleAtoiGood<E_101>(E_101_one, E_101_one); 701 702 enum E_bigint { 703 E_bigint_zero = 0, 704 E_bigint_one = 1, 705 E_bigint_max31 = static_cast<int32_t>(0x7FFFFFFF), 706 }; 707 708 VerifySimpleAtoiGood<E_bigint>(E_bigint_zero, E_bigint_zero); 709 VerifySimpleAtoiGood<E_bigint>(E_bigint_one, E_bigint_one); 710 VerifySimpleAtoiGood<E_bigint>(E_bigint_max31, E_bigint_max31); 711 712 enum E_fullint { 713 E_fullint_zero = 0, 714 E_fullint_one = 1, 715 E_fullint_max31 = static_cast<int32_t>(0x7FFFFFFF), 716 E_fullint_min32 = INT32_MIN, 717 }; 718 719 VerifySimpleAtoiGood<E_fullint>(E_fullint_zero, E_fullint_zero); 720 VerifySimpleAtoiGood<E_fullint>(E_fullint_one, E_fullint_one); 721 VerifySimpleAtoiGood<E_fullint>(E_fullint_max31, E_fullint_max31); 722 VerifySimpleAtoiGood<E_fullint>(E_fullint_min32, E_fullint_min32); 723 724 enum E_biguint { 725 E_biguint_zero = 0, 726 E_biguint_one = 1, 727 E_biguint_max31 = static_cast<uint32_t>(0x7FFFFFFF), 728 E_biguint_max32 = static_cast<uint32_t>(0xFFFFFFFF), 729 }; 730 731 VerifySimpleAtoiGood<E_biguint>(E_biguint_zero, E_biguint_zero); 732 VerifySimpleAtoiGood<E_biguint>(E_biguint_one, E_biguint_one); 733 VerifySimpleAtoiGood<E_biguint>(E_biguint_max31, E_biguint_max31); 734 VerifySimpleAtoiGood<E_biguint>(E_biguint_max32, E_biguint_max32); 735 } 736 737 template <typename int_type, typename in_val_type> 738 void VerifySimpleHexAtoiGood(in_val_type in_value, int_type exp_value) { 739 std::string s; 740 absl::strings_internal::OStringStream strm(&s); 741 if (in_value >= 0) { 742 if constexpr (std::is_arithmetic<in_val_type>::value) { 743 absl::StrAppend(&s, absl::Hex(in_value)); 744 } else { 745 // absl::Hex doesn't work with absl::(u)int128. 746 strm << std::hex << in_value; 747 } 748 } else { 749 // Inefficient for small integers, but works with all integral types. 750 strm << "-" << std::hex << -absl::uint128(in_value); 751 } 752 int_type x = static_cast<int_type>(~exp_value); 753 EXPECT_TRUE(SimpleHexAtoi(s, &x)) 754 << "in_value=" << std::hex << in_value << " s=" << s << " x=" << x; 755 EXPECT_EQ(exp_value, x); 756 x = static_cast<int_type>(~exp_value); 757 EXPECT_TRUE(SimpleHexAtoi( 758 s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions 759 EXPECT_EQ(exp_value, x); 760 } 761 762 template <typename int_type, typename in_val_type> 763 void VerifySimpleHexAtoiBad(in_val_type in_value) { 764 std::string s; 765 absl::strings_internal::OStringStream strm(&s); 766 if (in_value >= 0) { 767 if constexpr (std::is_arithmetic<in_val_type>::value) { 768 absl::StrAppend(&s, absl::Hex(in_value)); 769 } else { 770 // absl::Hex doesn't work with absl::(u)int128. 771 strm << std::hex << in_value; 772 } 773 } else { 774 // Inefficient for small integers, but works with all integral types. 775 strm << "-" << std::hex << -absl::uint128(in_value); 776 } 777 int_type x; 778 EXPECT_FALSE(SimpleHexAtoi(s, &x)); 779 EXPECT_FALSE(SimpleHexAtoi( 780 s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions 781 } 782 783 TEST(NumbersTest, HexAtoi) { 784 // SimpleHexAtoi(absl::string_view, int8_t) 785 VerifySimpleHexAtoiGood<int8_t>(0, 0); 786 VerifySimpleHexAtoiGood<int8_t>(0x42, 0x42); 787 VerifySimpleHexAtoiGood<int8_t>(-0x42, -0x42); 788 789 VerifySimpleHexAtoiGood<int8_t>(std::numeric_limits<int8_t>::min(), 790 std::numeric_limits<int8_t>::min()); 791 VerifySimpleHexAtoiGood<int8_t>(std::numeric_limits<int8_t>::max(), 792 std::numeric_limits<int8_t>::max()); 793 794 // SimpleHexAtoi(absl::string_view, uint8_t) 795 VerifySimpleHexAtoiGood<uint8_t>(0, 0); 796 VerifySimpleHexAtoiGood<uint8_t>(0x42, 0x42); 797 VerifySimpleHexAtoiBad<uint8_t>(-0x42); 798 799 VerifySimpleHexAtoiBad<uint8_t>(std::numeric_limits<int8_t>::min()); 800 VerifySimpleHexAtoiGood<uint8_t>(std::numeric_limits<int8_t>::max(), 801 std::numeric_limits<int8_t>::max()); 802 VerifySimpleHexAtoiGood<uint8_t>(std::numeric_limits<uint8_t>::max(), 803 std::numeric_limits<uint8_t>::max()); 804 VerifySimpleHexAtoiBad<uint8_t>(std::numeric_limits<int16_t>::min()); 805 VerifySimpleHexAtoiBad<uint8_t>(std::numeric_limits<int16_t>::max()); 806 VerifySimpleHexAtoiBad<uint8_t>(std::numeric_limits<uint16_t>::max()); 807 808 // SimpleHexAtoi(absl::string_view, int16_t) 809 VerifySimpleHexAtoiGood<int16_t>(0, 0); 810 VerifySimpleHexAtoiGood<int16_t>(0x42, 0x42); 811 VerifySimpleHexAtoiGood<int16_t>(-0x42, -0x42); 812 813 VerifySimpleHexAtoiGood<int16_t>(std::numeric_limits<int16_t>::min(), 814 std::numeric_limits<int16_t>::min()); 815 VerifySimpleHexAtoiGood<int16_t>(std::numeric_limits<int16_t>::max(), 816 std::numeric_limits<int16_t>::max()); 817 818 // SimpleHexAtoi(absl::string_view, uint16_t) 819 VerifySimpleHexAtoiGood<uint16_t>(0, 0); 820 VerifySimpleHexAtoiGood<uint16_t>(0x42, 0x42); 821 VerifySimpleHexAtoiBad<uint16_t>(-0x42); 822 823 VerifySimpleHexAtoiBad<uint16_t>(std::numeric_limits<int16_t>::min()); 824 VerifySimpleHexAtoiGood<uint16_t>(std::numeric_limits<int16_t>::max(), 825 std::numeric_limits<int16_t>::max()); 826 VerifySimpleHexAtoiGood<uint16_t>(std::numeric_limits<uint16_t>::max(), 827 std::numeric_limits<uint16_t>::max()); 828 VerifySimpleHexAtoiBad<uint16_t>(std::numeric_limits<int32_t>::min()); 829 VerifySimpleHexAtoiBad<uint16_t>(std::numeric_limits<int32_t>::max()); 830 VerifySimpleHexAtoiBad<uint16_t>(std::numeric_limits<uint32_t>::max()); 831 832 // SimpleHexAtoi(absl::string_view, int32_t) 833 VerifySimpleHexAtoiGood<int32_t>(0, 0); 834 VerifySimpleHexAtoiGood<int32_t>(0x42, 0x42); 835 VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42); 836 837 VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(), 838 std::numeric_limits<int32_t>::min()); 839 VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(), 840 std::numeric_limits<int32_t>::max()); 841 842 // SimpleHexAtoi(absl::string_view, uint32_t) 843 VerifySimpleHexAtoiGood<uint32_t>(0, 0); 844 VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42); 845 VerifySimpleHexAtoiBad<uint32_t>(-0x42); 846 847 VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min()); 848 VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(), 849 std::numeric_limits<int32_t>::max()); 850 VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(), 851 std::numeric_limits<uint32_t>::max()); 852 VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min()); 853 VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max()); 854 VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max()); 855 856 // SimpleHexAtoi(absl::string_view, int64_t) 857 VerifySimpleHexAtoiGood<int64_t>(0, 0); 858 VerifySimpleHexAtoiGood<int64_t>(0x42, 0x42); 859 VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42); 860 861 VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(), 862 std::numeric_limits<int32_t>::min()); 863 VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(), 864 std::numeric_limits<int32_t>::max()); 865 VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(), 866 std::numeric_limits<uint32_t>::max()); 867 VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(), 868 std::numeric_limits<int64_t>::min()); 869 VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(), 870 std::numeric_limits<int64_t>::max()); 871 VerifySimpleHexAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max()); 872 873 // SimpleHexAtoi(absl::string_view, uint64_t) 874 VerifySimpleHexAtoiGood<uint64_t>(0, 0); 875 VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42); 876 VerifySimpleHexAtoiBad<uint64_t>(-0x42); 877 878 VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min()); 879 VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(), 880 std::numeric_limits<int32_t>::max()); 881 VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(), 882 std::numeric_limits<uint32_t>::max()); 883 VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min()); 884 VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(), 885 std::numeric_limits<int64_t>::max()); 886 VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(), 887 std::numeric_limits<uint64_t>::max()); 888 889 // SimpleHexAtoi(absl::string_view, absl::uint128) 890 VerifySimpleHexAtoiGood<absl::uint128>(0, 0); 891 VerifySimpleHexAtoiGood<absl::uint128>(0x42, 0x42); 892 VerifySimpleHexAtoiBad<absl::uint128>(-0x42); 893 894 VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min()); 895 VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(), 896 std::numeric_limits<int32_t>::max()); 897 VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(), 898 std::numeric_limits<uint32_t>::max()); 899 VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min()); 900 VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(), 901 std::numeric_limits<int64_t>::max()); 902 VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(), 903 std::numeric_limits<uint64_t>::max()); 904 VerifySimpleHexAtoiGood<absl::uint128>( 905 std::numeric_limits<absl::uint128>::max(), 906 std::numeric_limits<absl::uint128>::max()); 907 908 // Some other types 909 VerifySimpleHexAtoiGood<short>(-0x42, -0x42); // NOLINT: runtime-int 910 VerifySimpleHexAtoiGood<int>(-0x42, -0x42); 911 VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42); 912 VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42); 913 VerifySimpleHexAtoiGood<unsigned int>(0x42, 0x42); 914 VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42); 915 VerifySimpleHexAtoiGood<long>(-0x42, -0x42); // NOLINT: runtime-int 916 VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42); 917 VerifySimpleHexAtoiGood<size_t>(0x42, 0x42); 918 VerifySimpleHexAtoiGood<std::string::size_type>(0x42, 0x42); 919 920 // Number prefix 921 int32_t value; 922 EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16)); 923 EXPECT_EQ(0x34234324, value); 924 925 EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16)); 926 EXPECT_EQ(0x34234324, value); 927 928 // ASCII whitespace 929 EXPECT_TRUE(safe_strto32_base(" \t\n 34234324", &value, 16)); 930 EXPECT_EQ(0x34234324, value); 931 932 EXPECT_TRUE(safe_strto32_base("34234324 \t\n ", &value, 16)); 933 EXPECT_EQ(0x34234324, value); 934 } 935 936 TEST(stringtest, safe_strto8_base) { 937 int8_t value; 938 EXPECT_TRUE(safe_strto8_base("0x34", &value, 16)); 939 EXPECT_EQ(0x34, value); 940 941 EXPECT_TRUE(safe_strto8_base("0X34", &value, 16)); 942 EXPECT_EQ(0x34, value); 943 944 EXPECT_TRUE(safe_strto8_base("34", &value, 16)); 945 EXPECT_EQ(0x34, value); 946 947 EXPECT_TRUE(safe_strto8_base("0", &value, 16)); 948 EXPECT_EQ(0, value); 949 950 EXPECT_TRUE(safe_strto8_base(" \t\n -0x34", &value, 16)); 951 EXPECT_EQ(-0x34, value); 952 953 EXPECT_TRUE(safe_strto8_base(" \t\n -34", &value, 16)); 954 EXPECT_EQ(-0x34, value); 955 956 EXPECT_TRUE(safe_strto8_base("76", &value, 8)); 957 EXPECT_EQ(076, value); 958 959 EXPECT_TRUE(safe_strto8_base("-0123", &value, 8)); 960 EXPECT_EQ(-0123, value); 961 962 EXPECT_FALSE(safe_strto8_base("183", &value, 8)); 963 964 // Autodetect base. 965 EXPECT_TRUE(safe_strto8_base("0", &value, 0)); 966 EXPECT_EQ(0, value); 967 968 EXPECT_TRUE(safe_strto8_base("077", &value, 0)); 969 EXPECT_EQ(077, value); // Octal interpretation 970 971 // Leading zero indicates octal, but then followed by invalid digit. 972 EXPECT_FALSE(safe_strto8_base("088", &value, 0)); 973 974 // Leading 0x indicated hex, but then followed by invalid digit. 975 EXPECT_FALSE(safe_strto8_base("0xG", &value, 0)); 976 977 // Base-10 version. 978 EXPECT_TRUE(safe_strto8_base("124", &value, 10)); 979 EXPECT_EQ(124, value); 980 981 EXPECT_TRUE(safe_strto8_base("0", &value, 10)); 982 EXPECT_EQ(0, value); 983 984 EXPECT_TRUE(safe_strto8_base(" \t\n -124", &value, 10)); 985 EXPECT_EQ(-124, value); 986 987 EXPECT_TRUE(safe_strto8_base("124 \n\t ", &value, 10)); 988 EXPECT_EQ(124, value); 989 990 // Invalid ints. 991 EXPECT_FALSE(safe_strto8_base("", &value, 10)); 992 EXPECT_FALSE(safe_strto8_base(" ", &value, 10)); 993 EXPECT_FALSE(safe_strto8_base("abc", &value, 10)); 994 EXPECT_FALSE(safe_strto8_base("34a", &value, 10)); 995 EXPECT_FALSE(safe_strto8_base("34.3", &value, 10)); 996 997 // Out of bounds. 998 EXPECT_FALSE(safe_strto8_base("128", &value, 10)); 999 EXPECT_FALSE(safe_strto8_base("-129", &value, 10)); 1000 1001 // String version. 1002 EXPECT_TRUE(safe_strto8_base(std::string("0x12"), &value, 16)); 1003 EXPECT_EQ(0x12, value); 1004 1005 // Base-10 string version. 1006 EXPECT_TRUE(safe_strto8_base("123", &value, 10)); 1007 EXPECT_EQ(123, value); 1008 } 1009 1010 TEST(stringtest, safe_strto16_base) { 1011 int16_t value; 1012 EXPECT_TRUE(safe_strto16_base("0x3423", &value, 16)); 1013 EXPECT_EQ(0x3423, value); 1014 1015 EXPECT_TRUE(safe_strto16_base("0X3423", &value, 16)); 1016 EXPECT_EQ(0x3423, value); 1017 1018 EXPECT_TRUE(safe_strto16_base("3423", &value, 16)); 1019 EXPECT_EQ(0x3423, value); 1020 1021 EXPECT_TRUE(safe_strto16_base("0", &value, 16)); 1022 EXPECT_EQ(0, value); 1023 1024 EXPECT_TRUE(safe_strto16_base(" \t\n -0x3423", &value, 16)); 1025 EXPECT_EQ(-0x3423, value); 1026 1027 EXPECT_TRUE(safe_strto16_base(" \t\n -3423", &value, 16)); 1028 EXPECT_EQ(-0x3423, value); 1029 1030 EXPECT_TRUE(safe_strto16_base("34567", &value, 8)); 1031 EXPECT_EQ(034567, value); 1032 1033 EXPECT_TRUE(safe_strto16_base("-01234", &value, 8)); 1034 EXPECT_EQ(-01234, value); 1035 1036 EXPECT_FALSE(safe_strto16_base("1834", &value, 8)); 1037 1038 // Autodetect base. 1039 EXPECT_TRUE(safe_strto16_base("0", &value, 0)); 1040 EXPECT_EQ(0, value); 1041 1042 EXPECT_TRUE(safe_strto16_base("077", &value, 0)); 1043 EXPECT_EQ(077, value); // Octal interpretation 1044 1045 // Leading zero indicates octal, but then followed by invalid digit. 1046 EXPECT_FALSE(safe_strto16_base("088", &value, 0)); 1047 1048 // Leading 0x indicated hex, but then followed by invalid digit. 1049 EXPECT_FALSE(safe_strto16_base("0xG", &value, 0)); 1050 1051 // Base-10 version. 1052 EXPECT_TRUE(safe_strto16_base("3423", &value, 10)); 1053 EXPECT_EQ(3423, value); 1054 1055 EXPECT_TRUE(safe_strto16_base("0", &value, 10)); 1056 EXPECT_EQ(0, value); 1057 1058 EXPECT_TRUE(safe_strto16_base(" \t\n -3423", &value, 10)); 1059 EXPECT_EQ(-3423, value); 1060 1061 EXPECT_TRUE(safe_strto16_base("3423 \n\t ", &value, 10)); 1062 EXPECT_EQ(3423, value); 1063 1064 // Invalid ints. 1065 EXPECT_FALSE(safe_strto16_base("", &value, 10)); 1066 EXPECT_FALSE(safe_strto16_base(" ", &value, 10)); 1067 EXPECT_FALSE(safe_strto16_base("abc", &value, 10)); 1068 EXPECT_FALSE(safe_strto16_base("324a", &value, 10)); 1069 EXPECT_FALSE(safe_strto16_base("4234.3", &value, 10)); 1070 1071 // Out of bounds. 1072 EXPECT_FALSE(safe_strto16_base("32768", &value, 10)); 1073 EXPECT_FALSE(safe_strto16_base("-32769", &value, 10)); 1074 1075 // String version. 1076 EXPECT_TRUE(safe_strto16_base(std::string("0x1234"), &value, 16)); 1077 EXPECT_EQ(0x1234, value); 1078 1079 // Base-10 string version. 1080 EXPECT_TRUE(safe_strto16_base("1234", &value, 10)); 1081 EXPECT_EQ(1234, value); 1082 } 1083 1084 TEST(stringtest, safe_strto32_base) { 1085 int32_t value; 1086 EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16)); 1087 EXPECT_EQ(0x34234324, value); 1088 1089 EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16)); 1090 EXPECT_EQ(0x34234324, value); 1091 1092 EXPECT_TRUE(safe_strto32_base("34234324", &value, 16)); 1093 EXPECT_EQ(0x34234324, value); 1094 1095 EXPECT_TRUE(safe_strto32_base("0", &value, 16)); 1096 EXPECT_EQ(0, value); 1097 1098 EXPECT_TRUE(safe_strto32_base(" \t\n -0x34234324", &value, 16)); 1099 EXPECT_EQ(-0x34234324, value); 1100 1101 EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 16)); 1102 EXPECT_EQ(-0x34234324, value); 1103 1104 EXPECT_TRUE(safe_strto32_base("7654321", &value, 8)); 1105 EXPECT_EQ(07654321, value); 1106 1107 EXPECT_TRUE(safe_strto32_base("-01234", &value, 8)); 1108 EXPECT_EQ(-01234, value); 1109 1110 EXPECT_FALSE(safe_strto32_base("1834", &value, 8)); 1111 1112 // Autodetect base. 1113 EXPECT_TRUE(safe_strto32_base("0", &value, 0)); 1114 EXPECT_EQ(0, value); 1115 1116 EXPECT_TRUE(safe_strto32_base("077", &value, 0)); 1117 EXPECT_EQ(077, value); // Octal interpretation 1118 1119 // Leading zero indicates octal, but then followed by invalid digit. 1120 EXPECT_FALSE(safe_strto32_base("088", &value, 0)); 1121 1122 // Leading 0x indicated hex, but then followed by invalid digit. 1123 EXPECT_FALSE(safe_strto32_base("0xG", &value, 0)); 1124 1125 // Base-10 version. 1126 EXPECT_TRUE(safe_strto32_base("34234324", &value, 10)); 1127 EXPECT_EQ(34234324, value); 1128 1129 EXPECT_TRUE(safe_strto32_base("0", &value, 10)); 1130 EXPECT_EQ(0, value); 1131 1132 EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 10)); 1133 EXPECT_EQ(-34234324, value); 1134 1135 EXPECT_TRUE(safe_strto32_base("34234324 \n\t ", &value, 10)); 1136 EXPECT_EQ(34234324, value); 1137 1138 // Invalid ints. 1139 EXPECT_FALSE(safe_strto32_base("", &value, 10)); 1140 EXPECT_FALSE(safe_strto32_base(" ", &value, 10)); 1141 EXPECT_FALSE(safe_strto32_base("abc", &value, 10)); 1142 EXPECT_FALSE(safe_strto32_base("34234324a", &value, 10)); 1143 EXPECT_FALSE(safe_strto32_base("34234.3", &value, 10)); 1144 1145 // Out of bounds. 1146 EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10)); 1147 EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10)); 1148 1149 // String version. 1150 EXPECT_TRUE(safe_strto32_base(std::string("0x1234"), &value, 16)); 1151 EXPECT_EQ(0x1234, value); 1152 1153 // Base-10 string version. 1154 EXPECT_TRUE(safe_strto32_base("1234", &value, 10)); 1155 EXPECT_EQ(1234, value); 1156 } 1157 1158 TEST(stringtest, safe_strto64_base) { 1159 int64_t value; 1160 EXPECT_TRUE(safe_strto64_base("0x3423432448783446", &value, 16)); 1161 EXPECT_EQ(int64_t{0x3423432448783446}, value); 1162 1163 EXPECT_TRUE(safe_strto64_base("3423432448783446", &value, 16)); 1164 EXPECT_EQ(int64_t{0x3423432448783446}, value); 1165 1166 EXPECT_TRUE(safe_strto64_base("0", &value, 16)); 1167 EXPECT_EQ(0, value); 1168 1169 EXPECT_TRUE(safe_strto64_base(" \t\n -0x3423432448783446", &value, 16)); 1170 EXPECT_EQ(int64_t{-0x3423432448783446}, value); 1171 1172 EXPECT_TRUE(safe_strto64_base(" \t\n -3423432448783446", &value, 16)); 1173 EXPECT_EQ(int64_t{-0x3423432448783446}, value); 1174 1175 EXPECT_TRUE(safe_strto64_base("123456701234567012", &value, 8)); 1176 EXPECT_EQ(int64_t{0123456701234567012}, value); 1177 1178 EXPECT_TRUE(safe_strto64_base("-017777777777777", &value, 8)); 1179 EXPECT_EQ(int64_t{-017777777777777}, value); 1180 1181 EXPECT_FALSE(safe_strto64_base("19777777777777", &value, 8)); 1182 1183 // Autodetect base. 1184 EXPECT_TRUE(safe_strto64_base("0", &value, 0)); 1185 EXPECT_EQ(0, value); 1186 1187 EXPECT_TRUE(safe_strto64_base("077", &value, 0)); 1188 EXPECT_EQ(077, value); // Octal interpretation 1189 1190 // Leading zero indicates octal, but then followed by invalid digit. 1191 EXPECT_FALSE(safe_strto64_base("088", &value, 0)); 1192 1193 // Leading 0x indicated hex, but then followed by invalid digit. 1194 EXPECT_FALSE(safe_strto64_base("0xG", &value, 0)); 1195 1196 // Base-10 version. 1197 EXPECT_TRUE(safe_strto64_base("34234324487834466", &value, 10)); 1198 EXPECT_EQ(int64_t{34234324487834466}, value); 1199 1200 EXPECT_TRUE(safe_strto64_base("0", &value, 10)); 1201 EXPECT_EQ(0, value); 1202 1203 EXPECT_TRUE(safe_strto64_base(" \t\n -34234324487834466", &value, 10)); 1204 EXPECT_EQ(int64_t{-34234324487834466}, value); 1205 1206 EXPECT_TRUE(safe_strto64_base("34234324487834466 \n\t ", &value, 10)); 1207 EXPECT_EQ(int64_t{34234324487834466}, value); 1208 1209 // Invalid ints. 1210 EXPECT_FALSE(safe_strto64_base("", &value, 10)); 1211 EXPECT_FALSE(safe_strto64_base(" ", &value, 10)); 1212 EXPECT_FALSE(safe_strto64_base("abc", &value, 10)); 1213 EXPECT_FALSE(safe_strto64_base("34234324487834466a", &value, 10)); 1214 EXPECT_FALSE(safe_strto64_base("34234487834466.3", &value, 10)); 1215 1216 // Out of bounds. 1217 EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10)); 1218 EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10)); 1219 1220 // String version. 1221 EXPECT_TRUE(safe_strto64_base(std::string("0x1234"), &value, 16)); 1222 EXPECT_EQ(0x1234, value); 1223 1224 // Base-10 string version. 1225 EXPECT_TRUE(safe_strto64_base("1234", &value, 10)); 1226 EXPECT_EQ(1234, value); 1227 } 1228 1229 TEST(stringtest, safe_strto8_range) { 1230 // These tests verify underflow/overflow behaviour. 1231 int8_t value; 1232 EXPECT_FALSE(safe_strto8_base("128", &value, 10)); 1233 EXPECT_EQ(std::numeric_limits<int8_t>::max(), value); 1234 1235 EXPECT_TRUE(safe_strto8_base("-128", &value, 10)); 1236 EXPECT_EQ(std::numeric_limits<int8_t>::min(), value); 1237 1238 EXPECT_FALSE(safe_strto8_base("-129", &value, 10)); 1239 EXPECT_EQ(std::numeric_limits<int8_t>::min(), value); 1240 } 1241 1242 TEST(stringtest, safe_strto16_range) { 1243 // These tests verify underflow/overflow behaviour. 1244 int16_t value; 1245 EXPECT_FALSE(safe_strto16_base("32768", &value, 10)); 1246 EXPECT_EQ(std::numeric_limits<int16_t>::max(), value); 1247 1248 EXPECT_TRUE(safe_strto16_base("-32768", &value, 10)); 1249 EXPECT_EQ(std::numeric_limits<int16_t>::min(), value); 1250 1251 EXPECT_FALSE(safe_strto16_base("-32769", &value, 10)); 1252 EXPECT_EQ(std::numeric_limits<int16_t>::min(), value); 1253 } 1254 1255 TEST(stringtest, safe_strto32_range) { 1256 // These tests verify underflow/overflow behaviour. 1257 int32_t value; 1258 EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10)); 1259 EXPECT_EQ(std::numeric_limits<int32_t>::max(), value); 1260 1261 EXPECT_TRUE(safe_strto32_base("-2147483648", &value, 10)); 1262 EXPECT_EQ(std::numeric_limits<int32_t>::min(), value); 1263 1264 EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10)); 1265 EXPECT_EQ(std::numeric_limits<int32_t>::min(), value); 1266 } 1267 1268 TEST(stringtest, safe_strto64_range) { 1269 // These tests verify underflow/overflow behaviour. 1270 int64_t value; 1271 EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10)); 1272 EXPECT_EQ(std::numeric_limits<int64_t>::max(), value); 1273 1274 EXPECT_TRUE(safe_strto64_base("-9223372036854775808", &value, 10)); 1275 EXPECT_EQ(std::numeric_limits<int64_t>::min(), value); 1276 1277 EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10)); 1278 EXPECT_EQ(std::numeric_limits<int64_t>::min(), value); 1279 } 1280 1281 TEST(stringtest, safe_strto8_leading_substring) { 1282 // These tests verify this comment in numbers.h: 1283 // On error, returns false, and sets *value to: [...] 1284 // conversion of leading substring if available ("123@@@" -> 123) 1285 // 0 if no leading substring available 1286 int8_t value; 1287 EXPECT_FALSE(safe_strto8_base("069@@@", &value, 10)); 1288 EXPECT_EQ(69, value); 1289 1290 EXPECT_FALSE(safe_strto8_base("01769@@@", &value, 8)); 1291 EXPECT_EQ(0176, value); 1292 1293 EXPECT_FALSE(safe_strto8_base("069balloons", &value, 10)); 1294 EXPECT_EQ(69, value); 1295 1296 EXPECT_FALSE(safe_strto8_base("07bland", &value, 16)); 1297 EXPECT_EQ(0x7b, value); 1298 1299 EXPECT_FALSE(safe_strto8_base("@@@", &value, 10)); 1300 EXPECT_EQ(0, value); // there was no leading substring 1301 } 1302 1303 TEST(stringtest, safe_strto16_leading_substring) { 1304 // These tests verify this comment in numbers.h: 1305 // On error, returns false, and sets *value to: [...] 1306 // conversion of leading substring if available ("123@@@" -> 123) 1307 // 0 if no leading substring available 1308 int16_t value; 1309 EXPECT_FALSE(safe_strto16_base("04069@@@", &value, 10)); 1310 EXPECT_EQ(4069, value); 1311 1312 EXPECT_FALSE(safe_strto16_base("04069@@@", &value, 8)); 1313 EXPECT_EQ(0406, value); 1314 1315 EXPECT_FALSE(safe_strto16_base("04069balloons", &value, 10)); 1316 EXPECT_EQ(4069, value); 1317 1318 EXPECT_FALSE(safe_strto16_base("069balloons", &value, 16)); 1319 EXPECT_EQ(0x69ba, value); 1320 1321 EXPECT_FALSE(safe_strto16_base("@@@", &value, 10)); 1322 EXPECT_EQ(0, value); // there was no leading substring 1323 } 1324 1325 TEST(stringtest, safe_strto32_leading_substring) { 1326 // These tests verify this comment in numbers.h: 1327 // On error, returns false, and sets *value to: [...] 1328 // conversion of leading substring if available ("123@@@" -> 123) 1329 // 0 if no leading substring available 1330 int32_t value; 1331 EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 10)); 1332 EXPECT_EQ(4069, value); 1333 1334 EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 8)); 1335 EXPECT_EQ(0406, value); 1336 1337 EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 10)); 1338 EXPECT_EQ(4069, value); 1339 1340 EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 16)); 1341 EXPECT_EQ(0x4069ba, value); 1342 1343 EXPECT_FALSE(safe_strto32_base("@@@", &value, 10)); 1344 EXPECT_EQ(0, value); // there was no leading substring 1345 } 1346 1347 TEST(stringtest, safe_strto64_leading_substring) { 1348 // These tests verify this comment in numbers.h: 1349 // On error, returns false, and sets *value to: [...] 1350 // conversion of leading substring if available ("123@@@" -> 123) 1351 // 0 if no leading substring available 1352 int64_t value; 1353 EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 10)); 1354 EXPECT_EQ(4069, value); 1355 1356 EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 8)); 1357 EXPECT_EQ(0406, value); 1358 1359 EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 10)); 1360 EXPECT_EQ(4069, value); 1361 1362 EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 16)); 1363 EXPECT_EQ(0x4069ba, value); 1364 1365 EXPECT_FALSE(safe_strto64_base("@@@", &value, 10)); 1366 EXPECT_EQ(0, value); // there was no leading substring 1367 } 1368 1369 const size_t kNumRandomTests = 10000; 1370 1371 template <typename IntType> 1372 void test_random_integer_parse_base(bool (*parse_func)(absl::string_view, 1373 IntType* value, 1374 int base)) { 1375 using RandomEngine = std::minstd_rand0; 1376 std::random_device rd; 1377 RandomEngine rng(rd()); 1378 std::uniform_int_distribution<IntType> random_int( 1379 std::numeric_limits<IntType>::min()); 1380 std::uniform_int_distribution<int> random_base(2, 36); 1381 for (size_t i = 0; i < kNumRandomTests; i++) { 1382 IntType value = random_int(rng); 1383 int base = random_base(rng); 1384 std::string str_value; 1385 EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); 1386 IntType parsed_value; 1387 1388 // Test successful parse 1389 EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); 1390 EXPECT_EQ(parsed_value, value); 1391 1392 // Test overflow 1393 EXPECT_FALSE( 1394 parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), 1395 &parsed_value, base)); 1396 1397 // Test underflow 1398 if (std::numeric_limits<IntType>::min() < 0) { 1399 EXPECT_FALSE( 1400 parse_func(absl::StrCat(std::numeric_limits<IntType>::min(), value), 1401 &parsed_value, base)); 1402 } else { 1403 EXPECT_FALSE(parse_func(absl::StrCat("-", value), &parsed_value, base)); 1404 } 1405 } 1406 } 1407 1408 TEST(stringtest, safe_strto16_random) { 1409 test_random_integer_parse_base<int16_t>(&safe_strto16_base); 1410 } 1411 TEST(stringtest, safe_strto32_random) { 1412 test_random_integer_parse_base<int32_t>(&safe_strto32_base); 1413 } 1414 TEST(stringtest, safe_strto64_random) { 1415 test_random_integer_parse_base<int64_t>(&safe_strto64_base); 1416 } 1417 TEST(stringtest, safe_strtou16_random) { 1418 test_random_integer_parse_base<uint16_t>(&safe_strtou16_base); 1419 } 1420 TEST(stringtest, safe_strtou32_random) { 1421 test_random_integer_parse_base<uint32_t>(&safe_strtou32_base); 1422 } 1423 TEST(stringtest, safe_strtou64_random) { 1424 test_random_integer_parse_base<uint64_t>(&safe_strtou64_base); 1425 } 1426 TEST(stringtest, safe_strtou128_random) { 1427 // random number generators don't work for uint128 so this code must be custom 1428 // implemented for uint128, but is generally the same as what's above. 1429 // test_random_integer_parse_base<absl::uint128>( 1430 // &absl::numbers_internal::safe_strtou128_base); 1431 using RandomEngine = std::minstd_rand0; 1432 using IntType = absl::uint128; 1433 constexpr auto parse_func = &absl::numbers_internal::safe_strtou128_base; 1434 1435 std::random_device rd; 1436 RandomEngine rng(rd()); 1437 std::uniform_int_distribution<uint64_t> random_uint64( 1438 std::numeric_limits<uint64_t>::min()); 1439 std::uniform_int_distribution<int> random_base(2, 36); 1440 1441 for (size_t i = 0; i < kNumRandomTests; i++) { 1442 IntType value = random_uint64(rng); 1443 value = (value << 64) + random_uint64(rng); 1444 int base = random_base(rng); 1445 std::string str_value; 1446 EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); 1447 IntType parsed_value; 1448 1449 // Test successful parse 1450 EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); 1451 EXPECT_EQ(parsed_value, value); 1452 1453 // Test overflow 1454 EXPECT_FALSE( 1455 parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), 1456 &parsed_value, base)); 1457 1458 // Test underflow 1459 EXPECT_FALSE(parse_func(absl::StrCat("-", value), &parsed_value, base)); 1460 } 1461 } 1462 TEST(stringtest, safe_strto128_random) { 1463 // random number generators don't work for int128 so this code must be custom 1464 // implemented for int128, but is generally the same as what's above. 1465 // test_random_integer_parse_base<absl::int128>( 1466 // &absl::numbers_internal::safe_strto128_base); 1467 using RandomEngine = std::minstd_rand0; 1468 using IntType = absl::int128; 1469 constexpr auto parse_func = &absl::numbers_internal::safe_strto128_base; 1470 1471 std::random_device rd; 1472 RandomEngine rng(rd()); 1473 std::uniform_int_distribution<int64_t> random_int64( 1474 std::numeric_limits<int64_t>::min()); 1475 std::uniform_int_distribution<uint64_t> random_uint64( 1476 std::numeric_limits<uint64_t>::min()); 1477 std::uniform_int_distribution<int> random_base(2, 36); 1478 1479 for (size_t i = 0; i < kNumRandomTests; ++i) { 1480 int64_t high = random_int64(rng); 1481 uint64_t low = random_uint64(rng); 1482 IntType value = absl::MakeInt128(high, low); 1483 1484 int base = random_base(rng); 1485 std::string str_value; 1486 EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); 1487 IntType parsed_value; 1488 1489 // Test successful parse 1490 EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); 1491 EXPECT_EQ(parsed_value, value); 1492 1493 // Test overflow 1494 EXPECT_FALSE( 1495 parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), 1496 &parsed_value, base)); 1497 1498 // Test underflow 1499 EXPECT_FALSE( 1500 parse_func(absl::StrCat(std::numeric_limits<IntType>::min(), value), 1501 &parsed_value, base)); 1502 } 1503 } 1504 1505 TEST(stringtest, safe_strtou8_exhaustive) { 1506 // Testing the entire space for uint8_t since it is small. 1507 using IntType = uint8_t; 1508 constexpr auto parse_func = &absl::numbers_internal::safe_strtou8_base; 1509 1510 for (int i = std::numeric_limits<IntType>::min(); 1511 i <= std::numeric_limits<IntType>::max(); i++) { 1512 IntType value = static_cast<IntType>(i); 1513 for (int base = 2; base <= 36; base++) { 1514 std::string str_value; 1515 EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); 1516 IntType parsed_value; 1517 1518 // Test successful parse 1519 EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); 1520 EXPECT_EQ(parsed_value, value); 1521 1522 // Test overflow 1523 EXPECT_FALSE( 1524 parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), 1525 &parsed_value, base)); 1526 // Test underflow 1527 EXPECT_FALSE(parse_func(absl::StrCat("-", value), &parsed_value, base)); 1528 } 1529 } 1530 } 1531 1532 TEST(stringtest, safe_strto8_exhaustive) { 1533 // Testing the entire space for int8_t since it is small. 1534 using IntType = int8_t; 1535 constexpr auto parse_func = &absl::numbers_internal::safe_strto8_base; 1536 1537 for (int i = std::numeric_limits<IntType>::min(); 1538 i <= std::numeric_limits<IntType>::max(); i++) { 1539 IntType value = static_cast<IntType>(i); 1540 for (int base = 2; base <= 36; base++) { 1541 std::string str_value; 1542 EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); 1543 IntType parsed_value; 1544 1545 // Test successful parse 1546 EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); 1547 EXPECT_EQ(parsed_value, value); 1548 1549 // Test overflow 1550 EXPECT_FALSE( 1551 parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), 1552 &parsed_value, base)); 1553 // Test underflow 1554 EXPECT_FALSE( 1555 parse_func(absl::StrCat(std::numeric_limits<IntType>::min(), value), 1556 &parsed_value, base)); 1557 } 1558 } 1559 } 1560 1561 TEST(stringtest, safe_strtou32_base) { 1562 for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) { 1563 const auto& e = strtouint32_test_cases()[i]; 1564 uint32_t value; 1565 EXPECT_EQ(e.expect_ok, safe_strtou32_base(e.str, &value, e.base)) 1566 << "str=\"" << e.str << "\" base=" << e.base; 1567 if (e.expect_ok) { 1568 EXPECT_EQ(e.expected, value) << "i=" << i << " str=\"" << e.str 1569 << "\" base=" << e.base; 1570 } 1571 } 1572 } 1573 1574 TEST(stringtest, safe_strtou32_base_length_delimited) { 1575 for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) { 1576 const auto& e = strtouint32_test_cases()[i]; 1577 std::string tmp(e.str); 1578 tmp.append("12"); // Adds garbage at the end. 1579 1580 uint32_t value; 1581 EXPECT_EQ(e.expect_ok, 1582 safe_strtou32_base(absl::string_view(tmp.data(), strlen(e.str)), 1583 &value, e.base)) 1584 << "str=\"" << e.str << "\" base=" << e.base; 1585 if (e.expect_ok) { 1586 EXPECT_EQ(e.expected, value) << "i=" << i << " str=" << e.str 1587 << " base=" << e.base; 1588 } 1589 } 1590 } 1591 1592 TEST(stringtest, safe_strtou64_base) { 1593 for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) { 1594 const auto& e = strtouint64_test_cases()[i]; 1595 uint64_t value; 1596 EXPECT_EQ(e.expect_ok, safe_strtou64_base(e.str, &value, e.base)) 1597 << "str=\"" << e.str << "\" base=" << e.base; 1598 if (e.expect_ok) { 1599 EXPECT_EQ(e.expected, value) << "str=" << e.str << " base=" << e.base; 1600 } 1601 } 1602 } 1603 1604 TEST(stringtest, safe_strtou64_base_length_delimited) { 1605 for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) { 1606 const auto& e = strtouint64_test_cases()[i]; 1607 std::string tmp(e.str); 1608 tmp.append("12"); // Adds garbage at the end. 1609 1610 uint64_t value; 1611 EXPECT_EQ(e.expect_ok, 1612 safe_strtou64_base(absl::string_view(tmp.data(), strlen(e.str)), 1613 &value, e.base)) 1614 << "str=\"" << e.str << "\" base=" << e.base; 1615 if (e.expect_ok) { 1616 EXPECT_EQ(e.expected, value) << "str=\"" << e.str << "\" base=" << e.base; 1617 } 1618 } 1619 } 1620 1621 // feenableexcept() and fedisableexcept() are extensions supported by some libc 1622 // implementations. 1623 #if defined(__GLIBC__) || defined(__BIONIC__) 1624 #define ABSL_HAVE_FEENABLEEXCEPT 1 1625 #define ABSL_HAVE_FEDISABLEEXCEPT 1 1626 #endif 1627 1628 class SimpleDtoaTest : public testing::Test { 1629 protected: 1630 void SetUp() override { 1631 // Store the current floating point env & clear away any pending exceptions. 1632 feholdexcept(&fp_env_); 1633 #ifdef ABSL_HAVE_FEENABLEEXCEPT 1634 // Turn on floating point exceptions. 1635 feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); 1636 #endif 1637 } 1638 1639 void TearDown() override { 1640 // Restore the floating point environment to the original state. 1641 // In theory fedisableexcept is unnecessary; fesetenv will also do it. 1642 // In practice, our toolchains have subtle bugs. 1643 #ifdef ABSL_HAVE_FEDISABLEEXCEPT 1644 fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); 1645 #endif 1646 fesetenv(&fp_env_); 1647 } 1648 1649 std::string ToNineDigits(double value) { 1650 char buffer[16]; // more than enough for %.9g 1651 snprintf(buffer, sizeof(buffer), "%.9g", value); 1652 return buffer; 1653 } 1654 1655 fenv_t fp_env_; 1656 }; 1657 1658 // Run the given runnable functor for "cases" test cases, chosen over the 1659 // available range of float. pi and e and 1/e are seeded, and then all 1660 // available integer powers of 2 and 10 are multiplied against them. In 1661 // addition to trying all those values, we try the next higher and next lower 1662 // float, and then we add additional test cases evenly distributed between them. 1663 // Each test case is passed to runnable as both a positive and negative value. 1664 template <typename R> 1665 void ExhaustiveFloat(uint32_t cases, R&& runnable) { 1666 runnable(0.0f); 1667 runnable(-0.0f); 1668 if (cases >= 2e9) { // more than 2 billion? Might as well run them all. 1669 for (float f = 0; f < std::numeric_limits<float>::max(); ) { 1670 f = nextafterf(f, std::numeric_limits<float>::max()); 1671 runnable(-f); 1672 runnable(f); 1673 } 1674 return; 1675 } 1676 std::set<float> floats = {3.4028234e38f}; 1677 for (float f : {1.0, 3.14159265, 2.718281828, 1 / 2.718281828}) { 1678 for (float testf = f; testf != 0; testf *= 0.1f) floats.insert(testf); 1679 for (float testf = f; testf != 0; testf *= 0.5f) floats.insert(testf); 1680 for (float testf = f; testf < 3e38f / 2; testf *= 2.0f) 1681 floats.insert(testf); 1682 for (float testf = f; testf < 3e38f / 10; testf *= 10) floats.insert(testf); 1683 } 1684 1685 float last = *floats.begin(); 1686 1687 runnable(last); 1688 runnable(-last); 1689 int iters_per_float = cases / floats.size(); 1690 if (iters_per_float == 0) iters_per_float = 1; 1691 for (float f : floats) { 1692 if (f == last) continue; 1693 float testf = std::nextafter(last, std::numeric_limits<float>::max()); 1694 runnable(testf); 1695 runnable(-testf); 1696 last = testf; 1697 if (f == last) continue; 1698 double step = (double{f} - last) / iters_per_float; 1699 for (double d = last + step; d < f; d += step) { 1700 testf = d; 1701 if (testf != last) { 1702 runnable(testf); 1703 runnable(-testf); 1704 last = testf; 1705 } 1706 } 1707 testf = std::nextafter(f, 0.0f); 1708 if (testf > last) { 1709 runnable(testf); 1710 runnable(-testf); 1711 last = testf; 1712 } 1713 if (f != last) { 1714 runnable(f); 1715 runnable(-f); 1716 last = f; 1717 } 1718 } 1719 } 1720 1721 TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) { 1722 uint64_t test_count = 0; 1723 std::vector<double> mismatches; 1724 auto checker = [&](double d) { 1725 if (d != d) return; // rule out NaNs 1726 ++test_count; 1727 char sixdigitsbuf[kSixDigitsToBufferSize] = {0}; 1728 SixDigitsToBuffer(d, sixdigitsbuf); 1729 char snprintfbuf[kSixDigitsToBufferSize] = {0}; 1730 snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d); 1731 if (strcmp(sixdigitsbuf, snprintfbuf) != 0) { 1732 mismatches.push_back(d); 1733 if (mismatches.size() < 10) { 1734 LOG(ERROR) << "Six-digit failure with double. d=" << d 1735 << " sixdigits=" << sixdigitsbuf 1736 << " printf(%g)=" << snprintfbuf; 1737 } 1738 } 1739 }; 1740 // Some quick sanity checks... 1741 checker(5e-324); 1742 checker(1e-308); 1743 checker(1.0); 1744 checker(1.000005); 1745 checker(1.7976931348623157e308); 1746 checker(0.00390625); 1747 #ifndef _MSC_VER 1748 // on MSVC, snprintf() rounds it to 0.00195313. SixDigitsToBuffer() rounds it 1749 // to 0.00195312 (round half to even). 1750 checker(0.001953125); 1751 #endif 1752 checker(0.005859375); 1753 // Some cases where the rounding is very very close 1754 checker(1.089095e-15); 1755 checker(3.274195e-55); 1756 checker(6.534355e-146); 1757 checker(2.920845e+234); 1758 1759 if (mismatches.empty()) { 1760 test_count = 0; 1761 ExhaustiveFloat(kFloatNumCases, checker); 1762 1763 test_count = 0; 1764 std::vector<int> digit_testcases{ 1765 100000, 100001, 100002, 100005, 100010, 100020, 100050, 100100, // misc 1766 195312, 195313, // 1.953125 is a case where we round down, just barely. 1767 200000, 500000, 800000, // misc mid-range cases 1768 585937, 585938, // 5.859375 is a case where we round up, just barely. 1769 900000, 990000, 999000, 999900, 999990, 999996, 999997, 999998, 999999}; 1770 if (kFloatNumCases >= 1e9) { 1771 // If at least 1 billion test cases were requested, user wants an 1772 // exhaustive test. So let's test all mantissas, too. 1773 constexpr int min_mantissa = 100000, max_mantissa = 999999; 1774 digit_testcases.resize(max_mantissa - min_mantissa + 1); 1775 std::iota(digit_testcases.begin(), digit_testcases.end(), min_mantissa); 1776 } 1777 1778 for (int exponent = -324; exponent <= 308; ++exponent) { 1779 double powten = absl::strings_internal::Pow10(exponent); 1780 if (powten == 0) powten = 5e-324; 1781 if (kFloatNumCases >= 1e9) { 1782 // The exhaustive test takes a very long time, so log progress. 1783 char buf[kSixDigitsToBufferSize]; 1784 LOG(INFO) << "Exp " << exponent << " powten=" << powten << "(" << powten 1785 << ") (" 1786 << absl::string_view(buf, SixDigitsToBuffer(powten, buf)) 1787 << ")"; 1788 } 1789 for (int digits : digit_testcases) { 1790 if (exponent == 308 && digits >= 179769) break; // don't overflow! 1791 double digiform = (digits + 0.5) * 0.00001; 1792 double testval = digiform * powten; 1793 double pretestval = nextafter(testval, 0); 1794 double posttestval = nextafter(testval, 1.7976931348623157e308); 1795 checker(testval); 1796 checker(pretestval); 1797 checker(posttestval); 1798 } 1799 } 1800 } else { 1801 EXPECT_EQ(mismatches.size(), 0); 1802 for (size_t i = 0; i < mismatches.size(); ++i) { 1803 if (i > 100) i = mismatches.size() - 1; 1804 double d = mismatches[i]; 1805 char sixdigitsbuf[kSixDigitsToBufferSize] = {0}; 1806 SixDigitsToBuffer(d, sixdigitsbuf); 1807 char snprintfbuf[kSixDigitsToBufferSize] = {0}; 1808 snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d); 1809 double before = nextafter(d, 0.0); 1810 double after = nextafter(d, 1.7976931348623157e308); 1811 char b1[32], b2[kSixDigitsToBufferSize]; 1812 LOG(ERROR) << "Mismatch #" << i << " d=" << d << " (" << ToNineDigits(d) 1813 << ") sixdigits='" << sixdigitsbuf << "' snprintf='" 1814 << snprintfbuf << "' Before.=" << PerfectDtoa(before) << " " 1815 << (SixDigitsToBuffer(before, b2), b2) << " vs snprintf=" 1816 << (snprintf(b1, sizeof(b1), "%g", before), b1) 1817 << " Perfect=" << PerfectDtoa(d) << " " 1818 << (SixDigitsToBuffer(d, b2), b2) 1819 << " vs snprintf=" << (snprintf(b1, sizeof(b1), "%g", d), b1) 1820 << " After.=." << PerfectDtoa(after) << " " 1821 << (SixDigitsToBuffer(after, b2), b2) << " vs snprintf=" 1822 << (snprintf(b1, sizeof(b1), "%g", after), b1); 1823 } 1824 } 1825 } 1826 1827 TEST(StrToInt8, Partial) { 1828 struct Int8TestLine { 1829 std::string input; 1830 bool status; 1831 int8_t value; 1832 }; 1833 const int8_t int8_min = std::numeric_limits<int8_t>::min(); 1834 const int8_t int8_max = std::numeric_limits<int8_t>::max(); 1835 Int8TestLine int8_test_line[] = { 1836 {"", false, 0}, 1837 {" ", false, 0}, 1838 {"-", false, 0}, 1839 {"123@@@", false, 123}, 1840 {absl::StrCat(int8_min, int8_max), false, int8_min}, 1841 {absl::StrCat(int8_max, int8_max), false, int8_max}, 1842 }; 1843 1844 for (const Int8TestLine& test_line : int8_test_line) { 1845 int8_t value = -2; 1846 bool status = safe_strto8_base(test_line.input, &value, 10); 1847 EXPECT_EQ(test_line.status, status) << test_line.input; 1848 EXPECT_EQ(test_line.value, value) << test_line.input; 1849 value = -2; 1850 status = safe_strto8_base(test_line.input, &value, 10); 1851 EXPECT_EQ(test_line.status, status) << test_line.input; 1852 EXPECT_EQ(test_line.value, value) << test_line.input; 1853 value = -2; 1854 status = safe_strto8_base(absl::string_view(test_line.input), &value, 10); 1855 EXPECT_EQ(test_line.status, status) << test_line.input; 1856 EXPECT_EQ(test_line.value, value) << test_line.input; 1857 } 1858 } 1859 1860 TEST(StrToUint8, Partial) { 1861 struct Uint8TestLine { 1862 std::string input; 1863 bool status; 1864 uint8_t value; 1865 }; 1866 const uint8_t uint8_max = std::numeric_limits<uint8_t>::max(); 1867 Uint8TestLine uint8_test_line[] = { 1868 {"", false, 0}, 1869 {" ", false, 0}, 1870 {"-", false, 0}, 1871 {"123@@@", false, 123}, 1872 {absl::StrCat(uint8_max, uint8_max), false, uint8_max}, 1873 }; 1874 1875 for (const Uint8TestLine& test_line : uint8_test_line) { 1876 uint8_t value = 2; 1877 bool status = safe_strtou8_base(test_line.input, &value, 10); 1878 EXPECT_EQ(test_line.status, status) << test_line.input; 1879 EXPECT_EQ(test_line.value, value) << test_line.input; 1880 value = 2; 1881 status = safe_strtou8_base(test_line.input, &value, 10); 1882 EXPECT_EQ(test_line.status, status) << test_line.input; 1883 EXPECT_EQ(test_line.value, value) << test_line.input; 1884 value = 2; 1885 status = safe_strtou8_base(absl::string_view(test_line.input), &value, 10); 1886 EXPECT_EQ(test_line.status, status) << test_line.input; 1887 EXPECT_EQ(test_line.value, value) << test_line.input; 1888 } 1889 } 1890 1891 TEST(StrToInt16, Partial) { 1892 struct Int16TestLine { 1893 std::string input; 1894 bool status; 1895 int16_t value; 1896 }; 1897 const int16_t int16_min = std::numeric_limits<int16_t>::min(); 1898 const int16_t int16_max = std::numeric_limits<int16_t>::max(); 1899 Int16TestLine int16_test_line[] = { 1900 {"", false, 0}, 1901 {" ", false, 0}, 1902 {"-", false, 0}, 1903 {"123@@@", false, 123}, 1904 {absl::StrCat(int16_min, int16_max), false, int16_min}, 1905 {absl::StrCat(int16_max, int16_max), false, int16_max}, 1906 }; 1907 1908 for (const Int16TestLine& test_line : int16_test_line) { 1909 int16_t value = -2; 1910 bool status = safe_strto16_base(test_line.input, &value, 10); 1911 EXPECT_EQ(test_line.status, status) << test_line.input; 1912 EXPECT_EQ(test_line.value, value) << test_line.input; 1913 value = -2; 1914 status = safe_strto16_base(test_line.input, &value, 10); 1915 EXPECT_EQ(test_line.status, status) << test_line.input; 1916 EXPECT_EQ(test_line.value, value) << test_line.input; 1917 value = -2; 1918 status = safe_strto16_base(absl::string_view(test_line.input), &value, 10); 1919 EXPECT_EQ(test_line.status, status) << test_line.input; 1920 EXPECT_EQ(test_line.value, value) << test_line.input; 1921 } 1922 } 1923 1924 TEST(StrToUint16, Partial) { 1925 struct Uint16TestLine { 1926 std::string input; 1927 bool status; 1928 uint16_t value; 1929 }; 1930 const uint16_t uint16_max = std::numeric_limits<uint16_t>::max(); 1931 Uint16TestLine uint16_test_line[] = { 1932 {"", false, 0}, 1933 {" ", false, 0}, 1934 {"-", false, 0}, 1935 {"123@@@", false, 123}, 1936 {absl::StrCat(uint16_max, uint16_max), false, uint16_max}, 1937 }; 1938 1939 for (const Uint16TestLine& test_line : uint16_test_line) { 1940 uint16_t value = 2; 1941 bool status = safe_strtou16_base(test_line.input, &value, 10); 1942 EXPECT_EQ(test_line.status, status) << test_line.input; 1943 EXPECT_EQ(test_line.value, value) << test_line.input; 1944 value = 2; 1945 status = safe_strtou16_base(test_line.input, &value, 10); 1946 EXPECT_EQ(test_line.status, status) << test_line.input; 1947 EXPECT_EQ(test_line.value, value) << test_line.input; 1948 value = 2; 1949 status = safe_strtou16_base(absl::string_view(test_line.input), &value, 10); 1950 EXPECT_EQ(test_line.status, status) << test_line.input; 1951 EXPECT_EQ(test_line.value, value) << test_line.input; 1952 } 1953 } 1954 1955 TEST(StrToInt32, Partial) { 1956 struct Int32TestLine { 1957 std::string input; 1958 bool status; 1959 int32_t value; 1960 }; 1961 const int32_t int32_min = std::numeric_limits<int32_t>::min(); 1962 const int32_t int32_max = std::numeric_limits<int32_t>::max(); 1963 Int32TestLine int32_test_line[] = { 1964 {"", false, 0}, 1965 {" ", false, 0}, 1966 {"-", false, 0}, 1967 {"123@@@", false, 123}, 1968 {absl::StrCat(int32_min, int32_max), false, int32_min}, 1969 {absl::StrCat(int32_max, int32_max), false, int32_max}, 1970 }; 1971 1972 for (const Int32TestLine& test_line : int32_test_line) { 1973 int32_t value = -2; 1974 bool status = safe_strto32_base(test_line.input, &value, 10); 1975 EXPECT_EQ(test_line.status, status) << test_line.input; 1976 EXPECT_EQ(test_line.value, value) << test_line.input; 1977 value = -2; 1978 status = safe_strto32_base(test_line.input, &value, 10); 1979 EXPECT_EQ(test_line.status, status) << test_line.input; 1980 EXPECT_EQ(test_line.value, value) << test_line.input; 1981 value = -2; 1982 status = safe_strto32_base(absl::string_view(test_line.input), &value, 10); 1983 EXPECT_EQ(test_line.status, status) << test_line.input; 1984 EXPECT_EQ(test_line.value, value) << test_line.input; 1985 } 1986 } 1987 1988 TEST(StrToUint32, Partial) { 1989 struct Uint32TestLine { 1990 std::string input; 1991 bool status; 1992 uint32_t value; 1993 }; 1994 const uint32_t uint32_max = std::numeric_limits<uint32_t>::max(); 1995 Uint32TestLine uint32_test_line[] = { 1996 {"", false, 0}, 1997 {" ", false, 0}, 1998 {"-", false, 0}, 1999 {"123@@@", false, 123}, 2000 {absl::StrCat(uint32_max, uint32_max), false, uint32_max}, 2001 }; 2002 2003 for (const Uint32TestLine& test_line : uint32_test_line) { 2004 uint32_t value = 2; 2005 bool status = safe_strtou32_base(test_line.input, &value, 10); 2006 EXPECT_EQ(test_line.status, status) << test_line.input; 2007 EXPECT_EQ(test_line.value, value) << test_line.input; 2008 value = 2; 2009 status = safe_strtou32_base(test_line.input, &value, 10); 2010 EXPECT_EQ(test_line.status, status) << test_line.input; 2011 EXPECT_EQ(test_line.value, value) << test_line.input; 2012 value = 2; 2013 status = safe_strtou32_base(absl::string_view(test_line.input), &value, 10); 2014 EXPECT_EQ(test_line.status, status) << test_line.input; 2015 EXPECT_EQ(test_line.value, value) << test_line.input; 2016 } 2017 } 2018 2019 TEST(StrToInt64, Partial) { 2020 struct Int64TestLine { 2021 std::string input; 2022 bool status; 2023 int64_t value; 2024 }; 2025 const int64_t int64_min = std::numeric_limits<int64_t>::min(); 2026 const int64_t int64_max = std::numeric_limits<int64_t>::max(); 2027 Int64TestLine int64_test_line[] = { 2028 {"", false, 0}, 2029 {" ", false, 0}, 2030 {"-", false, 0}, 2031 {"123@@@", false, 123}, 2032 {absl::StrCat(int64_min, int64_max), false, int64_min}, 2033 {absl::StrCat(int64_max, int64_max), false, int64_max}, 2034 }; 2035 2036 for (const Int64TestLine& test_line : int64_test_line) { 2037 int64_t value = -2; 2038 bool status = safe_strto64_base(test_line.input, &value, 10); 2039 EXPECT_EQ(test_line.status, status) << test_line.input; 2040 EXPECT_EQ(test_line.value, value) << test_line.input; 2041 value = -2; 2042 status = safe_strto64_base(test_line.input, &value, 10); 2043 EXPECT_EQ(test_line.status, status) << test_line.input; 2044 EXPECT_EQ(test_line.value, value) << test_line.input; 2045 value = -2; 2046 status = safe_strto64_base(absl::string_view(test_line.input), &value, 10); 2047 EXPECT_EQ(test_line.status, status) << test_line.input; 2048 EXPECT_EQ(test_line.value, value) << test_line.input; 2049 } 2050 } 2051 2052 TEST(StrToUint64, Partial) { 2053 struct Uint64TestLine { 2054 std::string input; 2055 bool status; 2056 uint64_t value; 2057 }; 2058 const uint64_t uint64_max = std::numeric_limits<uint64_t>::max(); 2059 Uint64TestLine uint64_test_line[] = { 2060 {"", false, 0}, 2061 {" ", false, 0}, 2062 {"-", false, 0}, 2063 {"123@@@", false, 123}, 2064 {absl::StrCat(uint64_max, uint64_max), false, uint64_max}, 2065 }; 2066 2067 for (const Uint64TestLine& test_line : uint64_test_line) { 2068 uint64_t value = 2; 2069 bool status = safe_strtou64_base(test_line.input, &value, 10); 2070 EXPECT_EQ(test_line.status, status) << test_line.input; 2071 EXPECT_EQ(test_line.value, value) << test_line.input; 2072 value = 2; 2073 status = safe_strtou64_base(test_line.input, &value, 10); 2074 EXPECT_EQ(test_line.status, status) << test_line.input; 2075 EXPECT_EQ(test_line.value, value) << test_line.input; 2076 value = 2; 2077 status = safe_strtou64_base(absl::string_view(test_line.input), &value, 10); 2078 EXPECT_EQ(test_line.status, status) << test_line.input; 2079 EXPECT_EQ(test_line.value, value) << test_line.input; 2080 } 2081 } 2082 2083 TEST(StrToInt32Base, PrefixOnly) { 2084 struct Int32TestLine { 2085 std::string input; 2086 bool status; 2087 int32_t value; 2088 }; 2089 Int32TestLine int32_test_line[] = { 2090 { "", false, 0 }, 2091 { "-", false, 0 }, 2092 { "-0", true, 0 }, 2093 { "0", true, 0 }, 2094 { "0x", false, 0 }, 2095 { "-0x", false, 0 }, 2096 }; 2097 const int base_array[] = { 0, 2, 8, 10, 16 }; 2098 2099 for (const Int32TestLine& line : int32_test_line) { 2100 for (const int base : base_array) { 2101 int32_t value = 2; 2102 bool status = safe_strto32_base(line.input.c_str(), &value, base); 2103 EXPECT_EQ(line.status, status) << line.input << " " << base; 2104 EXPECT_EQ(line.value, value) << line.input << " " << base; 2105 value = 2; 2106 status = safe_strto32_base(line.input, &value, base); 2107 EXPECT_EQ(line.status, status) << line.input << " " << base; 2108 EXPECT_EQ(line.value, value) << line.input << " " << base; 2109 value = 2; 2110 status = safe_strto32_base(absl::string_view(line.input), &value, base); 2111 EXPECT_EQ(line.status, status) << line.input << " " << base; 2112 EXPECT_EQ(line.value, value) << line.input << " " << base; 2113 } 2114 } 2115 } 2116 2117 TEST(StrToUint32Base, PrefixOnly) { 2118 struct Uint32TestLine { 2119 std::string input; 2120 bool status; 2121 uint32_t value; 2122 }; 2123 Uint32TestLine uint32_test_line[] = { 2124 { "", false, 0 }, 2125 { "0", true, 0 }, 2126 { "0x", false, 0 }, 2127 }; 2128 const int base_array[] = { 0, 2, 8, 10, 16 }; 2129 2130 for (const Uint32TestLine& line : uint32_test_line) { 2131 for (const int base : base_array) { 2132 uint32_t value = 2; 2133 bool status = safe_strtou32_base(line.input.c_str(), &value, base); 2134 EXPECT_EQ(line.status, status) << line.input << " " << base; 2135 EXPECT_EQ(line.value, value) << line.input << " " << base; 2136 value = 2; 2137 status = safe_strtou32_base(line.input, &value, base); 2138 EXPECT_EQ(line.status, status) << line.input << " " << base; 2139 EXPECT_EQ(line.value, value) << line.input << " " << base; 2140 value = 2; 2141 status = safe_strtou32_base(absl::string_view(line.input), &value, base); 2142 EXPECT_EQ(line.status, status) << line.input << " " << base; 2143 EXPECT_EQ(line.value, value) << line.input << " " << base; 2144 } 2145 } 2146 } 2147 2148 TEST(StrToInt64Base, PrefixOnly) { 2149 struct Int64TestLine { 2150 std::string input; 2151 bool status; 2152 int64_t value; 2153 }; 2154 Int64TestLine int64_test_line[] = { 2155 { "", false, 0 }, 2156 { "-", false, 0 }, 2157 { "-0", true, 0 }, 2158 { "0", true, 0 }, 2159 { "0x", false, 0 }, 2160 { "-0x", false, 0 }, 2161 }; 2162 const int base_array[] = { 0, 2, 8, 10, 16 }; 2163 2164 for (const Int64TestLine& line : int64_test_line) { 2165 for (const int base : base_array) { 2166 int64_t value = 2; 2167 bool status = safe_strto64_base(line.input.c_str(), &value, base); 2168 EXPECT_EQ(line.status, status) << line.input << " " << base; 2169 EXPECT_EQ(line.value, value) << line.input << " " << base; 2170 value = 2; 2171 status = safe_strto64_base(line.input, &value, base); 2172 EXPECT_EQ(line.status, status) << line.input << " " << base; 2173 EXPECT_EQ(line.value, value) << line.input << " " << base; 2174 value = 2; 2175 status = safe_strto64_base(absl::string_view(line.input), &value, base); 2176 EXPECT_EQ(line.status, status) << line.input << " " << base; 2177 EXPECT_EQ(line.value, value) << line.input << " " << base; 2178 } 2179 } 2180 } 2181 2182 TEST(StrToUint64Base, PrefixOnly) { 2183 struct Uint64TestLine { 2184 std::string input; 2185 bool status; 2186 uint64_t value; 2187 }; 2188 Uint64TestLine uint64_test_line[] = { 2189 { "", false, 0 }, 2190 { "0", true, 0 }, 2191 { "0x", false, 0 }, 2192 }; 2193 const int base_array[] = { 0, 2, 8, 10, 16 }; 2194 2195 for (const Uint64TestLine& line : uint64_test_line) { 2196 for (const int base : base_array) { 2197 uint64_t value = 2; 2198 bool status = safe_strtou64_base(line.input.c_str(), &value, base); 2199 EXPECT_EQ(line.status, status) << line.input << " " << base; 2200 EXPECT_EQ(line.value, value) << line.input << " " << base; 2201 value = 2; 2202 status = safe_strtou64_base(line.input, &value, base); 2203 EXPECT_EQ(line.status, status) << line.input << " " << base; 2204 EXPECT_EQ(line.value, value) << line.input << " " << base; 2205 value = 2; 2206 status = safe_strtou64_base(absl::string_view(line.input), &value, base); 2207 EXPECT_EQ(line.status, status) << line.input << " " << base; 2208 EXPECT_EQ(line.value, value) << line.input << " " << base; 2209 } 2210 } 2211 } 2212 2213 void TestFastHexToBufferZeroPad16(uint64_t v) { 2214 char buf[16]; 2215 auto digits = absl::numbers_internal::FastHexToBufferZeroPad16(v, buf); 2216 absl::string_view res(buf, 16); 2217 char buf2[17]; 2218 snprintf(buf2, sizeof(buf2), "%016" PRIx64, v); 2219 EXPECT_EQ(res, buf2) << v; 2220 size_t expected_digits = snprintf(buf2, sizeof(buf2), "%" PRIx64, v); 2221 EXPECT_EQ(digits, expected_digits) << v; 2222 } 2223 2224 TEST(FastHexToBufferZeroPad16, Smoke) { 2225 TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::min()); 2226 TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::max()); 2227 TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::min()); 2228 TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::max()); 2229 absl::BitGen rng; 2230 for (int i = 0; i < 100000; ++i) { 2231 TestFastHexToBufferZeroPad16( 2232 absl::LogUniform(rng, std::numeric_limits<uint64_t>::min(), 2233 std::numeric_limits<uint64_t>::max())); 2234 } 2235 } 2236 2237 template <typename Int> 2238 void ExpectWritesNull() { 2239 { 2240 char buf[absl::numbers_internal::kFastToBufferSize]; 2241 Int x = std::numeric_limits<Int>::min(); 2242 EXPECT_THAT(absl::numbers_internal::FastIntToBuffer(x, buf), Pointee('\0')); 2243 } 2244 { 2245 char buf[absl::numbers_internal::kFastToBufferSize]; 2246 Int x = std::numeric_limits<Int>::max(); 2247 EXPECT_THAT(absl::numbers_internal::FastIntToBuffer(x, buf), Pointee('\0')); 2248 } 2249 } 2250 2251 TEST(FastIntToBuffer, WritesNull) { 2252 ExpectWritesNull<int8_t>(); 2253 ExpectWritesNull<uint8_t>(); 2254 ExpectWritesNull<int16_t>(); 2255 ExpectWritesNull<uint16_t>(); 2256 ExpectWritesNull<int32_t>(); 2257 ExpectWritesNull<uint32_t>(); 2258 ExpectWritesNull<int64_t>(); 2259 ExpectWritesNull<uint32_t>(); 2260 } 2261 2262 } // namespace