| 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 | |
| 34 | fn pretty(f: f32) -> String { |
| 35 | ryu::Buffer::new().format(f).to_owned() |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 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] |
| 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)] |
| 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] |
| 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.copysign(1.0)), "NaN" ); |
| 79 | assert_eq!(pretty(f32::NAN.copysign(-1.0)), "NaN" ); |
| 80 | assert_eq!(pretty(f32::INFINITY), "inf" ); |
| 81 | assert_eq!(pretty(f32::NEG_INFINITY), "-inf" ); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn test_switch_to_subnormal() { |
| 86 | check!(1.1754944e-38); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn test_min_and_max() { |
| 91 | assert_eq!(f32::from_bits(0x7f7fffff), 3.4028235e38); |
| 92 | check!(3.4028235e38); |
| 93 | assert_eq!(f32::from_bits(1), 1e-45); |
| 94 | check!(1e-45); |
| 95 | } |
| 96 | |
| 97 | // Check that we return the exact boundary if it is the shortest |
| 98 | // representation, but only if the original floating point number is even. |
| 99 | #[test] |
| 100 | fn test_boundary_round_even() { |
| 101 | check!(33554450.0); |
| 102 | check!(9000000000.0); |
| 103 | check!(34366720000.0); |
| 104 | } |
| 105 | |
| 106 | // If the exact value is exactly halfway between two shortest representations, |
| 107 | // then we round to even. It seems like this only makes a difference if the |
| 108 | // last two digits are ...2|5 or ...7|5, and we cut off the 5. |
| 109 | #[test] |
| 110 | fn test_exact_value_round_even() { |
| 111 | check!(305404.12); |
| 112 | check!(8099.0312); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn test_lots_of_trailing_zeros() { |
| 117 | // Pattern for the first test: 00111001100000000000000000000000 |
| 118 | check!(0.00024414062); |
| 119 | check!(0.0024414062); |
| 120 | check!(0.0043945312); |
| 121 | check!(0.0063476562); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn test_regression() { |
| 126 | check!(4.7223665e21); |
| 127 | check!(8388608.0); |
| 128 | check!(16777216.0); |
| 129 | check!(33554436.0); |
| 130 | check!(67131496.0); |
| 131 | check!(1.9310392e-38); |
| 132 | check!(-2.47e-43); |
| 133 | check!(1.993244e-38); |
| 134 | check!(4103.9004); |
| 135 | check!(5339999700.0); |
| 136 | check!(6.0898e-39); |
| 137 | check!(0.0010310042); |
| 138 | check!(2.882326e17); |
| 139 | check!(7.038531e-26); |
| 140 | check!(9.223404e17); |
| 141 | check!(67108870.0); |
| 142 | check!(1e-44); |
| 143 | check!(2.816025e14); |
| 144 | check!(9.223372e18); |
| 145 | check!(1.5846086e29); |
| 146 | check!(1.1811161e19); |
| 147 | check!(5.368709e18); |
| 148 | check!(4.6143166e18); |
| 149 | check!(0.007812537); |
| 150 | check!(1e-45); |
| 151 | check!(1.18697725e20); |
| 152 | check!(1.00014165e-36); |
| 153 | check!(200.0); |
| 154 | check!(33554432.0); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn test_looks_like_pow5() { |
| 159 | // These numbers have a mantissa that is the largest power of 5 that fits, |
| 160 | // and an exponent that causes the computation for q to result in 10, which |
| 161 | // is a corner case for Ryƫ. |
| 162 | assert_eq!(f32::from_bits(0x5D1502F9), 6.7108864e17); |
| 163 | check!(6.7108864e17); |
| 164 | assert_eq!(f32::from_bits(0x5D9502F9), 1.3421773e18); |
| 165 | check!(1.3421773e18); |
| 166 | assert_eq!(f32::from_bits(0x5E1502F9), 2.6843546e18); |
| 167 | check!(2.6843546e18); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn test_output_length() { |
| 172 | check!(1.0); // already tested in Basic |
| 173 | check!(1.2); |
| 174 | check!(1.23); |
| 175 | check!(1.234); |
| 176 | check!(1.2345); |
| 177 | check!(1.23456); |
| 178 | check!(1.234567); |
| 179 | check!(1.2345678); |
| 180 | check!(1.23456735e-36); |
| 181 | } |
| 182 | |