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 #![allow(
22     clippy::approx_constant,
23     clippy::float_cmp,
24     clippy::non_ascii_literal,
25     clippy::unreadable_literal,
26     clippy::unseparated_literal_suffix
27 )]
28 
29 #[macro_use]
30 mod macros;
31 
32 use std::f32;
33 
pretty(f: f32) -> String34 fn pretty(f: f32) -> String {
35     ryu::Buffer::new().format(f).to_owned()
36 }
37 
38 #[test]
test_ryu()39 fn test_ryu() {
40     check!(0.3);
41     check!(1234000000000.0);
42     check!(1.234e13);
43     check!(2.71828);
44     check!(1.1e32);
45     check!(1.1e-32);
46     check!(2.7182817);
47     check!(1e-45);
48     check!(3.4028235e38);
49     check!(-0.001234);
50 }
51 
52 #[test]
test_random()53 fn test_random() {
54     let n = if cfg!(miri) { 100 } else { 1000000 };
55     let mut buffer = ryu::Buffer::new();
56     for _ in 0..n {
57         let f: f32 = rand::random();
58         assert_eq!(f, buffer.format_finite(f).parse().unwrap());
59     }
60 }
61 
62 #[test]
63 #[cfg_attr(miri, ignore)]
test_non_finite()64 fn test_non_finite() {
65     for i in 0u32..1 << 23 {
66         let f = f32::from_bits((((1 << 8) - 1) << 23) + i);
67         assert!(!f.is_finite(), "f={}", f);
68         ryu::Buffer::new().format_finite(f);
69     }
70 }
71 
72 #[test]
test_basic()73 fn test_basic() {
74     check!(0.0);
75     check!(-0.0);
76     check!(1.0);
77     check!(-1.0);
78     assert_eq!(pretty(f32::NAN), "NaN");
79     assert_eq!(pretty(f32::INFINITY), "inf");
80     assert_eq!(pretty(f32::NEG_INFINITY), "-inf");
81 }
82 
83 #[test]
test_switch_to_subnormal()84 fn test_switch_to_subnormal() {
85     check!(1.1754944e-38);
86 }
87 
88 #[test]
test_min_and_max()89 fn test_min_and_max() {
90     assert_eq!(f32::from_bits(0x7f7fffff), 3.4028235e38);
91     check!(3.4028235e38);
92     assert_eq!(f32::from_bits(1), 1e-45);
93     check!(1e-45);
94 }
95 
96 // Check that we return the exact boundary if it is the shortest
97 // representation, but only if the original floating point number is even.
98 #[test]
test_boundary_round_even()99 fn test_boundary_round_even() {
100     check!(33554450.0);
101     check!(9000000000.0);
102     check!(34366720000.0);
103 }
104 
105 // If the exact value is exactly halfway between two shortest representations,
106 // then we round to even. It seems like this only makes a difference if the
107 // last two digits are ...2|5 or ...7|5, and we cut off the 5.
108 #[test]
test_exact_value_round_even()109 fn test_exact_value_round_even() {
110     check!(305404.12);
111     check!(8099.0312);
112 }
113 
114 #[test]
test_lots_of_trailing_zeros()115 fn test_lots_of_trailing_zeros() {
116     // Pattern for the first test: 00111001100000000000000000000000
117     check!(0.00024414062);
118     check!(0.0024414062);
119     check!(0.0043945312);
120     check!(0.0063476562);
121 }
122 
123 #[test]
test_regression()124 fn test_regression() {
125     check!(4.7223665e21);
126     check!(8388608.0);
127     check!(16777216.0);
128     check!(33554436.0);
129     check!(67131496.0);
130     check!(1.9310392e-38);
131     check!(-2.47e-43);
132     check!(1.993244e-38);
133     check!(4103.9004);
134     check!(5339999700.0);
135     check!(6.0898e-39);
136     check!(0.0010310042);
137     check!(2.882326e17);
138     check!(7.038531e-26);
139     check!(9.223404e17);
140     check!(67108870.0);
141     check!(1e-44);
142     check!(2.816025e14);
143     check!(9.223372e18);
144     check!(1.5846086e29);
145     check!(1.1811161e19);
146     check!(5.368709e18);
147     check!(4.6143166e18);
148     check!(0.007812537);
149     check!(1e-45);
150     check!(1.18697725e20);
151     check!(1.00014165e-36);
152     check!(200.0);
153     check!(33554432.0);
154 }
155 
156 #[test]
test_looks_like_pow5()157 fn test_looks_like_pow5() {
158     // These numbers have a mantissa that is the largest power of 5 that fits,
159     // and an exponent that causes the computation for q to result in 10, which
160     // is a corner case for Ryū.
161     assert_eq!(f32::from_bits(0x5D1502F9), 6.7108864e17);
162     check!(6.7108864e17);
163     assert_eq!(f32::from_bits(0x5D9502F9), 1.3421773e18);
164     check!(1.3421773e18);
165     assert_eq!(f32::from_bits(0x5E1502F9), 2.6843546e18);
166     check!(2.6843546e18);
167 }
168 
169 #[test]
test_output_length()170 fn test_output_length() {
171     check!(1.0); // already tested in Basic
172     check!(1.2);
173     check!(1.23);
174     check!(1.234);
175     check!(1.2345);
176     check!(1.23456);
177     check!(1.234567);
178     check!(1.2345678);
179     check!(1.23456735e-36);
180 }
181