]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/flt2dec/estimator.rs
Rollup merge of #106823 - m-ou-se:format-args-as-str-guarantees, r=dtolnay
[rust.git] / library / core / tests / num / flt2dec / estimator.rs
1 use core::num::flt2dec::estimator::*;
2
3 #[test]
4 fn test_estimate_scaling_factor() {
5     macro_rules! assert_almost_eq {
6         ($actual:expr, $expected:expr) => {{
7             let actual = $actual;
8             let expected = $expected;
9             println!(
10                 "{} - {} = {} - {} = {}",
11                 stringify!($expected),
12                 stringify!($actual),
13                 expected,
14                 actual,
15                 expected - actual
16             );
17             assert!(
18                 expected == actual || expected == actual + 1,
19                 "expected {}, actual {}",
20                 expected,
21                 actual
22             );
23         }};
24     }
25
26     assert_almost_eq!(estimate_scaling_factor(1, 0), 0);
27     assert_almost_eq!(estimate_scaling_factor(2, 0), 1);
28     assert_almost_eq!(estimate_scaling_factor(10, 0), 1);
29     assert_almost_eq!(estimate_scaling_factor(11, 0), 2);
30     assert_almost_eq!(estimate_scaling_factor(100, 0), 2);
31     assert_almost_eq!(estimate_scaling_factor(101, 0), 3);
32     assert_almost_eq!(estimate_scaling_factor(10000000000000000000, 0), 19);
33     assert_almost_eq!(estimate_scaling_factor(10000000000000000001, 0), 20);
34
35     // 1/2^20 = 0.00000095367...
36     assert_almost_eq!(estimate_scaling_factor(1 * 1048576 / 1000000, -20), -6);
37     assert_almost_eq!(estimate_scaling_factor(1 * 1048576 / 1000000 + 1, -20), -5);
38     assert_almost_eq!(estimate_scaling_factor(10 * 1048576 / 1000000, -20), -5);
39     assert_almost_eq!(estimate_scaling_factor(10 * 1048576 / 1000000 + 1, -20), -4);
40     assert_almost_eq!(estimate_scaling_factor(100 * 1048576 / 1000000, -20), -4);
41     assert_almost_eq!(estimate_scaling_factor(100 * 1048576 / 1000000 + 1, -20), -3);
42     assert_almost_eq!(estimate_scaling_factor(1048575, -20), 0);
43     assert_almost_eq!(estimate_scaling_factor(1048576, -20), 0);
44     assert_almost_eq!(estimate_scaling_factor(1048577, -20), 1);
45     assert_almost_eq!(estimate_scaling_factor(10485759999999999999, -20), 13);
46     assert_almost_eq!(estimate_scaling_factor(10485760000000000000, -20), 13);
47     assert_almost_eq!(estimate_scaling_factor(10485760000000000001, -20), 14);
48
49     // extreme values:
50     // 2^-1074 = 4.94065... * 10^-324
51     // (2^53-1) * 2^971 = 1.79763... * 10^308
52     assert_almost_eq!(estimate_scaling_factor(1, -1074), -323);
53     assert_almost_eq!(estimate_scaling_factor(0x1fffffffffffff, 971), 309);
54
55     // Miri is too slow
56     let step = if cfg!(miri) { 37 } else { 1 };
57
58     for i in (-1074..972).step_by(step) {
59         let expected = super::ldexp_f64(1.0, i).log10().ceil();
60         assert_almost_eq!(estimate_scaling_factor(1, i as i16), expected as i16);
61     }
62 }