Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/serde_json/tests/lexical/parse.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / serde_json / tests / lexical / parse.rs` 204 lines ยท 17147 bytes Raw Blame History 1 // Adapted from https://github.com/Alexhuszagh/rust-lexical. 2 3 use crate::lexical::num::Float; 4 use crate::lexical::{parse_concise_float, parse_truncated_float}; 5 use core::f64; 6 use core::fmt::Debug; 7 8 fn check_concise_float(mantissa: u64, exponent: i32, expected: F) 9 where 10 F: Float + Debug, 11 { 12 assert_eq!(parse_concise_float::(mantissa, exponent), expected); 13 } 14 15 fn check_truncated_float(integer: &str, fraction: &str, exponent: i32, expected: F) 16 where 17 F: Float + Debug, 18 { 19 let integer = integer.as_bytes(); 20 let fraction = fraction.as_bytes(); 21 assert_eq!( 22 parse_truncated_float::(integer, fraction, exponent), 23 expected, 24 ); 25 } 26 27 #[test] 28 fn parse_f32_test() { 29 check_concise_float(0, 0, 0.0_f32); 30 check_concise_float(12345, -4, 1.2345_f32); 31 check_concise_float(12345, -3, 12.345_f32); 32 check_concise_float(`123456789`, -4, 12345.6789_f32); 33 check_concise_float(12345, 6, 1.`2345e10`_f32); 34 check_concise_float(12345, -42, 1.2345e-38_f32); 35 36 // Check expected rounding, using borderline cases. 37 // Round-down, halfway 38 check_concise_float(`16777216`, 0, `16777216`.0_f32); 39 check_concise_float(`16777217`, 0, `16777216`.0_f32); 40 check_concise_float(`16777218`, 0, `16777218`.0_f32); 41 check_concise_float(`33554432`, 0, `33554432`.0_f32); 42 check_concise_float(`33554434`, 0, `33554432`.0_f32); 43 check_concise_float(`33554436`, 0, `33554436`.0_f32); 44 check_concise_float(`17179869184`, 0, `17179869184`.0_f32); 45 check_concise_float(`17179870208`, 0, `17179869184`.0_f32); 46 check_concise_float(`17179871232`, 0, `17179871232`.0_f32); 47 48 // Round-up, halfway 49 check_concise_float(`16777218`, 0, `16777218`.0_f32); 50 check_concise_float(`16777219`, 0, `16777220`.0_f32); 51 check_concise_float(`16777220`, 0, `16777220`.0_f32); 52 53 check_concise_float(`33554436`, 0, `33554436`.0_f32); 54 check_concise_float(`33554438`, 0, `33554440`.0_f32); 55 check_concise_float(`33554440`, 0, `33554440`.0_f32); 56 57 check_concise_float(`17179871232`, 0, `17179871232`.0_f32); 58 check_concise_float(`17179872256`, 0, `17179873280`.0_f32); 59 check_concise_float(`17179873280`, 0, `17179873280`.0_f32); 60 61 // Round-up, above halfway 62 check_concise_float(`33554435`, 0, `33554436`.0_f32); 63 check_concise_float(`17179870209`, 0, `17179871232`.0_f32); 64 65 // Check exactly halfway, round-up at halfway 66 check_truncated_float("1", "`00000017881393432617187499`", 0, 1.`0000001`_f32); 67 check_truncated_float("1", "`000000178813934326171875`", 0, 1.`0000002`_f32); 68 check_truncated_float("1", "`00000017881393432617187501`", 0, 1.`0000002`_f32); 69 } 70 71 #[test] 72 fn parse_f64_test() { 73 check_concise_float(0, 0, 0.0_f64); 74 check_concise_float(12345, -4, 1.2345_f64); 75 check_concise_float(12345, -3, 12.345_f64); 76 check_concise_float(`123456789`, -4, 12345.6789_f64); 77 check_concise_float(12345, 6, 1.`2345e10`_f64); 78 check_concise_float(12345, -312, 1.2345e-308_f64); 79 80 // Check expected rounding, using borderline cases. 81 // Round-down, halfway 82 check_concise_float(`9007199254740992`, 0, `9007199254740992`.0_f64); 83 check_concise_float(`9007199254740993`, 0, `9007199254740992`.0_f64); 84 check_concise_float(`9007199254740994`, 0, `9007199254740994`.0_f64); 85 86 check_concise_float(`18014398509481984`, 0, `18014398509481984`.0_f64); 87 check_concise_float(`18014398509481986`, 0, `18014398509481984`.0_f64); 88 check_concise_float(`18014398509481988`, 0, `18014398509481988`.0_f64); 89 90 check_concise_float(`9223372036854775808`, 0, `9223372036854775808`.0_f64); 91 check_concise_float(`9223372036854776832`, 0, `9223372036854775808`.0_f64); 92 check_concise_float(`9223372036854777856`, 0, `9223372036854777856`.0_f64); 93 94 check_truncated_float( 95 "`11417981541647679048466287755595961091061972992`", 96 "", 97 0, 98 `11417981541647679048466287755595961091061972992`.0_f64, 99 ); 100 check_truncated_float( 101 "`11417981541647680316116887983825362587765178368`", 102 "", 103 0, 104 `11417981541647679048466287755595961091061972992`.0_f64, 105 ); 106 check_truncated_float( 107 "`11417981541647681583767488212054764084468383744`", 108 "", 109 0, 110 `11417981541647681583767488212054764084468383744`.0_f64, 111 ); 112 113 // Round-up, halfway 114 check_concise_float(`9007199254740994`, 0, `9007199254740994`.0_f64); 115 check_concise_float(`9007199254740995`, 0, `9007199254740996`.0_f64); 116 check_concise_float(`9007199254740996`, 0, `9007199254740996`.0_f64); 117 118 check_concise_float(`18014398509481988`, 0, `18014398509481988`.0_f64); 119 check_concise_float(`18014398509481990`, 0, `18014398509481992`.0_f64); 120 check_concise_float(`18014398509481992`, 0, `18014398509481992`.0_f64); 121 122 check_concise_float(`9223372036854777856`, 0, `9223372036854777856`.0_f64); 123 check_concise_float(`9223372036854778880`, 0, `9223372036854779904`.0_f64); 124 check_concise_float(`9223372036854779904`, 0, `9223372036854779904`.0_f64); 125 126 check_truncated_float( 127 "`11417981541647681583767488212054764084468383744`", 128 "", 129 0, 130 `11417981541647681583767488212054764084468383744`.0_f64, 131 ); 132 check_truncated_float( 133 "`11417981541647682851418088440284165581171589120`", 134 "", 135 0, 136 `11417981541647684119068688668513567077874794496`.0_f64, 137 ); 138 check_truncated_float( 139 "`11417981541647684119068688668513567077874794496`", 140 "", 141 0, 142 `11417981541647684119068688668513567077874794496`.0_f64, 143 ); 144 145 // Round-up, above halfway 146 check_concise_float(`9223372036854776833`, 0, `9223372036854777856`.0_f64); 147 check_truncated_float( 148 "`11417981541647680316116887983825362587765178369`", 149 "", 150 0, 151 `11417981541647681583767488212054764084468383744`.0_f64, 152 ); 153 154 // Rounding error 155 // Adapted from failures in strtod. 156 check_concise_float(`22250738585072014`, -324, 2.`2250738585072014e`-308_f64); 157 check_truncated_float("2", "`2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187499`", -308, 2.`225073858507201e`-308_f64); 158 check_truncated_float("2", "`22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508791414915891303962110687008643869459464552765720740782062174337998814106326732925355228688137214901298112245145188984905722230728525513315575501591439747639798341180199932396254828901710708185069063066665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505108060994073026293712895895000358379996720725430436028407889577179615094551674824347103070260914462157228988025818254518032570701886087211312807951223342628836862232150377566662250398253433597456888442390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718644338377048603786162277173854562306587467901408672332763671875`", -308, 2.`2250738585072014e`-308_f64); 159 check_truncated_float("2", "`2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187501`", -308, 2.`2250738585072014e`-308_f64); 160 check_truncated_float("`179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791`", "`9999999999999999999999999999999999999999999999999999999999999999999999`", 0, 1.`7976931348623157e`+308_f64); 161 check_truncated_float("7", "`4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984374999`", -324, 5.0e-324_f64); 162 check_truncated_float("7", "`4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375`", -324, 1.0e-323_f64); 163 check_truncated_float("7", "`4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375001`", -324, 1.0e-323_f64); 164 check_truncated_float("", "`0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125`", 0, 0.0_f64); 165 166 // Rounding error 167 // Adapted from: 168 // https://www.exploringbinary.com/how-glibc-strtod-works/ 169 check_truncated_float("", "`000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375`", 0, 2.`2250738585072011e`-308_f64); 170 171 // Rounding error 172 // Adapted from test-parse-random failures. 173 check_concise_float(1009, -31, 1.009e-28_f64); 174 check_concise_float(18294, 304, f64::INFINITY); 175 176 // Rounding error 177 // Adapted from a @dangrabcad's issue #20. 178 check_concise_float(`7689539722041643`, 149, 7.`689539722041643e164`_f64); 179 check_truncated_float("`768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`", "", 0, 7.`689539722041643e164`_f64); 180 check_truncated_float("`768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`", "0", 0, 7.`689539722041643e164`_f64); 181 182 // Check other cases similar to @dangrabcad's issue #20. 183 check_truncated_float("`9223372036854776833`", "0", 0, `9223372036854777856`.0_f64); 184 check_truncated_float( 185 "`11417981541647680316116887983825362587765178369`", 186 "0", 187 0, 188 `11417981541647681583767488212054764084468383744`.0_f64, 189 ); 190 check_concise_float(`90071992547409950`, -1, `9007199254740996`.0_f64); 191 check_concise_float(`180143985094819900`, -1, `18014398509481992`.0_f64); 192 check_truncated_float("`9223372036854778880`", "0", 0, `9223372036854779904`.0_f64); 193 check_truncated_float( 194 "`11417981541647682851418088440284165581171589120`", 195 "0", 196 0, 197 `11417981541647684119068688668513567077874794496`.0_f64, 198 ); 199 200 // Check other cases ostensibly identified via proptest. 201 check_truncated_float("`71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`", "0", 0, `71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`.0_f64); 202 check_truncated_float("`126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`", "0", 0, `126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`.0_f64); 203 check_truncated_float("`38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`", "0", 0, `38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000`.0_f64); 204 }