1 // Translated from C to Rust. The original C code can be found at
2 // https://github.com/ulfjack/ryu and carries the following license:
3 //
4 // Copyright 2018 Ulf Adams
5 //
6 // The contents of this file may be used under the terms of the Apache License,
7 // Version 2.0.
8 //
9 //    (See accompanying file LICENSE-Apache or copy at
10 //     http://www.apache.org/licenses/LICENSE-2.0)
11 //
12 // Alternatively, the contents of this file may be used under the terms of
13 // the Boost Software License, Version 1.0.
14 //    (See accompanying file LICENSE-Boost or copy at
15 //     https://www.boost.org/LICENSE_1_0.txt)
16 //
17 // Unless required by applicable law or agreed to in writing, this software
18 // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 // KIND, either express or implied.
20 
21 #[macro_use]
22 mod macros;
23 
24 use std::f32;
25 
pretty(f: f32) -> String26 fn pretty(f: f32) -> String {
27     ryu::Buffer::new().format(f).to_owned()
28 }
29 
30 #[test]
test_ryu()31 fn test_ryu() {
32     check!(0.3);
33     check!(1234000000000.0);
34     check!(1.234e13);
35     check!(2.71828);
36     check!(1.1e32);
37     check!(1.1e-32);
38     check!(2.7182817);
39     check!(1e-45);
40     check!(3.4028235e38);
41     check!(-0.001234);
42 }
43 
44 #[test]
test_random()45 fn test_random() {
46     let n = if cfg!(miri) { 100 } else { 1000000 };
47     let mut buffer = ryu::Buffer::new();
48     for _ in 0..n {
49         let f: f32 = rand::random();
50         assert_eq!(f, buffer.format_finite(f).parse().unwrap());
51     }
52 }
53 
54 #[test]
55 #[cfg_attr(miri, ignore)]
test_non_finite()56 fn test_non_finite() {
57     for i in 0u32..1 << 23 {
58         let f = f32::from_bits((((1 << 8) - 1) << 23) + i);
59         assert!(!f.is_finite(), "f={}", f);
60         ryu::Buffer::new().format_finite(f);
61     }
62 }
63 
64 #[test]
test_basic()65 fn test_basic() {
66     check!(0.0);
67     check!(-0.0);
68     check!(1.0);
69     check!(-1.0);
70     assert_eq!(pretty(f32::NAN), "NaN");
71     assert_eq!(pretty(f32::INFINITY), "inf");
72     assert_eq!(pretty(f32::NEG_INFINITY), "-inf");
73 }
74 
75 #[test]
test_switch_to_subnormal()76 fn test_switch_to_subnormal() {
77     check!(1.1754944e-38);
78 }
79 
80 #[test]
test_min_and_max()81 fn test_min_and_max() {
82     assert_eq!(f32::from_bits(0x7f7fffff), 3.4028235e38);
83     check!(3.4028235e38);
84     assert_eq!(f32::from_bits(1), 1e-45);
85     check!(1e-45);
86 }
87 
88 // Check that we return the exact boundary if it is the shortest
89 // representation, but only if the original floating point number is even.
90 #[test]
test_boundary_round_even()91 fn test_boundary_round_even() {
92     check!(33554450.0);
93     check!(9000000000.0);
94     check!(34366720000.0);
95 }
96 
97 // If the exact value is exactly halfway between two shortest representations,
98 // then we round to even. It seems like this only makes a difference if the
99 // last two digits are ...2|5 or ...7|5, and we cut off the 5.
100 #[test]
test_exact_value_round_even()101 fn test_exact_value_round_even() {
102     check!(305404.12);
103     check!(8099.0312);
104 }
105 
106 #[test]
test_lots_of_trailing_zeros()107 fn test_lots_of_trailing_zeros() {
108     // Pattern for the first test: 00111001100000000000000000000000
109     check!(0.00024414062);
110     check!(0.0024414062);
111     check!(0.0043945312);
112     check!(0.0063476562);
113 }
114 
115 #[test]
test_regression()116 fn test_regression() {
117     check!(4.7223665e21);
118     check!(8388608.0);
119     check!(16777216.0);
120     check!(33554436.0);
121     check!(67131496.0);
122     check!(1.9310392e-38);
123     check!(-2.47e-43);
124     check!(1.993244e-38);
125     check!(4103.9004);
126     check!(5339999700.0);
127     check!(6.0898e-39);
128     check!(0.0010310042);
129     check!(2.882326e17);
130     check!(7.038531e-26);
131     check!(9.223404e17);
132     check!(67108870.0);
133     check!(1e-44);
134     check!(2.816025e14);
135     check!(9.223372e18);
136     check!(1.5846086e29);
137     check!(1.1811161e19);
138     check!(5.368709e18);
139     check!(4.6143166e18);
140     check!(0.007812537);
141     check!(1e-45);
142     check!(1.18697725e20);
143     check!(1.00014165e-36);
144     check!(200.0);
145     check!(33554432.0);
146 }
147 
148 #[test]
test_looks_like_pow5()149 fn test_looks_like_pow5() {
150     // These numbers have a mantissa that is the largest power of 5 that fits,
151     // and an exponent that causes the computation for q to result in 10, which
152     // is a corner case for Ryū.
153     assert_eq!(f32::from_bits(0x5D1502F9), 6.7108864e17);
154     check!(6.7108864e17);
155     assert_eq!(f32::from_bits(0x5D9502F9), 1.3421773e18);
156     check!(1.3421773e18);
157     assert_eq!(f32::from_bits(0x5E1502F9), 2.6843546e18);
158     check!(2.6843546e18);
159 }
160 
161 #[test]
test_output_length()162 fn test_output_length() {
163     check!(1.0); // already tested in Basic
164     check!(1.2);
165     check!(1.23);
166     check!(1.234);
167     check!(1.2345);
168     check!(1.23456);
169     check!(1.234567);
170     check!(1.2345678);
171     check!(1.23456735e-36);
172 }
173