]> git.proxmox.com Git - rustc.git/blob - library/core/tests/num/dec2flt/mod.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / library / core / tests / num / dec2flt / mod.rs
1 #![allow(overflowing_literals)]
2
3 mod float;
4 mod lemire;
5 mod parse;
6
7 // Take a float literal, turn it into a string in various ways (that are all trusted
8 // to be correct) and see if those strings are parsed back to the value of the literal.
9 // Requires a *polymorphic literal*, i.e., one that can serve as f64 as well as f32.
10 macro_rules! test_literal {
11 ($x: expr) => {{
12 let x32: f32 = $x;
13 let x64: f64 = $x;
14 let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)];
15 for input in inputs {
16 assert_eq!(input.parse(), Ok(x64));
17 assert_eq!(input.parse(), Ok(x32));
18 let neg_input = &format!("-{input}");
19 assert_eq!(neg_input.parse(), Ok(-x64));
20 assert_eq!(neg_input.parse(), Ok(-x32));
21 }
22 }};
23 }
24
25 #[test]
26 fn ordinary() {
27 test_literal!(1.0);
28 test_literal!(3e-5);
29 test_literal!(0.1);
30 test_literal!(12345.);
31 test_literal!(0.9999999);
32 test_literal!(2.2250738585072014e-308);
33 }
34
35 #[test]
36 fn special_code_paths() {
37 test_literal!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
38 test_literal!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
39 test_literal!(1e23); // Triggers AlgorithmR
40 test_literal!(2075e23); // Triggers another path through AlgorithmR
41 test_literal!(8713e-23); // ... and yet another.
42 }
43
44 #[test]
45 fn large() {
46 test_literal!(1e300);
47 test_literal!(123456789.34567e250);
48 test_literal!(943794359898089732078308743689303290943794359843568973207830874368930329.);
49 }
50
51 #[test]
52 fn subnormals() {
53 test_literal!(5e-324);
54 test_literal!(91e-324);
55 test_literal!(1e-322);
56 test_literal!(13245643e-320);
57 test_literal!(2.22507385851e-308);
58 test_literal!(2.1e-308);
59 test_literal!(4.9406564584124654e-324);
60 }
61
62 #[test]
63 fn infinity() {
64 test_literal!(1e400);
65 test_literal!(1e309);
66 test_literal!(2e308);
67 test_literal!(1.7976931348624e308);
68 }
69
70 #[test]
71 fn zero() {
72 test_literal!(0.0);
73 test_literal!(1e-325);
74 test_literal!(1e-326);
75 test_literal!(1e-500);
76 }
77
78 #[test]
79 fn fast_path_correct() {
80 // This number triggers the fast path and is handled incorrectly when compiling on
81 // x86 without SSE2 (i.e., using the x87 FPU stack).
82 test_literal!(1.448997445238699);
83 }
84
85 #[test]
86 fn lonely_dot() {
87 assert!(".".parse::<f32>().is_err());
88 assert!(".".parse::<f64>().is_err());
89 }
90
91 #[test]
92 fn exponentiated_dot() {
93 assert!(".e0".parse::<f32>().is_err());
94 assert!(".e0".parse::<f64>().is_err());
95 }
96
97 #[test]
98 fn lonely_sign() {
99 assert!("+".parse::<f32>().is_err());
100 assert!("-".parse::<f64>().is_err());
101 }
102
103 #[test]
104 fn whitespace() {
105 assert!(" 1.0".parse::<f32>().is_err());
106 assert!("1.0 ".parse::<f64>().is_err());
107 }
108
109 #[test]
110 fn nan() {
111 assert!("NaN".parse::<f32>().unwrap().is_nan());
112 assert!("NaN".parse::<f64>().unwrap().is_nan());
113 }
114
115 #[test]
116 fn inf() {
117 assert_eq!("inf".parse(), Ok(f64::INFINITY));
118 assert_eq!("-inf".parse(), Ok(f64::NEG_INFINITY));
119 assert_eq!("inf".parse(), Ok(f32::INFINITY));
120 assert_eq!("-inf".parse(), Ok(f32::NEG_INFINITY));
121 }
122
123 #[test]
124 fn massive_exponent() {
125 let max = i64::MAX;
126 assert_eq!(format!("1e{max}000").parse(), Ok(f64::INFINITY));
127 assert_eq!(format!("1e-{max}000").parse(), Ok(0.0));
128 assert_eq!(format!("1e{max}000").parse(), Ok(f64::INFINITY));
129 }
130
131 #[test]
132 fn borderline_overflow() {
133 let mut s = "0.".to_string();
134 for _ in 0..375 {
135 s.push('3');
136 }
137 // At the time of this writing, this returns Err(..), but this is a bug that should be fixed.
138 // It makes no sense to enshrine that in a test, the important part is that it doesn't panic.
139 let _ = s.parse::<f64>();
140 }