| 1 |
use core::f64; |
| 2 |
use minimal_lexical::{num, parse}; |
| 3 |
|
| 4 |
fn check_parse_float<F: num::Float>(integer: &str, fraction: &str, exponent: i32, expected: F) { |
| 5 |
let integer = integer.as_bytes().iter(); |
| 6 |
let fraction = fraction.as_bytes().iter(); |
| 7 |
assert!(expected == parse::parse_float::<F, _, _>(integer, fraction, exponent)); |
| 8 |
} |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn parse_f32_test() { |
| 12 |
check_parse_float("", "", 0, 0.0_f32); |
| 13 |
check_parse_float("1", "2345", 0, 1.2345_f32); |
| 14 |
check_parse_float("12", "345", 0, 12.345_f32); |
| 15 |
check_parse_float("12345", "6789", 0, 12345.6789_f32); |
| 16 |
check_parse_float("1", "2345", 10, 1.2345e10_f32); |
| 17 |
check_parse_float("1", "2345", -38, 1.2345e-38_f32); |
| 18 |
|
| 19 |
// Check expected rounding, using borderline cases. |
| 20 |
// Round-down, halfway |
| 21 |
check_parse_float("16777216", "", 0, 16777216.0_f32); |
| 22 |
check_parse_float("16777217", "", 0, 16777216.0_f32); |
| 23 |
check_parse_float("16777218", "", 0, 16777218.0_f32); |
| 24 |
check_parse_float("33554432", "", 0, 33554432.0_f32); |
| 25 |
check_parse_float("33554434", "", 0, 33554432.0_f32); |
| 26 |
check_parse_float("33554436", "", 0, 33554436.0_f32); |
| 27 |
check_parse_float("17179869184", "", 0, 17179869184.0_f32); |
| 28 |
check_parse_float("17179870208", "", 0, 17179869184.0_f32); |
| 29 |
check_parse_float("17179871232", "", 0, 17179871232.0_f32); |
| 30 |
|
| 31 |
// Round-up, halfway |
| 32 |
check_parse_float("16777218", "", 0, 16777218.0_f32); |
| 33 |
check_parse_float("16777219", "", 0, 16777220.0_f32); |
| 34 |
check_parse_float("16777220", "", 0, 16777220.0_f32); |
| 35 |
|
| 36 |
check_parse_float("33554436", "", 0, 33554436.0_f32); |
| 37 |
check_parse_float("33554438", "", 0, 33554440.0_f32); |
| 38 |
check_parse_float("33554440", "", 0, 33554440.0_f32); |
| 39 |
|
| 40 |
check_parse_float("17179871232", "", 0, 17179871232.0_f32); |
| 41 |
check_parse_float("17179872256", "", 0, 17179873280.0_f32); |
| 42 |
check_parse_float("17179873280", "", 0, 17179873280.0_f32); |
| 43 |
|
| 44 |
// Round-up, above halfway |
| 45 |
check_parse_float("33554435", "", 0, 33554436.0_f32); |
| 46 |
check_parse_float("17179870209", "", 0, 17179871232.0_f32); |
| 47 |
|
| 48 |
// Check exactly halfway, round-up at halfway |
| 49 |
check_parse_float("1", "00000017881393432617187499", 0, 1.0000001_f32); |
| 50 |
check_parse_float("1", "000000178813934326171875", 0, 1.0000002_f32); |
| 51 |
check_parse_float("1", "00000017881393432617187501", 0, 1.0000002_f32); |
| 52 |
|
| 53 |
check_parse_float("", "000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625", 0, 1.1754943508222875e-38f32); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn parse_f64_test() { |
| 58 |
check_parse_float("", "", 0, 0.0_f64); |
| 59 |
check_parse_float("1", "2345", 0, 1.2345_f64); |
| 60 |
check_parse_float("12", "345", 0, 12.345_f64); |
| 61 |
check_parse_float("12345", "6789", 0, 12345.6789_f64); |
| 62 |
check_parse_float("1", "2345", 10, 1.2345e10_f64); |
| 63 |
check_parse_float("1", "2345", -308, 1.2345e-308_f64); |
| 64 |
|
| 65 |
// Check expected rounding, using borderline cases. |
| 66 |
// Round-down, halfway |
| 67 |
check_parse_float("9007199254740992", "", 0, 9007199254740992.0_f64); |
| 68 |
check_parse_float("9007199254740993", "", 0, 9007199254740992.0_f64); |
| 69 |
check_parse_float("9007199254740994", "", 0, 9007199254740994.0_f64); |
| 70 |
|
| 71 |
check_parse_float("18014398509481984", "", 0, 18014398509481984.0_f64); |
| 72 |
check_parse_float("18014398509481986", "", 0, 18014398509481984.0_f64); |
| 73 |
check_parse_float("18014398509481988", "", 0, 18014398509481988.0_f64); |
| 74 |
|
| 75 |
check_parse_float("9223372036854775808", "", 0, 9223372036854775808.0_f64); |
| 76 |
check_parse_float("9223372036854776832", "", 0, 9223372036854775808.0_f64); |
| 77 |
check_parse_float("9223372036854777856", "", 0, 9223372036854777856.0_f64); |
| 78 |
|
| 79 |
check_parse_float( |
| 80 |
"11417981541647679048466287755595961091061972992", |
| 81 |
"", |
| 82 |
0, |
| 83 |
11417981541647679048466287755595961091061972992.0_f64, |
| 84 |
); |
| 85 |
check_parse_float( |
| 86 |
"11417981541647680316116887983825362587765178368", |
| 87 |
"", |
| 88 |
0, |
| 89 |
11417981541647679048466287755595961091061972992.0_f64, |
| 90 |
); |
| 91 |
check_parse_float( |
| 92 |
"11417981541647681583767488212054764084468383744", |
| 93 |
"", |
| 94 |
0, |
| 95 |
11417981541647681583767488212054764084468383744.0_f64, |
| 96 |
); |
| 97 |
|
| 98 |
// Round-up, halfway |
| 99 |
check_parse_float("9007199254740994", "", 0, 9007199254740994.0_f64); |
| 100 |
check_parse_float("9007199254740995", "", 0, 9007199254740996.0_f64); |
| 101 |
check_parse_float("9007199254740996", "", 0, 9007199254740996.0_f64); |
| 102 |
|
| 103 |
check_parse_float("18014398509481988", "", 0, 18014398509481988.0_f64); |
| 104 |
check_parse_float("18014398509481990", "", 0, 18014398509481992.0_f64); |
| 105 |
check_parse_float("18014398509481992", "", 0, 18014398509481992.0_f64); |
| 106 |
|
| 107 |
check_parse_float("9223372036854777856", "", 0, 9223372036854777856.0_f64); |
| 108 |
check_parse_float("9223372036854778880", "", 0, 9223372036854779904.0_f64); |
| 109 |
check_parse_float("9223372036854779904", "", 0, 9223372036854779904.0_f64); |
| 110 |
|
| 111 |
check_parse_float( |
| 112 |
"11417981541647681583767488212054764084468383744", |
| 113 |
"", |
| 114 |
0, |
| 115 |
11417981541647681583767488212054764084468383744.0_f64, |
| 116 |
); |
| 117 |
check_parse_float( |
| 118 |
"11417981541647682851418088440284165581171589120", |
| 119 |
"", |
| 120 |
0, |
| 121 |
11417981541647684119068688668513567077874794496.0_f64, |
| 122 |
); |
| 123 |
check_parse_float( |
| 124 |
"11417981541647684119068688668513567077874794496", |
| 125 |
"", |
| 126 |
0, |
| 127 |
11417981541647684119068688668513567077874794496.0_f64, |
| 128 |
); |
| 129 |
|
| 130 |
// Round-up, above halfway |
| 131 |
check_parse_float("9223372036854776833", "", 0, 9223372036854777856.0_f64); |
| 132 |
check_parse_float( |
| 133 |
"11417981541647680316116887983825362587765178369", |
| 134 |
"", |
| 135 |
0, |
| 136 |
11417981541647681583767488212054764084468383744.0_f64, |
| 137 |
); |
| 138 |
|
| 139 |
// Rounding error |
| 140 |
// Adapted from failures in strtod. |
| 141 |
check_parse_float("2", "2250738585072014", -308, 2.2250738585072014e-308_f64); |
| 142 |
check_parse_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187499", -308, 2.225073858507201e-308_f64); |
| 143 |
check_parse_float("2", "22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508791414915891303962110687008643869459464552765720740782062174337998814106326732925355228688137214901298112245145188984905722230728525513315575501591439747639798341180199932396254828901710708185069063066665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505108060994073026293712895895000358379996720725430436028407889577179615094551674824347103070260914462157228988025818254518032570701886087211312807951223342628836862232150377566662250398253433597456888442390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718644338377048603786162277173854562306587467901408672332763671875", -308, 2.2250738585072014e-308_f64); |
| 144 |
check_parse_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187501", -308, 2.2250738585072014e-308_f64); |
| 145 |
check_parse_float("179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791", "9999999999999999999999999999999999999999999999999999999999999999999999", 0, 1.7976931348623157e+308_f64); |
| 146 |
check_parse_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984374999", -324, 5.0e-324_f64); |
| 147 |
check_parse_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375", -324, 1.0e-323_f64); |
| 148 |
check_parse_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375001", -324, 1.0e-323_f64); |
| 149 |
check_parse_float("", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125", 0, 0.0_f64); |
| 150 |
|
| 151 |
// Rounding error |
| 152 |
// Adapted from: |
| 153 |
// https://www.exploringbinary.com/how-glibc-strtod-works/ |
| 154 |
check_parse_float("", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0, 2.2250738585072011e-308_f64); |
| 155 |
|
| 156 |
// Rounding error |
| 157 |
// Adapted from test-parse-random failures. |
| 158 |
check_parse_float("1009", "", -31, 1.009e-28_f64); |
| 159 |
check_parse_float("18294", "", 304, f64::INFINITY); |
| 160 |
|
| 161 |
// Rounding error |
| 162 |
// Adapted from a @dangrabcad's issue #20. |
| 163 |
check_parse_float("7", "689539722041643", 164, 7.689539722041643e164_f64); |
| 164 |
check_parse_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", 0, 7.689539722041643e164_f64); |
| 165 |
check_parse_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 7.689539722041643e164_f64); |
| 166 |
|
| 167 |
// Check other cases similar to @dangrabcad's issue #20. |
| 168 |
check_parse_float("9223372036854776833", "0", 0, 9223372036854777856.0_f64); |
| 169 |
check_parse_float( |
| 170 |
"11417981541647680316116887983825362587765178369", |
| 171 |
"0", |
| 172 |
0, |
| 173 |
11417981541647681583767488212054764084468383744.0_f64, |
| 174 |
); |
| 175 |
check_parse_float("9007199254740995", "0", 0, 9007199254740996.0_f64); |
| 176 |
check_parse_float("18014398509481990", "0", 0, 18014398509481992.0_f64); |
| 177 |
check_parse_float("9223372036854778880", "0", 0, 9223372036854779904.0_f64); |
| 178 |
check_parse_float( |
| 179 |
"11417981541647682851418088440284165581171589120", |
| 180 |
"0", |
| 181 |
0, |
| 182 |
11417981541647684119068688668513567077874794496.0_f64, |
| 183 |
); |
| 184 |
|
| 185 |
// Check other cases ostensibly identified via proptest. |
| 186 |
check_parse_float("71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); |
| 187 |
check_parse_float("126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); |
| 188 |
check_parse_float("38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64); |
| 189 |
} |