Yi Kong | ce81bb6 | 2020-08-31 01:21:33 +0800 | [diff] [blame] | 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(dead_code)] |
| 22 | |
| 23 | #[path = "../src/common.rs"] |
| 24 | mod common; |
| 25 | |
| 26 | #[path = "../src/d2s_full_table.rs"] |
| 27 | mod d2s_full_table; |
| 28 | |
| 29 | #[path = "../src/d2s_intrinsics.rs"] |
| 30 | mod d2s_intrinsics; |
| 31 | |
| 32 | #[path = "../src/d2s.rs"] |
| 33 | mod d2s; |
| 34 | |
| 35 | #[path = "../src/f2s_intrinsics.rs"] |
| 36 | mod f2s_intrinsics; |
| 37 | |
| 38 | #[path = "../src/f2s.rs"] |
| 39 | mod f2s; |
| 40 | |
| 41 | #[path = "../src/s2f.rs"] |
| 42 | mod s2f; |
| 43 | |
| 44 | #[path = "../src/parse.rs"] |
| 45 | mod parse; |
| 46 | |
| 47 | use crate::parse::Error; |
| 48 | use crate::s2f::s2f; |
| 49 | |
| 50 | impl PartialEq for Error { |
| 51 | fn eq(&self, other: &Self) -> bool { |
| 52 | *self as u8 == *other as u8 |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn test_basic() { |
| 58 | assert_eq!(0.0, s2f(b"0").unwrap()); |
| 59 | assert_eq!(-0.0, s2f(b"-0").unwrap()); |
| 60 | assert_eq!(1.0, s2f(b"1").unwrap()); |
| 61 | assert_eq!(-1.0, s2f(b"-1").unwrap()); |
| 62 | assert_eq!(123456792.0, s2f(b"123456789").unwrap()); |
| 63 | assert_eq!(299792448.0, s2f(b"299792458").unwrap()); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn test_min_max() { |
| 68 | assert_eq!(1e-45, s2f(b"1e-45").unwrap()); |
| 69 | assert_eq!(f32::MIN_POSITIVE, s2f(b"1.1754944e-38").unwrap()); |
| 70 | assert_eq!(f32::MAX, s2f(b"3.4028235e+38").unwrap()); |
| 71 | } |
| 72 | |
| 73 | #[test] |
| 74 | fn test_mantissa_rounding_overflow() { |
| 75 | assert_eq!(1.0, s2f(b"0.999999999").unwrap()); |
| 76 | assert_eq!(f32::INFINITY, s2f(b"3.4028236e+38").unwrap()); |
| 77 | } |