charconv_test.cc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  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. #include "absl/strings/charconv.h"
  15. #include <cstdlib>
  16. #include <string>
  17. #include "gmock/gmock.h"
  18. #include "gtest/gtest.h"
  19. #include "absl/strings/internal/pow10_helper.h"
  20. #include "absl/strings/str_cat.h"
  21. #include "absl/strings/str_format.h"
  22. #ifdef _MSC_FULL_VER
  23. #define ABSL_COMPILER_DOES_EXACT_ROUNDING 0
  24. #define ABSL_STRTOD_HANDLES_NAN_CORRECTLY 0
  25. #else
  26. #define ABSL_COMPILER_DOES_EXACT_ROUNDING 1
  27. #define ABSL_STRTOD_HANDLES_NAN_CORRECTLY 1
  28. #endif
  29. namespace {
  30. using absl::strings_internal::Pow10;
  31. #if ABSL_COMPILER_DOES_EXACT_ROUNDING
  32. // Tests that the given string is accepted by absl::from_chars, and that it
  33. // converts exactly equal to the given number.
  34. void TestDoubleParse(absl::string_view str, double expected_number) {
  35. SCOPED_TRACE(str);
  36. double actual_number = 0.0;
  37. absl::from_chars_result result =
  38. absl::from_chars(str.data(), str.data() + str.length(), actual_number);
  39. EXPECT_EQ(result.ec, std::errc());
  40. EXPECT_EQ(result.ptr, str.data() + str.length());
  41. EXPECT_EQ(actual_number, expected_number);
  42. }
  43. void TestFloatParse(absl::string_view str, float expected_number) {
  44. SCOPED_TRACE(str);
  45. float actual_number = 0.0;
  46. absl::from_chars_result result =
  47. absl::from_chars(str.data(), str.data() + str.length(), actual_number);
  48. EXPECT_EQ(result.ec, std::errc());
  49. EXPECT_EQ(result.ptr, str.data() + str.length());
  50. EXPECT_EQ(actual_number, expected_number);
  51. }
  52. // Tests that the given double or single precision floating point literal is
  53. // parsed correctly by absl::from_chars.
  54. //
  55. // These convenience macros assume that the C++ compiler being used also does
  56. // fully correct decimal-to-binary conversions.
  57. #define FROM_CHARS_TEST_DOUBLE(number) \
  58. { \
  59. TestDoubleParse(#number, number); \
  60. TestDoubleParse("-" #number, -number); \
  61. }
  62. #define FROM_CHARS_TEST_FLOAT(number) \
  63. { \
  64. TestFloatParse(#number, number##f); \
  65. TestFloatParse("-" #number, -number##f); \
  66. }
  67. TEST(FromChars, NearRoundingCases) {
  68. // Cases from "A Program for Testing IEEE Decimal-Binary Conversion"
  69. // by Vern Paxson.
  70. // Forms that should round towards zero. (These are the hardest cases for
  71. // each decimal mantissa size.)
  72. FROM_CHARS_TEST_DOUBLE(5.e125);
  73. FROM_CHARS_TEST_DOUBLE(69.e267);
  74. FROM_CHARS_TEST_DOUBLE(999.e-026);
  75. FROM_CHARS_TEST_DOUBLE(7861.e-034);
  76. FROM_CHARS_TEST_DOUBLE(75569.e-254);
  77. FROM_CHARS_TEST_DOUBLE(928609.e-261);
  78. FROM_CHARS_TEST_DOUBLE(9210917.e080);
  79. FROM_CHARS_TEST_DOUBLE(84863171.e114);
  80. FROM_CHARS_TEST_DOUBLE(653777767.e273);
  81. FROM_CHARS_TEST_DOUBLE(5232604057.e-298);
  82. FROM_CHARS_TEST_DOUBLE(27235667517.e-109);
  83. FROM_CHARS_TEST_DOUBLE(653532977297.e-123);
  84. FROM_CHARS_TEST_DOUBLE(3142213164987.e-294);
  85. FROM_CHARS_TEST_DOUBLE(46202199371337.e-072);
  86. FROM_CHARS_TEST_DOUBLE(231010996856685.e-073);
  87. FROM_CHARS_TEST_DOUBLE(9324754620109615.e212);
  88. FROM_CHARS_TEST_DOUBLE(78459735791271921.e049);
  89. FROM_CHARS_TEST_DOUBLE(272104041512242479.e200);
  90. FROM_CHARS_TEST_DOUBLE(6802601037806061975.e198);
  91. FROM_CHARS_TEST_DOUBLE(20505426358836677347.e-221);
  92. FROM_CHARS_TEST_DOUBLE(836168422905420598437.e-234);
  93. FROM_CHARS_TEST_DOUBLE(4891559871276714924261.e222);
  94. FROM_CHARS_TEST_FLOAT(5.e-20);
  95. FROM_CHARS_TEST_FLOAT(67.e14);
  96. FROM_CHARS_TEST_FLOAT(985.e15);
  97. FROM_CHARS_TEST_FLOAT(7693.e-42);
  98. FROM_CHARS_TEST_FLOAT(55895.e-16);
  99. FROM_CHARS_TEST_FLOAT(996622.e-44);
  100. FROM_CHARS_TEST_FLOAT(7038531.e-32);
  101. FROM_CHARS_TEST_FLOAT(60419369.e-46);
  102. FROM_CHARS_TEST_FLOAT(702990899.e-20);
  103. FROM_CHARS_TEST_FLOAT(6930161142.e-48);
  104. FROM_CHARS_TEST_FLOAT(25933168707.e-13);
  105. FROM_CHARS_TEST_FLOAT(596428896559.e20);
  106. // Similarly, forms that should round away from zero.
  107. FROM_CHARS_TEST_DOUBLE(9.e-265);
  108. FROM_CHARS_TEST_DOUBLE(85.e-037);
  109. FROM_CHARS_TEST_DOUBLE(623.e100);
  110. FROM_CHARS_TEST_DOUBLE(3571.e263);
  111. FROM_CHARS_TEST_DOUBLE(81661.e153);
  112. FROM_CHARS_TEST_DOUBLE(920657.e-023);
  113. FROM_CHARS_TEST_DOUBLE(4603285.e-024);
  114. FROM_CHARS_TEST_DOUBLE(87575437.e-309);
  115. FROM_CHARS_TEST_DOUBLE(245540327.e122);
  116. FROM_CHARS_TEST_DOUBLE(6138508175.e120);
  117. FROM_CHARS_TEST_DOUBLE(83356057653.e193);
  118. FROM_CHARS_TEST_DOUBLE(619534293513.e124);
  119. FROM_CHARS_TEST_DOUBLE(2335141086879.e218);
  120. FROM_CHARS_TEST_DOUBLE(36167929443327.e-159);
  121. FROM_CHARS_TEST_DOUBLE(609610927149051.e-255);
  122. FROM_CHARS_TEST_DOUBLE(3743626360493413.e-165);
  123. FROM_CHARS_TEST_DOUBLE(94080055902682397.e-242);
  124. FROM_CHARS_TEST_DOUBLE(899810892172646163.e283);
  125. FROM_CHARS_TEST_DOUBLE(7120190517612959703.e120);
  126. FROM_CHARS_TEST_DOUBLE(25188282901709339043.e-252);
  127. FROM_CHARS_TEST_DOUBLE(308984926168550152811.e-052);
  128. FROM_CHARS_TEST_DOUBLE(6372891218502368041059.e064);
  129. FROM_CHARS_TEST_FLOAT(3.e-23);
  130. FROM_CHARS_TEST_FLOAT(57.e18);
  131. FROM_CHARS_TEST_FLOAT(789.e-35);
  132. FROM_CHARS_TEST_FLOAT(2539.e-18);
  133. FROM_CHARS_TEST_FLOAT(76173.e28);
  134. FROM_CHARS_TEST_FLOAT(887745.e-11);
  135. FROM_CHARS_TEST_FLOAT(5382571.e-37);
  136. FROM_CHARS_TEST_FLOAT(82381273.e-35);
  137. FROM_CHARS_TEST_FLOAT(750486563.e-38);
  138. FROM_CHARS_TEST_FLOAT(3752432815.e-39);
  139. FROM_CHARS_TEST_FLOAT(75224575729.e-45);
  140. FROM_CHARS_TEST_FLOAT(459926601011.e15);
  141. }
  142. #undef FROM_CHARS_TEST_DOUBLE
  143. #undef FROM_CHARS_TEST_FLOAT
  144. #endif
  145. float ToFloat(absl::string_view s) {
  146. float f;
  147. absl::from_chars(s.data(), s.data() + s.size(), f);
  148. return f;
  149. }
  150. double ToDouble(absl::string_view s) {
  151. double d;
  152. absl::from_chars(s.data(), s.data() + s.size(), d);
  153. return d;
  154. }
  155. // A duplication of the test cases in "NearRoundingCases" above, but with
  156. // expected values expressed with integers, using ldexp/ldexpf. These test
  157. // cases will work even on compilers that do not accurately round floating point
  158. // literals.
  159. TEST(FromChars, NearRoundingCasesExplicit) {
  160. EXPECT_EQ(ToDouble("5.e125"), ldexp(6653062250012735, 365));
  161. EXPECT_EQ(ToDouble("69.e267"), ldexp(4705683757438170, 841));
  162. EXPECT_EQ(ToDouble("999.e-026"), ldexp(6798841691080350, -129));
  163. EXPECT_EQ(ToDouble("7861.e-034"), ldexp(8975675289889240, -153));
  164. EXPECT_EQ(ToDouble("75569.e-254"), ldexp(6091718967192243, -880));
  165. EXPECT_EQ(ToDouble("928609.e-261"), ldexp(7849264900213743, -900));
  166. EXPECT_EQ(ToDouble("9210917.e080"), ldexp(8341110837370930, 236));
  167. EXPECT_EQ(ToDouble("84863171.e114"), ldexp(4625202867375927, 353));
  168. EXPECT_EQ(ToDouble("653777767.e273"), ldexp(5068902999763073, 884));
  169. EXPECT_EQ(ToDouble("5232604057.e-298"), ldexp(5741343011915040, -1010));
  170. EXPECT_EQ(ToDouble("27235667517.e-109"), ldexp(6707124626673586, -380));
  171. EXPECT_EQ(ToDouble("653532977297.e-123"), ldexp(7078246407265384, -422));
  172. EXPECT_EQ(ToDouble("3142213164987.e-294"), ldexp(8219991337640559, -988));
  173. EXPECT_EQ(ToDouble("46202199371337.e-072"), ldexp(5224462102115359, -246));
  174. EXPECT_EQ(ToDouble("231010996856685.e-073"), ldexp(5224462102115359, -247));
  175. EXPECT_EQ(ToDouble("9324754620109615.e212"), ldexp(5539753864394442, 705));
  176. EXPECT_EQ(ToDouble("78459735791271921.e049"), ldexp(8388176519442766, 166));
  177. EXPECT_EQ(ToDouble("272104041512242479.e200"), ldexp(5554409530847367, 670));
  178. EXPECT_EQ(ToDouble("6802601037806061975.e198"), ldexp(5554409530847367, 668));
  179. EXPECT_EQ(ToDouble("20505426358836677347.e-221"),
  180. ldexp(4524032052079546, -722));
  181. EXPECT_EQ(ToDouble("836168422905420598437.e-234"),
  182. ldexp(5070963299887562, -760));
  183. EXPECT_EQ(ToDouble("4891559871276714924261.e222"),
  184. ldexp(6452687840519111, 757));
  185. EXPECT_EQ(ToFloat("5.e-20"), ldexpf(15474250, -88));
  186. EXPECT_EQ(ToFloat("67.e14"), ldexpf(12479722, 29));
  187. EXPECT_EQ(ToFloat("985.e15"), ldexpf(14333636, 36));
  188. EXPECT_EQ(ToFloat("7693.e-42"), ldexpf(10979816, -150));
  189. EXPECT_EQ(ToFloat("55895.e-16"), ldexpf(12888509, -61));
  190. EXPECT_EQ(ToFloat("996622.e-44"), ldexpf(14224264, -150));
  191. EXPECT_EQ(ToFloat("7038531.e-32"), ldexpf(11420669, -107));
  192. EXPECT_EQ(ToFloat("60419369.e-46"), ldexpf(8623340, -150));
  193. EXPECT_EQ(ToFloat("702990899.e-20"), ldexpf(16209866, -61));
  194. EXPECT_EQ(ToFloat("6930161142.e-48"), ldexpf(9891056, -150));
  195. EXPECT_EQ(ToFloat("25933168707.e-13"), ldexpf(11138211, -32));
  196. EXPECT_EQ(ToFloat("596428896559.e20"), ldexpf(12333860, 82));
  197. EXPECT_EQ(ToDouble("9.e-265"), ldexp(8168427841980010, -930));
  198. EXPECT_EQ(ToDouble("85.e-037"), ldexp(6360455125664090, -169));
  199. EXPECT_EQ(ToDouble("623.e100"), ldexp(6263531988747231, 289));
  200. EXPECT_EQ(ToDouble("3571.e263"), ldexp(6234526311072170, 833));
  201. EXPECT_EQ(ToDouble("81661.e153"), ldexp(6696636728760206, 472));
  202. EXPECT_EQ(ToDouble("920657.e-023"), ldexp(5975405561110124, -109));
  203. EXPECT_EQ(ToDouble("4603285.e-024"), ldexp(5975405561110124, -110));
  204. EXPECT_EQ(ToDouble("87575437.e-309"), ldexp(8452160731874668, -1053));
  205. EXPECT_EQ(ToDouble("245540327.e122"), ldexp(4985336549131723, 381));
  206. EXPECT_EQ(ToDouble("6138508175.e120"), ldexp(4985336549131723, 379));
  207. EXPECT_EQ(ToDouble("83356057653.e193"), ldexp(5986732817132056, 625));
  208. EXPECT_EQ(ToDouble("619534293513.e124"), ldexp(4798406992060657, 399));
  209. EXPECT_EQ(ToDouble("2335141086879.e218"), ldexp(5419088166961646, 713));
  210. EXPECT_EQ(ToDouble("36167929443327.e-159"), ldexp(8135819834632444, -536));
  211. EXPECT_EQ(ToDouble("609610927149051.e-255"), ldexp(4576664294594737, -850));
  212. EXPECT_EQ(ToDouble("3743626360493413.e-165"), ldexp(6898586531774201, -549));
  213. EXPECT_EQ(ToDouble("94080055902682397.e-242"), ldexp(6273271706052298, -800));
  214. EXPECT_EQ(ToDouble("899810892172646163.e283"), ldexp(7563892574477827, 947));
  215. EXPECT_EQ(ToDouble("7120190517612959703.e120"), ldexp(5385467232557565, 409));
  216. EXPECT_EQ(ToDouble("25188282901709339043.e-252"),
  217. ldexp(5635662608542340, -825));
  218. EXPECT_EQ(ToDouble("308984926168550152811.e-052"),
  219. ldexp(5644774693823803, -157));
  220. EXPECT_EQ(ToDouble("6372891218502368041059.e064"),
  221. ldexp(4616868614322430, 233));
  222. EXPECT_EQ(ToFloat("3.e-23"), ldexpf(9507380, -98));
  223. EXPECT_EQ(ToFloat("57.e18"), ldexpf(12960300, 42));
  224. EXPECT_EQ(ToFloat("789.e-35"), ldexpf(10739312, -130));
  225. EXPECT_EQ(ToFloat("2539.e-18"), ldexpf(11990089, -72));
  226. EXPECT_EQ(ToFloat("76173.e28"), ldexpf(9845130, 86));
  227. EXPECT_EQ(ToFloat("887745.e-11"), ldexpf(9760860, -40));
  228. EXPECT_EQ(ToFloat("5382571.e-37"), ldexpf(11447463, -124));
  229. EXPECT_EQ(ToFloat("82381273.e-35"), ldexpf(8554961, -113));
  230. EXPECT_EQ(ToFloat("750486563.e-38"), ldexpf(9975678, -120));
  231. EXPECT_EQ(ToFloat("3752432815.e-39"), ldexpf(9975678, -121));
  232. EXPECT_EQ(ToFloat("75224575729.e-45"), ldexpf(13105970, -137));
  233. EXPECT_EQ(ToFloat("459926601011.e15"), ldexpf(12466336, 65));
  234. }
  235. // Common test logic for converting a string which lies exactly halfway between
  236. // two target floats.
  237. //
  238. // mantissa and exponent represent the precise value between two floating point
  239. // numbers, `expected_low` and `expected_high`. The floating point
  240. // representation to parse in `StrCat(mantissa, "e", exponent)`.
  241. //
  242. // This function checks that an input just slightly less than the exact value
  243. // is rounded down to `expected_low`, and an input just slightly greater than
  244. // the exact value is rounded up to `expected_high`.
  245. //
  246. // The exact value should round to `expected_half`, which must be either
  247. // `expected_low` or `expected_high`.
  248. template <typename FloatType>
  249. void TestHalfwayValue(const std::string& mantissa, int exponent,
  250. FloatType expected_low, FloatType expected_high,
  251. FloatType expected_half) {
  252. std::string low_rep = mantissa;
  253. low_rep[low_rep.size() - 1] -= 1;
  254. absl::StrAppend(&low_rep, std::string(1000, '9'), "e", exponent);
  255. FloatType actual_low = 0;
  256. absl::from_chars(low_rep.data(), low_rep.data() + low_rep.size(), actual_low);
  257. EXPECT_EQ(expected_low, actual_low);
  258. std::string high_rep =
  259. absl::StrCat(mantissa, std::string(1000, '0'), "1e", exponent);
  260. FloatType actual_high = 0;
  261. absl::from_chars(high_rep.data(), high_rep.data() + high_rep.size(),
  262. actual_high);
  263. EXPECT_EQ(expected_high, actual_high);
  264. std::string halfway_rep = absl::StrCat(mantissa, "e", exponent);
  265. FloatType actual_half = 0;
  266. absl::from_chars(halfway_rep.data(), halfway_rep.data() + halfway_rep.size(),
  267. actual_half);
  268. EXPECT_EQ(expected_half, actual_half);
  269. }
  270. TEST(FromChars, DoubleRounding) {
  271. const double zero = 0.0;
  272. const double first_subnormal = nextafter(zero, 1.0);
  273. const double second_subnormal = nextafter(first_subnormal, 1.0);
  274. const double first_normal = DBL_MIN;
  275. const double last_subnormal = nextafter(first_normal, 0.0);
  276. const double second_normal = nextafter(first_normal, 1.0);
  277. const double last_normal = DBL_MAX;
  278. const double penultimate_normal = nextafter(last_normal, 0.0);
  279. // Various test cases for numbers between two representable floats. Each
  280. // call to TestHalfwayValue tests a number just below and just above the
  281. // halfway point, as well as the number exactly between them.
  282. // Test between zero and first_subnormal. Round-to-even tie rounds down.
  283. TestHalfwayValue(
  284. "2."
  285. "470328229206232720882843964341106861825299013071623822127928412503377536"
  286. "351043759326499181808179961898982823477228588654633283551779698981993873"
  287. "980053909390631503565951557022639229085839244910518443593180284993653615"
  288. "250031937045767824921936562366986365848075700158576926990370631192827955"
  289. "855133292783433840935197801553124659726357957462276646527282722005637400"
  290. "648549997709659947045402082816622623785739345073633900796776193057750674"
  291. "017632467360096895134053553745851666113422376667860416215968046191446729"
  292. "184030053005753084904876539171138659164623952491262365388187963623937328"
  293. "042389101867234849766823508986338858792562830275599565752445550725518931"
  294. "369083625477918694866799496832404970582102851318545139621383772282614543"
  295. "7693412532098591327667236328125",
  296. -324, zero, first_subnormal, zero);
  297. // first_subnormal and second_subnormal. Round-to-even tie rounds up.
  298. TestHalfwayValue(
  299. "7."
  300. "410984687618698162648531893023320585475897039214871466383785237510132609"
  301. "053131277979497545424539885696948470431685765963899850655339096945981621"
  302. "940161728171894510697854671067917687257517734731555330779540854980960845"
  303. "750095811137303474765809687100959097544227100475730780971111893578483867"
  304. "565399878350301522805593404659373979179073872386829939581848166016912201"
  305. "945649993128979841136206248449867871357218035220901702390328579173252022"
  306. "052897402080290685402160661237554998340267130003581248647904138574340187"
  307. "552090159017259254714629617513415977493871857473787096164563890871811984"
  308. "127167305601704549300470526959016576377688490826798697257336652176556794"
  309. "107250876433756084600398490497214911746308553955635418864151316847843631"
  310. "3080237596295773983001708984375",
  311. -324, first_subnormal, second_subnormal, second_subnormal);
  312. // last_subnormal and first_normal. Round-to-even tie rounds up.
  313. TestHalfwayValue(
  314. "2."
  315. "225073858507201136057409796709131975934819546351645648023426109724822222"
  316. "021076945516529523908135087914149158913039621106870086438694594645527657"
  317. "207407820621743379988141063267329253552286881372149012981122451451889849"
  318. "057222307285255133155755015914397476397983411801999323962548289017107081"
  319. "850690630666655994938275772572015763062690663332647565300009245888316433"
  320. "037779791869612049497390377829704905051080609940730262937128958950003583"
  321. "799967207254304360284078895771796150945516748243471030702609144621572289"
  322. "880258182545180325707018860872113128079512233426288368622321503775666622"
  323. "503982534335974568884423900265498198385487948292206894721689831099698365"
  324. "846814022854243330660339850886445804001034933970427567186443383770486037"
  325. "86162277173854562306587467901408672332763671875",
  326. -308, last_subnormal, first_normal, first_normal);
  327. // first_normal and second_normal. Round-to-even tie rounds down.
  328. TestHalfwayValue(
  329. "2."
  330. "225073858507201630123055637955676152503612414573018013083228724049586647"
  331. "606759446192036794116886953213985520549032000903434781884412325572184367"
  332. "563347617020518175998922941393629966742598285899994830148971433555578567"
  333. "693279306015978183162142425067962460785295885199272493577688320732492479"
  334. "924816869232247165964934329258783950102250973957579510571600738343645738"
  335. "494324192997092179207389919761694314131497173265255020084997973676783743"
  336. "155205818804439163810572367791175177756227497413804253387084478193655533"
  337. "073867420834526162513029462022730109054820067654020201547112002028139700"
  338. "141575259123440177362244273712468151750189745559978653234255886219611516"
  339. "335924167958029604477064946470184777360934300451421683607013647479513962"
  340. "13837722826145437693412532098591327667236328125",
  341. -308, first_normal, second_normal, first_normal);
  342. // penultimate_normal and last_normal. Round-to-even rounds down.
  343. TestHalfwayValue(
  344. "1."
  345. "797693134862315608353258760581052985162070023416521662616611746258695532"
  346. "672923265745300992879465492467506314903358770175220871059269879629062776"
  347. "047355692132901909191523941804762171253349609463563872612866401980290377"
  348. "995141836029815117562837277714038305214839639239356331336428021390916694"
  349. "57927874464075218944",
  350. 308, penultimate_normal, last_normal, penultimate_normal);
  351. }
  352. // Same test cases as DoubleRounding, now with new and improved Much Smaller
  353. // Precision!
  354. TEST(FromChars, FloatRounding) {
  355. const float zero = 0.0;
  356. const float first_subnormal = nextafterf(zero, 1.0);
  357. const float second_subnormal = nextafterf(first_subnormal, 1.0);
  358. const float first_normal = FLT_MIN;
  359. const float last_subnormal = nextafterf(first_normal, 0.0);
  360. const float second_normal = nextafterf(first_normal, 1.0);
  361. const float last_normal = FLT_MAX;
  362. const float penultimate_normal = nextafterf(last_normal, 0.0);
  363. // Test between zero and first_subnormal. Round-to-even tie rounds down.
  364. TestHalfwayValue(
  365. "7."
  366. "006492321624085354618647916449580656401309709382578858785341419448955413"
  367. "42930300743319094181060791015625",
  368. -46, zero, first_subnormal, zero);
  369. // first_subnormal and second_subnormal. Round-to-even tie rounds up.
  370. TestHalfwayValue(
  371. "2."
  372. "101947696487225606385594374934874196920392912814773657635602425834686624"
  373. "028790902229957282543182373046875",
  374. -45, first_subnormal, second_subnormal, second_subnormal);
  375. // last_subnormal and first_normal. Round-to-even tie rounds up.
  376. TestHalfwayValue(
  377. "1."
  378. "175494280757364291727882991035766513322858992758990427682963118425003064"
  379. "9651730385585324256680905818939208984375",
  380. -38, last_subnormal, first_normal, first_normal);
  381. // first_normal and second_normal. Round-to-even tie rounds down.
  382. TestHalfwayValue(
  383. "1."
  384. "175494420887210724209590083408724842314472120785184615334540294131831453"
  385. "9442813071445925743319094181060791015625",
  386. -38, first_normal, second_normal, first_normal);
  387. // penultimate_normal and last_normal. Round-to-even rounds down.
  388. TestHalfwayValue("3.40282336497324057985868971510891282432", 38,
  389. penultimate_normal, last_normal, penultimate_normal);
  390. }
  391. TEST(FromChars, Underflow) {
  392. // Check that underflow is handled correctly, according to the specification
  393. // in DR 3081.
  394. double d;
  395. float f;
  396. absl::from_chars_result result;
  397. std::string negative_underflow = "-1e-1000";
  398. const char* begin = negative_underflow.data();
  399. const char* end = begin + negative_underflow.size();
  400. d = 100.0;
  401. result = absl::from_chars(begin, end, d);
  402. EXPECT_EQ(result.ptr, end);
  403. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  404. EXPECT_TRUE(std::signbit(d)); // negative
  405. EXPECT_GE(d, -std::numeric_limits<double>::min());
  406. f = 100.0;
  407. result = absl::from_chars(begin, end, f);
  408. EXPECT_EQ(result.ptr, end);
  409. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  410. EXPECT_TRUE(std::signbit(f)); // negative
  411. EXPECT_GE(f, -std::numeric_limits<float>::min());
  412. std::string positive_underflow = "1e-1000";
  413. begin = positive_underflow.data();
  414. end = begin + positive_underflow.size();
  415. d = -100.0;
  416. result = absl::from_chars(begin, end, d);
  417. EXPECT_EQ(result.ptr, end);
  418. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  419. EXPECT_FALSE(std::signbit(d)); // positive
  420. EXPECT_LE(d, std::numeric_limits<double>::min());
  421. f = -100.0;
  422. result = absl::from_chars(begin, end, f);
  423. EXPECT_EQ(result.ptr, end);
  424. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  425. EXPECT_FALSE(std::signbit(f)); // positive
  426. EXPECT_LE(f, std::numeric_limits<float>::min());
  427. }
  428. TEST(FromChars, Overflow) {
  429. // Check that overflow is handled correctly, according to the specification
  430. // in DR 3081.
  431. double d;
  432. float f;
  433. absl::from_chars_result result;
  434. std::string negative_overflow = "-1e1000";
  435. const char* begin = negative_overflow.data();
  436. const char* end = begin + negative_overflow.size();
  437. d = 100.0;
  438. result = absl::from_chars(begin, end, d);
  439. EXPECT_EQ(result.ptr, end);
  440. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  441. EXPECT_TRUE(std::signbit(d)); // negative
  442. EXPECT_EQ(d, -std::numeric_limits<double>::max());
  443. f = 100.0;
  444. result = absl::from_chars(begin, end, f);
  445. EXPECT_EQ(result.ptr, end);
  446. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  447. EXPECT_TRUE(std::signbit(f)); // negative
  448. EXPECT_EQ(f, -std::numeric_limits<float>::max());
  449. std::string positive_overflow = "1e1000";
  450. begin = positive_overflow.data();
  451. end = begin + positive_overflow.size();
  452. d = -100.0;
  453. result = absl::from_chars(begin, end, d);
  454. EXPECT_EQ(result.ptr, end);
  455. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  456. EXPECT_FALSE(std::signbit(d)); // positive
  457. EXPECT_EQ(d, std::numeric_limits<double>::max());
  458. f = -100.0;
  459. result = absl::from_chars(begin, end, f);
  460. EXPECT_EQ(result.ptr, end);
  461. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  462. EXPECT_FALSE(std::signbit(f)); // positive
  463. EXPECT_EQ(f, std::numeric_limits<float>::max());
  464. }
  465. TEST(FromChars, RegressionTestsFromFuzzer) {
  466. absl::string_view src = "0x21900000p00000000099";
  467. float f;
  468. auto result = absl::from_chars(src.data(), src.data() + src.size(), f);
  469. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  470. }
  471. TEST(FromChars, ReturnValuePtr) {
  472. // Check that `ptr` points one past the number scanned, even if that number
  473. // is not representable.
  474. double d;
  475. absl::from_chars_result result;
  476. std::string normal = "3.14@#$%@#$%";
  477. result = absl::from_chars(normal.data(), normal.data() + normal.size(), d);
  478. EXPECT_EQ(result.ec, std::errc());
  479. EXPECT_EQ(result.ptr - normal.data(), 4);
  480. std::string overflow = "1e1000@#$%@#$%";
  481. result = absl::from_chars(overflow.data(),
  482. overflow.data() + overflow.size(), d);
  483. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  484. EXPECT_EQ(result.ptr - overflow.data(), 6);
  485. std::string garbage = "#$%@#$%";
  486. result = absl::from_chars(garbage.data(),
  487. garbage.data() + garbage.size(), d);
  488. EXPECT_EQ(result.ec, std::errc::invalid_argument);
  489. EXPECT_EQ(result.ptr - garbage.data(), 0);
  490. }
  491. // Check for a wide range of inputs that strtod() and absl::from_chars() exactly
  492. // agree on the conversion amount.
  493. //
  494. // This test assumes the platform's strtod() uses perfect round_to_nearest
  495. // rounding.
  496. TEST(FromChars, TestVersusStrtod) {
  497. for (int mantissa = 1000000; mantissa <= 9999999; mantissa += 501) {
  498. for (int exponent = -300; exponent < 300; ++exponent) {
  499. std::string candidate = absl::StrCat(mantissa, "e", exponent);
  500. double strtod_value = strtod(candidate.c_str(), nullptr);
  501. double absl_value = 0;
  502. absl::from_chars(candidate.data(), candidate.data() + candidate.size(),
  503. absl_value);
  504. ASSERT_EQ(strtod_value, absl_value) << candidate;
  505. }
  506. }
  507. }
  508. // Check for a wide range of inputs that strtof() and absl::from_chars() exactly
  509. // agree on the conversion amount.
  510. //
  511. // This test assumes the platform's strtof() uses perfect round_to_nearest
  512. // rounding.
  513. TEST(FromChars, TestVersusStrtof) {
  514. for (int mantissa = 1000000; mantissa <= 9999999; mantissa += 501) {
  515. for (int exponent = -43; exponent < 32; ++exponent) {
  516. std::string candidate = absl::StrCat(mantissa, "e", exponent);
  517. float strtod_value = strtof(candidate.c_str(), nullptr);
  518. float absl_value = 0;
  519. absl::from_chars(candidate.data(), candidate.data() + candidate.size(),
  520. absl_value);
  521. ASSERT_EQ(strtod_value, absl_value) << candidate;
  522. }
  523. }
  524. }
  525. // Tests if two floating point values have identical bit layouts. (EXPECT_EQ
  526. // is not suitable for NaN testing, since NaNs are never equal.)
  527. template <typename Float>
  528. bool Identical(Float a, Float b) {
  529. return 0 == memcmp(&a, &b, sizeof(Float));
  530. }
  531. // Check that NaNs are parsed correctly. The spec requires that
  532. // std::from_chars on "NaN(123abc)" return the same value as std::nan("123abc").
  533. // How such an n-char-sequence affects the generated NaN is unspecified, so we
  534. // just test for symmetry with std::nan and strtod here.
  535. //
  536. // (In Linux, this parses the value as a number and stuffs that number into the
  537. // free bits of a quiet NaN.)
  538. TEST(FromChars, NaNDoubles) {
  539. for (std::string n_char_sequence :
  540. {"", "1", "2", "3", "fff", "FFF", "200000", "400000", "4000000000000",
  541. "8000000000000", "abc123", "legal_but_unexpected",
  542. "99999999999999999999999", "_"}) {
  543. std::string input = absl::StrCat("nan(", n_char_sequence, ")");
  544. SCOPED_TRACE(input);
  545. double from_chars_double;
  546. absl::from_chars(input.data(), input.data() + input.size(),
  547. from_chars_double);
  548. double std_nan_double = std::nan(n_char_sequence.c_str());
  549. EXPECT_TRUE(Identical(from_chars_double, std_nan_double));
  550. // Also check that we match strtod()'s behavior. This test assumes that the
  551. // platform has a compliant strtod().
  552. #if ABSL_STRTOD_HANDLES_NAN_CORRECTLY
  553. double strtod_double = strtod(input.c_str(), nullptr);
  554. EXPECT_TRUE(Identical(from_chars_double, strtod_double));
  555. #endif // ABSL_STRTOD_HANDLES_NAN_CORRECTLY
  556. // Check that we can parse a negative NaN
  557. std::string negative_input = "-" + input;
  558. double negative_from_chars_double;
  559. absl::from_chars(negative_input.data(),
  560. negative_input.data() + negative_input.size(),
  561. negative_from_chars_double);
  562. EXPECT_TRUE(std::signbit(negative_from_chars_double));
  563. EXPECT_FALSE(Identical(negative_from_chars_double, from_chars_double));
  564. from_chars_double = std::copysign(from_chars_double, -1.0);
  565. EXPECT_TRUE(Identical(negative_from_chars_double, from_chars_double));
  566. }
  567. }
  568. TEST(FromChars, NaNFloats) {
  569. for (std::string n_char_sequence :
  570. {"", "1", "2", "3", "fff", "FFF", "200000", "400000", "4000000000000",
  571. "8000000000000", "abc123", "legal_but_unexpected",
  572. "99999999999999999999999", "_"}) {
  573. std::string input = absl::StrCat("nan(", n_char_sequence, ")");
  574. SCOPED_TRACE(input);
  575. float from_chars_float;
  576. absl::from_chars(input.data(), input.data() + input.size(),
  577. from_chars_float);
  578. float std_nan_float = std::nanf(n_char_sequence.c_str());
  579. EXPECT_TRUE(Identical(from_chars_float, std_nan_float));
  580. // Also check that we match strtof()'s behavior. This test assumes that the
  581. // platform has a compliant strtof().
  582. #if ABSL_STRTOD_HANDLES_NAN_CORRECTLY
  583. float strtof_float = strtof(input.c_str(), nullptr);
  584. EXPECT_TRUE(Identical(from_chars_float, strtof_float));
  585. #endif // ABSL_STRTOD_HANDLES_NAN_CORRECTLY
  586. // Check that we can parse a negative NaN
  587. std::string negative_input = "-" + input;
  588. float negative_from_chars_float;
  589. absl::from_chars(negative_input.data(),
  590. negative_input.data() + negative_input.size(),
  591. negative_from_chars_float);
  592. EXPECT_TRUE(std::signbit(negative_from_chars_float));
  593. EXPECT_FALSE(Identical(negative_from_chars_float, from_chars_float));
  594. // Use the (float, float) overload of std::copysign to prevent narrowing;
  595. // see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98251.
  596. from_chars_float = std::copysign(from_chars_float, -1.0f);
  597. EXPECT_TRUE(Identical(negative_from_chars_float, from_chars_float));
  598. }
  599. }
  600. // Returns an integer larger than step. The values grow exponentially.
  601. int NextStep(int step) {
  602. return step + (step >> 2) + 1;
  603. }
  604. // Test a conversion on a family of input strings, checking that the calculation
  605. // is correct for in-bounds values, and that overflow and underflow are done
  606. // correctly for out-of-bounds values.
  607. //
  608. // input_generator maps from an integer index to a string to test.
  609. // expected_generator maps from an integer index to an expected Float value.
  610. // from_chars conversion of input_generator(i) should result in
  611. // expected_generator(i).
  612. //
  613. // lower_bound and upper_bound denote the smallest and largest values for which
  614. // the conversion is expected to succeed.
  615. template <typename Float>
  616. void TestOverflowAndUnderflow(
  617. const std::function<std::string(int)>& input_generator,
  618. const std::function<Float(int)>& expected_generator, int lower_bound,
  619. int upper_bound) {
  620. // test legal values near lower_bound
  621. int index, step;
  622. for (index = lower_bound, step = 1; index < upper_bound;
  623. index += step, step = NextStep(step)) {
  624. std::string input = input_generator(index);
  625. SCOPED_TRACE(input);
  626. Float expected = expected_generator(index);
  627. Float actual;
  628. auto result =
  629. absl::from_chars(input.data(), input.data() + input.size(), actual);
  630. EXPECT_EQ(result.ec, std::errc());
  631. EXPECT_EQ(expected, actual)
  632. << absl::StrFormat("%a vs %a", expected, actual);
  633. }
  634. // test legal values near upper_bound
  635. for (index = upper_bound, step = 1; index > lower_bound;
  636. index -= step, step = NextStep(step)) {
  637. std::string input = input_generator(index);
  638. SCOPED_TRACE(input);
  639. Float expected = expected_generator(index);
  640. Float actual;
  641. auto result =
  642. absl::from_chars(input.data(), input.data() + input.size(), actual);
  643. EXPECT_EQ(result.ec, std::errc());
  644. EXPECT_EQ(expected, actual)
  645. << absl::StrFormat("%a vs %a", expected, actual);
  646. }
  647. // Test underflow values below lower_bound
  648. for (index = lower_bound - 1, step = 1; index > -1000000;
  649. index -= step, step = NextStep(step)) {
  650. std::string input = input_generator(index);
  651. SCOPED_TRACE(input);
  652. Float actual;
  653. auto result =
  654. absl::from_chars(input.data(), input.data() + input.size(), actual);
  655. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  656. EXPECT_LT(actual, 1.0); // check for underflow
  657. }
  658. // Test overflow values above upper_bound
  659. for (index = upper_bound + 1, step = 1; index < 1000000;
  660. index += step, step = NextStep(step)) {
  661. std::string input = input_generator(index);
  662. SCOPED_TRACE(input);
  663. Float actual;
  664. auto result =
  665. absl::from_chars(input.data(), input.data() + input.size(), actual);
  666. EXPECT_EQ(result.ec, std::errc::result_out_of_range);
  667. EXPECT_GT(actual, 1.0); // check for overflow
  668. }
  669. }
  670. // Check that overflow and underflow are caught correctly for hex doubles.
  671. //
  672. // The largest representable double is 0x1.fffffffffffffp+1023, and the
  673. // smallest representable subnormal is 0x0.0000000000001p-1022, which equals
  674. // 0x1p-1074. Therefore 1023 and -1074 are the limits of acceptable exponents
  675. // in this test.
  676. TEST(FromChars, HexdecimalDoubleLimits) {
  677. auto input_gen = [](int index) { return absl::StrCat("0x1.0p", index); };
  678. auto expected_gen = [](int index) { return std::ldexp(1.0, index); };
  679. TestOverflowAndUnderflow<double>(input_gen, expected_gen, -1074, 1023);
  680. }
  681. // Check that overflow and underflow are caught correctly for hex floats.
  682. //
  683. // The largest representable float is 0x1.fffffep+127, and the smallest
  684. // representable subnormal is 0x0.000002p-126, which equals 0x1p-149.
  685. // Therefore 127 and -149 are the limits of acceptable exponents in this test.
  686. TEST(FromChars, HexdecimalFloatLimits) {
  687. auto input_gen = [](int index) { return absl::StrCat("0x1.0p", index); };
  688. auto expected_gen = [](int index) { return std::ldexp(1.0f, index); };
  689. TestOverflowAndUnderflow<float>(input_gen, expected_gen, -149, 127);
  690. }
  691. // Check that overflow and underflow are caught correctly for decimal doubles.
  692. //
  693. // The largest representable double is about 1.8e308, and the smallest
  694. // representable subnormal is about 5e-324. '1e-324' therefore rounds away from
  695. // the smallest representable positive value. -323 and 308 are the limits of
  696. // acceptable exponents in this test.
  697. TEST(FromChars, DecimalDoubleLimits) {
  698. auto input_gen = [](int index) { return absl::StrCat("1.0e", index); };
  699. auto expected_gen = [](int index) { return Pow10(index); };
  700. TestOverflowAndUnderflow<double>(input_gen, expected_gen, -323, 308);
  701. }
  702. // Check that overflow and underflow are caught correctly for decimal floats.
  703. //
  704. // The largest representable float is about 3.4e38, and the smallest
  705. // representable subnormal is about 1.45e-45. '1e-45' therefore rounds towards
  706. // the smallest representable positive value. -45 and 38 are the limits of
  707. // acceptable exponents in this test.
  708. TEST(FromChars, DecimalFloatLimits) {
  709. auto input_gen = [](int index) { return absl::StrCat("1.0e", index); };
  710. auto expected_gen = [](int index) { return Pow10(index); };
  711. TestOverflowAndUnderflow<float>(input_gen, expected_gen, -45, 38);
  712. }
  713. } // namespace