| 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 <vector> |
| 38 |
| 39 | #include "gmock/gmock.h" |
| 40 | #include "gtest/gtest.h" |
| 41 | #include "absl/log/log.h" |
| 42 | #include "absl/numeric/int128.h" |
| 43 | #include "absl/random/distributions.h" |
| 44 | #include "absl/random/random.h" |
| 45 | #include "absl/strings/internal/numbers_test_common.h" |
| 46 | #include "absl/strings/internal/ostringstream.h" |
| 47 | #include "absl/strings/internal/pow10_helper.h" |
| 48 | #include "absl/strings/str_cat.h" |
| 49 | #include "absl/strings/string_view.h" |
| 50 |
| 51 | namespace { |
| 52 |
| 53 | using absl::SimpleAtoi; |
| 54 | using absl::SimpleHexAtoi; |
| 55 | using absl::numbers_internal::kSixDigitsToBufferSize; |
| 56 | using absl::numbers_internal::safe_strto32_base; |
| 57 | using absl::numbers_internal::safe_strto64_base; |
| 58 | using absl::numbers_internal::safe_strtou32_base; |
| 59 | using absl::numbers_internal::safe_strtou64_base; |
| 60 | using absl::numbers_internal::SixDigitsToBuffer; |
| 61 | using absl::strings_internal::Itoa; |
| 62 | using absl::strings_internal::strtouint32_test_cases; |
| 63 | using absl::strings_internal::strtouint64_test_cases; |
| 64 | using testing::Eq; |
| 65 | using testing::MatchesRegex; |
| 66 | using testing::Pointee; |
| 67 |
| 68 | // Number of floats to test with. |
| 69 | // 5,000,000 is a reasonable default for a test that only takes a few seconds. |
| 70 | // 1,000,000,000+ triggers checking for all possible mantissa values for |
| 71 | // double-precision tests. 2,000,000,000+ triggers checking for every possible |
| 72 | // single-precision float. |
| 73 | const int kFloatNumCases = 5000000; |
| 74 |
| 75 | // This is a slow, brute-force routine to compute the exact base-10 |
| 76 | // representation of a double-precision floating-point number. It |
| 77 | // is useful for debugging only. |
| 78 | std::string PerfectDtoa(double d) { |
| 79 | if (d == 0) return "0"; |
| 80 | if (d < 0) return "-" + PerfectDtoa(-d); |
| 81 |
| 82 | // Basic theory: decompose d into mantissa and exp, where |
| 83 | // d = mantissa * 2^exp, and exp is as close to zero as possible. |
| 84 | int64_t mantissa, exp = 0; |
| 85 | while (d >= 1ULL << 63) ++exp, d *= 0.5; |
| 86 | while ((mantissa = d) != d) --exp, d *= 2.0; |
| 87 |
| 88 | // Then convert mantissa to ASCII, and either double it (if |
| 89 | // exp > 0) or halve it (if exp < 0) repeatedly. "halve it" |
| 90 | // in this case means multiplying it by five and dividing by 10. |
| 91 | constexpr int maxlen = 1100; // worst case is actually 1030 or so. |
| 92 | char buf[maxlen + 5]; |
| 93 | for (int64_t num = mantissa, pos = maxlen; --pos >= 0;) { |
| 94 | buf[pos] = '0' + (num % 10); |
| 95 | num /= 10; |
| 96 | } |
| 97 | char* begin = &buf[0]; |
| 98 | char* end = buf + maxlen; |
| 99 | for (int i = 0; i != exp; i += (exp > 0) ? 1 : -1) { |
| 100 | int carry = 0; |
| 101 | for (char* p = end; --p != begin;) { |
| 102 | int dig = *p - '0'; |
| 103 | dig = dig * (exp > 0 ? 2 : 5) + carry; |
| 104 | carry = dig / 10; |
| 105 | dig %= 10; |
| 106 | *p = '0' + dig; |
| 107 | } |
| 108 | } |
| 109 | if (exp < 0) { |
| 110 | // "dividing by 10" above means we have to add the decimal point. |
| 111 | memmove(end + 1 + exp, end + exp, 1 - exp); |
| 112 | end[exp] = '.'; |
| 113 | ++end; |
| 114 | } |
| 115 | while (*begin == '0' && begin[1] != '.') ++begin; |
| 116 | return {begin, end}; |
| 117 | } |
| 118 |
| 119 | TEST(ToString, PerfectDtoa) { |
| 120 | EXPECT_THAT(PerfectDtoa(1), Eq("1")); |
| 121 | EXPECT_THAT(PerfectDtoa(0.1), |
| 122 | Eq("0.1000000000000000055511151231257827021181583404541015625")); |
| 123 | EXPECT_THAT(PerfectDtoa(1e24), Eq("999999999999999983222784")); |
| 124 | EXPECT_THAT(PerfectDtoa(5e-324), MatchesRegex("0.0000.*625")); |
| 125 | for (int i = 0; i < 100; ++i) { |
| 126 | for (double multiplier : |
| 127 | {1e-300, 1e-200, 1e-100, 0.1, 1.0, 10.0, 1e100, 1e300}) { |
| 128 | double d = multiplier * i; |
| 129 | std::string s = PerfectDtoa(d); |
| 130 | EXPECT_DOUBLE_EQ(d, strtod(s.c_str(), nullptr)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 |
| 135 | template <typename integer> |
| 136 | struct MyInteger { |
| 137 | integer i; |
| 138 | explicit constexpr MyInteger(integer i) : i(i) {} |
| 139 | constexpr operator integer() const { return i; } |
| 140 |
| 141 | constexpr MyInteger operator+(MyInteger other) const { return i + other.i; } |
| 142 | constexpr MyInteger operator-(MyInteger other) const { return i - other.i; } |
| 143 | constexpr MyInteger operator*(MyInteger other) const { return i * other.i; } |
| 144 | constexpr MyInteger operator/(MyInteger other) const { return i / other.i; } |
| 145 |
| 146 | constexpr bool operator<(MyInteger other) const { return i < other.i; } |
| 147 | constexpr bool operator<=(MyInteger other) const { return i <= other.i; } |
| 148 | constexpr bool operator==(MyInteger other) const { return i == other.i; } |
| 149 | constexpr bool operator>=(MyInteger other) const { return i >= other.i; } |
| 150 | constexpr bool operator>(MyInteger other) const { return i > other.i; } |
| 151 | constexpr bool operator!=(MyInteger other) const { return i != other.i; } |
| 152 |
| 153 | integer as_integer() const { return i; } |
| 154 | }; |
| 155 |
| 156 | typedef MyInteger<int64_t> MyInt64; |
| 157 | typedef MyInteger<uint64_t> MyUInt64; |
| 158 |
| 159 | void CheckInt32(int32_t x) { |
| 160 | char buffer[absl::numbers_internal::kFastToBufferSize]; |
| 161 | char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); |
| 162 | std::string expected = std::to_string(x); |
| 163 | EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; |
| 164 |
| 165 | char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); |
| 166 | EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; |
| 167 | } |
| 168 |
| 169 | void CheckInt64(int64_t x) { |
| 170 | char buffer[absl::numbers_internal::kFastToBufferSize + 3]; |
| 171 | buffer[0] = '*'; |
| 172 | buffer[23] = '*'; |
| 173 | buffer[24] = '*'; |
| 174 | char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); |
| 175 | std::string expected = std::to_string(x); |
| 176 | EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; |
| 177 | EXPECT_EQ(buffer[0], '*'); |
| 178 | EXPECT_EQ(buffer[23], '*'); |
| 179 | EXPECT_EQ(buffer[24], '*'); |
| 180 |
| 181 | char* my_actual = |
| 182 | absl::numbers_internal::FastIntToBuffer(MyInt64(x), &buffer[1]); |
| 183 | EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; |
| 184 | } |
| 185 |
| 186 | void CheckUInt32(uint32_t x) { |
| 187 | char buffer[absl::numbers_internal::kFastToBufferSize]; |
| 188 | char* actual = absl::numbers_internal::FastIntToBuffer(x, buffer); |
| 189 | std::string expected = std::to_string(x); |
| 190 | EXPECT_EQ(expected, std::string(buffer, actual)) << " Input " << x; |
| 191 |
| 192 | char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, buffer); |
| 193 | EXPECT_EQ(expected, std::string(buffer, generic_actual)) << " Input " << x; |
| 194 | } |
| 195 |
| 196 | void CheckUInt64(uint64_t x) { |
| 197 | char buffer[absl::numbers_internal::kFastToBufferSize + 1]; |
| 198 | char* actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); |
| 199 | std::string expected = std::to_string(x); |
| 200 | EXPECT_EQ(expected, std::string(&buffer[1], actual)) << " Input " << x; |
| 201 |
| 202 | char* generic_actual = absl::numbers_internal::FastIntToBuffer(x, &buffer[1]); |
| 203 | EXPECT_EQ(expected, std::string(&buffer[1], generic_actual)) |
| 204 | << " Input " << x; |
| 205 |
| 206 | char* my_actual = |
| 207 | absl::numbers_internal::FastIntToBuffer(MyUInt64(x), &buffer[1]); |
| 208 | EXPECT_EQ(expected, std::string(&buffer[1], my_actual)) << " Input " << x; |
| 209 | } |
| 210 |
| 211 | void CheckHex64(uint64_t v) { |
| 212 | char expected[16 + 1]; |
| 213 | std::string actual = absl::StrCat(absl::Hex(v, absl::kZeroPad16)); |
| 214 | snprintf(expected, sizeof(expected), "%016" PRIx64, static_cast<uint64_t>(v)); |
| 215 | EXPECT_EQ(expected, actual) << " Input " << v; |
| 216 | actual = absl::StrCat(absl::Hex(v, absl::kSpacePad16)); |
| 217 | snprintf(expected, sizeof(expected), "%16" PRIx64, static_cast<uint64_t>(v)); |
| 218 | EXPECT_EQ(expected, actual) << " Input " << v; |
| 219 | } |
| 220 |
| 221 | TEST(Numbers, TestFastPrints) { |
| 222 | for (int i = -100; i <= 100; i++) { |
| 223 | CheckInt32(i); |
| 224 | CheckInt64(i); |
| 225 | } |
| 226 | for (int i = 0; i <= 100; i++) { |
| 227 | CheckUInt32(i); |
| 228 | CheckUInt64(i); |
| 229 | } |
| 230 | // Test min int to make sure that works |
| 231 | CheckInt32(INT_MIN); |
| 232 | CheckInt32(INT_MAX); |
| 233 | CheckInt64(LONG_MIN); |
| 234 | CheckInt64(uint64_t{1000000000}); |
| 235 | CheckInt64(uint64_t{9999999999}); |
| 236 | CheckInt64(uint64_t{100000000000000}); |
| 237 | CheckInt64(uint64_t{999999999999999}); |
| 238 | CheckInt64(uint64_t{1000000000000000000}); |
| 239 | CheckInt64(uint64_t{1199999999999999999}); |
| 240 | CheckInt64(int64_t{-700000000000000000}); |
| 241 | CheckInt64(LONG_MAX); |
| 242 | CheckUInt32(std::numeric_limits<uint32_t>::max()); |
| 243 | CheckUInt64(uint64_t{1000000000}); |
| 244 | CheckUInt64(uint64_t{9999999999}); |
| 245 | CheckUInt64(uint64_t{100000000000000}); |
| 246 | CheckUInt64(uint64_t{999999999999999}); |
| 247 | CheckUInt64(uint64_t{1000000000000000000}); |
| 248 | CheckUInt64(uint64_t{1199999999999999999}); |
| 249 | CheckUInt64(std::numeric_limits<uint64_t>::max()); |
| 250 |
| 251 | for (int i = 0; i < 10000; i++) { |
| 252 | CheckHex64(i); |
| 253 | } |
| 254 | CheckHex64(uint64_t{0x123456789abcdef0}); |
| 255 | } |
| 256 |
| 257 | template <typename int_type, typename in_val_type> |
| 258 | void VerifySimpleAtoiGood(in_val_type in_value, int_type exp_value) { |
| 259 | std::string s; |
| 260 | // (u)int128 can be streamed but not StrCat'd. |
| 261 | absl::strings_internal::OStringStream(&s) << in_value; |
| 262 | int_type x = static_cast<int_type>(~exp_value); |
| 263 | EXPECT_TRUE(SimpleAtoi(s, &x)) |
| 264 | << "in_value=" << in_value << " s=" << s << " x=" << x; |
| 265 | EXPECT_EQ(exp_value, x); |
| 266 | x = static_cast<int_type>(~exp_value); |
| 267 | EXPECT_TRUE(SimpleAtoi(s.c_str(), &x)); |
| 268 | EXPECT_EQ(exp_value, x); |
| 269 | } |
| 270 |
| 271 | template <typename int_type, typename in_val_type> |
| 272 | void VerifySimpleAtoiBad(in_val_type in_value) { |
| 273 | std::string s; |
| 274 | // (u)int128 can be streamed but not StrCat'd. |
| 275 | absl::strings_internal::OStringStream(&s) << in_value; |
| 276 | int_type x; |
| 277 | EXPECT_FALSE(SimpleAtoi(s, &x)); |
| 278 | EXPECT_FALSE(SimpleAtoi(s.c_str(), &x)); |
| 279 | } |
| 280 |
| 281 | TEST(NumbersTest, Atoi) { |
| 282 | // SimpleAtoi(absl::string_view, int32_t) |
| 283 | VerifySimpleAtoiGood<int32_t>(0, 0); |
| 284 | VerifySimpleAtoiGood<int32_t>(42, 42); |
| 285 | VerifySimpleAtoiGood<int32_t>(-42, -42); |
| 286 |
| 287 | VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(), |
| 288 | std::numeric_limits<int32_t>::min()); |
| 289 | VerifySimpleAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(), |
| 290 | std::numeric_limits<int32_t>::max()); |
| 291 |
| 292 | // SimpleAtoi(absl::string_view, uint32_t) |
| 293 | VerifySimpleAtoiGood<uint32_t>(0, 0); |
| 294 | VerifySimpleAtoiGood<uint32_t>(42, 42); |
| 295 | VerifySimpleAtoiBad<uint32_t>(-42); |
| 296 |
| 297 | VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min()); |
| 298 | VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(), |
| 299 | std::numeric_limits<int32_t>::max()); |
| 300 | VerifySimpleAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(), |
| 301 | std::numeric_limits<uint32_t>::max()); |
| 302 | VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min()); |
| 303 | VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max()); |
| 304 | VerifySimpleAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max()); |
| 305 |
| 306 | // SimpleAtoi(absl::string_view, int64_t) |
| 307 | VerifySimpleAtoiGood<int64_t>(0, 0); |
| 308 | VerifySimpleAtoiGood<int64_t>(42, 42); |
| 309 | VerifySimpleAtoiGood<int64_t>(-42, -42); |
| 310 |
| 311 | VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(), |
| 312 | std::numeric_limits<int32_t>::min()); |
| 313 | VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(), |
| 314 | std::numeric_limits<int32_t>::max()); |
| 315 | VerifySimpleAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(), |
| 316 | std::numeric_limits<uint32_t>::max()); |
| 317 | VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(), |
| 318 | std::numeric_limits<int64_t>::min()); |
| 319 | VerifySimpleAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(), |
| 320 | std::numeric_limits<int64_t>::max()); |
| 321 | VerifySimpleAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max()); |
| 322 |
| 323 | // SimpleAtoi(absl::string_view, uint64_t) |
| 324 | VerifySimpleAtoiGood<uint64_t>(0, 0); |
| 325 | VerifySimpleAtoiGood<uint64_t>(42, 42); |
| 326 | VerifySimpleAtoiBad<uint64_t>(-42); |
| 327 |
| 328 | VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min()); |
| 329 | VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(), |
| 330 | std::numeric_limits<int32_t>::max()); |
| 331 | VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(), |
| 332 | std::numeric_limits<uint32_t>::max()); |
| 333 | VerifySimpleAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min()); |
| 334 | VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(), |
| 335 | std::numeric_limits<int64_t>::max()); |
| 336 | VerifySimpleAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(), |
| 337 | std::numeric_limits<uint64_t>::max()); |
| 338 |
| 339 | // SimpleAtoi(absl::string_view, absl::uint128) |
| 340 | VerifySimpleAtoiGood<absl::uint128>(0, 0); |
| 341 | VerifySimpleAtoiGood<absl::uint128>(42, 42); |
| 342 | VerifySimpleAtoiBad<absl::uint128>(-42); |
| 343 |
| 344 | VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min()); |
| 345 | VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(), |
| 346 | std::numeric_limits<int32_t>::max()); |
| 347 | VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(), |
| 348 | std::numeric_limits<uint32_t>::max()); |
| 349 | VerifySimpleAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min()); |
| 350 | VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(), |
| 351 | std::numeric_limits<int64_t>::max()); |
| 352 | VerifySimpleAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(), |
| 353 | std::numeric_limits<uint64_t>::max()); |
| 354 | VerifySimpleAtoiGood<absl::uint128>( |
| 355 | std::numeric_limits<absl::uint128>::max(), |
| 356 | std::numeric_limits<absl::uint128>::max()); |
| 357 |
| 358 | // SimpleAtoi(absl::string_view, absl::int128) |
| 359 | VerifySimpleAtoiGood<absl::int128>(0, 0); |
| 360 | VerifySimpleAtoiGood<absl::int128>(42, 42); |
| 361 | VerifySimpleAtoiGood<absl::int128>(-42, -42); |
| 362 |
| 363 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::min(), |
| 364 | std::numeric_limits<int32_t>::min()); |
| 365 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int32_t>::max(), |
| 366 | std::numeric_limits<int32_t>::max()); |
| 367 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint32_t>::max(), |
| 368 | std::numeric_limits<uint32_t>::max()); |
| 369 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::min(), |
| 370 | std::numeric_limits<int64_t>::min()); |
| 371 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<int64_t>::max(), |
| 372 | std::numeric_limits<int64_t>::max()); |
| 373 | VerifySimpleAtoiGood<absl::int128>(std::numeric_limits<uint64_t>::max(), |
| 374 | std::numeric_limits<uint64_t>::max()); |
| 375 | VerifySimpleAtoiGood<absl::int128>( |
| 376 | std::numeric_limits<absl::int128>::min(), |
| 377 | std::numeric_limits<absl::int128>::min()); |
| 378 | VerifySimpleAtoiGood<absl::int128>( |
| 379 | std::numeric_limits<absl::int128>::max(), |
| 380 | std::numeric_limits<absl::int128>::max()); |
| 381 | VerifySimpleAtoiBad<absl::int128>(std::numeric_limits<absl::uint128>::max()); |
| 382 |
| 383 | // Some other types |
| 384 | VerifySimpleAtoiGood<int>(-42, -42); |
| 385 | VerifySimpleAtoiGood<int32_t>(-42, -42); |
| 386 | VerifySimpleAtoiGood<uint32_t>(42, 42); |
| 387 | VerifySimpleAtoiGood<unsigned int>(42, 42); |
| 388 | VerifySimpleAtoiGood<int64_t>(-42, -42); |
| 389 | VerifySimpleAtoiGood<long>(-42, -42); // NOLINT: runtime-int |
| 390 | VerifySimpleAtoiGood<uint64_t>(42, 42); |
| 391 | VerifySimpleAtoiGood<size_t>(42, 42); |
| 392 | VerifySimpleAtoiGood<std::string::size_type>(42, 42); |
| 393 | } |
| 394 |
| 395 | TEST(NumbersTest, Atod) { |
| 396 | // DBL_TRUE_MIN and FLT_TRUE_MIN were not mandated in <cfloat> before C++17. |
| 397 | #if !defined(DBL_TRUE_MIN) |
| 398 | static constexpr double DBL_TRUE_MIN = |
| 399 | 4.940656458412465441765687928682213723650598026143247644255856825e-324; |
| 400 | #endif |
| 401 | #if !defined(FLT_TRUE_MIN) |
| 402 | static constexpr float FLT_TRUE_MIN = |
| 403 | 1.401298464324817070923729583289916131280261941876515771757068284e-45f; |
| 404 | #endif |
| 405 |
| 406 | double d; |
| 407 | float f; |
| 408 |
| 409 | // NaN can be spelled in multiple ways. |
| 410 | EXPECT_TRUE(absl::SimpleAtod("NaN", &d)); |
| 411 | EXPECT_TRUE(std::isnan(d)); |
| 412 | EXPECT_TRUE(absl::SimpleAtod("nAN", &d)); |
| 413 | EXPECT_TRUE(std::isnan(d)); |
| 414 | EXPECT_TRUE(absl::SimpleAtod("-nan", &d)); |
| 415 | EXPECT_TRUE(std::isnan(d)); |
| 416 |
| 417 | // Likewise for Infinity. |
| 418 | EXPECT_TRUE(absl::SimpleAtod("inf", &d)); |
| 419 | EXPECT_TRUE(std::isinf(d) && (d > 0)); |
| 420 | EXPECT_TRUE(absl::SimpleAtod("+Infinity", &d)); |
| 421 | EXPECT_TRUE(std::isinf(d) && (d > 0)); |
| 422 | EXPECT_TRUE(absl::SimpleAtod("-INF", &d)); |
| 423 | EXPECT_TRUE(std::isinf(d) && (d < 0)); |
| 424 |
| 425 | // Parse DBL_MAX. Parsing something more than twice as big should also |
| 426 | // produce infinity. |
| 427 | EXPECT_TRUE(absl::SimpleAtod("1.7976931348623157e+308", &d)); |
| 428 | EXPECT_EQ(d, 1.7976931348623157e+308); |
| 429 | EXPECT_TRUE(absl::SimpleAtod("5e308", &d)); |
| 430 | EXPECT_TRUE(std::isinf(d) && (d > 0)); |
| 431 | // Ditto, but for FLT_MAX. |
| 432 | EXPECT_TRUE(absl::SimpleAtof("3.4028234663852886e+38", &f)); |
| 433 | EXPECT_EQ(f, 3.4028234663852886e+38f); |
| 434 | EXPECT_TRUE(absl::SimpleAtof("7e38", &f)); |
| 435 | EXPECT_TRUE(std::isinf(f) && (f > 0)); |
| 436 |
| 437 | // Parse the largest N such that parsing 1eN produces a finite value and the |
| 438 | // smallest M = N + 1 such that parsing 1eM produces infinity. |
| 439 | // |
| 440 | // The 309 exponent (and 39) confirms the "definition of |
| 441 | // kEiselLemireMaxExclExp10" comment in charconv.cc. |
| 442 | EXPECT_TRUE(absl::SimpleAtod("1e308", &d)); |
| 443 | EXPECT_EQ(d, 1e308); |
| 444 | EXPECT_FALSE(std::isinf(d)); |
| 445 | EXPECT_TRUE(absl::SimpleAtod("1e309", &d)); |
| 446 | EXPECT_TRUE(std::isinf(d)); |
| 447 | // Ditto, but for Atof instead of Atod. |
| 448 | EXPECT_TRUE(absl::SimpleAtof("1e38", &f)); |
| 449 | EXPECT_EQ(f, 1e38f); |
| 450 | EXPECT_FALSE(std::isinf(f)); |
| 451 | EXPECT_TRUE(absl::SimpleAtof("1e39", &f)); |
| 452 | EXPECT_TRUE(std::isinf(f)); |
| 453 |
| 454 | // Parse the largest N such that parsing 9.999999999999999999eN, with 19 |
| 455 | // nines, produces a finite value. |
| 456 | // |
| 457 | // 9999999999999999999, with 19 nines but no decimal point, is the largest |
| 458 | // "repeated nines" integer that fits in a uint64_t. |
| 459 | EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e307", &d)); |
| 460 | EXPECT_EQ(d, 9.999999999999999999e307); |
| 461 | EXPECT_FALSE(std::isinf(d)); |
| 462 | EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e308", &d)); |
| 463 | EXPECT_TRUE(std::isinf(d)); |
| 464 | // Ditto, but for Atof instead of Atod. |
| 465 | EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e37", &f)); |
| 466 | EXPECT_EQ(f, 9.999999999999999999e37f); |
| 467 | EXPECT_FALSE(std::isinf(f)); |
| 468 | EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e38", &f)); |
| 469 | EXPECT_TRUE(std::isinf(f)); |
| 470 |
| 471 | // Parse DBL_MIN (normal), DBL_TRUE_MIN (subnormal) and (DBL_TRUE_MIN / 10) |
| 472 | // (effectively zero). |
| 473 | EXPECT_TRUE(absl::SimpleAtod("2.2250738585072014e-308", &d)); |
| 474 | EXPECT_EQ(d, 2.2250738585072014e-308); |
| 475 | EXPECT_TRUE(absl::SimpleAtod("4.9406564584124654e-324", &d)); |
| 476 | EXPECT_EQ(d, 4.9406564584124654e-324); |
| 477 | EXPECT_TRUE(absl::SimpleAtod("4.9406564584124654e-325", &d)); |
| 478 | EXPECT_EQ(d, 0); |
| 479 | // Ditto, but for FLT_MIN, FLT_TRUE_MIN and (FLT_TRUE_MIN / 10). |
| 480 | EXPECT_TRUE(absl::SimpleAtof("1.1754943508222875e-38", &f)); |
| 481 | EXPECT_EQ(f, 1.1754943508222875e-38f); |
| 482 | EXPECT_TRUE(absl::SimpleAtof("1.4012984643248171e-45", &f)); |
| 483 | EXPECT_EQ(f, 1.4012984643248171e-45f); |
| 484 | EXPECT_TRUE(absl::SimpleAtof("1.4012984643248171e-46", &f)); |
| 485 | EXPECT_EQ(f, 0); |
| 486 |
| 487 | // Parse the largest N (the most negative -N) such that parsing 1e-N produces |
| 488 | // a normal or subnormal (but still positive) or zero value. |
| 489 | EXPECT_TRUE(absl::SimpleAtod("1e-307", &d)); |
| 490 | EXPECT_EQ(d, 1e-307); |
| 491 | EXPECT_GE(d, DBL_MIN); |
| 492 | EXPECT_LT(d, DBL_MIN * 10); |
| 493 | EXPECT_TRUE(absl::SimpleAtod("1e-323", &d)); |
| 494 | EXPECT_EQ(d, 1e-323); |
| 495 | EXPECT_GE(d, DBL_TRUE_MIN); |
| 496 | EXPECT_LT(d, DBL_TRUE_MIN * 10); |
| 497 | EXPECT_TRUE(absl::SimpleAtod("1e-324", &d)); |
| 498 | EXPECT_EQ(d, 0); |
| 499 | // Ditto, but for Atof instead of Atod. |
| 500 | EXPECT_TRUE(absl::SimpleAtof("1e-37", &f)); |
| 501 | EXPECT_EQ(f, 1e-37f); |
| 502 | EXPECT_GE(f, FLT_MIN); |
| 503 | EXPECT_LT(f, FLT_MIN * 10); |
| 504 | EXPECT_TRUE(absl::SimpleAtof("1e-45", &f)); |
| 505 | EXPECT_EQ(f, 1e-45f); |
| 506 | EXPECT_GE(f, FLT_TRUE_MIN); |
| 507 | EXPECT_LT(f, FLT_TRUE_MIN * 10); |
| 508 | EXPECT_TRUE(absl::SimpleAtof("1e-46", &f)); |
| 509 | EXPECT_EQ(f, 0); |
| 510 |
| 511 | // Parse the largest N (the most negative -N) such that parsing |
| 512 | // 9.999999999999999999e-N, with 19 nines, produces a normal or subnormal |
| 513 | // (but still positive) or zero value. |
| 514 | // |
| 515 | // 9999999999999999999, with 19 nines but no decimal point, is the largest |
| 516 | // "repeated nines" integer that fits in a uint64_t. |
| 517 | // |
| 518 | // The -324/-325 exponents (and -46/-47) confirms the "definition of |
| 519 | // kEiselLemireMinInclExp10" comment in charconv.cc. |
| 520 | EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-308", &d)); |
| 521 | EXPECT_EQ(d, 9.999999999999999999e-308); |
| 522 | EXPECT_GE(d, DBL_MIN); |
| 523 | EXPECT_LT(d, DBL_MIN * 10); |
| 524 | EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-324", &d)); |
| 525 | EXPECT_EQ(d, 9.999999999999999999e-324); |
| 526 | EXPECT_GE(d, DBL_TRUE_MIN); |
| 527 | EXPECT_LT(d, DBL_TRUE_MIN * 10); |
| 528 | EXPECT_TRUE(absl::SimpleAtod("9.999999999999999999e-325", &d)); |
| 529 | EXPECT_EQ(d, 0); |
| 530 | // Ditto, but for Atof instead of Atod. |
| 531 | EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-38", &f)); |
| 532 | EXPECT_EQ(f, 9.999999999999999999e-38f); |
| 533 | EXPECT_GE(f, FLT_MIN); |
| 534 | EXPECT_LT(f, FLT_MIN * 10); |
| 535 | EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-46", &f)); |
| 536 | EXPECT_EQ(f, 9.999999999999999999e-46f); |
| 537 | EXPECT_GE(f, FLT_TRUE_MIN); |
| 538 | EXPECT_LT(f, FLT_TRUE_MIN * 10); |
| 539 | EXPECT_TRUE(absl::SimpleAtof("9.999999999999999999e-47", &f)); |
| 540 | EXPECT_EQ(f, 0); |
| 541 |
| 542 | // Leading and/or trailing whitespace is OK. |
| 543 | EXPECT_TRUE(absl::SimpleAtod(" \t\r\n 2.718", &d)); |
| 544 | EXPECT_EQ(d, 2.718); |
| 545 | EXPECT_TRUE(absl::SimpleAtod(" 3.141 ", &d)); |
| 546 | EXPECT_EQ(d, 3.141); |
| 547 |
| 548 | // Leading or trailing not-whitespace is not OK. |
| 549 | EXPECT_FALSE(absl::SimpleAtod("n 0", &d)); |
| 550 | EXPECT_FALSE(absl::SimpleAtod("0n ", &d)); |
| 551 |
| 552 | // Multiple leading 0s are OK. |
| 553 | EXPECT_TRUE(absl::SimpleAtod("000123", &d)); |
| 554 | EXPECT_EQ(d, 123); |
| 555 | EXPECT_TRUE(absl::SimpleAtod("000.456", &d)); |
| 556 | EXPECT_EQ(d, 0.456); |
| 557 |
| 558 | // An absent leading 0 (for a fraction < 1) is OK. |
| 559 | EXPECT_TRUE(absl::SimpleAtod(".5", &d)); |
| 560 | EXPECT_EQ(d, 0.5); |
| 561 | EXPECT_TRUE(absl::SimpleAtod("-.707", &d)); |
| 562 | EXPECT_EQ(d, -0.707); |
| 563 |
| 564 | // Unary + is OK. |
| 565 | EXPECT_TRUE(absl::SimpleAtod("+6.0221408e+23", &d)); |
| 566 | EXPECT_EQ(d, 6.0221408e+23); |
| 567 |
| 568 | // Underscores are not OK. |
| 569 | EXPECT_FALSE(absl::SimpleAtod("123_456", &d)); |
| 570 |
| 571 | // The decimal separator must be '.' and is never ','. |
| 572 | EXPECT_TRUE(absl::SimpleAtod("8.9", &d)); |
| 573 | EXPECT_FALSE(absl::SimpleAtod("8,9", &d)); |
| 574 |
| 575 | // These examples are called out in the EiselLemire function's comments. |
| 576 | EXPECT_TRUE(absl::SimpleAtod("4503599627370497.5", &d)); |
| 577 | EXPECT_EQ(d, 4503599627370497.5); |
| 578 | EXPECT_TRUE(absl::SimpleAtod("1e+23", &d)); |
| 579 | EXPECT_EQ(d, 1e+23); |
| 580 | EXPECT_TRUE(absl::SimpleAtod("9223372036854775807", &d)); |
| 581 | EXPECT_EQ(d, 9223372036854775807); |
| 582 | // Ditto, but for Atof instead of Atod. |
| 583 | EXPECT_TRUE(absl::SimpleAtof("0.0625", &f)); |
| 584 | EXPECT_EQ(f, 0.0625f); |
| 585 | EXPECT_TRUE(absl::SimpleAtof("20040229.0", &f)); |
| 586 | EXPECT_EQ(f, 20040229.0f); |
| 587 | EXPECT_TRUE(absl::SimpleAtof("2147483647.0", &f)); |
| 588 | EXPECT_EQ(f, 2147483647.0f); |
| 589 |
| 590 | // Some parsing algorithms don't always round correctly (but absl::SimpleAtod |
| 591 | // should). This test case comes from |
| 592 | // https://github.com/serde-rs/json/issues/707 |
| 593 | // |
| 594 | // See also atod_manual_test.cc for running many more test cases. |
| 595 | EXPECT_TRUE(absl::SimpleAtod("122.416294033786585", &d)); |
| 596 | EXPECT_EQ(d, 122.416294033786585); |
| 597 | EXPECT_TRUE(absl::SimpleAtof("122.416294033786585", &f)); |
| 598 | EXPECT_EQ(f, 122.416294033786585f); |
| 599 | } |
| 600 |
| 601 | TEST(NumbersTest, Prefixes) { |
| 602 | double d; |
| 603 | EXPECT_FALSE(absl::SimpleAtod("++1", &d)); |
| 604 | EXPECT_FALSE(absl::SimpleAtod("+-1", &d)); |
| 605 | EXPECT_FALSE(absl::SimpleAtod("-+1", &d)); |
| 606 | EXPECT_FALSE(absl::SimpleAtod("--1", &d)); |
| 607 | EXPECT_TRUE(absl::SimpleAtod("-1", &d)); |
| 608 | EXPECT_EQ(d, -1.); |
| 609 | EXPECT_TRUE(absl::SimpleAtod("+1", &d)); |
| 610 | EXPECT_EQ(d, +1.); |
| 611 |
| 612 | float f; |
| 613 | EXPECT_FALSE(absl::SimpleAtof("++1", &f)); |
| 614 | EXPECT_FALSE(absl::SimpleAtof("+-1", &f)); |
| 615 | EXPECT_FALSE(absl::SimpleAtof("-+1", &f)); |
| 616 | EXPECT_FALSE(absl::SimpleAtof("--1", &f)); |
| 617 | EXPECT_TRUE(absl::SimpleAtof("-1", &f)); |
| 618 | EXPECT_EQ(f, -1.f); |
| 619 | EXPECT_TRUE(absl::SimpleAtof("+1", &f)); |
| 620 | EXPECT_EQ(f, +1.f); |
| 621 | } |
| 622 |
| 623 | TEST(NumbersTest, Atoenum) { |
| 624 | enum E01 { |
| 625 | E01_zero = 0, |
| 626 | E01_one = 1, |
| 627 | }; |
| 628 |
| 629 | VerifySimpleAtoiGood<E01>(E01_zero, E01_zero); |
| 630 | VerifySimpleAtoiGood<E01>(E01_one, E01_one); |
| 631 |
| 632 | enum E_101 { |
| 633 | E_101_minusone = -1, |
| 634 | E_101_zero = 0, |
| 635 | E_101_one = 1, |
| 636 | }; |
| 637 |
| 638 | VerifySimpleAtoiGood<E_101>(E_101_minusone, E_101_minusone); |
| 639 | VerifySimpleAtoiGood<E_101>(E_101_zero, E_101_zero); |
| 640 | VerifySimpleAtoiGood<E_101>(E_101_one, E_101_one); |
| 641 |
| 642 | enum E_bigint { |
| 643 | E_bigint_zero = 0, |
| 644 | E_bigint_one = 1, |
| 645 | E_bigint_max31 = static_cast<int32_t>(0x7FFFFFFF), |
| 646 | }; |
| 647 |
| 648 | VerifySimpleAtoiGood<E_bigint>(E_bigint_zero, E_bigint_zero); |
| 649 | VerifySimpleAtoiGood<E_bigint>(E_bigint_one, E_bigint_one); |
| 650 | VerifySimpleAtoiGood<E_bigint>(E_bigint_max31, E_bigint_max31); |
| 651 |
| 652 | enum E_fullint { |
| 653 | E_fullint_zero = 0, |
| 654 | E_fullint_one = 1, |
| 655 | E_fullint_max31 = static_cast<int32_t>(0x7FFFFFFF), |
| 656 | E_fullint_min32 = INT32_MIN, |
| 657 | }; |
| 658 |
| 659 | VerifySimpleAtoiGood<E_fullint>(E_fullint_zero, E_fullint_zero); |
| 660 | VerifySimpleAtoiGood<E_fullint>(E_fullint_one, E_fullint_one); |
| 661 | VerifySimpleAtoiGood<E_fullint>(E_fullint_max31, E_fullint_max31); |
| 662 | VerifySimpleAtoiGood<E_fullint>(E_fullint_min32, E_fullint_min32); |
| 663 |
| 664 | enum E_biguint { |
| 665 | E_biguint_zero = 0, |
| 666 | E_biguint_one = 1, |
| 667 | E_biguint_max31 = static_cast<uint32_t>(0x7FFFFFFF), |
| 668 | E_biguint_max32 = static_cast<uint32_t>(0xFFFFFFFF), |
| 669 | }; |
| 670 |
| 671 | VerifySimpleAtoiGood<E_biguint>(E_biguint_zero, E_biguint_zero); |
| 672 | VerifySimpleAtoiGood<E_biguint>(E_biguint_one, E_biguint_one); |
| 673 | VerifySimpleAtoiGood<E_biguint>(E_biguint_max31, E_biguint_max31); |
| 674 | VerifySimpleAtoiGood<E_biguint>(E_biguint_max32, E_biguint_max32); |
| 675 | } |
| 676 |
| 677 | template <typename int_type, typename in_val_type> |
| 678 | void VerifySimpleHexAtoiGood(in_val_type in_value, int_type exp_value) { |
| 679 | std::string s; |
| 680 | // uint128 can be streamed but not StrCat'd |
| 681 | absl::strings_internal::OStringStream strm(&s); |
| 682 | if (in_value >= 0) { |
| 683 | strm << std::hex << in_value; |
| 684 | } else { |
| 685 | // Inefficient for small integers, but works with all integral types. |
| 686 | strm << "-" << std::hex << -absl::uint128(in_value); |
| 687 | } |
| 688 | int_type x = static_cast<int_type>(~exp_value); |
| 689 | EXPECT_TRUE(SimpleHexAtoi(s, &x)) |
| 690 | << "in_value=" << std::hex << in_value << " s=" << s << " x=" << x; |
| 691 | EXPECT_EQ(exp_value, x); |
| 692 | x = static_cast<int_type>(~exp_value); |
| 693 | EXPECT_TRUE(SimpleHexAtoi( |
| 694 | s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions |
| 695 | EXPECT_EQ(exp_value, x); |
| 696 | } |
| 697 |
| 698 | template <typename int_type, typename in_val_type> |
| 699 | void VerifySimpleHexAtoiBad(in_val_type in_value) { |
| 700 | std::string s; |
| 701 | // uint128 can be streamed but not StrCat'd |
| 702 | absl::strings_internal::OStringStream strm(&s); |
| 703 | if (in_value >= 0) { |
| 704 | strm << std::hex << in_value; |
| 705 | } else { |
| 706 | // Inefficient for small integers, but works with all integral types. |
| 707 | strm << "-" << std::hex << -absl::uint128(in_value); |
| 708 | } |
| 709 | int_type x; |
| 710 | EXPECT_FALSE(SimpleHexAtoi(s, &x)); |
| 711 | EXPECT_FALSE(SimpleHexAtoi( |
| 712 | s.c_str(), &x)); // NOLINT: readability-redundant-string-conversions |
| 713 | } |
| 714 |
| 715 | TEST(NumbersTest, HexAtoi) { |
| 716 | // SimpleHexAtoi(absl::string_view, int32_t) |
| 717 | VerifySimpleHexAtoiGood<int32_t>(0, 0); |
| 718 | VerifySimpleHexAtoiGood<int32_t>(0x42, 0x42); |
| 719 | VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42); |
| 720 |
| 721 | VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::min(), |
| 722 | std::numeric_limits<int32_t>::min()); |
| 723 | VerifySimpleHexAtoiGood<int32_t>(std::numeric_limits<int32_t>::max(), |
| 724 | std::numeric_limits<int32_t>::max()); |
| 725 |
| 726 | // SimpleHexAtoi(absl::string_view, uint32_t) |
| 727 | VerifySimpleHexAtoiGood<uint32_t>(0, 0); |
| 728 | VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42); |
| 729 | VerifySimpleHexAtoiBad<uint32_t>(-0x42); |
| 730 |
| 731 | VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int32_t>::min()); |
| 732 | VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<int32_t>::max(), |
| 733 | std::numeric_limits<int32_t>::max()); |
| 734 | VerifySimpleHexAtoiGood<uint32_t>(std::numeric_limits<uint32_t>::max(), |
| 735 | std::numeric_limits<uint32_t>::max()); |
| 736 | VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::min()); |
| 737 | VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<int64_t>::max()); |
| 738 | VerifySimpleHexAtoiBad<uint32_t>(std::numeric_limits<uint64_t>::max()); |
| 739 |
| 740 | // SimpleHexAtoi(absl::string_view, int64_t) |
| 741 | VerifySimpleHexAtoiGood<int64_t>(0, 0); |
| 742 | VerifySimpleHexAtoiGood<int64_t>(0x42, 0x42); |
| 743 | VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42); |
| 744 |
| 745 | VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::min(), |
| 746 | std::numeric_limits<int32_t>::min()); |
| 747 | VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int32_t>::max(), |
| 748 | std::numeric_limits<int32_t>::max()); |
| 749 | VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<uint32_t>::max(), |
| 750 | std::numeric_limits<uint32_t>::max()); |
| 751 | VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::min(), |
| 752 | std::numeric_limits<int64_t>::min()); |
| 753 | VerifySimpleHexAtoiGood<int64_t>(std::numeric_limits<int64_t>::max(), |
| 754 | std::numeric_limits<int64_t>::max()); |
| 755 | VerifySimpleHexAtoiBad<int64_t>(std::numeric_limits<uint64_t>::max()); |
| 756 |
| 757 | // SimpleHexAtoi(absl::string_view, uint64_t) |
| 758 | VerifySimpleHexAtoiGood<uint64_t>(0, 0); |
| 759 | VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42); |
| 760 | VerifySimpleHexAtoiBad<uint64_t>(-0x42); |
| 761 |
| 762 | VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int32_t>::min()); |
| 763 | VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int32_t>::max(), |
| 764 | std::numeric_limits<int32_t>::max()); |
| 765 | VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint32_t>::max(), |
| 766 | std::numeric_limits<uint32_t>::max()); |
| 767 | VerifySimpleHexAtoiBad<uint64_t>(std::numeric_limits<int64_t>::min()); |
| 768 | VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<int64_t>::max(), |
| 769 | std::numeric_limits<int64_t>::max()); |
| 770 | VerifySimpleHexAtoiGood<uint64_t>(std::numeric_limits<uint64_t>::max(), |
| 771 | std::numeric_limits<uint64_t>::max()); |
| 772 |
| 773 | // SimpleHexAtoi(absl::string_view, absl::uint128) |
| 774 | VerifySimpleHexAtoiGood<absl::uint128>(0, 0); |
| 775 | VerifySimpleHexAtoiGood<absl::uint128>(0x42, 0x42); |
| 776 | VerifySimpleHexAtoiBad<absl::uint128>(-0x42); |
| 777 |
| 778 | VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int32_t>::min()); |
| 779 | VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int32_t>::max(), |
| 780 | std::numeric_limits<int32_t>::max()); |
| 781 | VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint32_t>::max(), |
| 782 | std::numeric_limits<uint32_t>::max()); |
| 783 | VerifySimpleHexAtoiBad<absl::uint128>(std::numeric_limits<int64_t>::min()); |
| 784 | VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<int64_t>::max(), |
| 785 | std::numeric_limits<int64_t>::max()); |
| 786 | VerifySimpleHexAtoiGood<absl::uint128>(std::numeric_limits<uint64_t>::max(), |
| 787 | std::numeric_limits<uint64_t>::max()); |
| 788 | VerifySimpleHexAtoiGood<absl::uint128>( |
| 789 | std::numeric_limits<absl::uint128>::max(), |
| 790 | std::numeric_limits<absl::uint128>::max()); |
| 791 |
| 792 | // Some other types |
| 793 | VerifySimpleHexAtoiGood<int>(-0x42, -0x42); |
| 794 | VerifySimpleHexAtoiGood<int32_t>(-0x42, -0x42); |
| 795 | VerifySimpleHexAtoiGood<uint32_t>(0x42, 0x42); |
| 796 | VerifySimpleHexAtoiGood<unsigned int>(0x42, 0x42); |
| 797 | VerifySimpleHexAtoiGood<int64_t>(-0x42, -0x42); |
| 798 | VerifySimpleHexAtoiGood<long>(-0x42, -0x42); // NOLINT: runtime-int |
| 799 | VerifySimpleHexAtoiGood<uint64_t>(0x42, 0x42); |
| 800 | VerifySimpleHexAtoiGood<size_t>(0x42, 0x42); |
| 801 | VerifySimpleHexAtoiGood<std::string::size_type>(0x42, 0x42); |
| 802 |
| 803 | // Number prefix |
| 804 | int32_t value; |
| 805 | EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16)); |
| 806 | EXPECT_EQ(0x34234324, value); |
| 807 |
| 808 | EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16)); |
| 809 | EXPECT_EQ(0x34234324, value); |
| 810 |
| 811 | // ASCII whitespace |
| 812 | EXPECT_TRUE(safe_strto32_base(" \t\n 34234324", &value, 16)); |
| 813 | EXPECT_EQ(0x34234324, value); |
| 814 |
| 815 | EXPECT_TRUE(safe_strto32_base("34234324 \t\n ", &value, 16)); |
| 816 | EXPECT_EQ(0x34234324, value); |
| 817 | } |
| 818 |
| 819 | TEST(stringtest, safe_strto32_base) { |
| 820 | int32_t value; |
| 821 | EXPECT_TRUE(safe_strto32_base("0x34234324", &value, 16)); |
| 822 | EXPECT_EQ(0x34234324, value); |
| 823 |
| 824 | EXPECT_TRUE(safe_strto32_base("0X34234324", &value, 16)); |
| 825 | EXPECT_EQ(0x34234324, value); |
| 826 |
| 827 | EXPECT_TRUE(safe_strto32_base("34234324", &value, 16)); |
| 828 | EXPECT_EQ(0x34234324, value); |
| 829 |
| 830 | EXPECT_TRUE(safe_strto32_base("0", &value, 16)); |
| 831 | EXPECT_EQ(0, value); |
| 832 |
| 833 | EXPECT_TRUE(safe_strto32_base(" \t\n -0x34234324", &value, 16)); |
| 834 | EXPECT_EQ(-0x34234324, value); |
| 835 |
| 836 | EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 16)); |
| 837 | EXPECT_EQ(-0x34234324, value); |
| 838 |
| 839 | EXPECT_TRUE(safe_strto32_base("7654321", &value, 8)); |
| 840 | EXPECT_EQ(07654321, value); |
| 841 |
| 842 | EXPECT_TRUE(safe_strto32_base("-01234", &value, 8)); |
| 843 | EXPECT_EQ(-01234, value); |
| 844 |
| 845 | EXPECT_FALSE(safe_strto32_base("1834", &value, 8)); |
| 846 |
| 847 | // Autodetect base. |
| 848 | EXPECT_TRUE(safe_strto32_base("0", &value, 0)); |
| 849 | EXPECT_EQ(0, value); |
| 850 |
| 851 | EXPECT_TRUE(safe_strto32_base("077", &value, 0)); |
| 852 | EXPECT_EQ(077, value); // Octal interpretation |
| 853 |
| 854 | // Leading zero indicates octal, but then followed by invalid digit. |
| 855 | EXPECT_FALSE(safe_strto32_base("088", &value, 0)); |
| 856 |
| 857 | // Leading 0x indicated hex, but then followed by invalid digit. |
| 858 | EXPECT_FALSE(safe_strto32_base("0xG", &value, 0)); |
| 859 |
| 860 | // Base-10 version. |
| 861 | EXPECT_TRUE(safe_strto32_base("34234324", &value, 10)); |
| 862 | EXPECT_EQ(34234324, value); |
| 863 |
| 864 | EXPECT_TRUE(safe_strto32_base("0", &value, 10)); |
| 865 | EXPECT_EQ(0, value); |
| 866 |
| 867 | EXPECT_TRUE(safe_strto32_base(" \t\n -34234324", &value, 10)); |
| 868 | EXPECT_EQ(-34234324, value); |
| 869 |
| 870 | EXPECT_TRUE(safe_strto32_base("34234324 \n\t ", &value, 10)); |
| 871 | EXPECT_EQ(34234324, value); |
| 872 |
| 873 | // Invalid ints. |
| 874 | EXPECT_FALSE(safe_strto32_base("", &value, 10)); |
| 875 | EXPECT_FALSE(safe_strto32_base(" ", &value, 10)); |
| 876 | EXPECT_FALSE(safe_strto32_base("abc", &value, 10)); |
| 877 | EXPECT_FALSE(safe_strto32_base("34234324a", &value, 10)); |
| 878 | EXPECT_FALSE(safe_strto32_base("34234.3", &value, 10)); |
| 879 |
| 880 | // Out of bounds. |
| 881 | EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10)); |
| 882 | EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10)); |
| 883 |
| 884 | // String version. |
| 885 | EXPECT_TRUE(safe_strto32_base(std::string("0x1234"), &value, 16)); |
| 886 | EXPECT_EQ(0x1234, value); |
| 887 |
| 888 | // Base-10 string version. |
| 889 | EXPECT_TRUE(safe_strto32_base("1234", &value, 10)); |
| 890 | EXPECT_EQ(1234, value); |
| 891 | } |
| 892 |
| 893 | TEST(stringtest, safe_strto32_range) { |
| 894 | // These tests verify underflow/overflow behaviour. |
| 895 | int32_t value; |
| 896 | EXPECT_FALSE(safe_strto32_base("2147483648", &value, 10)); |
| 897 | EXPECT_EQ(std::numeric_limits<int32_t>::max(), value); |
| 898 |
| 899 | EXPECT_TRUE(safe_strto32_base("-2147483648", &value, 10)); |
| 900 | EXPECT_EQ(std::numeric_limits<int32_t>::min(), value); |
| 901 |
| 902 | EXPECT_FALSE(safe_strto32_base("-2147483649", &value, 10)); |
| 903 | EXPECT_EQ(std::numeric_limits<int32_t>::min(), value); |
| 904 | } |
| 905 |
| 906 | TEST(stringtest, safe_strto64_range) { |
| 907 | // These tests verify underflow/overflow behaviour. |
| 908 | int64_t value; |
| 909 | EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10)); |
| 910 | EXPECT_EQ(std::numeric_limits<int64_t>::max(), value); |
| 911 |
| 912 | EXPECT_TRUE(safe_strto64_base("-9223372036854775808", &value, 10)); |
| 913 | EXPECT_EQ(std::numeric_limits<int64_t>::min(), value); |
| 914 |
| 915 | EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10)); |
| 916 | EXPECT_EQ(std::numeric_limits<int64_t>::min(), value); |
| 917 | } |
| 918 |
| 919 | TEST(stringtest, safe_strto32_leading_substring) { |
| 920 | // These tests verify this comment in numbers.h: |
| 921 | // On error, returns false, and sets *value to: [...] |
| 922 | // conversion of leading substring if available ("123@@@" -> 123) |
| 923 | // 0 if no leading substring available |
| 924 | int32_t value; |
| 925 | EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 10)); |
| 926 | EXPECT_EQ(4069, value); |
| 927 |
| 928 | EXPECT_FALSE(safe_strto32_base("04069@@@", &value, 8)); |
| 929 | EXPECT_EQ(0406, value); |
| 930 |
| 931 | EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 10)); |
| 932 | EXPECT_EQ(4069, value); |
| 933 |
| 934 | EXPECT_FALSE(safe_strto32_base("04069balloons", &value, 16)); |
| 935 | EXPECT_EQ(0x4069ba, value); |
| 936 |
| 937 | EXPECT_FALSE(safe_strto32_base("@@@", &value, 10)); |
| 938 | EXPECT_EQ(0, value); // there was no leading substring |
| 939 | } |
| 940 |
| 941 | TEST(stringtest, safe_strto64_leading_substring) { |
| 942 | // These tests verify this comment in numbers.h: |
| 943 | // On error, returns false, and sets *value to: [...] |
| 944 | // conversion of leading substring if available ("123@@@" -> 123) |
| 945 | // 0 if no leading substring available |
| 946 | int64_t value; |
| 947 | EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 10)); |
| 948 | EXPECT_EQ(4069, value); |
| 949 |
| 950 | EXPECT_FALSE(safe_strto64_base("04069@@@", &value, 8)); |
| 951 | EXPECT_EQ(0406, value); |
| 952 |
| 953 | EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 10)); |
| 954 | EXPECT_EQ(4069, value); |
| 955 |
| 956 | EXPECT_FALSE(safe_strto64_base("04069balloons", &value, 16)); |
| 957 | EXPECT_EQ(0x4069ba, value); |
| 958 |
| 959 | EXPECT_FALSE(safe_strto64_base("@@@", &value, 10)); |
| 960 | EXPECT_EQ(0, value); // there was no leading substring |
| 961 | } |
| 962 |
| 963 | TEST(stringtest, safe_strto64_base) { |
| 964 | int64_t value; |
| 965 | EXPECT_TRUE(safe_strto64_base("0x3423432448783446", &value, 16)); |
| 966 | EXPECT_EQ(int64_t{0x3423432448783446}, value); |
| 967 |
| 968 | EXPECT_TRUE(safe_strto64_base("3423432448783446", &value, 16)); |
| 969 | EXPECT_EQ(int64_t{0x3423432448783446}, value); |
| 970 |
| 971 | EXPECT_TRUE(safe_strto64_base("0", &value, 16)); |
| 972 | EXPECT_EQ(0, value); |
| 973 |
| 974 | EXPECT_TRUE(safe_strto64_base(" \t\n -0x3423432448783446", &value, 16)); |
| 975 | EXPECT_EQ(int64_t{-0x3423432448783446}, value); |
| 976 |
| 977 | EXPECT_TRUE(safe_strto64_base(" \t\n -3423432448783446", &value, 16)); |
| 978 | EXPECT_EQ(int64_t{-0x3423432448783446}, value); |
| 979 |
| 980 | EXPECT_TRUE(safe_strto64_base("123456701234567012", &value, 8)); |
| 981 | EXPECT_EQ(int64_t{0123456701234567012}, value); |
| 982 |
| 983 | EXPECT_TRUE(safe_strto64_base("-017777777777777", &value, 8)); |
| 984 | EXPECT_EQ(int64_t{-017777777777777}, value); |
| 985 |
| 986 | EXPECT_FALSE(safe_strto64_base("19777777777777", &value, 8)); |
| 987 |
| 988 | // Autodetect base. |
| 989 | EXPECT_TRUE(safe_strto64_base("0", &value, 0)); |
| 990 | EXPECT_EQ(0, value); |
| 991 |
| 992 | EXPECT_TRUE(safe_strto64_base("077", &value, 0)); |
| 993 | EXPECT_EQ(077, value); // Octal interpretation |
| 994 |
| 995 | // Leading zero indicates octal, but then followed by invalid digit. |
| 996 | EXPECT_FALSE(safe_strto64_base("088", &value, 0)); |
| 997 |
| 998 | // Leading 0x indicated hex, but then followed by invalid digit. |
| 999 | EXPECT_FALSE(safe_strto64_base("0xG", &value, 0)); |
| 1000 |
| 1001 | // Base-10 version. |
| 1002 | EXPECT_TRUE(safe_strto64_base("34234324487834466", &value, 10)); |
| 1003 | EXPECT_EQ(int64_t{34234324487834466}, value); |
| 1004 |
| 1005 | EXPECT_TRUE(safe_strto64_base("0", &value, 10)); |
| 1006 | EXPECT_EQ(0, value); |
| 1007 |
| 1008 | EXPECT_TRUE(safe_strto64_base(" \t\n -34234324487834466", &value, 10)); |
| 1009 | EXPECT_EQ(int64_t{-34234324487834466}, value); |
| 1010 |
| 1011 | EXPECT_TRUE(safe_strto64_base("34234324487834466 \n\t ", &value, 10)); |
| 1012 | EXPECT_EQ(int64_t{34234324487834466}, value); |
| 1013 |
| 1014 | // Invalid ints. |
| 1015 | EXPECT_FALSE(safe_strto64_base("", &value, 10)); |
| 1016 | EXPECT_FALSE(safe_strto64_base(" ", &value, 10)); |
| 1017 | EXPECT_FALSE(safe_strto64_base("abc", &value, 10)); |
| 1018 | EXPECT_FALSE(safe_strto64_base("34234324487834466a", &value, 10)); |
| 1019 | EXPECT_FALSE(safe_strto64_base("34234487834466.3", &value, 10)); |
| 1020 |
| 1021 | // Out of bounds. |
| 1022 | EXPECT_FALSE(safe_strto64_base("9223372036854775808", &value, 10)); |
| 1023 | EXPECT_FALSE(safe_strto64_base("-9223372036854775809", &value, 10)); |
| 1024 |
| 1025 | // String version. |
| 1026 | EXPECT_TRUE(safe_strto64_base(std::string("0x1234"), &value, 16)); |
| 1027 | EXPECT_EQ(0x1234, value); |
| 1028 |
| 1029 | // Base-10 string version. |
| 1030 | EXPECT_TRUE(safe_strto64_base("1234", &value, 10)); |
| 1031 | EXPECT_EQ(1234, value); |
| 1032 | } |
| 1033 |
| 1034 | const size_t kNumRandomTests = 10000; |
| 1035 |
| 1036 | template <typename IntType> |
| 1037 | void test_random_integer_parse_base(bool (*parse_func)(absl::string_view, |
| 1038 | IntType* value, |
| 1039 | int base)) { |
| 1040 | using RandomEngine = std::minstd_rand0; |
| 1041 | std::random_device rd; |
| 1042 | RandomEngine rng(rd()); |
| 1043 | std::uniform_int_distribution<IntType> random_int( |
| 1044 | std::numeric_limits<IntType>::min()); |
| 1045 | std::uniform_int_distribution<int> random_base(2, 35); |
| 1046 | for (size_t i = 0; i < kNumRandomTests; i++) { |
| 1047 | IntType value = random_int(rng); |
| 1048 | int base = random_base(rng); |
| 1049 | std::string str_value; |
| 1050 | EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); |
| 1051 | IntType parsed_value; |
| 1052 |
| 1053 | // Test successful parse |
| 1054 | EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); |
| 1055 | EXPECT_EQ(parsed_value, value); |
| 1056 |
| 1057 | // Test overflow |
| 1058 | EXPECT_FALSE( |
| 1059 | parse_func(absl::StrCat(std::numeric_limits<IntType>::max(), value), |
| 1060 | &parsed_value, base)); |
| 1061 |
| 1062 | // Test underflow |
| 1063 | if (std::numeric_limits<IntType>::min() < 0) { |
| 1064 | EXPECT_FALSE( |
| 1065 | parse_func(absl::StrCat(std::numeric_limits<IntType>::min(), value), |
| 1066 | &parsed_value, base)); |
| 1067 | } else { |
| 1068 | EXPECT_FALSE(parse_func(absl::StrCat("-", value), &parsed_value, base)); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 |
| 1073 | TEST(stringtest, safe_strto32_random) { |
| 1074 | test_random_integer_parse_base<int32_t>(&safe_strto32_base); |
| 1075 | } |
| 1076 | TEST(stringtest, safe_strto64_random) { |
| 1077 | test_random_integer_parse_base<int64_t>(&safe_strto64_base); |
| 1078 | } |
| 1079 | TEST(stringtest, safe_strtou32_random) { |
| 1080 | test_random_integer_parse_base<uint32_t>(&safe_strtou32_base); |
| 1081 | } |
| 1082 | TEST(stringtest, safe_strtou64_random) { |
| 1083 | test_random_integer_parse_base<uint64_t>(&safe_strtou64_base); |
| 1084 | } |
| 1085 | TEST(stringtest, safe_strtou128_random) { |
| 1086 | // random number generators don't work for uint128, and |
| 1087 | // uint128 can be streamed but not StrCat'd, so this code must be custom |
| 1088 | // implemented for uint128, but is generally the same as what's above. |
| 1089 | // test_random_integer_parse_base<absl::uint128>( |
| 1090 | // &absl::numbers_internal::safe_strtou128_base); |
| 1091 | using RandomEngine = std::minstd_rand0; |
| 1092 | using IntType = absl::uint128; |
| 1093 | constexpr auto parse_func = &absl::numbers_internal::safe_strtou128_base; |
| 1094 |
| 1095 | std::random_device rd; |
| 1096 | RandomEngine rng(rd()); |
| 1097 | std::uniform_int_distribution<uint64_t> random_uint64( |
| 1098 | std::numeric_limits<uint64_t>::min()); |
| 1099 | std::uniform_int_distribution<int> random_base(2, 35); |
| 1100 |
| 1101 | for (size_t i = 0; i < kNumRandomTests; i++) { |
| 1102 | IntType value = random_uint64(rng); |
| 1103 | value = (value << 64) + random_uint64(rng); |
| 1104 | int base = random_base(rng); |
| 1105 | std::string str_value; |
| 1106 | EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); |
| 1107 | IntType parsed_value; |
| 1108 |
| 1109 | // Test successful parse |
| 1110 | EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); |
| 1111 | EXPECT_EQ(parsed_value, value); |
| 1112 |
| 1113 | // Test overflow |
| 1114 | std::string s; |
| 1115 | absl::strings_internal::OStringStream(&s) |
| 1116 | << std::numeric_limits<IntType>::max() << value; |
| 1117 | EXPECT_FALSE(parse_func(s, &parsed_value, base)); |
| 1118 |
| 1119 | // Test underflow |
| 1120 | s.clear(); |
| 1121 | absl::strings_internal::OStringStream(&s) << "-" << value; |
| 1122 | EXPECT_FALSE(parse_func(s, &parsed_value, base)); |
| 1123 | } |
| 1124 | } |
| 1125 | TEST(stringtest, safe_strto128_random) { |
| 1126 | // random number generators don't work for int128, and |
| 1127 | // int128 can be streamed but not StrCat'd, so this code must be custom |
| 1128 | // implemented for int128, but is generally the same as what's above. |
| 1129 | // test_random_integer_parse_base<absl::int128>( |
| 1130 | // &absl::numbers_internal::safe_strto128_base); |
| 1131 | using RandomEngine = std::minstd_rand0; |
| 1132 | using IntType = absl::int128; |
| 1133 | constexpr auto parse_func = &absl::numbers_internal::safe_strto128_base; |
| 1134 |
| 1135 | std::random_device rd; |
| 1136 | RandomEngine rng(rd()); |
| 1137 | std::uniform_int_distribution<int64_t> random_int64( |
| 1138 | std::numeric_limits<int64_t>::min()); |
| 1139 | std::uniform_int_distribution<uint64_t> random_uint64( |
| 1140 | std::numeric_limits<uint64_t>::min()); |
| 1141 | std::uniform_int_distribution<int> random_base(2, 35); |
| 1142 |
| 1143 | for (size_t i = 0; i < kNumRandomTests; ++i) { |
| 1144 | int64_t high = random_int64(rng); |
| 1145 | uint64_t low = random_uint64(rng); |
| 1146 | IntType value = absl::MakeInt128(high, low); |
| 1147 |
| 1148 | int base = random_base(rng); |
| 1149 | std::string str_value; |
| 1150 | EXPECT_TRUE(Itoa<IntType>(value, base, &str_value)); |
| 1151 | IntType parsed_value; |
| 1152 |
| 1153 | // Test successful parse |
| 1154 | EXPECT_TRUE(parse_func(str_value, &parsed_value, base)); |
| 1155 | EXPECT_EQ(parsed_value, value); |
| 1156 |
| 1157 | // Test overflow |
| 1158 | std::string s; |
| 1159 | absl::strings_internal::OStringStream(&s) |
| 1160 | << std::numeric_limits<IntType>::max() << value; |
| 1161 | EXPECT_FALSE(parse_func(s, &parsed_value, base)); |
| 1162 |
| 1163 | // Test underflow |
| 1164 | s.clear(); |
| 1165 | absl::strings_internal::OStringStream(&s) |
| 1166 | << std::numeric_limits<IntType>::min() << value; |
| 1167 | EXPECT_FALSE(parse_func(s, &parsed_value, base)); |
| 1168 | } |
| 1169 | } |
| 1170 |
| 1171 | TEST(stringtest, safe_strtou32_base) { |
| 1172 | for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) { |
| 1173 | const auto& e = strtouint32_test_cases()[i]; |
| 1174 | uint32_t value; |
| 1175 | EXPECT_EQ(e.expect_ok, safe_strtou32_base(e.str, &value, e.base)) |
| 1176 | << "str=\"" << e.str << "\" base=" << e.base; |
| 1177 | if (e.expect_ok) { |
| 1178 | EXPECT_EQ(e.expected, value) << "i=" << i << " str=\"" << e.str |
| 1179 | << "\" base=" << e.base; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 |
| 1184 | TEST(stringtest, safe_strtou32_base_length_delimited) { |
| 1185 | for (int i = 0; strtouint32_test_cases()[i].str != nullptr; ++i) { |
| 1186 | const auto& e = strtouint32_test_cases()[i]; |
| 1187 | std::string tmp(e.str); |
| 1188 | tmp.append("12"); // Adds garbage at the end. |
| 1189 |
| 1190 | uint32_t value; |
| 1191 | EXPECT_EQ(e.expect_ok, |
| 1192 | safe_strtou32_base(absl::string_view(tmp.data(), strlen(e.str)), |
| 1193 | &value, e.base)) |
| 1194 | << "str=\"" << e.str << "\" base=" << e.base; |
| 1195 | if (e.expect_ok) { |
| 1196 | EXPECT_EQ(e.expected, value) << "i=" << i << " str=" << e.str |
| 1197 | << " base=" << e.base; |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 |
| 1202 | TEST(stringtest, safe_strtou64_base) { |
| 1203 | for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) { |
| 1204 | const auto& e = strtouint64_test_cases()[i]; |
| 1205 | uint64_t value; |
| 1206 | EXPECT_EQ(e.expect_ok, safe_strtou64_base(e.str, &value, e.base)) |
| 1207 | << "str=\"" << e.str << "\" base=" << e.base; |
| 1208 | if (e.expect_ok) { |
| 1209 | EXPECT_EQ(e.expected, value) << "str=" << e.str << " base=" << e.base; |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 |
| 1214 | TEST(stringtest, safe_strtou64_base_length_delimited) { |
| 1215 | for (int i = 0; strtouint64_test_cases()[i].str != nullptr; ++i) { |
| 1216 | const auto& e = strtouint64_test_cases()[i]; |
| 1217 | std::string tmp(e.str); |
| 1218 | tmp.append("12"); // Adds garbage at the end. |
| 1219 |
| 1220 | uint64_t value; |
| 1221 | EXPECT_EQ(e.expect_ok, |
| 1222 | safe_strtou64_base(absl::string_view(tmp.data(), strlen(e.str)), |
| 1223 | &value, e.base)) |
| 1224 | << "str=\"" << e.str << "\" base=" << e.base; |
| 1225 | if (e.expect_ok) { |
| 1226 | EXPECT_EQ(e.expected, value) << "str=\"" << e.str << "\" base=" << e.base; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 |
| 1231 | // feenableexcept() and fedisableexcept() are extensions supported by some libc |
| 1232 | // implementations. |
| 1233 | #if defined(__GLIBC__) || defined(__BIONIC__) |
| 1234 | #define ABSL_HAVE_FEENABLEEXCEPT 1 |
| 1235 | #define ABSL_HAVE_FEDISABLEEXCEPT 1 |
| 1236 | #endif |
| 1237 |
| 1238 | class SimpleDtoaTest : public testing::Test { |
| 1239 | protected: |
| 1240 | void SetUp() override { |
| 1241 | // Store the current floating point env & clear away any pending exceptions. |
| 1242 | feholdexcept(&fp_env_); |
| 1243 | #ifdef ABSL_HAVE_FEENABLEEXCEPT |
| 1244 | // Turn on floating point exceptions. |
| 1245 | feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| 1246 | #endif |
| 1247 | } |
| 1248 |
| 1249 | void TearDown() override { |
| 1250 | // Restore the floating point environment to the original state. |
| 1251 | // In theory fedisableexcept is unnecessary; fesetenv will also do it. |
| 1252 | // In practice, our toolchains have subtle bugs. |
| 1253 | #ifdef ABSL_HAVE_FEDISABLEEXCEPT |
| 1254 | fedisableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); |
| 1255 | #endif |
| 1256 | fesetenv(&fp_env_); |
| 1257 | } |
| 1258 |
| 1259 | std::string ToNineDigits(double value) { |
| 1260 | char buffer[16]; // more than enough for %.9g |
| 1261 | snprintf(buffer, sizeof(buffer), "%.9g", value); |
| 1262 | return buffer; |
| 1263 | } |
| 1264 |
| 1265 | fenv_t fp_env_; |
| 1266 | }; |
| 1267 |
| 1268 | // Run the given runnable functor for "cases" test cases, chosen over the |
| 1269 | // available range of float. pi and e and 1/e are seeded, and then all |
| 1270 | // available integer powers of 2 and 10 are multiplied against them. In |
| 1271 | // addition to trying all those values, we try the next higher and next lower |
| 1272 | // float, and then we add additional test cases evenly distributed between them. |
| 1273 | // Each test case is passed to runnable as both a positive and negative value. |
| 1274 | template <typename R> |
| 1275 | void ExhaustiveFloat(uint32_t cases, R&& runnable) { |
| 1276 | runnable(0.0f); |
| 1277 | runnable(-0.0f); |
| 1278 | if (cases >= 2e9) { // more than 2 billion? Might as well run them all. |
| 1279 | for (float f = 0; f < std::numeric_limits<float>::max(); ) { |
| 1280 | f = nextafterf(f, std::numeric_limits<float>::max()); |
| 1281 | runnable(-f); |
| 1282 | runnable(f); |
| 1283 | } |
| 1284 | return; |
| 1285 | } |
| 1286 | std::set<float> floats = {3.4028234e38f}; |
| 1287 | for (float f : {1.0, 3.14159265, 2.718281828, 1 / 2.718281828}) { |
| 1288 | for (float testf = f; testf != 0; testf *= 0.1f) floats.insert(testf); |
| 1289 | for (float testf = f; testf != 0; testf *= 0.5f) floats.insert(testf); |
| 1290 | for (float testf = f; testf < 3e38f / 2; testf *= 2.0f) |
| 1291 | floats.insert(testf); |
| 1292 | for (float testf = f; testf < 3e38f / 10; testf *= 10) floats.insert(testf); |
| 1293 | } |
| 1294 |
| 1295 | float last = *floats.begin(); |
| 1296 |
| 1297 | runnable(last); |
| 1298 | runnable(-last); |
| 1299 | int iters_per_float = cases / floats.size(); |
| 1300 | if (iters_per_float == 0) iters_per_float = 1; |
| 1301 | for (float f : floats) { |
| 1302 | if (f == last) continue; |
| 1303 | float testf = std::nextafter(last, std::numeric_limits<float>::max()); |
| 1304 | runnable(testf); |
| 1305 | runnable(-testf); |
| 1306 | last = testf; |
| 1307 | if (f == last) continue; |
| 1308 | double step = (double{f} - last) / iters_per_float; |
| 1309 | for (double d = last + step; d < f; d += step) { |
| 1310 | testf = d; |
| 1311 | if (testf != last) { |
| 1312 | runnable(testf); |
| 1313 | runnable(-testf); |
| 1314 | last = testf; |
| 1315 | } |
| 1316 | } |
| 1317 | testf = std::nextafter(f, 0.0f); |
| 1318 | if (testf > last) { |
| 1319 | runnable(testf); |
| 1320 | runnable(-testf); |
| 1321 | last = testf; |
| 1322 | } |
| 1323 | if (f != last) { |
| 1324 | runnable(f); |
| 1325 | runnable(-f); |
| 1326 | last = f; |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 |
| 1331 | TEST_F(SimpleDtoaTest, ExhaustiveDoubleToSixDigits) { |
| 1332 | uint64_t test_count = 0; |
| 1333 | std::vector<double> mismatches; |
| 1334 | auto checker = [&](double d) { |
| 1335 | if (d != d) return; // rule out NaNs |
| 1336 | ++test_count; |
| 1337 | char sixdigitsbuf[kSixDigitsToBufferSize] = {0}; |
| 1338 | SixDigitsToBuffer(d, sixdigitsbuf); |
| 1339 | char snprintfbuf[kSixDigitsToBufferSize] = {0}; |
| 1340 | snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d); |
| 1341 | if (strcmp(sixdigitsbuf, snprintfbuf) != 0) { |
| 1342 | mismatches.push_back(d); |
| 1343 | if (mismatches.size() < 10) { |
| 1344 | LOG(ERROR) << "Six-digit failure with double. d=" << d |
| 1345 | << " sixdigits=" << sixdigitsbuf |
| 1346 | << " printf(%g)=" << snprintfbuf; |
| 1347 | } |
| 1348 | } |
| 1349 | }; |
| 1350 | // Some quick sanity checks... |
| 1351 | checker(5e-324); |
| 1352 | checker(1e-308); |
| 1353 | checker(1.0); |
| 1354 | checker(1.000005); |
| 1355 | checker(1.7976931348623157e308); |
| 1356 | checker(0.00390625); |
| 1357 | #ifndef _MSC_VER |
| 1358 | // on MSVC, snprintf() rounds it to 0.00195313. SixDigitsToBuffer() rounds it |
| 1359 | // to 0.00195312 (round half to even). |
| 1360 | checker(0.001953125); |
| 1361 | #endif |
| 1362 | checker(0.005859375); |
| 1363 | // Some cases where the rounding is very very close |
| 1364 | checker(1.089095e-15); |
| 1365 | checker(3.274195e-55); |
| 1366 | checker(6.534355e-146); |
| 1367 | checker(2.920845e+234); |
| 1368 |
| 1369 | if (mismatches.empty()) { |
| 1370 | test_count = 0; |
| 1371 | ExhaustiveFloat(kFloatNumCases, checker); |
| 1372 |
| 1373 | test_count = 0; |
| 1374 | std::vector<int> digit_testcases{ |
| 1375 | 100000, 100001, 100002, 100005, 100010, 100020, 100050, 100100, // misc |
| 1376 | 195312, 195313, // 1.953125 is a case where we round down, just barely. |
| 1377 | 200000, 500000, 800000, // misc mid-range cases |
| 1378 | 585937, 585938, // 5.859375 is a case where we round up, just barely. |
| 1379 | 900000, 990000, 999000, 999900, 999990, 999996, 999997, 999998, 999999}; |
| 1380 | if (kFloatNumCases >= 1e9) { |
| 1381 | // If at least 1 billion test cases were requested, user wants an |
| 1382 | // exhaustive test. So let's test all mantissas, too. |
| 1383 | constexpr int min_mantissa = 100000, max_mantissa = 999999; |
| 1384 | digit_testcases.resize(max_mantissa - min_mantissa + 1); |
| 1385 | std::iota(digit_testcases.begin(), digit_testcases.end(), min_mantissa); |
| 1386 | } |
| 1387 |
| 1388 | for (int exponent = -324; exponent <= 308; ++exponent) { |
| 1389 | double powten = absl::strings_internal::Pow10(exponent); |
| 1390 | if (powten == 0) powten = 5e-324; |
| 1391 | if (kFloatNumCases >= 1e9) { |
| 1392 | // The exhaustive test takes a very long time, so log progress. |
| 1393 | char buf[kSixDigitsToBufferSize]; |
| 1394 | LOG(INFO) << "Exp " << exponent << " powten=" << powten << "(" << powten |
| 1395 | << ") (" |
| 1396 | << absl::string_view(buf, SixDigitsToBuffer(powten, buf)) |
| 1397 | << ")"; |
| 1398 | } |
| 1399 | for (int digits : digit_testcases) { |
| 1400 | if (exponent == 308 && digits >= 179769) break; // don't overflow! |
| 1401 | double digiform = (digits + 0.5) * 0.00001; |
| 1402 | double testval = digiform * powten; |
| 1403 | double pretestval = nextafter(testval, 0); |
| 1404 | double posttestval = nextafter(testval, 1.7976931348623157e308); |
| 1405 | checker(testval); |
| 1406 | checker(pretestval); |
| 1407 | checker(posttestval); |
| 1408 | } |
| 1409 | } |
| 1410 | } else { |
| 1411 | EXPECT_EQ(mismatches.size(), 0); |
| 1412 | for (size_t i = 0; i < mismatches.size(); ++i) { |
| 1413 | if (i > 100) i = mismatches.size() - 1; |
| 1414 | double d = mismatches[i]; |
| 1415 | char sixdigitsbuf[kSixDigitsToBufferSize] = {0}; |
| 1416 | SixDigitsToBuffer(d, sixdigitsbuf); |
| 1417 | char snprintfbuf[kSixDigitsToBufferSize] = {0}; |
| 1418 | snprintf(snprintfbuf, kSixDigitsToBufferSize, "%g", d); |
| 1419 | double before = nextafter(d, 0.0); |
| 1420 | double after = nextafter(d, 1.7976931348623157e308); |
| 1421 | char b1[32], b2[kSixDigitsToBufferSize]; |
| 1422 | LOG(ERROR) << "Mismatch #" << i << " d=" << d << " (" << ToNineDigits(d) |
| 1423 | << ") sixdigits='" << sixdigitsbuf << "' snprintf='" |
| 1424 | << snprintfbuf << "' Before.=" << PerfectDtoa(before) << " " |
| 1425 | << (SixDigitsToBuffer(before, b2), b2) << " vs snprintf=" |
| 1426 | << (snprintf(b1, sizeof(b1), "%g", before), b1) |
| 1427 | << " Perfect=" << PerfectDtoa(d) << " " |
| 1428 | << (SixDigitsToBuffer(d, b2), b2) |
| 1429 | << " vs snprintf=" << (snprintf(b1, sizeof(b1), "%g", d), b1) |
| 1430 | << " After.=." << PerfectDtoa(after) << " " |
| 1431 | << (SixDigitsToBuffer(after, b2), b2) << " vs snprintf=" |
| 1432 | << (snprintf(b1, sizeof(b1), "%g", after), b1); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 |
| 1437 | TEST(StrToInt32, Partial) { |
| 1438 | struct Int32TestLine { |
| 1439 | std::string input; |
| 1440 | bool status; |
| 1441 | int32_t value; |
| 1442 | }; |
| 1443 | const int32_t int32_min = std::numeric_limits<int32_t>::min(); |
| 1444 | const int32_t int32_max = std::numeric_limits<int32_t>::max(); |
| 1445 | Int32TestLine int32_test_line[] = { |
| 1446 | {"", false, 0}, |
| 1447 | {" ", false, 0}, |
| 1448 | {"-", false, 0}, |
| 1449 | {"123@@@", false, 123}, |
| 1450 | {absl::StrCat(int32_min, int32_max), false, int32_min}, |
| 1451 | {absl::StrCat(int32_max, int32_max), false, int32_max}, |
| 1452 | }; |
| 1453 |
| 1454 | for (const Int32TestLine& test_line : int32_test_line) { |
| 1455 | int32_t value = -2; |
| 1456 | bool status = safe_strto32_base(test_line.input, &value, 10); |
| 1457 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1458 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1459 | value = -2; |
| 1460 | status = safe_strto32_base(test_line.input, &value, 10); |
| 1461 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1462 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1463 | value = -2; |
| 1464 | status = safe_strto32_base(absl::string_view(test_line.input), &value, 10); |
| 1465 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1466 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1467 | } |
| 1468 | } |
| 1469 |
| 1470 | TEST(StrToUint32, Partial) { |
| 1471 | struct Uint32TestLine { |
| 1472 | std::string input; |
| 1473 | bool status; |
| 1474 | uint32_t value; |
| 1475 | }; |
| 1476 | const uint32_t uint32_max = std::numeric_limits<uint32_t>::max(); |
| 1477 | Uint32TestLine uint32_test_line[] = { |
| 1478 | {"", false, 0}, |
| 1479 | {" ", false, 0}, |
| 1480 | {"-", false, 0}, |
| 1481 | {"123@@@", false, 123}, |
| 1482 | {absl::StrCat(uint32_max, uint32_max), false, uint32_max}, |
| 1483 | }; |
| 1484 |
| 1485 | for (const Uint32TestLine& test_line : uint32_test_line) { |
| 1486 | uint32_t value = 2; |
| 1487 | bool status = safe_strtou32_base(test_line.input, &value, 10); |
| 1488 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1489 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1490 | value = 2; |
| 1491 | status = safe_strtou32_base(test_line.input, &value, 10); |
| 1492 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1493 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1494 | value = 2; |
| 1495 | status = safe_strtou32_base(absl::string_view(test_line.input), &value, 10); |
| 1496 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1497 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1498 | } |
| 1499 | } |
| 1500 |
| 1501 | TEST(StrToInt64, Partial) { |
| 1502 | struct Int64TestLine { |
| 1503 | std::string input; |
| 1504 | bool status; |
| 1505 | int64_t value; |
| 1506 | }; |
| 1507 | const int64_t int64_min = std::numeric_limits<int64_t>::min(); |
| 1508 | const int64_t int64_max = std::numeric_limits<int64_t>::max(); |
| 1509 | Int64TestLine int64_test_line[] = { |
| 1510 | {"", false, 0}, |
| 1511 | {" ", false, 0}, |
| 1512 | {"-", false, 0}, |
| 1513 | {"123@@@", false, 123}, |
| 1514 | {absl::StrCat(int64_min, int64_max), false, int64_min}, |
| 1515 | {absl::StrCat(int64_max, int64_max), false, int64_max}, |
| 1516 | }; |
| 1517 |
| 1518 | for (const Int64TestLine& test_line : int64_test_line) { |
| 1519 | int64_t value = -2; |
| 1520 | bool status = safe_strto64_base(test_line.input, &value, 10); |
| 1521 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1522 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1523 | value = -2; |
| 1524 | status = safe_strto64_base(test_line.input, &value, 10); |
| 1525 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1526 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1527 | value = -2; |
| 1528 | status = safe_strto64_base(absl::string_view(test_line.input), &value, 10); |
| 1529 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1530 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1531 | } |
| 1532 | } |
| 1533 |
| 1534 | TEST(StrToUint64, Partial) { |
| 1535 | struct Uint64TestLine { |
| 1536 | std::string input; |
| 1537 | bool status; |
| 1538 | uint64_t value; |
| 1539 | }; |
| 1540 | const uint64_t uint64_max = std::numeric_limits<uint64_t>::max(); |
| 1541 | Uint64TestLine uint64_test_line[] = { |
| 1542 | {"", false, 0}, |
| 1543 | {" ", false, 0}, |
| 1544 | {"-", false, 0}, |
| 1545 | {"123@@@", false, 123}, |
| 1546 | {absl::StrCat(uint64_max, uint64_max), false, uint64_max}, |
| 1547 | }; |
| 1548 |
| 1549 | for (const Uint64TestLine& test_line : uint64_test_line) { |
| 1550 | uint64_t value = 2; |
| 1551 | bool status = safe_strtou64_base(test_line.input, &value, 10); |
| 1552 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1553 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1554 | value = 2; |
| 1555 | status = safe_strtou64_base(test_line.input, &value, 10); |
| 1556 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1557 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1558 | value = 2; |
| 1559 | status = safe_strtou64_base(absl::string_view(test_line.input), &value, 10); |
| 1560 | EXPECT_EQ(test_line.status, status) << test_line.input; |
| 1561 | EXPECT_EQ(test_line.value, value) << test_line.input; |
| 1562 | } |
| 1563 | } |
| 1564 |
| 1565 | TEST(StrToInt32Base, PrefixOnly) { |
| 1566 | struct Int32TestLine { |
| 1567 | std::string input; |
| 1568 | bool status; |
| 1569 | int32_t value; |
| 1570 | }; |
| 1571 | Int32TestLine int32_test_line[] = { |
| 1572 | { "", false, 0 }, |
| 1573 | { "-", false, 0 }, |
| 1574 | { "-0", true, 0 }, |
| 1575 | { "0", true, 0 }, |
| 1576 | { "0x", false, 0 }, |
| 1577 | { "-0x", false, 0 }, |
| 1578 | }; |
| 1579 | const int base_array[] = { 0, 2, 8, 10, 16 }; |
| 1580 |
| 1581 | for (const Int32TestLine& line : int32_test_line) { |
| 1582 | for (const int base : base_array) { |
| 1583 | int32_t value = 2; |
| 1584 | bool status = safe_strto32_base(line.input.c_str(), &value, base); |
| 1585 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1586 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1587 | value = 2; |
| 1588 | status = safe_strto32_base(line.input, &value, base); |
| 1589 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1590 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1591 | value = 2; |
| 1592 | status = safe_strto32_base(absl::string_view(line.input), &value, base); |
| 1593 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1594 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 |
| 1599 | TEST(StrToUint32Base, PrefixOnly) { |
| 1600 | struct Uint32TestLine { |
| 1601 | std::string input; |
| 1602 | bool status; |
| 1603 | uint32_t value; |
| 1604 | }; |
| 1605 | Uint32TestLine uint32_test_line[] = { |
| 1606 | { "", false, 0 }, |
| 1607 | { "0", true, 0 }, |
| 1608 | { "0x", false, 0 }, |
| 1609 | }; |
| 1610 | const int base_array[] = { 0, 2, 8, 10, 16 }; |
| 1611 |
| 1612 | for (const Uint32TestLine& line : uint32_test_line) { |
| 1613 | for (const int base : base_array) { |
| 1614 | uint32_t value = 2; |
| 1615 | bool status = safe_strtou32_base(line.input.c_str(), &value, base); |
| 1616 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1617 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1618 | value = 2; |
| 1619 | status = safe_strtou32_base(line.input, &value, base); |
| 1620 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1621 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1622 | value = 2; |
| 1623 | status = safe_strtou32_base(absl::string_view(line.input), &value, base); |
| 1624 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1625 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1626 | } |
| 1627 | } |
| 1628 | } |
| 1629 |
| 1630 | TEST(StrToInt64Base, PrefixOnly) { |
| 1631 | struct Int64TestLine { |
| 1632 | std::string input; |
| 1633 | bool status; |
| 1634 | int64_t value; |
| 1635 | }; |
| 1636 | Int64TestLine int64_test_line[] = { |
| 1637 | { "", false, 0 }, |
| 1638 | { "-", false, 0 }, |
| 1639 | { "-0", true, 0 }, |
| 1640 | { "0", true, 0 }, |
| 1641 | { "0x", false, 0 }, |
| 1642 | { "-0x", false, 0 }, |
| 1643 | }; |
| 1644 | const int base_array[] = { 0, 2, 8, 10, 16 }; |
| 1645 |
| 1646 | for (const Int64TestLine& line : int64_test_line) { |
| 1647 | for (const int base : base_array) { |
| 1648 | int64_t value = 2; |
| 1649 | bool status = safe_strto64_base(line.input.c_str(), &value, base); |
| 1650 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1651 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1652 | value = 2; |
| 1653 | status = safe_strto64_base(line.input, &value, base); |
| 1654 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1655 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1656 | value = 2; |
| 1657 | status = safe_strto64_base(absl::string_view(line.input), &value, base); |
| 1658 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1659 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 |
| 1664 | TEST(StrToUint64Base, PrefixOnly) { |
| 1665 | struct Uint64TestLine { |
| 1666 | std::string input; |
| 1667 | bool status; |
| 1668 | uint64_t value; |
| 1669 | }; |
| 1670 | Uint64TestLine uint64_test_line[] = { |
| 1671 | { "", false, 0 }, |
| 1672 | { "0", true, 0 }, |
| 1673 | { "0x", false, 0 }, |
| 1674 | }; |
| 1675 | const int base_array[] = { 0, 2, 8, 10, 16 }; |
| 1676 |
| 1677 | for (const Uint64TestLine& line : uint64_test_line) { |
| 1678 | for (const int base : base_array) { |
| 1679 | uint64_t value = 2; |
| 1680 | bool status = safe_strtou64_base(line.input.c_str(), &value, base); |
| 1681 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1682 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1683 | value = 2; |
| 1684 | status = safe_strtou64_base(line.input, &value, base); |
| 1685 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1686 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1687 | value = 2; |
| 1688 | status = safe_strtou64_base(absl::string_view(line.input), &value, base); |
| 1689 | EXPECT_EQ(line.status, status) << line.input << " " << base; |
| 1690 | EXPECT_EQ(line.value, value) << line.input << " " << base; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 |
| 1695 | void TestFastHexToBufferZeroPad16(uint64_t v) { |
| 1696 | char buf[16]; |
| 1697 | auto digits = absl::numbers_internal::FastHexToBufferZeroPad16(v, buf); |
| 1698 | absl::string_view res(buf, 16); |
| 1699 | char buf2[17]; |
| 1700 | snprintf(buf2, sizeof(buf2), "%016" PRIx64, v); |
| 1701 | EXPECT_EQ(res, buf2) << v; |
| 1702 | size_t expected_digits = snprintf(buf2, sizeof(buf2), "%" PRIx64, v); |
| 1703 | EXPECT_EQ(digits, expected_digits) << v; |
| 1704 | } |
| 1705 |
| 1706 | TEST(FastHexToBufferZeroPad16, Smoke) { |
| 1707 | TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::min()); |
| 1708 | TestFastHexToBufferZeroPad16(std::numeric_limits<uint64_t>::max()); |
| 1709 | TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::min()); |
| 1710 | TestFastHexToBufferZeroPad16(std::numeric_limits<int64_t>::max()); |
| 1711 | absl::BitGen rng; |
| 1712 | for (int i = 0; i < 100000; ++i) { |
| 1713 | TestFastHexToBufferZeroPad16( |
| 1714 | absl::LogUniform(rng, std::numeric_limits<uint64_t>::min(), |
| 1715 | std::numeric_limits<uint64_t>::max())); |
| 1716 | } |
| 1717 | } |
| 1718 |
| 1719 | template <typename Int> |
| 1720 | void ExpectWritesNull() { |
| 1721 | { |
| 1722 | char buf[absl::numbers_internal::kFastToBufferSize]; |
| 1723 | Int x = std::numeric_limits<Int>::min(); |
| 1724 | EXPECT_THAT(absl::numbers_internal::FastIntToBuffer(x, buf), Pointee('\0')); |
| 1725 | } |
| 1726 | { |
| 1727 | char buf[absl::numbers_internal::kFastToBufferSize]; |
| 1728 | Int x = std::numeric_limits<Int>::max(); |
| 1729 | EXPECT_THAT(absl::numbers_internal::FastIntToBuffer(x, buf), Pointee('\0')); |
| 1730 | } |
| 1731 | } |
| 1732 |
| 1733 | TEST(FastIntToBuffer, WritesNull) { |
| 1734 | ExpectWritesNull<int32_t>(); |
| 1735 | ExpectWritesNull<uint32_t>(); |
| 1736 | ExpectWritesNull<int64_t>(); |
| 1737 | ExpectWritesNull<uint32_t>(); |
| 1738 | } |
| 1739 |
| 1740 | } // namespace |
| 1741 |