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