| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "absl/strings/internal/charconv_parse.h" |
|
|
| #include <string> |
| #include <utility> |
|
|
| #include "gmock/gmock.h" |
| #include "gtest/gtest.h" |
| #include "absl/log/check.h" |
| #include "absl/strings/str_cat.h" |
|
|
| using absl::chars_format; |
| using absl::strings_internal::FloatType; |
| using absl::strings_internal::ParsedFloat; |
| using absl::strings_internal::ParseFloat; |
|
|
| namespace { |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <int base> |
| void ExpectParsedFloat(std::string s, absl::chars_format format_flags, |
| FloatType expected_type, uint64_t expected_mantissa, |
| int expected_exponent, |
| int expected_literal_exponent = -999) { |
| SCOPED_TRACE(s); |
|
|
| int begin_subrange = -1; |
| int end_subrange = -1; |
| |
| |
| std::string::size_type open_bracket_pos = s.find('['); |
| if (open_bracket_pos != std::string::npos) { |
| begin_subrange = static_cast<int>(open_bracket_pos); |
| s.replace(open_bracket_pos, 1, ""); |
| std::string::size_type close_bracket_pos = s.find(']'); |
| CHECK_NE(close_bracket_pos, absl::string_view::npos) |
| << "Test input contains [ without matching ]"; |
| end_subrange = static_cast<int>(close_bracket_pos); |
| s.replace(close_bracket_pos, 1, ""); |
| } |
| const std::string::size_type expected_characters_matched = s.find('$'); |
| CHECK_NE(expected_characters_matched, std::string::npos) |
| << "Input string must contain $"; |
| s.replace(expected_characters_matched, 1, ""); |
|
|
| ParsedFloat parsed = |
| ParseFloat<base>(s.data(), s.data() + s.size(), format_flags); |
|
|
| EXPECT_NE(parsed.end, nullptr); |
| if (parsed.end == nullptr) { |
| return; |
| } |
| EXPECT_EQ(parsed.type, expected_type); |
| if (begin_subrange == -1) { |
| EXPECT_EQ(parsed.subrange_begin, nullptr); |
| EXPECT_EQ(parsed.subrange_end, nullptr); |
| } else { |
| EXPECT_EQ(parsed.subrange_begin, s.data() + begin_subrange); |
| EXPECT_EQ(parsed.subrange_end, s.data() + end_subrange); |
| } |
| if (parsed.type == FloatType::kNumber) { |
| EXPECT_EQ(parsed.mantissa, expected_mantissa); |
| EXPECT_EQ(parsed.exponent, expected_exponent); |
| if (expected_literal_exponent != -999) { |
| EXPECT_EQ(parsed.literal_exponent, expected_literal_exponent); |
| } |
| } |
| auto characters_matched = static_cast<int>(parsed.end - s.data()); |
| EXPECT_EQ(characters_matched, expected_characters_matched); |
| } |
|
|
| |
| |
| |
| |
| |
| template <int base> |
| void ExpectNumber(std::string s, absl::chars_format format_flags, |
| uint64_t expected_mantissa, int expected_exponent, |
| int expected_literal_exponent = -999) { |
| ExpectParsedFloat<base>(std::move(s), format_flags, FloatType::kNumber, |
| expected_mantissa, expected_exponent, |
| expected_literal_exponent); |
| } |
|
|
| |
| |
| |
| |
| void ExpectSpecial(const std::string& s, absl::chars_format format_flags, |
| FloatType type) { |
| ExpectParsedFloat<10>(s, format_flags, type, 0, 0); |
| ExpectParsedFloat<16>(s, format_flags, type, 0, 0); |
| } |
|
|
| |
| template <int base> |
| void ExpectFailedParse(absl::string_view s, absl::chars_format format_flags) { |
| ParsedFloat parsed = |
| ParseFloat<base>(s.data(), s.data() + s.size(), format_flags); |
| EXPECT_EQ(parsed.end, nullptr); |
| } |
|
|
| TEST(ParseFloat, SimpleValue) { |
| |
| ExpectNumber<10>("1.23456789e5$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e+5$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789E5$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e05$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123.456789e3$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("0.000123456789e9$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123456.789$", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123456789e-3$", chars_format::general, 123456789, -3); |
|
|
| ExpectNumber<16>("1.234abcdefp28$", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1.234abcdefp+28$", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1.234ABCDEFp28$", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1.234AbCdEfP0028$", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("123.4abcdefp20$", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("0.0001234abcdefp44$", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("1234abcd.ef$", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1234abcdefp-8$", chars_format::general, 0x1234abcdef, -8); |
|
|
| |
| ExpectNumber<10>("0001.2345678900e005$", chars_format::general, 12345678900, |
| -5); |
| ExpectNumber<16>("0001.234abcdef000p28$", chars_format::general, |
| 0x1234abcdef000, -20); |
|
|
| |
| |
| ExpectNumber<10>("1.23456789e5$ ", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e5$e5e5", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e5$.25", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e5$-", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("1.23456789e5$PUPPERS!!!", chars_format::general, 123456789, |
| -3); |
| ExpectNumber<10>("123456.789$efghij", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123456.789$e", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123456.789$p5", chars_format::general, 123456789, -3); |
| ExpectNumber<10>("123456.789$.10", chars_format::general, 123456789, -3); |
|
|
| ExpectNumber<16>("1.234abcdefp28$ ", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("1.234abcdefp28$p28", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("1.234abcdefp28$.125", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("1.234abcdefp28$-", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1.234abcdefp28$KITTEHS!!!", chars_format::general, |
| 0x1234abcdef, -8); |
| ExpectNumber<16>("1234abcd.ef$ghijk", chars_format::general, 0x1234abcdef, |
| -8); |
| ExpectNumber<16>("1234abcd.ef$p", chars_format::general, 0x1234abcdef, -8); |
| ExpectNumber<16>("1234abcd.ef$.10", chars_format::general, 0x1234abcdef, -8); |
|
|
| |
| ExpectNumber<10>("9999999999999999999$", chars_format::general, |
| 9999999999999999999u, 0); |
| ExpectNumber<16>("fffffffffffffff$", chars_format::general, |
| 0xfffffffffffffffu, 0); |
|
|
| |
| ExpectNumber<10>("0$", chars_format::general, 0, 0); |
| ExpectNumber<16>("0$", chars_format::general, 0, 0); |
| ExpectNumber<10>("000000000000000000000000000000000000000$", |
| chars_format::general, 0, 0); |
| ExpectNumber<16>("000000000000000000000000000000000000000$", |
| chars_format::general, 0, 0); |
| ExpectNumber<10>("0000000000000000000000.000000000000000000$", |
| chars_format::general, 0, 0); |
| ExpectNumber<16>("0000000000000000000000.000000000000000000$", |
| chars_format::general, 0, 0); |
| ExpectNumber<10>("0.00000000000000000000000000000000e123456$", |
| chars_format::general, 0, 0); |
| ExpectNumber<16>("0.00000000000000000000000000000000p123456$", |
| chars_format::general, 0, 0); |
| } |
|
|
| TEST(ParseFloat, LargeDecimalMantissa) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| ExpectNumber<10>("100000000000000000000000000$", chars_format::general, |
| 1000000000000000000, |
| 8); |
|
|
| ExpectNumber<10>("123456789123456789100000000$", chars_format::general, |
| 1234567891234567891, |
| 8); |
|
|
| ExpectNumber<10>("[123456789123456789123456789]$", chars_format::general, |
| 1234567891234567891, |
| 8, |
| 0); |
|
|
| ExpectNumber<10>("[123456789123456789100000009]$", chars_format::general, |
| 1234567891234567891, |
| 8, |
| 0); |
|
|
| ExpectNumber<10>("[123456789123456789120000000]$", chars_format::general, |
| 1234567891234567891, |
| 8, |
| 0); |
|
|
| |
| ExpectNumber<10>("[00000000123456789123456789123456789]$", |
| chars_format::general, 1234567891234567891, |
| 8, |
| 0); |
|
|
| ExpectNumber<10>("00000000123456789123456789100000000$", |
| chars_format::general, 1234567891234567891, |
| 8); |
|
|
| |
| |
| ExpectNumber<10>("1.234567891234567891e123$", chars_format::general, |
| 1234567891234567891, 105); |
| ExpectNumber<10>("[1.23456789123456789123456789]e123$", chars_format::general, |
| 1234567891234567891, |
| 105, |
| 123); |
|
|
| |
| |
| |
| ExpectNumber<10>("[1999999999999999999999]$", chars_format::general, |
| 1999999999999999999, |
| 3, |
| 0); |
| } |
|
|
| TEST(ParseFloat, LargeHexadecimalMantissa) { |
| |
| |
| |
| |
| |
|
|
| ExpectNumber<16>("123456789abcdef123456789abcdef$", chars_format::general, |
| 0x123456789abcdef, 60); |
|
|
| |
| ExpectNumber<16>("000000123456789abcdef123456789abcdef$", |
| chars_format::general, 0x123456789abcdef, 60); |
|
|
| |
| |
| ExpectNumber<16>("1.23456789abcdefp100$", chars_format::general, |
| 0x123456789abcdef, 44); |
| ExpectNumber<16>("1.23456789abcdef123456789abcdefp100$", |
| chars_format::general, 0x123456789abcdef, 44); |
|
|
| |
| |
| ExpectNumber<16>("123456789abcdee123456789abcdee$", chars_format::general, |
| 0x123456789abcdef, 60); |
| ExpectNumber<16>("123456789abcdee000000000000001$", chars_format::general, |
| 0x123456789abcdef, 60); |
| ExpectNumber<16>("123456789abcdee000000000000000$", chars_format::general, |
| 0x123456789abcdee, 60); |
| } |
|
|
| TEST(ParseFloat, ScientificVsFixed) { |
| |
| |
| ExpectNumber<10>("1.23456789$e5", chars_format::fixed, 123456789, -8); |
| ExpectNumber<10>("123456.789$", chars_format::fixed, 123456789, -3); |
| ExpectNumber<16>("1.234abcdef$p28", chars_format::fixed, 0x1234abcdef, -36); |
| ExpectNumber<16>("1234abcd.ef$", chars_format::fixed, 0x1234abcdef, -8); |
|
|
| |
| ExpectNumber<10>("1.23456789e5$", chars_format::scientific, 123456789, -3); |
| ExpectFailedParse<10>("-123456.789$", chars_format::scientific); |
| ExpectNumber<16>("1.234abcdefp28$", chars_format::scientific, 0x1234abcdef, |
| -8); |
| ExpectFailedParse<16>("1234abcd.ef$", chars_format::scientific); |
| } |
|
|
| TEST(ParseFloat, Infinity) { |
| ExpectFailedParse<10>("in", chars_format::general); |
| ExpectFailedParse<16>("in", chars_format::general); |
| ExpectFailedParse<10>("inx", chars_format::general); |
| ExpectFailedParse<16>("inx", chars_format::general); |
| ExpectSpecial("inf$", chars_format::general, FloatType::kInfinity); |
| ExpectSpecial("Inf$", chars_format::general, FloatType::kInfinity); |
| ExpectSpecial("INF$", chars_format::general, FloatType::kInfinity); |
| ExpectSpecial("inf$inite", chars_format::general, FloatType::kInfinity); |
| ExpectSpecial("iNfInItY$", chars_format::general, FloatType::kInfinity); |
| ExpectSpecial("infinity$!!!", chars_format::general, FloatType::kInfinity); |
| } |
|
|
| TEST(ParseFloat, NaN) { |
| ExpectFailedParse<10>("na", chars_format::general); |
| ExpectFailedParse<16>("na", chars_format::general); |
| ExpectFailedParse<10>("nah", chars_format::general); |
| ExpectFailedParse<16>("nah", chars_format::general); |
| ExpectSpecial("nan$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("NaN$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("nAn$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("NAN$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("NaN$aNaNaNaNaBatman!", chars_format::general, FloatType::kNan); |
|
|
| |
| |
| |
| |
| |
| |
| ExpectSpecial("nan([0xabcdef])$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("nan([0xabcdef])$...", chars_format::general, FloatType::kNan); |
| ExpectSpecial("nan([0xabcdef])$)...", chars_format::general, FloatType::kNan); |
| ExpectSpecial("nan([])$", chars_format::general, FloatType::kNan); |
| ExpectSpecial("nan([aAzZ09_])$", chars_format::general, FloatType::kNan); |
| |
| ExpectSpecial("nan$(bad-char)", chars_format::general, FloatType::kNan); |
| |
| ExpectSpecial("nan$(0xabcdef", chars_format::general, FloatType::kNan); |
| } |
|
|
| } |
|
|