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 #![allow( 23 clippy::cast_lossless, 24 clippy::cast_possible_truncation, 25 clippy::cast_possible_wrap, 26 clippy::cast_possible_wrap, 27 clippy::cast_sign_loss, 28 clippy::checked_conversions, 29 clippy::float_cmp, 30 clippy::manual_range_contains, 31 clippy::similar_names, 32 clippy::too_many_lines, 33 clippy::unreadable_literal, 34 clippy::unseparated_literal_suffix, 35 clippy::wildcard_imports 36 )] 37 38 #[path = "../src/common.rs"] 39 mod common; 40 41 #[cfg(not(feature = "small"))] 42 #[path = "../src/d2s_full_table.rs"] 43 mod d2s_full_table; 44 45 #[path = "../src/d2s_intrinsics.rs"] 46 mod d2s_intrinsics; 47 48 #[cfg(feature = "small")] 49 #[path = "../src/d2s_small_table.rs"] 50 mod d2s_small_table; 51 52 #[path = "../src/d2s.rs"] 53 mod d2s; 54 55 #[path = "../src/f2s_intrinsics.rs"] 56 mod f2s_intrinsics; 57 58 #[path = "../src/f2s.rs"] 59 mod f2s; 60 61 #[path = "../src/s2f.rs"] 62 mod s2f; 63 64 #[path = "../src/parse.rs"] 65 mod parse; 66 67 use crate::parse::Error; 68 use crate::s2f::s2f; 69 70 impl PartialEq for Error { eq(&self, other: &Self) -> bool71 fn eq(&self, other: &Self) -> bool { 72 *self as u8 == *other as u8 73 } 74 } 75 76 #[test] test_basic()77 fn test_basic() { 78 assert_eq!(0.0, s2f(b"0").unwrap()); 79 assert_eq!(-0.0, s2f(b"-0").unwrap()); 80 assert_eq!(1.0, s2f(b"1").unwrap()); 81 assert_eq!(-1.0, s2f(b"-1").unwrap()); 82 assert_eq!(123456792.0, s2f(b"123456789").unwrap()); 83 assert_eq!(299792448.0, s2f(b"299792458").unwrap()); 84 } 85 86 #[test] test_min_max()87 fn test_min_max() { 88 assert_eq!(1e-45, s2f(b"1e-45").unwrap()); 89 assert_eq!(f32::MIN_POSITIVE, s2f(b"1.1754944e-38").unwrap()); 90 assert_eq!(f32::MAX, s2f(b"3.4028235e+38").unwrap()); 91 } 92 93 #[test] test_mantissa_rounding_overflow()94 fn test_mantissa_rounding_overflow() { 95 assert_eq!(1.0, s2f(b"0.999999999").unwrap()); 96 assert_eq!(f32::INFINITY, s2f(b"3.4028236e+38").unwrap()); 97 assert_eq!(1.1754944e-38, s2f(b"1.17549430e-38").unwrap()); // FLT_MIN 98 assert_eq!(1.1754944e-38, s2f(b"1.17549431e-38").unwrap()); 99 assert_eq!(1.1754944e-38, s2f(b"1.17549432e-38").unwrap()); 100 assert_eq!(1.1754944e-38, s2f(b"1.17549433e-38").unwrap()); 101 assert_eq!(1.1754944e-38, s2f(b"1.17549434e-38").unwrap()); 102 assert_eq!(1.1754944e-38, s2f(b"1.17549435e-38").unwrap()); 103 } 104 105 #[test] test_trailing_zeros()106 fn test_trailing_zeros() { 107 assert_eq!(26843550.0, s2f(b"26843549.5").unwrap()); 108 assert_eq!(50000004.0, s2f(b"50000002.5").unwrap()); 109 assert_eq!(99999992.0, s2f(b"99999989.5").unwrap()); 110 } 111