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