00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/strings/internal/charconv_parse.h"
00016
00017 #include <string>
00018 #include <utility>
00019
00020 #include "gmock/gmock.h"
00021 #include "gtest/gtest.h"
00022 #include "absl/base/internal/raw_logging.h"
00023 #include "absl/strings/str_cat.h"
00024
00025 using absl::chars_format;
00026 using absl::strings_internal::FloatType;
00027 using absl::strings_internal::ParsedFloat;
00028 using absl::strings_internal::ParseFloat;
00029
00030 namespace {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 template <int base>
00044 void ExpectParsedFloat(std::string s, absl::chars_format format_flags,
00045 FloatType expected_type, uint64_t expected_mantissa,
00046 int expected_exponent,
00047 int expected_literal_exponent = -999) {
00048 SCOPED_TRACE(s);
00049
00050 int begin_subrange = -1;
00051 int end_subrange = -1;
00052
00053
00054 std::string::size_type open_bracket_pos = s.find('[');
00055 if (open_bracket_pos != std::string::npos) {
00056 begin_subrange = static_cast<int>(open_bracket_pos);
00057 s.replace(open_bracket_pos, 1, "");
00058 std::string::size_type close_bracket_pos = s.find(']');
00059 ABSL_RAW_CHECK(close_bracket_pos != absl::string_view::npos,
00060 "Test input contains [ without matching ]");
00061 end_subrange = static_cast<int>(close_bracket_pos);
00062 s.replace(close_bracket_pos, 1, "");
00063 }
00064 const std::string::size_type expected_characters_matched = s.find('$');
00065 ABSL_RAW_CHECK(expected_characters_matched != std::string::npos,
00066 "Input std::string must contain $");
00067 s.replace(expected_characters_matched, 1, "");
00068
00069 ParsedFloat parsed =
00070 ParseFloat<base>(s.data(), s.data() + s.size(), format_flags);
00071
00072 EXPECT_NE(parsed.end, nullptr);
00073 if (parsed.end == nullptr) {
00074 return;
00075 }
00076 EXPECT_EQ(parsed.type, expected_type);
00077 if (begin_subrange == -1) {
00078 EXPECT_EQ(parsed.subrange_begin, nullptr);
00079 EXPECT_EQ(parsed.subrange_end, nullptr);
00080 } else {
00081 EXPECT_EQ(parsed.subrange_begin, s.data() + begin_subrange);
00082 EXPECT_EQ(parsed.subrange_end, s.data() + end_subrange);
00083 }
00084 if (parsed.type == FloatType::kNumber) {
00085 EXPECT_EQ(parsed.mantissa, expected_mantissa);
00086 EXPECT_EQ(parsed.exponent, expected_exponent);
00087 if (expected_literal_exponent != -999) {
00088 EXPECT_EQ(parsed.literal_exponent, expected_literal_exponent);
00089 }
00090 }
00091 auto characters_matched = static_cast<int>(parsed.end - s.data());
00092 EXPECT_EQ(characters_matched, expected_characters_matched);
00093 }
00094
00095
00096
00097
00098
00099
00100 template <int base>
00101 void ExpectNumber(std::string s, absl::chars_format format_flags,
00102 uint64_t expected_mantissa, int expected_exponent,
00103 int expected_literal_exponent = -999) {
00104 ExpectParsedFloat<base>(std::move(s), format_flags, FloatType::kNumber,
00105 expected_mantissa, expected_exponent,
00106 expected_literal_exponent);
00107 }
00108
00109
00110
00111
00112
00113 void ExpectSpecial(const std::string& s, absl::chars_format format_flags,
00114 FloatType type) {
00115 ExpectParsedFloat<10>(s, format_flags, type, 0, 0);
00116 ExpectParsedFloat<16>(s, format_flags, type, 0, 0);
00117 }
00118
00119
00120 template <int base>
00121 void ExpectFailedParse(absl::string_view s, absl::chars_format format_flags) {
00122 ParsedFloat parsed =
00123 ParseFloat<base>(s.data(), s.data() + s.size(), format_flags);
00124 EXPECT_EQ(parsed.end, nullptr);
00125 }
00126
00127 TEST(ParseFloat, SimpleValue) {
00128
00129 ExpectNumber<10>("1.23456789e5$", chars_format::general, 123456789, -3);
00130 ExpectNumber<10>("1.23456789e+5$", chars_format::general, 123456789, -3);
00131 ExpectNumber<10>("1.23456789E5$", chars_format::general, 123456789, -3);
00132 ExpectNumber<10>("1.23456789e05$", chars_format::general, 123456789, -3);
00133 ExpectNumber<10>("123.456789e3$", chars_format::general, 123456789, -3);
00134 ExpectNumber<10>("0.000123456789e9$", chars_format::general, 123456789, -3);
00135 ExpectNumber<10>("123456.789$", chars_format::general, 123456789, -3);
00136 ExpectNumber<10>("123456789e-3$", chars_format::general, 123456789, -3);
00137
00138 ExpectNumber<16>("1.234abcdefp28$", chars_format::general, 0x1234abcdef, -8);
00139 ExpectNumber<16>("1.234abcdefp+28$", chars_format::general, 0x1234abcdef, -8);
00140 ExpectNumber<16>("1.234ABCDEFp28$", chars_format::general, 0x1234abcdef, -8);
00141 ExpectNumber<16>("1.234AbCdEfP0028$", chars_format::general, 0x1234abcdef,
00142 -8);
00143 ExpectNumber<16>("123.4abcdefp20$", chars_format::general, 0x1234abcdef, -8);
00144 ExpectNumber<16>("0.0001234abcdefp44$", chars_format::general, 0x1234abcdef,
00145 -8);
00146 ExpectNumber<16>("1234abcd.ef$", chars_format::general, 0x1234abcdef, -8);
00147 ExpectNumber<16>("1234abcdefp-8$", chars_format::general, 0x1234abcdef, -8);
00148
00149
00150 ExpectNumber<10>("0001.2345678900e005$", chars_format::general, 12345678900,
00151 -5);
00152 ExpectNumber<16>("0001.234abcdef000p28$", chars_format::general,
00153 0x1234abcdef000, -20);
00154
00155
00156
00157 ExpectNumber<10>("1.23456789e5$ ", chars_format::general, 123456789, -3);
00158 ExpectNumber<10>("1.23456789e5$e5e5", chars_format::general, 123456789, -3);
00159 ExpectNumber<10>("1.23456789e5$.25", chars_format::general, 123456789, -3);
00160 ExpectNumber<10>("1.23456789e5$-", chars_format::general, 123456789, -3);
00161 ExpectNumber<10>("1.23456789e5$PUPPERS!!!", chars_format::general, 123456789,
00162 -3);
00163 ExpectNumber<10>("123456.789$efghij", chars_format::general, 123456789, -3);
00164 ExpectNumber<10>("123456.789$e", chars_format::general, 123456789, -3);
00165 ExpectNumber<10>("123456.789$p5", chars_format::general, 123456789, -3);
00166 ExpectNumber<10>("123456.789$.10", chars_format::general, 123456789, -3);
00167
00168 ExpectNumber<16>("1.234abcdefp28$ ", chars_format::general, 0x1234abcdef,
00169 -8);
00170 ExpectNumber<16>("1.234abcdefp28$p28", chars_format::general, 0x1234abcdef,
00171 -8);
00172 ExpectNumber<16>("1.234abcdefp28$.125", chars_format::general, 0x1234abcdef,
00173 -8);
00174 ExpectNumber<16>("1.234abcdefp28$-", chars_format::general, 0x1234abcdef, -8);
00175 ExpectNumber<16>("1.234abcdefp28$KITTEHS!!!", chars_format::general,
00176 0x1234abcdef, -8);
00177 ExpectNumber<16>("1234abcd.ef$ghijk", chars_format::general, 0x1234abcdef,
00178 -8);
00179 ExpectNumber<16>("1234abcd.ef$p", chars_format::general, 0x1234abcdef, -8);
00180 ExpectNumber<16>("1234abcd.ef$.10", chars_format::general, 0x1234abcdef, -8);
00181
00182
00183 ExpectNumber<10>("9999999999999999999$", chars_format::general,
00184 9999999999999999999u, 0);
00185 ExpectNumber<16>("fffffffffffffff$", chars_format::general,
00186 0xfffffffffffffffu, 0);
00187
00188
00189 ExpectNumber<10>("0$", chars_format::general, 0, 0);
00190 ExpectNumber<16>("0$", chars_format::general, 0, 0);
00191 ExpectNumber<10>("000000000000000000000000000000000000000$",
00192 chars_format::general, 0, 0);
00193 ExpectNumber<16>("000000000000000000000000000000000000000$",
00194 chars_format::general, 0, 0);
00195 ExpectNumber<10>("0000000000000000000000.000000000000000000$",
00196 chars_format::general, 0, 0);
00197 ExpectNumber<16>("0000000000000000000000.000000000000000000$",
00198 chars_format::general, 0, 0);
00199 ExpectNumber<10>("0.00000000000000000000000000000000e123456$",
00200 chars_format::general, 0, 0);
00201 ExpectNumber<16>("0.00000000000000000000000000000000p123456$",
00202 chars_format::general, 0, 0);
00203 }
00204
00205 TEST(ParseFloat, LargeDecimalMantissa) {
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 ExpectNumber<10>("100000000000000000000000000$", chars_format::general,
00219 1000000000000000000,
00220 8);
00221
00222 ExpectNumber<10>("123456789123456789100000000$", chars_format::general,
00223 1234567891234567891,
00224 8);
00225
00226 ExpectNumber<10>("[123456789123456789123456789]$", chars_format::general,
00227 1234567891234567891,
00228 8,
00229 0);
00230
00231 ExpectNumber<10>("[123456789123456789100000009]$", chars_format::general,
00232 1234567891234567891,
00233 8,
00234 0);
00235
00236 ExpectNumber<10>("[123456789123456789120000000]$", chars_format::general,
00237 1234567891234567891,
00238 8,
00239 0);
00240
00241
00242 ExpectNumber<10>("[00000000123456789123456789123456789]$",
00243 chars_format::general, 1234567891234567891,
00244 8,
00245 0);
00246
00247 ExpectNumber<10>("00000000123456789123456789100000000$",
00248 chars_format::general, 1234567891234567891,
00249 8);
00250
00251
00252
00253 ExpectNumber<10>("1.234567891234567891e123$", chars_format::general,
00254 1234567891234567891, 105);
00255 ExpectNumber<10>("[1.23456789123456789123456789]e123$", chars_format::general,
00256 1234567891234567891,
00257 105,
00258 123);
00259
00260
00261
00262
00263 ExpectNumber<10>("[1999999999999999999999]$", chars_format::general,
00264 1999999999999999999,
00265 3,
00266 0);
00267 }
00268
00269 TEST(ParseFloat, LargeHexadecimalMantissa) {
00270
00271
00272
00273
00274
00275
00276 ExpectNumber<16>("123456789abcdef123456789abcdef$", chars_format::general,
00277 0x123456789abcdef, 60);
00278
00279
00280 ExpectNumber<16>("000000123456789abcdef123456789abcdef$",
00281 chars_format::general, 0x123456789abcdef, 60);
00282
00283
00284
00285 ExpectNumber<16>("1.23456789abcdefp100$", chars_format::general,
00286 0x123456789abcdef, 44);
00287 ExpectNumber<16>("1.23456789abcdef123456789abcdefp100$",
00288 chars_format::general, 0x123456789abcdef, 44);
00289
00290
00291
00292 ExpectNumber<16>("123456789abcdee123456789abcdee$", chars_format::general,
00293 0x123456789abcdef, 60);
00294 ExpectNumber<16>("123456789abcdee000000000000001$", chars_format::general,
00295 0x123456789abcdef, 60);
00296 ExpectNumber<16>("123456789abcdee000000000000000$", chars_format::general,
00297 0x123456789abcdee, 60);
00298 }
00299
00300 TEST(ParseFloat, ScientificVsFixed) {
00301
00302
00303 ExpectNumber<10>("1.23456789$e5", chars_format::fixed, 123456789, -8);
00304 ExpectNumber<10>("123456.789$", chars_format::fixed, 123456789, -3);
00305 ExpectNumber<16>("1.234abcdef$p28", chars_format::fixed, 0x1234abcdef, -36);
00306 ExpectNumber<16>("1234abcd.ef$", chars_format::fixed, 0x1234abcdef, -8);
00307
00308
00309 ExpectNumber<10>("1.23456789e5$", chars_format::scientific, 123456789, -3);
00310 ExpectFailedParse<10>("-123456.789$", chars_format::scientific);
00311 ExpectNumber<16>("1.234abcdefp28$", chars_format::scientific, 0x1234abcdef,
00312 -8);
00313 ExpectFailedParse<16>("1234abcd.ef$", chars_format::scientific);
00314 }
00315
00316 TEST(ParseFloat, Infinity) {
00317 ExpectFailedParse<10>("in", chars_format::general);
00318 ExpectFailedParse<16>("in", chars_format::general);
00319 ExpectFailedParse<10>("inx", chars_format::general);
00320 ExpectFailedParse<16>("inx", chars_format::general);
00321 ExpectSpecial("inf$", chars_format::general, FloatType::kInfinity);
00322 ExpectSpecial("Inf$", chars_format::general, FloatType::kInfinity);
00323 ExpectSpecial("INF$", chars_format::general, FloatType::kInfinity);
00324 ExpectSpecial("inf$inite", chars_format::general, FloatType::kInfinity);
00325 ExpectSpecial("iNfInItY$", chars_format::general, FloatType::kInfinity);
00326 ExpectSpecial("infinity$!!!", chars_format::general, FloatType::kInfinity);
00327 }
00328
00329 TEST(ParseFloat, NaN) {
00330 ExpectFailedParse<10>("na", chars_format::general);
00331 ExpectFailedParse<16>("na", chars_format::general);
00332 ExpectFailedParse<10>("nah", chars_format::general);
00333 ExpectFailedParse<16>("nah", chars_format::general);
00334 ExpectSpecial("nan$", chars_format::general, FloatType::kNan);
00335 ExpectSpecial("NaN$", chars_format::general, FloatType::kNan);
00336 ExpectSpecial("nAn$", chars_format::general, FloatType::kNan);
00337 ExpectSpecial("NAN$", chars_format::general, FloatType::kNan);
00338 ExpectSpecial("NaN$aNaNaNaNaBatman!", chars_format::general, FloatType::kNan);
00339
00340
00341
00342
00343
00344
00345
00346 ExpectSpecial("nan([0xabcdef])$", chars_format::general, FloatType::kNan);
00347 ExpectSpecial("nan([0xabcdef])$...", chars_format::general, FloatType::kNan);
00348 ExpectSpecial("nan([0xabcdef])$)...", chars_format::general, FloatType::kNan);
00349 ExpectSpecial("nan([])$", chars_format::general, FloatType::kNan);
00350 ExpectSpecial("nan([aAzZ09_])$", chars_format::general, FloatType::kNan);
00351
00352 ExpectSpecial("nan$(bad-char)", chars_format::general, FloatType::kNan);
00353
00354 ExpectSpecial("nan$(0xabcdef", chars_format::general, FloatType::kNan);
00355 }
00356
00357 }