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 #![cfg(not(feature = "small"))]
22 #![allow(dead_code)]
23 #![allow(
24     clippy::cast_lossless,
25     clippy::cast_possible_truncation,
26     clippy::cast_possible_wrap,
27     clippy::cast_sign_loss,
28     clippy::excessive_precision,
29     clippy::float_cmp,
30     clippy::similar_names,
31     clippy::too_many_lines,
32     clippy::unreadable_literal,
33     clippy::unseparated_literal_suffix,
34     clippy::wildcard_imports
35 )]
36 
37 #[path = "../src/common.rs"]
38 mod common;
39 
40 #[path = "../src/d2s_full_table.rs"]
41 mod d2s_full_table;
42 
43 #[path = "../src/d2s_intrinsics.rs"]
44 mod d2s_intrinsics;
45 
46 #[path = "../src/d2s.rs"]
47 mod d2s;
48 
49 #[path = "../src/s2d.rs"]
50 mod s2d;
51 
52 #[path = "../src/parse.rs"]
53 mod parse;
54 
55 use crate::parse::Error;
56 use crate::s2d::s2d;
57 
58 impl PartialEq for Error {
eq(&self, other: &Self) -> bool59     fn eq(&self, other: &Self) -> bool {
60         *self as u8 == *other as u8
61     }
62 }
63 
64 #[test]
test_bad_input()65 fn test_bad_input() {
66     assert_eq!(Error::MalformedInput, s2d(b"x").unwrap_err());
67     assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
68     assert_eq!(Error::MalformedInput, s2d(b"..").unwrap_err());
69     assert_eq!(Error::MalformedInput, s2d(b"1..1").unwrap_err());
70     assert_eq!(Error::MalformedInput, s2d(b"1ee1").unwrap_err());
71     assert_eq!(Error::MalformedInput, s2d(b"1e.1").unwrap_err());
72     assert_eq!(Error::InputTooShort, s2d(b"").unwrap_err());
73     assert_eq!(Error::InputTooLong, s2d(b"123456789012345678").unwrap_err());
74     assert_eq!(Error::InputTooLong, s2d(b"1e12345").unwrap_err());
75 }
76 
77 #[test]
test_basic()78 fn test_basic() {
79     assert_eq!(0.0, s2d(b"0").unwrap());
80     assert_eq!(-0.0, s2d(b"-0").unwrap());
81     assert_eq!(1.0, s2d(b"1").unwrap());
82     assert_eq!(2.0, s2d(b"2").unwrap());
83     assert_eq!(123456789.0, s2d(b"123456789").unwrap());
84     assert_eq!(123.456, s2d(b"123.456").unwrap());
85     assert_eq!(123.456, s2d(b"123456e-3").unwrap());
86     assert_eq!(123.456, s2d(b"1234.56e-1").unwrap());
87     assert_eq!(1.453, s2d(b"1.453").unwrap());
88     assert_eq!(1453.0, s2d(b"1.453e+3").unwrap());
89     assert_eq!(0.0, s2d(b".0").unwrap());
90     assert_eq!(1.0, s2d(b"1e0").unwrap());
91     assert_eq!(1.0, s2d(b"1E0").unwrap());
92     assert_eq!(1.0, s2d(b"000001.000000").unwrap());
93     assert_eq!(0.2316419, s2d(b"0.2316419").unwrap());
94 }
95 
96 #[test]
test_min_max()97 fn test_min_max() {
98     assert_eq!(
99         1.7976931348623157e308,
100         s2d(b"1.7976931348623157e308").unwrap(),
101     );
102     assert_eq!(5E-324, s2d(b"5E-324").unwrap());
103 }
104 
105 #[test]
test_mantissa_rounding_overflow()106 fn test_mantissa_rounding_overflow() {
107     // This results in binary mantissa that is all ones and requires rounding up
108     // because it is closer to 1 than to the next smaller float. This is a
109     // regression test that the mantissa overflow is handled correctly by
110     // increasing the exponent.
111     assert_eq!(1.0, s2d(b"0.99999999999999999").unwrap());
112     // This number overflows the mantissa *and* the IEEE exponent.
113     assert_eq!(f64::INFINITY, s2d(b"1.7976931348623159e308").unwrap());
114 }
115 
116 #[test]
test_underflow()117 fn test_underflow() {
118     assert_eq!(0.0, s2d(b"2.4e-324").unwrap());
119     assert_eq!(0.0, s2d(b"1e-324").unwrap());
120     assert_eq!(0.0, s2d(b"9.99999e-325").unwrap());
121     // These are just about halfway between 0 and the smallest float.
122     // The first is just below the halfway point, the second just above.
123     assert_eq!(0.0, s2d(b"2.4703282292062327e-324").unwrap());
124     assert_eq!(5e-324, s2d(b"2.4703282292062328e-324").unwrap());
125 }
126 
127 #[test]
test_overflow()128 fn test_overflow() {
129     assert_eq!(f64::INFINITY, s2d(b"2e308").unwrap());
130     assert_eq!(f64::INFINITY, s2d(b"1e309").unwrap());
131 }
132 
133 #[test]
test_table_size_denormal()134 fn test_table_size_denormal() {
135     assert_eq!(5e-324, s2d(b"4.9406564584124654e-324").unwrap());
136 }
137 
138 #[test]
test_issue157()139 fn test_issue157() {
140     assert_eq!(
141         1.2999999999999999E+154,
142         s2d(b"1.2999999999999999E+154").unwrap(),
143     );
144 }
145 
146 #[test]
test_issue173()147 fn test_issue173() {
148     // Denormal boundary
149     assert_eq!(
150         2.2250738585072012e-308,
151         s2d(b"2.2250738585072012e-308").unwrap(),
152     );
153     assert_eq!(
154         2.2250738585072013e-308,
155         s2d(b"2.2250738585072013e-308").unwrap(),
156     );
157     assert_eq!(
158         2.2250738585072014e-308,
159         s2d(b"2.2250738585072014e-308").unwrap(),
160     );
161 }
162