charconv_parse_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
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 // Check that a given string input is parsed to the expected mantissa and
00033 // exponent.
00034 //
00035 // Input string `s` must contain a '$' character.  It marks the end of the
00036 // characters that should be consumed by the match.  It is stripped from the
00037 // input to ParseFloat.
00038 //
00039 // If input string `s` contains '[' and ']' characters, these mark the region
00040 // of characters that should be marked as the "subrange".  For NaNs, this is
00041 // the location of the extended NaN string.  For numbers, this is the location
00042 // of the full, over-large mantissa.
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   // If s contains '[' and ']', then strip these characters and set the subrange
00053   // indices appropriately.
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;  // The following tests are not useful if we fully failed to parse
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 // Check that a given string input is parsed to the expected mantissa and
00096 // exponent.
00097 //
00098 // Input string `s` must contain a '$' character.  It marks the end of the
00099 // characters that were consumed by the match.
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 // Check that a given string input is parsed to the given special value.
00110 //
00111 // This tests against both number bases, since infinities and NaNs have
00112 // identical representations in both modes.
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 // Check that a given input string is not matched by Float.
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   // Test that various forms of floating point numbers all parse correctly.
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   // ExpectNumber does not attempt to drop trailing zeroes.
00150   ExpectNumber<10>("0001.2345678900e005$", chars_format::general, 12345678900,
00151                    -5);
00152   ExpectNumber<16>("0001.234abcdef000p28$", chars_format::general,
00153                    0x1234abcdef000, -20);
00154 
00155   // Ensure non-matching characters after a number are ignored, even when they
00156   // look like potentially matching characters.
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   // Ensure we can read a full resolution mantissa without overflow.
00183   ExpectNumber<10>("9999999999999999999$", chars_format::general,
00184                    9999999999999999999u, 0);
00185   ExpectNumber<16>("fffffffffffffff$", chars_format::general,
00186                    0xfffffffffffffffu, 0);
00187 
00188   // Check that zero is consistently read.
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   // After 19 significant decimal digits in the mantissa, ParsedFloat will
00207   // truncate additional digits.  We need to test that:
00208   //   1) the truncation to 19 digits happens
00209   //   2) the returned exponent reflects the dropped significant digits
00210   //   3) a correct literal_exponent is set
00211   //
00212   // If and only if a significant digit is found after 19 digits, then the
00213   // entirety of the mantissa in case the exact value is needed to make a
00214   // rounding decision.  The [ and ] characters below denote where such a
00215   // subregion was marked by by ParseFloat.  They are not part of the input.
00216 
00217   // Mark a capture group only if a dropped digit is significant (nonzero).
00218   ExpectNumber<10>("100000000000000000000000000$", chars_format::general,
00219                    1000000000000000000,
00220                    /* adjusted exponent */ 8);
00221 
00222   ExpectNumber<10>("123456789123456789100000000$", chars_format::general,
00223                    1234567891234567891,
00224                    /* adjusted exponent */ 8);
00225 
00226   ExpectNumber<10>("[123456789123456789123456789]$", chars_format::general,
00227                    1234567891234567891,
00228                    /* adjusted exponent */ 8,
00229                    /* literal exponent */ 0);
00230 
00231   ExpectNumber<10>("[123456789123456789100000009]$", chars_format::general,
00232                    1234567891234567891,
00233                    /* adjusted exponent */ 8,
00234                    /* literal exponent */ 0);
00235 
00236   ExpectNumber<10>("[123456789123456789120000000]$", chars_format::general,
00237                    1234567891234567891,
00238                    /* adjusted exponent */ 8,
00239                    /* literal exponent */ 0);
00240 
00241   // Leading zeroes should not count towards the 19 significant digit limit
00242   ExpectNumber<10>("[00000000123456789123456789123456789]$",
00243                    chars_format::general, 1234567891234567891,
00244                    /* adjusted exponent */ 8,
00245                    /* literal exponent */ 0);
00246 
00247   ExpectNumber<10>("00000000123456789123456789100000000$",
00248                    chars_format::general, 1234567891234567891,
00249                    /* adjusted exponent */ 8);
00250 
00251   // Truncated digits after the decimal point should not cause a further
00252   // exponent adjustment.
00253   ExpectNumber<10>("1.234567891234567891e123$", chars_format::general,
00254                    1234567891234567891, 105);
00255   ExpectNumber<10>("[1.23456789123456789123456789]e123$", chars_format::general,
00256                    1234567891234567891,
00257                    /* adjusted exponent */ 105,
00258                    /* literal exponent */ 123);
00259 
00260   // Ensure we truncate, and not round.  (The from_chars algorithm we use
00261   // depends on our guess missing low, if it misses, so we need the rounding
00262   // error to be downward.)
00263   ExpectNumber<10>("[1999999999999999999999]$", chars_format::general,
00264                    1999999999999999999,
00265                    /* adjusted exponent */ 3,
00266                    /* literal exponent */ 0);
00267 }
00268 
00269 TEST(ParseFloat, LargeHexadecimalMantissa) {
00270   // After 15 significant hex digits in the mantissa, ParsedFloat will treat
00271   // additional digits as sticky,  We need to test that:
00272   //   1) The truncation to 15 digits happens
00273   //   2) The returned exponent reflects the dropped significant digits
00274   //   3) If a nonzero digit is dropped, the low bit of mantissa is set.
00275 
00276   ExpectNumber<16>("123456789abcdef123456789abcdef$", chars_format::general,
00277                    0x123456789abcdef, 60);
00278 
00279   // Leading zeroes should not count towards the 15 significant digit limit
00280   ExpectNumber<16>("000000123456789abcdef123456789abcdef$",
00281                    chars_format::general, 0x123456789abcdef, 60);
00282 
00283   // Truncated digits after the radix point should not cause a further
00284   // exponent adjustment.
00285   ExpectNumber<16>("1.23456789abcdefp100$", chars_format::general,
00286                    0x123456789abcdef, 44);
00287   ExpectNumber<16>("1.23456789abcdef123456789abcdefp100$",
00288                    chars_format::general, 0x123456789abcdef, 44);
00289 
00290   // test sticky digit behavior.  The low bit should be set iff any dropped
00291   // digit is nonzero.
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   // In fixed mode, an exponent is never matched (but the remainder of the
00302   // number will be matched.)
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   // In scientific mode, numbers don't match *unless* they have an exponent.
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   // A parenthesized sequence of the characters [a-zA-Z0-9_] is allowed to
00341   // appear after an NaN.  Check that this is allowed, and that the correct
00342   // characters are grouped.
00343   //
00344   // (The characters [ and ] in the pattern below delimit the expected matched
00345   // subgroup; they are not part of the input passed to ParseFloat.)
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   // If the subgroup contains illegal characters, don't match it at all.
00352   ExpectSpecial("nan$(bad-char)", chars_format::general, FloatType::kNan);
00353   // Also cope with a missing close paren.
00354   ExpectSpecial("nan$(0xabcdef", chars_format::general, FloatType::kNan);
00355 }
00356 
00357 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14