summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_json/tests/lexical
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/serde_json/tests/lexical
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_json/tests/lexical')
-rw-r--r--third_party/rust/serde_json/tests/lexical/algorithm.rs110
-rw-r--r--third_party/rust/serde_json/tests/lexical/exponent.rs54
-rw-r--r--third_party/rust/serde_json/tests/lexical/float.rs581
-rw-r--r--third_party/rust/serde_json/tests/lexical/math.rs211
-rw-r--r--third_party/rust/serde_json/tests/lexical/num.rs76
-rw-r--r--third_party/rust/serde_json/tests/lexical/parse.rs204
-rw-r--r--third_party/rust/serde_json/tests/lexical/rounding.rs316
7 files changed, 1552 insertions, 0 deletions
diff --git a/third_party/rust/serde_json/tests/lexical/algorithm.rs b/third_party/rust/serde_json/tests/lexical/algorithm.rs
new file mode 100644
index 0000000000..7f3a2c6241
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/algorithm.rs
@@ -0,0 +1,110 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::algorithm::*;
+use crate::lexical::num::Float;
+
+#[test]
+fn float_fast_path_test() {
+ // valid
+ let mantissa = (1 << f32::MANTISSA_SIZE) - 1;
+ let (min_exp, max_exp) = f32::exponent_limit();
+ for exp in min_exp..=max_exp {
+ let f = fast_path::<f32>(mantissa, exp);
+ assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
+ }
+
+ // Check slightly above valid exponents
+ let f = fast_path::<f32>(123, 15);
+ assert_eq!(f, Some(1.23e+17));
+
+ // Exponent is 1 too high, pushes over the mantissa.
+ let f = fast_path::<f32>(123, 16);
+ assert!(f.is_none());
+
+ // Mantissa is too large, checked_mul should overflow.
+ let f = fast_path::<f32>(mantissa, 11);
+ assert!(f.is_none());
+
+ // invalid exponents
+ let (min_exp, max_exp) = f32::exponent_limit();
+ let f = fast_path::<f32>(mantissa, min_exp - 1);
+ assert!(f.is_none(), "exponent under min_exp");
+
+ let f = fast_path::<f32>(mantissa, max_exp + 1);
+ assert!(f.is_none(), "exponent above max_exp");
+}
+
+#[test]
+fn double_fast_path_test() {
+ // valid
+ let mantissa = (1 << f64::MANTISSA_SIZE) - 1;
+ let (min_exp, max_exp) = f64::exponent_limit();
+ for exp in min_exp..=max_exp {
+ let f = fast_path::<f64>(mantissa, exp);
+ assert!(f.is_some(), "should be valid {:?}.", (mantissa, exp));
+ }
+
+ // invalid exponents
+ let (min_exp, max_exp) = f64::exponent_limit();
+ let f = fast_path::<f64>(mantissa, min_exp - 1);
+ assert!(f.is_none(), "exponent under min_exp");
+
+ let f = fast_path::<f64>(mantissa, max_exp + 1);
+ assert!(f.is_none(), "exponent above max_exp");
+
+ assert_eq!(
+ Some(0.04628372940652459),
+ fast_path::<f64>(4628372940652459, -17)
+ );
+ assert_eq!(None, fast_path::<f64>(26383446160308229, -272));
+}
+
+#[test]
+fn moderate_path_test() {
+ let (f, valid) = moderate_path::<f64>(1234567890, -1, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.0);
+
+ let (f, valid) = moderate_path::<f64>(1234567891, -1, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1);
+
+ let (f, valid) = moderate_path::<f64>(12345678912, -2, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12);
+
+ let (f, valid) = moderate_path::<f64>(123456789123, -3, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.123);
+
+ let (f, valid) = moderate_path::<f64>(1234567891234, -4, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1234);
+
+ let (f, valid) = moderate_path::<f64>(12345678912345, -5, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12345);
+
+ let (f, valid) = moderate_path::<f64>(123456789123456, -6, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.123456);
+
+ let (f, valid) = moderate_path::<f64>(1234567891234567, -7, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.1234567);
+
+ let (f, valid) = moderate_path::<f64>(12345678912345679, -8, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 123456789.12345679);
+
+ let (f, valid) = moderate_path::<f64>(4628372940652459, -17, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 0.04628372940652459);
+
+ let (f, valid) = moderate_path::<f64>(26383446160308229, -272, false);
+ assert!(valid, "should be valid");
+ assert_eq!(f.into_float::<f64>(), 2.6383446160308229e-256);
+
+ let (_, valid) = moderate_path::<f64>(26383446160308230, -272, false);
+ assert!(!valid, "should be invalid");
+}
diff --git a/third_party/rust/serde_json/tests/lexical/exponent.rs b/third_party/rust/serde_json/tests/lexical/exponent.rs
new file mode 100644
index 0000000000..f7a847be38
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/exponent.rs
@@ -0,0 +1,54 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::exponent::*;
+
+#[test]
+fn scientific_exponent_test() {
+ // 0 digits in the integer
+ assert_eq!(scientific_exponent(0, 0, 5), -6);
+ assert_eq!(scientific_exponent(10, 0, 5), 4);
+ assert_eq!(scientific_exponent(-10, 0, 5), -16);
+
+ // >0 digits in the integer
+ assert_eq!(scientific_exponent(0, 1, 5), 0);
+ assert_eq!(scientific_exponent(0, 2, 5), 1);
+ assert_eq!(scientific_exponent(0, 2, 20), 1);
+ assert_eq!(scientific_exponent(10, 2, 20), 11);
+ assert_eq!(scientific_exponent(-10, 2, 20), -9);
+
+ // Underflow
+ assert_eq!(
+ scientific_exponent(i32::min_value(), 0, 0),
+ i32::min_value()
+ );
+ assert_eq!(
+ scientific_exponent(i32::min_value(), 0, 5),
+ i32::min_value()
+ );
+
+ // Overflow
+ assert_eq!(
+ scientific_exponent(i32::max_value(), 0, 0),
+ i32::max_value() - 1
+ );
+ assert_eq!(
+ scientific_exponent(i32::max_value(), 5, 0),
+ i32::max_value()
+ );
+}
+
+#[test]
+fn mantissa_exponent_test() {
+ assert_eq!(mantissa_exponent(10, 5, 0), 5);
+ assert_eq!(mantissa_exponent(0, 5, 0), -5);
+ assert_eq!(
+ mantissa_exponent(i32::max_value(), 5, 0),
+ i32::max_value() - 5
+ );
+ assert_eq!(mantissa_exponent(i32::max_value(), 0, 5), i32::max_value());
+ assert_eq!(mantissa_exponent(i32::min_value(), 5, 0), i32::min_value());
+ assert_eq!(
+ mantissa_exponent(i32::min_value(), 0, 5),
+ i32::min_value() + 5
+ );
+}
diff --git a/third_party/rust/serde_json/tests/lexical/float.rs b/third_party/rust/serde_json/tests/lexical/float.rs
new file mode 100644
index 0000000000..c87e7e1ed9
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/float.rs
@@ -0,0 +1,581 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::float::ExtendedFloat;
+use crate::lexical::rounding::round_nearest_tie_even;
+use std::{f32, f64};
+
+// NORMALIZE
+
+fn check_normalize(mant: u64, exp: i32, shift: u32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ assert_eq!(x.normalize(), shift);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn normalize_test() {
+ // F32
+ // 0
+ check_normalize(0, 0, 0, 0, 0);
+
+ // min value
+ check_normalize(1, -149, 63, 9223372036854775808, -212);
+
+ // 1.0e-40
+ check_normalize(71362, -149, 47, 10043308644012916736, -196);
+
+ // 1.0e-20
+ check_normalize(12379400, -90, 40, 13611294244890214400, -130);
+
+ // 1.0
+ check_normalize(8388608, -23, 40, 9223372036854775808, -63);
+
+ // 1e20
+ check_normalize(11368684, 43, 40, 12500000250510966784, 3);
+
+ // max value
+ check_normalize(16777213, 104, 40, 18446740775174668288, 64);
+
+ // F64
+
+ // min value
+ check_normalize(1, -1074, 63, 9223372036854775808, -1137);
+
+ // 1.0e-250
+ check_normalize(6448907850777164, -883, 11, 13207363278391631872, -894);
+
+ // 1.0e-150
+ check_normalize(7371020360979573, -551, 11, 15095849699286165504, -562);
+
+ // 1.0e-45
+ check_normalize(6427752177035961, -202, 11, 13164036458569648128, -213);
+
+ // 1.0e-40
+ check_normalize(4903985730770844, -185, 11, 10043362776618688512, -196);
+
+ // 1.0e-20
+ check_normalize(6646139978924579, -119, 11, 13611294676837537792, -130);
+
+ // 1.0
+ check_normalize(4503599627370496, -52, 11, 9223372036854775808, -63);
+
+ // 1e20
+ check_normalize(6103515625000000, 14, 11, 12500000000000000000, 3);
+
+ // 1e40
+ check_normalize(8271806125530277, 80, 11, 16940658945086007296, 69);
+
+ // 1e150
+ check_normalize(5503284107318959, 446, 11, 11270725851789228032, 435);
+
+ // 1e250
+ check_normalize(6290184345309700, 778, 11, 12882297539194265600, 767);
+
+ // max value
+ check_normalize(9007199254740991, 971, 11, 18446744073709549568, 960);
+}
+
+// ROUND
+
+fn check_round_to_f32(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ x.round_to_native::<f32, _>(round_nearest_tie_even);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn round_to_f32_test() {
+ // This is lossy, so some of these values are **slightly** rounded.
+
+ // underflow
+ check_round_to_f32(9223372036854775808, -213, 0, -149);
+
+ // min value
+ check_round_to_f32(9223372036854775808, -212, 1, -149);
+
+ // 1.0e-40
+ check_round_to_f32(10043308644012916736, -196, 71362, -149);
+
+ // 1.0e-20
+ check_round_to_f32(13611294244890214400, -130, 12379400, -90);
+
+ // 1.0
+ check_round_to_f32(9223372036854775808, -63, 8388608, -23);
+
+ // 1e20
+ check_round_to_f32(12500000250510966784, 3, 11368684, 43);
+
+ // max value
+ check_round_to_f32(18446740775174668288, 64, 16777213, 104);
+
+ // overflow
+ check_round_to_f32(18446740775174668288, 65, 16777213, 105);
+}
+
+fn check_round_to_f64(mant: u64, exp: i32, r_mant: u64, r_exp: i32) {
+ let mut x = ExtendedFloat { mant, exp };
+ x.round_to_native::<f64, _>(round_nearest_tie_even);
+ assert_eq!(
+ x,
+ ExtendedFloat {
+ mant: r_mant,
+ exp: r_exp
+ }
+ );
+}
+
+#[test]
+fn round_to_f64_test() {
+ // This is lossy, so some of these values are **slightly** rounded.
+
+ // underflow
+ check_round_to_f64(9223372036854775808, -1138, 0, -1074);
+
+ // min value
+ check_round_to_f64(9223372036854775808, -1137, 1, -1074);
+
+ // 1.0e-250
+ check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551);
+
+ // 1.0e-150
+ check_round_to_f64(15095849699286165504, -562, 7371020360979573, -551);
+
+ // 1.0e-45
+ check_round_to_f64(13164036458569648128, -213, 6427752177035961, -202);
+
+ // 1.0e-40
+ check_round_to_f64(10043362776618688512, -196, 4903985730770844, -185);
+
+ // 1.0e-20
+ check_round_to_f64(13611294676837537792, -130, 6646139978924579, -119);
+
+ // 1.0
+ check_round_to_f64(9223372036854775808, -63, 4503599627370496, -52);
+
+ // 1e20
+ check_round_to_f64(12500000000000000000, 3, 6103515625000000, 14);
+
+ // 1e40
+ check_round_to_f64(16940658945086007296, 69, 8271806125530277, 80);
+
+ // 1e150
+ check_round_to_f64(11270725851789228032, 435, 5503284107318959, 446);
+
+ // 1e250
+ check_round_to_f64(12882297539194265600, 767, 6290184345309700, 778);
+
+ // max value
+ check_round_to_f64(18446744073709549568, 960, 9007199254740991, 971);
+
+ // Bug fixes
+ // 1.2345e-308
+ check_round_to_f64(10234494226754558294, -1086, 2498655817078750, -1074);
+}
+
+fn assert_normalized_eq(mut x: ExtendedFloat, mut y: ExtendedFloat) {
+ x.normalize();
+ y.normalize();
+ assert_eq!(x, y);
+}
+
+#[test]
+fn from_float() {
+ let values: [f32; 26] = [
+ 1e-40, 2e-40, 1e-35, 2e-35, 1e-30, 2e-30, 1e-25, 2e-25, 1e-20, 2e-20, 1e-15, 2e-15, 1e-10,
+ 2e-10, 1e-5, 2e-5, 1.0, 2.0, 1e5, 2e5, 1e10, 2e10, 1e15, 2e15, 1e20, 2e20,
+ ];
+ for value in &values {
+ assert_normalized_eq(
+ ExtendedFloat::from_float(*value),
+ ExtendedFloat::from_float(*value as f64),
+ );
+ }
+}
+
+// TO
+
+// Sample of interesting numbers to check during standard test builds.
+const INTEGERS: [u64; 32] = [
+ 0, // 0x0
+ 1, // 0x1
+ 7, // 0x7
+ 15, // 0xF
+ 112, // 0x70
+ 119, // 0x77
+ 127, // 0x7F
+ 240, // 0xF0
+ 247, // 0xF7
+ 255, // 0xFF
+ 2032, // 0x7F0
+ 2039, // 0x7F7
+ 2047, // 0x7FF
+ 4080, // 0xFF0
+ 4087, // 0xFF7
+ 4095, // 0xFFF
+ 65520, // 0xFFF0
+ 65527, // 0xFFF7
+ 65535, // 0xFFFF
+ 1048560, // 0xFFFF0
+ 1048567, // 0xFFFF7
+ 1048575, // 0xFFFFF
+ 16777200, // 0xFFFFF0
+ 16777207, // 0xFFFFF7
+ 16777215, // 0xFFFFFF
+ 268435440, // 0xFFFFFF0
+ 268435447, // 0xFFFFFF7
+ 268435455, // 0xFFFFFFF
+ 4294967280, // 0xFFFFFFF0
+ 4294967287, // 0xFFFFFFF7
+ 4294967295, // 0xFFFFFFFF
+ 18446744073709551615, // 0xFFFFFFFFFFFFFFFF
+];
+
+#[test]
+fn to_f32_test() {
+ // underflow
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -213,
+ };
+ assert_eq!(x.into_float::<f32>(), 0.0);
+
+ // min value
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -212,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-45);
+
+ // 1.0e-40
+ let x = ExtendedFloat {
+ mant: 10043308644012916736,
+ exp: -196,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-40);
+
+ // 1.0e-20
+ let x = ExtendedFloat {
+ mant: 13611294244890214400,
+ exp: -130,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e-20);
+
+ // 1.0
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -63,
+ };
+ assert_eq!(x.into_float::<f32>(), 1.0);
+
+ // 1e20
+ let x = ExtendedFloat {
+ mant: 12500000250510966784,
+ exp: 3,
+ };
+ assert_eq!(x.into_float::<f32>(), 1e20);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 18446740775174668288,
+ exp: 64,
+ };
+ assert_eq!(x.into_float::<f32>(), 3.402823e38);
+
+ // almost max, high exp
+ let x = ExtendedFloat {
+ mant: 1048575,
+ exp: 108,
+ };
+ assert_eq!(x.into_float::<f32>(), 3.4028204e38);
+
+ // max value + 1
+ let x = ExtendedFloat {
+ mant: 16777216,
+ exp: 104,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // max value + 1
+ let x = ExtendedFloat {
+ mant: 1048576,
+ exp: 108,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // 1e40
+ let x = ExtendedFloat {
+ mant: 16940658945086007296,
+ exp: 69,
+ };
+ assert_eq!(x.into_float::<f32>(), f32::INFINITY);
+
+ // Integers.
+ for int in &INTEGERS {
+ let fp = ExtendedFloat { mant: *int, exp: 0 };
+ assert_eq!(fp.into_float::<f32>(), *int as f32, "{:?} as f32", *int);
+ }
+}
+
+#[test]
+fn to_f64_test() {
+ // underflow
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -1138,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ // min value
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -1137,
+ };
+ assert_eq!(x.into_float::<f64>(), 5e-324);
+
+ // 1.0e-250
+ let x = ExtendedFloat {
+ mant: 13207363278391631872,
+ exp: -894,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-250);
+
+ // 1.0e-150
+ let x = ExtendedFloat {
+ mant: 15095849699286165504,
+ exp: -562,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-150);
+
+ // 1.0e-45
+ let x = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-45);
+
+ // 1.0e-40
+ let x = ExtendedFloat {
+ mant: 10043362776618688512,
+ exp: -196,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-40);
+
+ // 1.0e-20
+ let x = ExtendedFloat {
+ mant: 13611294676837537792,
+ exp: -130,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e-20);
+
+ // 1.0
+ let x = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -63,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.0);
+
+ // 1e20
+ let x = ExtendedFloat {
+ mant: 12500000000000000000,
+ exp: 3,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e20);
+
+ // 1e40
+ let x = ExtendedFloat {
+ mant: 16940658945086007296,
+ exp: 69,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e40);
+
+ // 1e150
+ let x = ExtendedFloat {
+ mant: 11270725851789228032,
+ exp: 435,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e150);
+
+ // 1e250
+ let x = ExtendedFloat {
+ mant: 12882297539194265600,
+ exp: 767,
+ };
+ assert_eq!(x.into_float::<f64>(), 1e250);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 9007199254740991,
+ exp: 971,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);
+
+ // max value
+ let x = ExtendedFloat {
+ mant: 18446744073709549568,
+ exp: 960,
+ };
+ assert_eq!(x.into_float::<f64>(), 1.7976931348623157e308);
+
+ // overflow
+ let x = ExtendedFloat {
+ mant: 9007199254740992,
+ exp: 971,
+ };
+ assert_eq!(x.into_float::<f64>(), f64::INFINITY);
+
+ // overflow
+ let x = ExtendedFloat {
+ mant: 18446744073709549568,
+ exp: 961,
+ };
+ assert_eq!(x.into_float::<f64>(), f64::INFINITY);
+
+ // Underflow
+ // Adapted from failures in strtod.
+ let x = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709550712,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ let x = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709551460,
+ };
+ assert_eq!(x.into_float::<f64>(), 0.0);
+
+ let x = ExtendedFloat {
+ exp: -1138,
+ mant: 9223372036854776103,
+ };
+ assert_eq!(x.into_float::<f64>(), 5e-324);
+
+ // Integers.
+ for int in &INTEGERS {
+ let fp = ExtendedFloat { mant: *int, exp: 0 };
+ assert_eq!(fp.into_float::<f64>(), *int as f64, "{:?} as f64", *int);
+ }
+}
+
+// OPERATIONS
+
+fn check_mul(a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
+ let r = a.mul(&b);
+ assert_eq!(r, c);
+}
+
+#[test]
+fn mul_test() {
+ // Normalized (64-bit mantissa)
+ let a = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ let b = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -62,
+ };
+ let c = ExtendedFloat {
+ mant: 6582018229284824064,
+ exp: -211,
+ };
+ check_mul(a, b, c);
+
+ // Check with integers
+ // 64-bit mantissa
+ let mut a = ExtendedFloat { mant: 10, exp: 0 };
+ let mut b = ExtendedFloat { mant: 10, exp: 0 };
+ a.normalize();
+ b.normalize();
+ assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);
+
+ // Check both values need high bits set.
+ let a = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ assert_eq!(a.mul(&b).into_float::<f64>(), 4.0);
+
+ // Check both values need high bits set.
+ let a = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ assert_eq!(a.mul(&b).into_float::<f64>(), 100.0);
+}
+
+fn check_imul(mut a: ExtendedFloat, b: ExtendedFloat, c: ExtendedFloat) {
+ a.imul(&b);
+ assert_eq!(a, c);
+}
+
+#[test]
+fn imul_test() {
+ // Normalized (64-bit mantissa)
+ let a = ExtendedFloat {
+ mant: 13164036458569648128,
+ exp: -213,
+ };
+ let b = ExtendedFloat {
+ mant: 9223372036854775808,
+ exp: -62,
+ };
+ let c = ExtendedFloat {
+ mant: 6582018229284824064,
+ exp: -211,
+ };
+ check_imul(a, b, c);
+
+ // Check with integers
+ // 64-bit mantissa
+ let mut a = ExtendedFloat { mant: 10, exp: 0 };
+ let mut b = ExtendedFloat { mant: 10, exp: 0 };
+ a.normalize();
+ b.normalize();
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 100.0);
+
+ // Check both values need high bits set.
+ let mut a = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 1 << 32,
+ exp: -31,
+ };
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 4.0);
+
+ // Check both values need high bits set.
+ let mut a = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ let b = ExtendedFloat {
+ mant: 10 << 31,
+ exp: -31,
+ };
+ a.imul(&b);
+ assert_eq!(a.into_float::<f64>(), 100.0);
+}
diff --git a/third_party/rust/serde_json/tests/lexical/math.rs b/third_party/rust/serde_json/tests/lexical/math.rs
new file mode 100644
index 0000000000..79d3ef3ee5
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/math.rs
@@ -0,0 +1,211 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::math::{Limb, Math};
+use std::cmp;
+
+#[derive(Clone, Default)]
+struct Bigint {
+ data: Vec<Limb>,
+}
+
+impl Math for Bigint {
+ fn data(&self) -> &Vec<Limb> {
+ &self.data
+ }
+
+ fn data_mut(&mut self) -> &mut Vec<Limb> {
+ &mut self.data
+ }
+}
+
+#[cfg(limb_width_32)]
+pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
+ x.iter().cloned().collect()
+}
+
+#[cfg(limb_width_64)]
+pub(crate) fn from_u32(x: &[u32]) -> Vec<Limb> {
+ let mut v = Vec::<Limb>::default();
+ for xi in x.chunks(2) {
+ match xi.len() {
+ 1 => v.push(xi[0] as u64),
+ 2 => v.push(((xi[1] as u64) << 32) | (xi[0] as u64)),
+ _ => unreachable!(),
+ }
+ }
+
+ v
+}
+
+#[test]
+fn compare_test() {
+ // Simple
+ let x = Bigint {
+ data: from_u32(&[1]),
+ };
+ let y = Bigint {
+ data: from_u32(&[2]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Less);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Greater);
+
+ // Check asymmetric
+ let x = Bigint {
+ data: from_u32(&[5, 1]),
+ };
+ let y = Bigint {
+ data: from_u32(&[2]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+
+ // Check when we use reverse ordering properly.
+ let x = Bigint {
+ data: from_u32(&[5, 1, 9]),
+ };
+ let y = Bigint {
+ data: from_u32(&[6, 2, 8]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+
+ // Complex scenario, check it properly uses reverse ordering.
+ let x = Bigint {
+ data: from_u32(&[0, 1, 9]),
+ };
+ let y = Bigint {
+ data: from_u32(&[4294967295, 0, 9]),
+ };
+ assert_eq!(x.compare(&y), cmp::Ordering::Greater);
+ assert_eq!(x.compare(&x), cmp::Ordering::Equal);
+ assert_eq!(y.compare(&x), cmp::Ordering::Less);
+}
+
+#[test]
+fn hi64_test() {
+ assert_eq!(Bigint::from_u64(0xA).hi64(), (0xA000000000000000, false));
+ assert_eq!(Bigint::from_u64(0xAB).hi64(), (0xAB00000000000000, false));
+ assert_eq!(
+ Bigint::from_u64(0xAB00000000).hi64(),
+ (0xAB00000000000000, false)
+ );
+ assert_eq!(
+ Bigint::from_u64(0xA23456789A).hi64(),
+ (0xA23456789A000000, false)
+ );
+}
+
+#[test]
+fn bit_length_test() {
+ let x = Bigint {
+ data: from_u32(&[0, 0, 0, 1]),
+ };
+ assert_eq!(x.bit_length(), 97);
+
+ let x = Bigint {
+ data: from_u32(&[0, 0, 0, 3]),
+ };
+ assert_eq!(x.bit_length(), 98);
+
+ let x = Bigint {
+ data: from_u32(&[1 << 31]),
+ };
+ assert_eq!(x.bit_length(), 32);
+}
+
+#[test]
+fn iadd_small_test() {
+ // Overflow check (single)
+ // This should set all the internal data values to 0, the top
+ // value to (1<<31), and the bottom value to (4>>1).
+ // This is because the max_value + 1 leads to all 0s, we set the
+ // topmost bit to 1.
+ let mut x = Bigint {
+ data: from_u32(&[4294967295]),
+ };
+ x.iadd_small(5);
+ assert_eq!(x.data, from_u32(&[4, 1]));
+
+ // No overflow, single value
+ let mut x = Bigint {
+ data: from_u32(&[5]),
+ };
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[12]));
+
+ // Single carry, internal overflow
+ let mut x = Bigint::from_u64(0x80000000FFFFFFFF);
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[6, 0x80000001]));
+
+ // Double carry, overflow
+ let mut x = Bigint::from_u64(0xFFFFFFFFFFFFFFFF);
+ x.iadd_small(7);
+ assert_eq!(x.data, from_u32(&[6, 0, 1]));
+}
+
+#[test]
+fn imul_small_test() {
+ // No overflow check, 1-int.
+ let mut x = Bigint {
+ data: from_u32(&[5]),
+ };
+ x.imul_small(7);
+ assert_eq!(x.data, from_u32(&[35]));
+
+ // No overflow check, 2-ints.
+ let mut x = Bigint::from_u64(0x4000000040000);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[0x00140000, 0x140000]));
+
+ // Overflow, 1 carry.
+ let mut x = Bigint {
+ data: from_u32(&[0x33333334]),
+ };
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 1]));
+
+ // Overflow, 1 carry, internal.
+ let mut x = Bigint::from_u64(0x133333334);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 6]));
+
+ // Overflow, 2 carries.
+ let mut x = Bigint::from_u64(0x3333333333333334);
+ x.imul_small(5);
+ assert_eq!(x.data, from_u32(&[4, 0, 1]));
+}
+
+#[test]
+fn shl_test() {
+ // Pattern generated via `''.join(["1" +"0"*i for i in range(20)])`
+ let mut big = Bigint {
+ data: from_u32(&[0xD2210408]),
+ };
+ big.ishl(5);
+ assert_eq!(big.data, from_u32(&[0x44208100, 0x1A]));
+ big.ishl(32);
+ assert_eq!(big.data, from_u32(&[0, 0x44208100, 0x1A]));
+ big.ishl(27);
+ assert_eq!(big.data, from_u32(&[0, 0, 0xD2210408]));
+
+ // 96-bits of previous pattern
+ let mut big = Bigint {
+ data: from_u32(&[0x20020010, 0x8040100, 0xD2210408]),
+ };
+ big.ishl(5);
+ assert_eq!(big.data, from_u32(&[0x400200, 0x802004, 0x44208101, 0x1A]));
+ big.ishl(32);
+ assert_eq!(
+ big.data,
+ from_u32(&[0, 0x400200, 0x802004, 0x44208101, 0x1A])
+ );
+ big.ishl(27);
+ assert_eq!(
+ big.data,
+ from_u32(&[0, 0, 0x20020010, 0x8040100, 0xD2210408])
+ );
+}
diff --git a/third_party/rust/serde_json/tests/lexical/num.rs b/third_party/rust/serde_json/tests/lexical/num.rs
new file mode 100644
index 0000000000..1a94be0130
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/num.rs
@@ -0,0 +1,76 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::num::{AsPrimitive, Float, Integer, Number};
+
+fn check_as_primitive<T: AsPrimitive>(t: T) {
+ let _: u32 = t.as_u32();
+ let _: u64 = t.as_u64();
+ let _: u128 = t.as_u128();
+ let _: usize = t.as_usize();
+ let _: f32 = t.as_f32();
+ let _: f64 = t.as_f64();
+}
+
+#[test]
+fn as_primitive_test() {
+ check_as_primitive(1u32);
+ check_as_primitive(1u64);
+ check_as_primitive(1u128);
+ check_as_primitive(1usize);
+ check_as_primitive(1f32);
+ check_as_primitive(1f64);
+}
+
+fn check_number<T: Number>(x: T, y: T) {
+ // Copy, partialeq, partialord
+ let _ = x;
+ assert!(x < y);
+ assert!(x != y);
+
+ // Operations
+ let _ = y + x;
+
+ // Conversions already tested.
+}
+
+#[test]
+fn number_test() {
+ check_number(1u32, 5);
+ check_number(1u64, 5);
+ check_number(1u128, 5);
+ check_number(1usize, 5);
+ check_number(1f32, 5.0);
+ check_number(1f64, 5.0);
+}
+
+fn check_integer<T: Integer>(x: T) {
+ // Bitwise operations
+ let _ = x & T::ZERO;
+}
+
+#[test]
+fn integer_test() {
+ check_integer(65u32);
+ check_integer(65u64);
+ check_integer(65u128);
+ check_integer(65usize);
+}
+
+fn check_float<T: Float>(x: T) {
+ // Check functions
+ let _ = x.pow10(5);
+ let _ = x.to_bits();
+ assert!(T::from_bits(x.to_bits()) == x);
+
+ // Check properties
+ let _ = x.to_bits() & T::SIGN_MASK;
+ let _ = x.to_bits() & T::EXPONENT_MASK;
+ let _ = x.to_bits() & T::HIDDEN_BIT_MASK;
+ let _ = x.to_bits() & T::MANTISSA_MASK;
+}
+
+#[test]
+fn float_test() {
+ check_float(123f32);
+ check_float(123f64);
+}
diff --git a/third_party/rust/serde_json/tests/lexical/parse.rs b/third_party/rust/serde_json/tests/lexical/parse.rs
new file mode 100644
index 0000000000..80ca25e772
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/parse.rs
@@ -0,0 +1,204 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::num::Float;
+use crate::lexical::parse::{parse_concise_float, parse_truncated_float};
+use core::f64;
+use core::fmt::Debug;
+
+fn check_concise_float<F>(mantissa: u64, exponent: i32, expected: F)
+where
+ F: Float + Debug,
+{
+ assert_eq!(parse_concise_float::<F>(mantissa, exponent), expected);
+}
+
+fn check_truncated_float<F>(integer: &str, fraction: &str, exponent: i32, expected: F)
+where
+ F: Float + Debug,
+{
+ let integer = integer.as_bytes();
+ let fraction = fraction.as_bytes();
+ assert_eq!(
+ parse_truncated_float::<F>(integer, fraction, exponent),
+ expected,
+ );
+}
+
+#[test]
+fn parse_f32_test() {
+ check_concise_float(0, 0, 0.0_f32);
+ check_concise_float(12345, -4, 1.2345_f32);
+ check_concise_float(12345, -3, 12.345_f32);
+ check_concise_float(123456789, -4, 12345.6789_f32);
+ check_concise_float(12345, 6, 1.2345e10_f32);
+ check_concise_float(12345, -42, 1.2345e-38_f32);
+
+ // Check expected rounding, using borderline cases.
+ // Round-down, halfway
+ check_concise_float(16777216, 0, 16777216.0_f32);
+ check_concise_float(16777217, 0, 16777216.0_f32);
+ check_concise_float(16777218, 0, 16777218.0_f32);
+ check_concise_float(33554432, 0, 33554432.0_f32);
+ check_concise_float(33554434, 0, 33554432.0_f32);
+ check_concise_float(33554436, 0, 33554436.0_f32);
+ check_concise_float(17179869184, 0, 17179869184.0_f32);
+ check_concise_float(17179870208, 0, 17179869184.0_f32);
+ check_concise_float(17179871232, 0, 17179871232.0_f32);
+
+ // Round-up, halfway
+ check_concise_float(16777218, 0, 16777218.0_f32);
+ check_concise_float(16777219, 0, 16777220.0_f32);
+ check_concise_float(16777220, 0, 16777220.0_f32);
+
+ check_concise_float(33554436, 0, 33554436.0_f32);
+ check_concise_float(33554438, 0, 33554440.0_f32);
+ check_concise_float(33554440, 0, 33554440.0_f32);
+
+ check_concise_float(17179871232, 0, 17179871232.0_f32);
+ check_concise_float(17179872256, 0, 17179873280.0_f32);
+ check_concise_float(17179873280, 0, 17179873280.0_f32);
+
+ // Round-up, above halfway
+ check_concise_float(33554435, 0, 33554436.0_f32);
+ check_concise_float(17179870209, 0, 17179871232.0_f32);
+
+ // Check exactly halfway, round-up at halfway
+ check_truncated_float("1", "00000017881393432617187499", 0, 1.0000001_f32);
+ check_truncated_float("1", "000000178813934326171875", 0, 1.0000002_f32);
+ check_truncated_float("1", "00000017881393432617187501", 0, 1.0000002_f32);
+}
+
+#[test]
+fn parse_f64_test() {
+ check_concise_float(0, 0, 0.0_f64);
+ check_concise_float(12345, -4, 1.2345_f64);
+ check_concise_float(12345, -3, 12.345_f64);
+ check_concise_float(123456789, -4, 12345.6789_f64);
+ check_concise_float(12345, 6, 1.2345e10_f64);
+ check_concise_float(12345, -312, 1.2345e-308_f64);
+
+ // Check expected rounding, using borderline cases.
+ // Round-down, halfway
+ check_concise_float(9007199254740992, 0, 9007199254740992.0_f64);
+ check_concise_float(9007199254740993, 0, 9007199254740992.0_f64);
+ check_concise_float(9007199254740994, 0, 9007199254740994.0_f64);
+
+ check_concise_float(18014398509481984, 0, 18014398509481984.0_f64);
+ check_concise_float(18014398509481986, 0, 18014398509481984.0_f64);
+ check_concise_float(18014398509481988, 0, 18014398509481988.0_f64);
+
+ check_concise_float(9223372036854775808, 0, 9223372036854775808.0_f64);
+ check_concise_float(9223372036854776832, 0, 9223372036854775808.0_f64);
+ check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64);
+
+ check_truncated_float(
+ "11417981541647679048466287755595961091061972992",
+ "",
+ 0,
+ 11417981541647679048466287755595961091061972992.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178368",
+ "",
+ 0,
+ 11417981541647679048466287755595961091061972992.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647681583767488212054764084468383744",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+
+ // Round-up, halfway
+ check_concise_float(9007199254740994, 0, 9007199254740994.0_f64);
+ check_concise_float(9007199254740995, 0, 9007199254740996.0_f64);
+ check_concise_float(9007199254740996, 0, 9007199254740996.0_f64);
+
+ check_concise_float(18014398509481988, 0, 18014398509481988.0_f64);
+ check_concise_float(18014398509481990, 0, 18014398509481992.0_f64);
+ check_concise_float(18014398509481992, 0, 18014398509481992.0_f64);
+
+ check_concise_float(9223372036854777856, 0, 9223372036854777856.0_f64);
+ check_concise_float(9223372036854778880, 0, 9223372036854779904.0_f64);
+ check_concise_float(9223372036854779904, 0, 9223372036854779904.0_f64);
+
+ check_truncated_float(
+ "11417981541647681583767488212054764084468383744",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647682851418088440284165581171589120",
+ "",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+ check_truncated_float(
+ "11417981541647684119068688668513567077874794496",
+ "",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+
+ // Round-up, above halfway
+ check_concise_float(9223372036854776833, 0, 9223372036854777856.0_f64);
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178369",
+ "",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+
+ // Rounding error
+ // Adapted from failures in strtod.
+ check_concise_float(22250738585072014, -324, 2.2250738585072014e-308_f64);
+ check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187499", -308, 2.225073858507201e-308_f64);
+ check_truncated_float("2", "22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508791414915891303962110687008643869459464552765720740782062174337998814106326732925355228688137214901298112245145188984905722230728525513315575501591439747639798341180199932396254828901710708185069063066665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505108060994073026293712895895000358379996720725430436028407889577179615094551674824347103070260914462157228988025818254518032570701886087211312807951223342628836862232150377566662250398253433597456888442390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042756718644338377048603786162277173854562306587467901408672332763671875", -308, 2.2250738585072014e-308_f64);
+ check_truncated_float("2", "2250738585072011360574097967091319759348195463516456480234261097248222220210769455165295239081350879141491589130396211068700864386945946455276572074078206217433799881410632673292535522868813721490129811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306666559949382757725720157630626906633326475653000092458883164330377797918696120494973903778297049050510806099407302629371289589500035837999672072543043602840788957717961509455167482434710307026091446215722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844239002654981983854879482922068947216898310996983658468140228542433306603398508864458040010349339704275671864433837704860378616227717385456230658746790140867233276367187501", -308, 2.2250738585072014e-308_f64);
+ check_truncated_float("179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791", "9999999999999999999999999999999999999999999999999999999999999999999999", 0, 1.7976931348623157e+308_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984374999", -324, 5.0e-324_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375", -324, 1.0e-323_f64);
+ check_truncated_float("7", "4109846876186981626485318930233205854758970392148714663837852375101326090531312779794975454245398856969484704316857659638998506553390969459816219401617281718945106978546710679176872575177347315553307795408549809608457500958111373034747658096871009590975442271004757307809711118935784838675653998783503015228055934046593739791790738723868299395818481660169122019456499931289798411362062484498678713572180352209017023903285791732520220528974020802906854021606612375549983402671300035812486479041385743401875520901590172592547146296175134159774938718574737870961645638908718119841271673056017045493004705269590165763776884908267986972573366521765567941072508764337560846003984904972149117463085539556354188641513168478436313080237596295773983001708984375001", -324, 1.0e-323_f64);
+ check_truncated_float("", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125", 0, 0.0_f64);
+
+ // Rounding error
+ // Adapted from:
+ // https://www.exploringbinary.com/how-glibc-strtod-works/
+ check_truncated_float("", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375", 0, 2.2250738585072011e-308_f64);
+
+ // Rounding error
+ // Adapted from test-parse-random failures.
+ check_concise_float(1009, -31, 1.009e-28_f64);
+ check_concise_float(18294, 304, f64::INFINITY);
+
+ // Rounding error
+ // Adapted from a @dangrabcad's issue #20.
+ check_concise_float(7689539722041643, 149, 7.689539722041643e164_f64);
+ check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "", 0, 7.689539722041643e164_f64);
+ check_truncated_float("768953972204164300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 7.689539722041643e164_f64);
+
+ // Check other cases similar to @dangrabcad's issue #20.
+ check_truncated_float("9223372036854776833", "0", 0, 9223372036854777856.0_f64);
+ check_truncated_float(
+ "11417981541647680316116887983825362587765178369",
+ "0",
+ 0,
+ 11417981541647681583767488212054764084468383744.0_f64,
+ );
+ check_concise_float(90071992547409950, -1, 9007199254740996.0_f64);
+ check_concise_float(180143985094819900, -1, 18014398509481992.0_f64);
+ check_truncated_float("9223372036854778880", "0", 0, 9223372036854779904.0_f64);
+ check_truncated_float(
+ "11417981541647682851418088440284165581171589120",
+ "0",
+ 0,
+ 11417981541647684119068688668513567077874794496.0_f64,
+ );
+
+ // Check other cases ostensibly identified via proptest.
+ check_truncated_float("71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 71610528364411830000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+ check_truncated_float("126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 126769393745745060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+ check_truncated_float("38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "0", 0, 38652960461239320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0_f64);
+}
diff --git a/third_party/rust/serde_json/tests/lexical/rounding.rs b/third_party/rust/serde_json/tests/lexical/rounding.rs
new file mode 100644
index 0000000000..7ea17716c3
--- /dev/null
+++ b/third_party/rust/serde_json/tests/lexical/rounding.rs
@@ -0,0 +1,316 @@
+// Adapted from https://github.com/Alexhuszagh/rust-lexical.
+
+use crate::lexical::float::ExtendedFloat;
+use crate::lexical::num::Float;
+use crate::lexical::rounding::*;
+
+// MASKS
+
+#[test]
+fn lower_n_mask_test() {
+ assert_eq!(lower_n_mask(0u64), 0b0);
+ assert_eq!(lower_n_mask(1u64), 0b1);
+ assert_eq!(lower_n_mask(2u64), 0b11);
+ assert_eq!(lower_n_mask(10u64), 0b1111111111);
+ assert_eq!(lower_n_mask(32u64), 0b11111111111111111111111111111111);
+}
+
+#[test]
+fn lower_n_halfway_test() {
+ assert_eq!(lower_n_halfway(0u64), 0b0);
+ assert_eq!(lower_n_halfway(1u64), 0b1);
+ assert_eq!(lower_n_halfway(2u64), 0b10);
+ assert_eq!(lower_n_halfway(10u64), 0b1000000000);
+ assert_eq!(lower_n_halfway(32u64), 0b10000000000000000000000000000000);
+}
+
+#[test]
+fn nth_bit_test() {
+ assert_eq!(nth_bit(0u64), 0b1);
+ assert_eq!(nth_bit(1u64), 0b10);
+ assert_eq!(nth_bit(2u64), 0b100);
+ assert_eq!(nth_bit(10u64), 0b10000000000);
+ assert_eq!(nth_bit(31u64), 0b10000000000000000000000000000000);
+}
+
+#[test]
+fn internal_n_mask_test() {
+ assert_eq!(internal_n_mask(1u64, 0u64), 0b0);
+ assert_eq!(internal_n_mask(1u64, 1u64), 0b1);
+ assert_eq!(internal_n_mask(2u64, 1u64), 0b10);
+ assert_eq!(internal_n_mask(4u64, 2u64), 0b1100);
+ assert_eq!(internal_n_mask(10u64, 2u64), 0b1100000000);
+ assert_eq!(internal_n_mask(10u64, 4u64), 0b1111000000);
+ assert_eq!(
+ internal_n_mask(32u64, 4u64),
+ 0b11110000000000000000000000000000
+ );
+}
+
+// NEAREST ROUNDING
+
+#[test]
+fn round_nearest_test() {
+ // Check exactly halfway (b'1100000')
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(!above);
+ assert!(halfway);
+ assert_eq!(fp.mant, 1);
+
+ // Check above halfway (b'1100001')
+ let mut fp = ExtendedFloat { mant: 0x61, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(above);
+ assert!(!halfway);
+ assert_eq!(fp.mant, 1);
+
+ // Check below halfway (b'1011111')
+ let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 };
+ let (above, halfway) = round_nearest(&mut fp, 6);
+ assert!(!above);
+ assert!(!halfway);
+ assert_eq!(fp.mant, 1);
+}
+
+// DIRECTED ROUNDING
+
+#[test]
+fn round_downward_test() {
+ // b0000000
+ let mut fp = ExtendedFloat { mant: 0x00, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+
+ // b1000000
+ let mut fp = ExtendedFloat { mant: 0x40, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // b1100000
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // b1110000
+ let mut fp = ExtendedFloat { mant: 0x70, exp: 0 };
+ round_downward(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+}
+
+#[test]
+fn round_nearest_tie_even_test() {
+ // Check round-up, halfway
+ let mut fp = ExtendedFloat { mant: 0x60, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 2);
+
+ // Check round-down, halfway
+ let mut fp = ExtendedFloat { mant: 0x20, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+
+ // Check round-up, above halfway
+ let mut fp = ExtendedFloat { mant: 0x61, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 2);
+
+ let mut fp = ExtendedFloat { mant: 0x21, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ // Check round-down, below halfway
+ let mut fp = ExtendedFloat { mant: 0x5F, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 1);
+
+ let mut fp = ExtendedFloat { mant: 0x1F, exp: 0 };
+ round_nearest_tie_even(&mut fp, 6);
+ assert_eq!(fp.mant, 0);
+}
+
+// HIGH-LEVEL
+
+#[test]
+fn round_to_float_test() {
+ // Denormal
+ let mut fp = ExtendedFloat {
+ mant: 1 << 63,
+ exp: f64::DENORMAL_EXPONENT - 15,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 48);
+ assert_eq!(fp.exp, f64::DENORMAL_EXPONENT);
+
+ // Halfway, round-down (b'1000000000000000000000000000000000000000000000000000010000000000')
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000400,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ // Halfway, round-up (b'1000000000000000000000000000000000000000000000000000110000000000')
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000C00,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Above halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000401,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000C01,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Below halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x80000000000003FF,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x8000000000000BFF,
+ exp: -63,
+ };
+ round_to_float::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+}
+
+#[test]
+fn avoid_overflow_test() {
+ // Avoid overflow, fails by 1
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 5,
+ };
+ avoid_overflow::<f64>(&mut fp);
+ assert_eq!(fp.mant, 0xFFFFFFFFFFFF);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT + 5);
+
+ // Avoid overflow, succeeds
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 4,
+ };
+ avoid_overflow::<f64>(&mut fp);
+ assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT - 1);
+}
+
+#[test]
+fn round_to_native_test() {
+ // Overflow
+ let mut fp = ExtendedFloat {
+ mant: 0xFFFFFFFFFFFF,
+ exp: f64::MAX_EXPONENT + 4,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0x1FFFFFFFFFFFE0);
+ assert_eq!(fp.exp, f64::MAX_EXPONENT - 1);
+
+ // Need denormal
+ let mut fp = ExtendedFloat {
+ mant: 1,
+ exp: f64::DENORMAL_EXPONENT + 48,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 48);
+ assert_eq!(fp.exp, f64::DENORMAL_EXPONENT);
+
+ // Halfway, round-down (b'10000000000000000000000000000000000000000000000000000100000')
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000020,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ // Halfway, round-up (b'10000000000000000000000000000000000000000000000000001100000')
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000060,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Above halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000021,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x400000000000061,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 2);
+ assert_eq!(fp.exp, -52);
+
+ // Below halfway
+ let mut fp = ExtendedFloat {
+ mant: 0x40000000000001F,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1 << 52);
+ assert_eq!(fp.exp, -52);
+
+ let mut fp = ExtendedFloat {
+ mant: 0x40000000000005F,
+ exp: -58,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, (1 << 52) + 1);
+ assert_eq!(fp.exp, -52);
+
+ // Underflow
+ // Adapted from failures in strtod.
+ let mut fp = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709550712,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0);
+ assert_eq!(fp.exp, 0);
+
+ let mut fp = ExtendedFloat {
+ exp: -1139,
+ mant: 18446744073709551460,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 0);
+ assert_eq!(fp.exp, 0);
+
+ let mut fp = ExtendedFloat {
+ exp: -1138,
+ mant: 9223372036854776103,
+ };
+ round_to_native::<f64, _>(&mut fp, round_nearest_tie_even);
+ assert_eq!(fp.mant, 1);
+ assert_eq!(fp.exp, -1074);
+}