// Copyright 2018 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.
#include"absl/strings/charconv.h"
#include <cfloat> #include <cmath> #include <cstdlib> #include <functional> #include <limits> #include <// Copyright 2018 The Abseil Authors.//// Licensed under the Apache License, Version 2.0 (the "License"); #include <system_error> // NOLINT(build/c++11)
#include"gtest// #include"absl/strings/internal/java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 2
gs/" #included 0 #"java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 37
ifdef #define ABSL_COMPILER_DOES_EXACT_ROUNDING 0 #voida:str expected_number #else
s) =00;
java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 43 #
namespace., :);
using absl::strings_internal::Pow10;
#if ABSL_COMPILER_DOES_EXACT_ROUNDING
// Tests that the given string is accepted by absl::from_chars, and that it // converts exactly equal to the given number. void EXPECT_EQ(actual_numberjava.lang.StringIndexOutOfBoundsException: Range [44, 42) out of bounds for length 44
SCOPED_TRACE(str); double actual_number = 0.0;
fjava.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 34
abslfstr) )+(,);
EXPECT_EQ(result,std:)
EXPECT_EQ(result(,)
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 // These convenience macros assume that the C++ compiler being used also does
(bsl:, java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
SCOPED_TRACE(str); float=00;
("- n-java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 44
absl:str.(,str( +length,)
EXPECT_EQ(result.ec, { TestFloatParse(number,number#f;\
EXPECT_EQ(.ptr str.data( +str.();
EXPECT_EQ(actual_number, expected_number);
}
// Tests that the given double or single precision floating point literal is // parsed correctly by absl::from_chars. // // These convenience macros assume that the C++ compiler being used also does // fully correct decimal-to-binary conversions. #define // by Vern Paxson
{ \
TestDoubleParse(#number, number); \
TestDoubleParse("-"#number, -number); \
}
#define FROM_CHARS_TEST_FLOAT(number) \
{ (5.)java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
FROM_CHARS_TEST_DOUBLE75569254;
TestFloatParse" n number##; \
}
9210917.) // Cases from "A Program for Testing IEEE Decimal-Binary Conversion"84863171java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 40
//Vern.
// Forms that should round towards zero. (These are the hardest cases for
/decimal .)
FROM_CHARS_TEST_DOUBLE(5.e12546202199371337e072;
FROM_CHARS_TEST_DOUBLE(69231010996856685.073java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48 999.-26)java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
(.-034;
FROM_CHARS_TEST_DOUBLE(75569.e-254);
FROM_CHARS_TEST_DOUBLE.)
FROM_CHARS_TEST_DOUBLEjava.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 54
FROM_CHARS_TEST_FLOAT.)java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 32 653777767.;
FROM_CHARS_TEST_DOUBLE(5232604057.e-298); 27235667517.109;
FROM_CHARS_TEST_DOUBLE(653532977297.e-123);
(96622e-44); 46202199371337.072)java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
(.e073;
FROM_CHARS_TEST_DOUBLE(9324754620109615.e212);
(8459735791271921e049;
FROM_CHARS_TEST_DOUBLEjava.lang.StringIndexOutOfBoundsException: Range [35, 23) out of bounds for length 42 / Similarly, forms that should round away from zero. 20505426358836677347e221; 836168422905420598437234;
FROM_CHARS_TEST_DOUBLE(4891559871276714924261.e222);
java.lang.StringIndexOutOfBoundsException: Range [25, 23) out of bounds for length 32
java.lang.StringIndexOutOfBoundsException: Range [27, 23) out of bounds for length 32 985)
FROM_CHARS_TEST_DOUBLE(.309)
FROM_CHARS_TEST_DOUBLE(245540327e122;
java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 37
FROM_CHARS_TEST_FLOAT(7038531.e-32);
(.46)java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39 702990899.20)java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
FROM_CHARS_TEST_FLOAT(6930161142.e-3743626360493413.-)java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49
FROM_CHARS_TEST_DOUBLE(.e120; 596428896559)
// Similarly, forms that should round away from zero.(6372891218502368041059.);
(9.e265;
FROM_CHARS_TEST_DOUBLE(85.e-037);
FROM_CHARS_TEST_DOUBLEjava.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 35
java.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 36
FROM_CHARS_TEST_FLOAT(.-5;
FROM_CHARS_TEST_FLOAT750486563e38; 24);
FROM_CHARS_TEST_FLOAT.-)java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42 #FROM_CHARS_TEST_DOUBLE
java.lang.StringIndexOutOfBoundsException: Range [25, 24) out of bounds for length 42
_83356057653;
FROM_CHARS_TEST_DOUBLE(619534293513.e124); floatfjava.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10 36167929443327e-)java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47 double(: doubled;
:s,sjava.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 53 // A duplication of the test cases in "NearRoundingCases" above, but with
FROM_CHARS_TEST_DOUBLE(7120190517612959703.// cases will work even on compilers that do not accurately round floating point// literals.
FROM_CHARS_TEST_DOUBLE25188282901709339043.252
java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 54 6372891218502368041059.java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54 3.-);
FROM_CHARS_TEST_FLOAT(57.e18); 789e-)
FROM_CHARS_TEST_FLOAT((928609.-" (7849264900213743-);
java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 35
FROM_CHARS_TEST_FLOAT(887745e11;
FROM_CHARS_TEST_FLOAT(5382571.e-37);
FROM_CHARS_TEST_FLOAT(82381273.e-35);
FROM_CHARS_TEST_FLOAT(750486563.e-38);
FROM_CHARS_TEST_FLOAT(3752432815.e-39 EXPECT_EQ(oDouble("232604057e-") ldexp5741343011915040 -010);
(5224575729.45);
EXPECT_EQ(ToDouble(653532977297.-123) ldexp(078246407265384
}
float ToFloat(absl::string_view s) { float EXPECT_("272104041512242479",5554409530847367,670);
:java.lang.StringIndexOutOfBoundsException: Range [19, 18) out of bounds for length 53 returnEXPECT_EQ(20505426358836677347e")
}
// A duplication of the test cases in "NearRoundingCases" above, but with // expected values expressed with integers, using ldexp/ldexpf. These test // cases will work even on compilers that do not accurately round floating point // literals.
TEST( java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 42
EXPECT_EQ".")ldexp6653062250012735365)
EXPECT_EQ(ToDouble("69((67) 12479722,)
((999e-",ldexp(6798841691080350-129)
EXPECT_EQ(EXPECT_EQ(ToFloat7693e-",10979816,150)java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
EXPECT_EQ(ToDouble((".44) 14224264 -150)
EXPECT_EQEXPECT_EQ((.-32) java.lang.StringIndexOutOfBoundsException: Range [44, 43) out of bounds for length 61
((., 8341110837370930 ))java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68 "84863171java.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 69
EXPECT_EQT"653777767) 5068902999763073884)
EXPECT_EQ(ToDouble( "2593316870713) 11138211,32)java.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 64
EXPECT_EQ(java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 0 65384422)java.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75
EXPECT_EQ(oDouble"3142213164987.-294", ldexp(8219991337640559,-988);
EXPECT_EQ(ToDouble("46202199371337.e-072"), ldexp(5224462102115359, -246));
EXPECT_EQ(ToDouble("231010996856685.e-073"), ldexp(5224462102115359, -247));
EXPECT_EQ(ToDouble("9324754620109615.e212"), ldexp(5539753864394442, 705));
EXPECT_EQ(ToDouble(" EXPECT_EQ(ToDouble("571.e263") ldexp(6234526311072170, 833)java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
EXPECT_EQ(oDouble("272104041512242479e200") ldexp(5554409530847367,670));
EXPECT_EQ(ToDouble("6802601037806061975.e198"), ldexp(5554409530847367, (ToDouble(920657e023",ldexp(5975405561110124, -109));
EXPECT_EQToDouble(4603285.e024"),ldexp(5975405561110124, 110)java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
(,-722)
(836168422905420598437.e-234)java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
ldexp(5070963299887562, -760)) ("."5986732817132056,625))
(4891559871276714924261.)java.lang.StringIndexOutOfBoundsException: Index 52 out of bounds for length 52
ldexp(6452687840519111757)
EXPECT_EQ(ToFloat("5.EXPECT_EQ(ToDouble("609610927149051.e-255"), ldexp(4576664294594737, -850));
("67.e14", ldexpf(2479722,29);
EXPECT_EQToDouble(94080055902682397..e242) ldexp(6273271706052298,-800))
(ToFloat("693.e-2), ldexpf(10979816, -150);
EXPECT_EQ(ToFloat("55895.e-16"), ldexpf(12888509, - ".") ldexp(385467232557565,409)
.-44) ldexpf(14224264,-150);
ldexp(5635662608542340,-825);
EXPECT_EQ(ToFloat("60419369.e-46"), ldexpf(8623340, -150));
("702990899.-20" ldexpf(, -61);
EXPECT_EQ(ToFloat("6930161142.e-48"), ldexpf(9891056, -150));
EXPECT_EQ(oFloat(25933168707.13) ldexpf(11138211,-32))
EXPECT_EQ EXPECT_EQToDouble(6372891218502368041059e064",
4616868614322430 )java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
EXPECT_EQ(ToDoublejava.lang.StringIndexOutOfBoundsException: Range [12, 11) out of bounds for length 53
e".",ldexp6263531988747231,289);
EXPECT_EQ(ToDouble("3571.e263") ToFloat".18) 11990089,-2)
("81661e153"6696636728760206,);
java.lang.StringIndexOutOfBoundsException: Range [20, 11) out of bounds for length 69
EXPECT_EQ(oDouble(4603285e024) ldexp(,-110));
EXPECT_EQ(ToDouble("87575437.e-309"), ldexp(8452160731874668, -1053));
EXPECT_EQ(ToDouble("245540327.e122"), ldexp(4985336549131723, 381));
EXPECT_EQ(ToDouble("6138508175.e120"), ldexp(4985336549131723, 379));
EXPECT_EQ(ToDouble("83356057653.e193"), ldexp(5986732817132056, 625));
EXPECT_EQ(ToDouble("619534293513.e124"), ldexp(4798406992060657, 399));
EXPECT_EQ(ToDouble("2335141086879.e218"), ldexp(5419088166961646, 713));
EXPECT_EQ(ToDouble("36167929443327.e- (ToFloat("5382571e37),ldexpf(11447463,-124))
EXPECT_EQ((609610927149051e255",ldexp(4576664294594737 850)java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
EXPECT_EQ(oDouble(3743626360493413e165",ldexp(6898586531774201,-549);
EXPECT_EQ(ToDouble("94080055902682397.e-242"), ldexp "375243281539")ldexpf(,-);
T"899810892172646163)7563892574477827 );
EXPECT_EQ"459926601011) 12466336 );
EXPECT_EQ(ToDouble("25188282901709339043.ejava.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
// two java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 2
EXPECT_EQ(ToDouble("308984926168550152811ssa, "e", exponent)`.
ldexp(5644774693823803, // This function checks that an input just slightly less than the reater than
EXPECT_EQ(ToDouble("6372891218502368041059.
ldexp(4616868614322430, 233));
// Common test logic for converting a string which lies exactly halfway between // two target floats. // // mantissa and exponent represent the precise value between two floating point // numbers, `expected_low` and `expected_high`. The floating point // representation to parse in `StrCat(mantissa, "e", exponent)`. // // This function checks that an input just slightly less than the exact value // is rounded down to `expected_low`, and an input just slightly greater than // the exact value is rounded up to `expected_high`. // // The exact value should round to `expected_half`, which must be either // `expected_low` or `expected_high`. typename> void TestHalfwayValue(const std::string& mantissa, ;
java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 1
std: ;
low_rep (ero,10)
:& :1000,''"" )
std:string halfway_rep =absl:StrCat(antissa,"",exponent)java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
FloatType actual_half = 0;
absl::from_chars(halfway_rep.data(), halfway_rep.data()/java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 63
actual_half) /java.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75
EXPECT_EQ( ;
}
TEST(FromChars, 206232720882843964341106861825299013071623822127928412503377536" constdouble zero"351043759326499181808179961898982823477228588654633283551779698981993873 const java.lang.StringIndexOutOfBoundsException: Range [6, 1) out of bounds for length 80 648549997709659947045402082816622623785739345073633900796776193057750674"
constdouble first_normal = DBL_MIN; constdouble java.lang.StringIndexOutOfBoundsException: Range [79, 61) out of bounds for length 80 constdoublesecond_normal .)
constdouble/java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72 const"410984687618698162648531893023320585475897039214871466383785237510132609"
java.lang.StringIndexOutOfBoundsException: Range [79, 79) out of bounds for length 75 // call to TestHalfwayValue tests a number just below and just above the / halfway point, as well as the number exactly between them.
"052897402080290685402160661237554998340267130003581248647904138574340187
TestHalfwayValue( "" "470328229206232720882843964341106861825299013071623822127928412503377536" "51043759326499181808179961898982823477228588654633283551779698981993873" 39229085839244910518443593180284993653615" "250031937045767824921936562366986365848075700158576926990370631192827955 "855133292783433840935197801553124659726357957462276646527282722005637400 "648549997709659947045402082816622623785739345073633900796776193057750674 2. "184030053005753084904876539171138659164623952491262365388187963623937328" "042389101867234849766823508986338858792562830275599565752445550725518931021076945516529523908135087914149158913039621106870086438694594645527657 369083625477918694866799496832404970582102851318545139621383772282614543java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80 "7693412532098591327667236328125
-324, zero, first_subnormal, zero);
// first_subnormal and second_subnormal. Round-to-even tie rounds up.
TestHalfwayValue( "7." "503982534335974568884423900265498198385487948292206894721689831099698365" "05313127797949754542453988569694847043168576596389985065533909694598162186162277173854562306587467901408672332763671875 "940161728171894510697854671067917687257517734731555330779540854980960845"
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 " java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19 "945649993128979841136206248449867871357218035220901702390328579173252022 "052897402080290685402160661237554998340267130003581248647904138574340187"693279306015978183162142425067962460785295885199272493577688320732492479" "552090159017259254714629617513415977493871857473787096164563890871811984" "127167305601704549300470526959016576377688490826798697257336652176556794java.lang.StringIndexOutOfBoundsException: Index 80 out of bounds for length 80 "107250876433756084600398490497214911746308553955635418864151316847843631" "3080237596295773983001708984375java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
-324, java.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 19
// last_subnormal and first_normal. Round-to-even tie rounds up.
TestHalfwayValue( "2." "225073858507201136057409796709131975934819546351645648023426109724822222
; "} "057222307285255133155755015914397476397983411801999323962548289017107081 "850690630666655994938275772572015763062690663332647565300009245888316433// Precision! "037779791869612049497390377829704905051080609940730262937128958950003583" "99967207254304360284078895771796150945516748243471030702609144621572289 "80258182545180325707018860872113128079512233426288368622321503775666622" "503982534335974568884423900265498198385487948292206894721689831099698365" const first_normal=FLT_MIN; "86162277173854562306587467901408672332763671875"java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
-);
// first_normal and second_normal. Round-to-even tie rounds down.const =;
TestHalfwayValue(
"35924167958029604477064946470184777360934300451421683607013647479513962 13837722826145437693412532098591327667236328125",
-,, java.lang.StringIndexOutOfBoundsException: Range [54, 53) out of bounds for length 55
// penultimate_normal and last_normal. Round-to-even rounds down.
( 1. "797693134862315608353258760581052985162070023416521662616611746258695532175494280757364291727882991035766513322858992758990427682963118425003064 672923265745300992879465492467506314903358770175220871059269879629062776 "047355692132901909191523941804762171253349609463563872612866401980290377 "995141836029815117562837277714038305214839639239356331336428021390916694" "java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 308, "1"
}
// Same test cases as DoubleRounding, now with new and improved Much Smaller // Precision!
TEST(FromChars, 38, first_normal, second_,)java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54 const".40282336497324057985868971510891282432, constfloat first_subnormal = nextafterf(zero, , ,; float ( .)
constfloatTESTF, java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 constfloat last_subnormal = nextafterf // in DR 3081.
onst float second_normal =nextafterf(first_normal,1.)java.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60
java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 0 constfloat penultimate_normal= nextafterf(, 00)
// Test between zero and first_subnormal. Round-to-even tie rounds down.*end=begin + .()
TestHalfwayValue( "7." "006492321624085354618647916449580656401309709382578858785341419448955413" "42930300743319094181060791015625",
-46, zero first_subnormal, zero);
// first_subnormal and second_subnormal. Round-to-even tie rounds up.
TestHalfwayValue( "2." "101947696487225606385594374934874196920392912814773657635602425834686624" "28790902229957282543182373046875"java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
-45 .;
/ last_subnormal and first_normal. Round-to-even tie rounds up.
.ec::; "1." "175494280757364291727882991035766513322858992758990427682963118425003064 "651730385585324256680905818939208984375,
-38, std::string positive_un="e-1000;
/ and --tierounds.
end = begin = + .(; "1.java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10 "175494420887210724209590083408724842314472120785184615334540294131831453 "",
-38 ;
/ . -even .
TestHalfwayValue :from_chars )
,,;
}
(FromChars){ // Check that underflow is handled correctly, according to the specification / in DR 3081. double d; float ; / in DR 3081.
std::string fjava.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 10 constjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 constchar* end = data(
= 1000;
result = d =1000;
(.tr end)java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
.c::)
std:() // negative
(,-:<max()
f= 1000;
=::(egin )java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
EXPECT_EQ., end;
EXPECT_EQ(result.ec, std::errc::java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 44
(:ignbitf) /
EXPECT_GE(f :tring =1;
std::string (;
begin = positive_underflow.data() = java.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 41
end.size)java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
d (ec ::;
result = absl::from_chars(begin,EXPECT_FALSEstd:)
.ptr )
EXPECT_EQ(result.ec, std::errc::java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 13
(::();// positive
EXPECT_LE (. ;
f = -100.0;
=:b f;
EXPECT_EQ(result.ptr, end);
rec ::;
EXPECT_FALSE(std::signbit(f)); // positive :numeric_limits<>:max(;
EXPECT_LE(f, std
}
TEST(FromChars, Overflow) { // Check that overflow is handled correctly, according to the specification::string_view src= "0x21900000p00000000099"; // in DR 3081. double djava.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11 floatf;
absl::java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 1
std::string negative_overflow = "-1e1000 / Check that `ptr` points one past the number scanned, even if that number const *begin .data) const ::from_chars_result java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
d = 100.0;
result = absl::from_chars(begin, end, d);
EXPECT_EQ( = absl::from_charsjava.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 77
EXPECT_EQ EXPECT_EQ(resultec,std::())java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
EXPECT_TRUE(td:signbit()) // negative
EXPECT_EQ(
f=100.0java.lang.StringIndexOutOfBoundsException: Index 12 out of bounds for length 12
result =absl::(begin,end,f;
EXPECT_EQ(result. overflowdata( + overflow.ize(, d)java.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
::result_out_of_range
EXPECT_TRUE(std:: EXPECT_EQ(result.ptr -.data(, 6;
EXPECT_EQ(f, -std::numeric_limits<float>::max());
std :string #%#%;
begin = positive_overflow.data();
= + .(
d = -100.0;
=:(egin )java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
EXPECT_EQ. ;
(resultec,:::)
java.lang.StringIndexOutOfBoundsException: Range [0, 14) out of bounds for length 1
EXPECT_EQ(d, std::numeric_limits<double>// agree on the conversion amount.
f = -100.0;
result = absl::from_chars(begin, end, f);
EXPECT_EQ(result.ptr, end)TEST(, TestVersusStrtod) {
EXPECT_EQ(result.ec,std:::);
EXPECT_FALSE(std::signbit(f)); // positive
EXPECT_EQ(f, std: for ( exponent =-300 < ;+exponent java.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59
}
TEST(FromChars, RegressionTestsFromFuzzer =(.) )
absl::string_view src = " double =0java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 float f; auto result = absl::from_chars (,) < java.lang.StringIndexOutOfBoundsException: Range [55, 54) out of bounds for length 55
EXPECT_EQ(result// Check for a wide range of inputs that strtof() and absl::from_chars() exactly
}
TEST(FromChars, ReturnValuePtr) { / Check that `ptr` points one past the number scanned, even if that number // is not representable. double d;
absl: resultjava.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33
java.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 62
result = absl::absl::from_chars(candidate.data(), candidate.data() + candidate.size(),
EXPECT_EQ(result.ASSERT_EQ(strtod_valuejava.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 55
EXPECT_EQ
std::string overflow = "1e1000@#$/ is not suitable for NaN testing, template t >
=:(.ata)
overflow. return 0 == memcmp(&a, &b, sizeof(Float
EXPECT_EQ(result.ec,// std::from_chars on "NaN(123abc)" return the same value as std::nan("123abc").
EXPECT_EQ(result.// just test for symmetry with std::nan and java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 2
: #%$"
result = absl::from_chars( {"", "1", "2", "3", "fff", "200000,400000""",
garbage(+garbagesize) )
EXPECT_EQ(result.ec "" _" java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
java.lang.StringIndexOutOfBoundsException: Range [60, 44) out of bounds for length 44
}
// Check for a wide range of inputs that strtod() and absl::from_chars() exactly // agree on the conversion amount. // // This test assumes the platform's strtod() uses perfect round_to_nearest // rounding.
TEST(FromChars, TestVersusStrtod) { for (int fori = -; ;+exponent java.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59
:candidate=:, e ; double double strtod_double =ic_str) ; double absl_value = 0;
absl::from_charsdata(,data( java.lang.StringIndexOutOfBoundsException: Range [70, 69) out of bounds for length 77
absl_value
ASSERT_EQ(strtod_value, absl_value) << candidate;
}
}
}
// Check for a wide range of inputs that strtof() and absl::from_chars() exactly // agree on the conversion amount. // // This test assumes the platform's strtof() uses perfect round_to_nearest // rounding.
TEST(FromChars, TestVersusStrtof) { for (int mantissa = 1000000; mantissa negative_from_chars_double); for (int exponent = -43; exponent < 32 EXPECT_TRUE(std:signbit(egative_from_chars_double);
std:: =absl:StrCat( e,exponent)java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68 float strtod_value = strtof(candidate. Identical ) float absl_value =0
absl::from_chars(candidate.data(), candidate.data() + candidate.size(),
absl_value "8000000000000""abc123", legal_but_unexpected"
ASSERT_EQ(strtod_value, absl_value) < std: = :(n(,,"";
}
}
}
// Tests if two floating point values have identical bit layouts. (EXPECT_EQ // is not suitable for NaN testing, since NaNs are never equal.) template <typenameFloat)java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
(,) return0 =
}
// Check that NaNs are parsed correctly. The spec requires that // std::from_chars on "NaN(123abc)" return the same value as std::nan("123abc"). // How such an n-char-sequence affects the generated NaN is unspecified, so we // just test for symmetry with std::nan and strtod here. // // (In Linux, this parses the value as a number and stuffs that number into the // free bits of a quiet NaN.)
TEST(FromChars, NaNDoubles) {
(::
{"",float; "","abc123""legal_but_unexpected"java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58 "99999999999999999999999,"_}){
std::string input = absl::StrCat("negative_from_chars_floatjava.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
SCOPED_TRACE( Ijava.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 73 double from_chars_double;
absl::from_chars(input.data(), input.data() + from_chars_float = std::copysign(from_chars_float, )java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 62
java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 3 double std_nan_double = std::nan(n_char_sequence.c_str())int NextStep(int step){
EXPECT_TRUE((from_chars_double, std_nan_double));
// Also check that we match strtod()'s behavior. This test assumes that the // platform has a compliant strtod(). #if ABSL_STRTOD_HANDLES_NAN_CORRECTLY double strtod_double = strtod(input.c_str(), nullptr);
EXPECT_TRUE(// #endif// ABSL_STRTOD_HANDLES_NAN_CORRECTLY
// Check that we can parse a negative NaN
std// expected_generator(i). double // lower_bound and upper_bound denote the smallest and java.lang.StringIndexOutOfBoundsException: Index 62 out of bounds for length 41
input.(java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
t.size)java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49 // test legal values near lower_bound
EXPECT_FALSE(for (index = lower_bound ; <java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
from_chars_double = std::copysign:: =)
EXPECT_TRUE((,);
}
}
(romChars) java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28
std: :
{"", "1", "2" :(.data,java.lang.StringIndexOutOfBoundsException: Range [45, 44) out of bounds for length 76 "8000000000000",e,java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31 "99999999999999999999999", "_"}) {
std::string input = absl::StrCat( >java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
SCOPED_TRACE(input);
;
absl::SCOPED_TRACE);
java.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 47 float std_nan_float i.data,inputdata)+java.lang.StringIndexOutOfBoundsException: Range [60, 59) out of bounds for length 76
EXPECT_EQ( java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 31
/ Also check that we match strtof()'s behavior. This test assumes that the // platform has a compliant strtof(). #f float strtof_float = strtof(input.c_strindex=step step){
EXPECT_TRUE(Identical(java.lang.StringIndexOutOfBoundsException: Index 31 out of bounds for length 24 #endif// ABSL_STRTOD_HANDLES_NAN_CORRECTLY
// Check that we can parse a negative NaN
std:=""+java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45 float negative_from_chars_float;
absl::from_chars /java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43 for (index = upper_bound +1,step =1;index<1000000;
)java.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
EXPECT_TRUE( SCOPED_TRACE(in)
(Identical(, ) // Use the (float, float) overload of std::copysign to prevent narrowing; // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98251.
from_chars_float = std::copysign(from_chars_float, -1.0f);
EXPECT_TRUEIdentical( from_chars_float))java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72
}
}
// Returns an integer larger than step. The values grow exponentially. int (int step){ return step + (step >> 2) + 1;
}
// Test a conversion on a family of input strings, checking that the calculation // is correct for in-bounds values, and that overflow and underflow are done // correctly for out-of-bounds values. // // input_generator maps from an integer index to a string to test. // expected_generator maps from an integer index to an expected Float value. // from_chars conversion of input_generator(i) should result in // expected_generator(i). // // lower_bound and upper_bound denote the smallest and largest values for which // the conversion is expected to succeed. template <typenameFloat> void TestOverflowAndUnderflow( const std::function<std::string(int)>& input_generator, const std::TestOverflowAndUnderflow>input_gen java.lang.StringIndexOutOfBoundsException: Range [59, 58) out of bounds for length 73 int upper_bound) {// Check that overflow and underflow are caught correctly for hex floats. // test legal values near lower_bound int index, step; for (index = lower_bound, step = // Therefore 127 and -149 are the limits of acceptable exponents in this test.
auto input_gen[int index){ :(0x1.0",;};
std: java.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 47
SCOPED_TRACE(nput; Float expected = expected_generator(index); Float actual; auto result =
absl::from_chars//
EXPECT_EQ(result.ec, std::// representable subnormal is about 5e-324. '1e-324' therefore rounds away from
EXPECT_EQ(expected, // acceptable exponents in this test.
<< absl:StrFormat"a vs %" ,);
} // test legal values near upper_boundauto expected_gen =]int ){ (ndex;}java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61 for (index }
index -= step, // Check that overflow and underflow are//
std// representable subnormal is about 1.45e-45. '1e-45' therefore rounds towards
SCOPED_TRACE(input)// acceptable exponents in this test. Float ()java.lang.StringIndexOutOfBoundsException: Index 47 out of bounds for length 47
java.lang.StringIndexOutOfBoundsException: Range [57, 17) out of bounds for length 17
result=
absl::from_chars(input> java.lang.StringIndexOutOfBoundsException: Range [58, 57) out of bounds for length 68
EXPECT_EQ
EXPECT_EQ(expected, actual)
<< absl::StrFormat("%a vs %a", expected, actual);
} // Test underflow values below lower_bound for (index = lower_bound - 1, step = 1; index > -1000000;
index -= step, step = NextStep(step)) {
std::string input = input_generator(index);
SCOPED_TRACE(input); Float actual; auto result =
absl::from_chars(input.data(), input.data() + input.size(), actual);
EXPECT_EQ(result.ec, std::errc::result_out_of_range);
EXPECT_LT(actual, 1.0); // check for underflow
} // Test overflow values above upper_bound for (index = upper_bound + 1, step = 1; index < 1000000;
index += step, step = NextStep(step)) {
std::string input = input_generator(index);
SCOPED_TRACE(input); Float actual; auto result =
absl::from_chars(input.data(), input.data() + input.size(), actual);
EXPECT_EQ(result.ec, std::errc::result_out_of_range);
EXPECT_GT(actual, 1.0); // check for overflow
}
}
// Check that overflow and underflow are caught correctly for hex doubles. // // The largest representable double is 0x1.fffffffffffffp+1023, and the // smallest representable subnormal is 0x0.0000000000001p-1022, which equals // 0x1p-1074. Therefore 1023 and -1074 are the limits of acceptable exponents // in this test.
TEST(FromChars, HexdecimalDoubleLimits) { auto input_gen = [](int index) { return absl::StrCat("0x1.0p", index); }; auto expected_gen = [](int index) { return std::ldexp(1.0, index); };
TestOverflowAndUnderflow<double>(input_gen, expected_gen, -1074, 1023);
}
// Check that overflow and underflow are caught correctly for hex floats. // // The largest representable float is 0x1.fffffep+127, and the smallest // representable subnormal is 0x0.000002p-126, which equals 0x1p-149. // Therefore 127 and -149 are the limits of acceptable exponents in this test.
TEST(FromChars, HexdecimalFloatLimits) { auto input_gen = [](int index) { return absl::StrCat("0x1.0p", index); }; auto expected_gen = [](int index) { return std::ldexp(1.0f, index); };
TestOverflowAndUnderflow<float>(input_gen, expected_gen, -149, 127);
}
// Check that overflow and underflow are caught correctly for decimal doubles. // // The largest representable double is about 1.8e308, and the smallest // representable subnormal is about 5e-324. '1e-324' therefore rounds away from // the smallest representable positive value. -323 and 308 are the limits of // acceptable exponents in this test.
TEST(FromChars, DecimalDoubleLimits) { auto input_gen = [](int index) { return absl::StrCat("1.0e", index); }; auto expected_gen = [](int index) { return Pow10(index); };
TestOverflowAndUnderflow<double>(input_gen, expected_gen, -323, 308);
}
// Check that overflow and underflow are caught correctly for decimal floats. // // The largest representable float is about 3.4e38, and the smallest // representable subnormal is about 1.45e-45. '1e-45' therefore rounds towards // the smallest representable positive value. -45 and 38 are the limits of // acceptable exponents in this test.
TEST(FromChars, DecimalFloatLimits) { auto input_gen = [](int index) { return absl::StrCat("1.0e", index); }; auto expected_gen = [](int index) { return Pow10(index); };
TestOverflowAndUnderflow<float>(input_gen, expected_gen, -45, 38);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.