]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/dec2flt/mod.rs
fix spacing issue in trpl/documentation doc
[rust.git] / src / libcoretest / num / dec2flt / mod.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(overflowing_literals)]
12
13 use std::{i64, f32, f64};
14 use test;
15 use core::num::dec2flt::{to_f32, to_f64};
16
17 mod parse;
18 mod rawfp;
19
20 // Take an float literal, turn it into a string in various ways (that are all trusted
21 // to be correct) and see if those strings are parsed back to the value of the literal.
22 // Requires a *polymorphic literal*, i.e. one that can serve as f64 as well as f32.
23 macro_rules! test_literal {
24     ($x: expr) => ({
25         let x32: f32 = $x;
26         let x64: f64 = $x;
27         let inputs = &[stringify!($x).into(), format!("{:?}", x64), format!("{:e}", x64)];
28         for input in inputs {
29             if input != "inf" {
30                 assert_eq!(to_f64(input), Ok(x64));
31                 assert_eq!(to_f32(input), Ok(x32));
32                 let neg_input = &format!("-{}", input);
33                 assert_eq!(to_f64(neg_input), Ok(-x64));
34                 assert_eq!(to_f32(neg_input), Ok(-x32));
35             }
36         }
37     })
38 }
39
40 #[test]
41 fn ordinary() {
42     test_literal!(1.0);
43     test_literal!(3e-5);
44     test_literal!(0.1);
45     test_literal!(12345.);
46     test_literal!(0.9999999);
47     test_literal!(2.2250738585072014e-308);
48 }
49
50 #[test]
51 fn special_code_paths() {
52     test_literal!(36893488147419103229.0); // 2^65 - 3, triggers half-to-even with even significand
53     test_literal!(101e-33); // Triggers the tricky underflow case in AlgorithmM (for f32)
54     test_literal!(1e23); // Triggers AlgorithmR
55     test_literal!(2075e23); // Triggers another path through AlgorithmR
56     test_literal!(8713e-23); // ... and yet another.
57 }
58
59 #[test]
60 fn large() {
61     test_literal!(1e300);
62     test_literal!(123456789.34567e250);
63     test_literal!(943794359898089732078308743689303290943794359843568973207830874368930329.);
64 }
65
66 #[test]
67 fn subnormals() {
68     test_literal!(5e-324);
69     test_literal!(91e-324);
70     test_literal!(1e-322);
71     test_literal!(13245643e-320);
72     test_literal!(2.22507385851e-308);
73     test_literal!(2.1e-308);
74     test_literal!(4.9406564584124654e-324);
75 }
76
77 #[test]
78 fn infinity() {
79     test_literal!(1e400);
80     test_literal!(1e309);
81     test_literal!(2e308);
82     test_literal!(1.7976931348624e308);
83 }
84
85 #[test]
86 fn zero() {
87     test_literal!(0.0);
88     test_literal!(1e-325);
89     test_literal!(1e-326);
90     test_literal!(1e-500);
91 }
92
93 #[test]
94 fn fast_path_correct() {
95     // This number triggers the fast path and is handled incorrectly when compiling on
96     // x86 without SSE2 (i.e., using the x87 FPU stack).
97     test_literal!(1.448997445238699);
98 }
99
100 #[test]
101 fn lonely_dot() {
102     assert_eq!(to_f64("."), Ok(0.0));
103 }
104
105 #[test]
106 fn nan() {
107     assert!(to_f64("NaN").unwrap().is_nan());
108     assert!(to_f32("NaN").unwrap().is_nan());
109 }
110
111 #[test]
112 fn inf() {
113     assert_eq!(to_f64("inf"), Ok(f64::INFINITY));
114     assert_eq!(to_f64("-inf"), Ok(f64::NEG_INFINITY));
115     assert_eq!(to_f32("inf"), Ok(f32::INFINITY));
116     assert_eq!(to_f32("-inf"), Ok(f32::NEG_INFINITY));
117 }
118
119 #[test]
120 fn massive_exponent() {
121     let max = i64::MAX;
122     assert_eq!(to_f64(&format!("1e{}000", max)), Ok(f64::INFINITY));
123     assert_eq!(to_f64(&format!("1e-{}000", max)), Ok(0.0));
124     assert_eq!(to_f64(&format!("1e{}000", max)), Ok(f64::INFINITY));
125 }
126
127 #[bench]
128 fn bench_0(b: &mut test::Bencher) {
129     b.iter(|| to_f64("0.0"));
130 }
131
132 #[bench]
133 fn bench_42(b: &mut test::Bencher) {
134     b.iter(|| to_f64("42"));
135 }
136
137 #[bench]
138 fn bench_huge_int(b: &mut test::Bencher) {
139     // 2^128 - 1
140     b.iter(|| to_f64("170141183460469231731687303715884105727"));
141 }
142
143 #[bench]
144 fn bench_short_decimal(b: &mut test::Bencher) {
145     b.iter(|| to_f64("1234.5678"));
146 }
147
148 #[bench]
149 fn bench_pi_long(b: &mut test::Bencher) {
150     b.iter(|| to_f64("3.14159265358979323846264338327950288"));
151 }
152
153 #[bench]
154 fn bench_pi_short(b: &mut test::Bencher) {
155     b.iter(|| to_f64("3.141592653589793"))
156 }
157
158 #[bench]
159 fn bench_1e150(b: &mut test::Bencher) {
160     b.iter(|| to_f64("1e150"));
161 }
162
163 #[bench]
164 fn bench_long_decimal_and_exp(b: &mut test::Bencher) {
165     b.iter(|| to_f64("727501488517303786137132964064381141071e-123"));
166 }
167
168 #[bench]
169 fn bench_min_subnormal(b: &mut test::Bencher) {
170     b.iter(|| to_f64("5e-324"));
171 }
172
173 #[bench]
174 fn bench_min_normal(b: &mut test::Bencher) {
175     b.iter(|| to_f64("2.2250738585072014e-308"));
176 }
177
178 #[bench]
179 fn bench_max(b: &mut test::Bencher) {
180     b.iter(|| to_f64("1.7976931348623157e308"));
181 }